Skip to content

RayPerceptionSensorComponent3D does not function as expected when the agent's scale is not 1. #3321

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

Closed
WanNJ opened this issue Jan 30, 2020 · 7 comments · Fixed by #3334
Closed
Assignees
Labels
bug Issue describes a potential bug in ml-agents.

Comments

@WanNJ
Copy link

WanNJ commented Jan 30, 2020

All the agents in the example scenes have a scale of 1. However, if changed the scale to other numbers, the Debug gizmo would also scale, resulting in the wrong rays drawn in the editor.

I haven't checked whether the observed values are correct yet. Presumably, it may have the same issue.

  • ML-Agents v0.13.0
  • TensorFlow v2.0
  • Environment_: Change any of the agent's scale to not equal to 1.

To reproduce this bug, just change the scale of the agent of WallJump example and run using the trained model. You will see that the rays drawn in the editor is wrong.

@WanNJ WanNJ added the bug Issue describes a potential bug in ml-agents. label Jan 30, 2020
@chriselion chriselion self-assigned this Jan 30, 2020
@chriselion
Copy link
Contributor

I'll look into this tomorrow.

Can you explain more what are you seeing and what do you expect? I would expect that when the scale changes, the length of the rays also increase.

@WanNJ
Copy link
Author

WanNJ commented Jan 30, 2020

I'll look into this tomorrow.

Can you explain more what are you seeing and what do you expect? I would expect that when the scale changes, the length of the rays also increase.

The expected: the red sphere drawn representing the hit point should be the actual hitpoint. However, when the scale changes, the red sphere is drawn to somewhere else, scaling with the scale.

I don't know if the observation feed into the RL component has the same problem or not. The below screenshot shows this bug when I change the training area's scale to 1.5.
Screen Shot 2020-01-30 at 10 59 42

@chriselion
Copy link
Contributor

Thanks, the screenshot helps a lot. Looking into it now, I get the feeling the scale is being applied twice somehow.

@chriselion
Copy link
Contributor

chriselion commented Jan 30, 2020

OK, I think I know what's going on. I believe this is just a visual bug, but it's definitely something we should fix.
When we do the raycasting here:

castHit = Physics.SphereCast(startPositionWorld, castRadius, rayDirection, out rayHit,
rayLength, layerMask);
}
else
{
castHit = Physics.Raycast(startPositionWorld, rayDirection, out rayHit,
rayLength, layerMask);
}
hitFraction = castHit ? rayHit.distance / rayLength : 1.0f;

we cast up to rayLength units, and save the hit fraction. This fraction is based on rayLength units.

When doing the gizmo drawing here

var rayDirection = endPositionWorld - startPositionWorld;
rayDirection *= rayInfo.hitFraction;

We try to use the hit fraction to interpolate between the start and end points. Since these start and end points are affected by the scale, they won't necessarily be rayLength units apart. So we'd need to scale the hit fraction (for display only) by rayLength / rayDirection.magnitude.

I think a cleaner fix would be to use the scaled ray length (rayDirection.magnitude) instead of rayLength for raycasting and hit fraction determination in PerceiveStatic. This means that the effective length of the rays would change with the scale; the downside is that this would break existing trained models [edit: would break existing models with non-1 scale].

I'll have a PR for this tomorrow or early next week. In the meantime, I'm pretty confident that training with a RayPerceptionSensor and scale will still work.

@WanNJ
Copy link
Author

WanNJ commented Jan 31, 2020

Thanks, Chris. Ml-agents is such an awesome project because of developers like you.

@chriselion
Copy link
Contributor

This was fixed in #3334

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue describes a potential bug in ml-agents.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants