Skip to content

Check controlled state safely#115

Merged
aaravpandya merged 6 commits into
mainfrom
ap_FixUnsafeGet
May 14, 2024
Merged

Check controlled state safely#115
aaravpandya merged 6 commits into
mainfrom
ap_FixUnsafeGet

Conversation

@aaravpandya

Copy link
Copy Markdown
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<>.

Comment thread src/headless.cpp Outdated
},
.maxNumControlledVehicles = 0,
},
.enableBatchRenderer = true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

<3

Copy link
Copy Markdown
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

@SamanKazemkhani SamanKazemkhani left a comment

Copy link
Copy Markdown
Collaborator

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.

Comment thread 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
Copy Markdown
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;
}

Comment thread src/sim.cpp Outdated

}
else if(ctx.get<ControlledState>(candidateCollision.b).controlledState == ControlMode::EXPERT)
else if(controlledStateB.valid())

Copy link
Copy Markdown
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
@aaravpandya aaravpandya deleted the ap_FixUnsafeGet branch October 4, 2024 03:19
wangbingke0 pushed a commit to wangbingke0/gpudrive that referenced this pull request Feb 13, 2026
* Check controlled state safely

* Revert headless

* Use lambda function and inclusive or

* Type def the lambda

* Invert the booleans
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