Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check controlled state safely #115

Merged
merged 6 commits into from
May 14, 2024
Merged

Check controlled state safely #115

merged 6 commits into from
May 14, 2024

Conversation

aaravpandya
Copy link
Collaborator

In the previous, we were checking the control state on entities to determine if they were non valid experts. However, that is error prone as not all entities have control states (physics entities like roads). This PR fixes that by using the safer getCheck<>.

src/headless.cpp Outdated
@@ -71,10 +71,7 @@ int main(int argc, char *argv[])
.distanceToExpertThreshold = 0.5
},
.maxNumControlledVehicles = 0,
},
.enableBatchRenderer = true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching it

Copy link
Collaborator

@SamanKazemkhani SamanKazemkhani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only blocking comment is about the else if.

src/sim.cpp Outdated
// Technically there would never be a collision between (non valid expert) and (non expert). So one of the below checks would always be false.
if(ctx.get<ControlledState>(candidateCollision.a).controlledState == ControlMode::EXPERT)
if(controlledStateA.valid())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bodies of these if statements are identical save for the Loc. It would be nice if you factored out the logic into a lambda:

    auto isExpertAgentInInvalidState = [&](Loc loc) {
      auto maybeControlledState = ctx.getCheck<ControlledState>(loc);
      if (not maybeControlledState.valid()) {
        return false;
      }

      if (maybeControlledState.value().controlledState != ControlMode::EXPERT) {
        return false;
      }

      auto currStep = getCurrentStep(ctx.get<StepsRemaining>(loc));
      return not ctx.get<Trajectory>(loc).valids[currStep];
    };

then the if-statements would be

Suggested change
if(controlledStateA.valid())
if(isExpertAgentInInvalidState(candidateCollision.a)) {
return;
}

src/sim.cpp Outdated
}
else if(ctx.get<ControlledState>(candidateCollision.b).controlledState == ControlMode::EXPERT)
else if(controlledStateB.valid())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be an else if. For instance, if candidateCollision.a is a non-expert agent and candidateCollision.b is an expert-agent in an invalid state, this function will not be short-ciruited.

@aaravpandya aaravpandya merged commit 09a28e9 into main May 14, 2024
1 check passed
@aaravpandya aaravpandya deleted the ap_FixUnsafeGet branch October 4, 2024 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants