ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

Clarification regarding Ball3DAgent rotation information

Open asolano opened this issue 9 years ago • 5 comments

Greetings,

I am trying to understand the Ball3D example to use it as a base for a similar environment we are working with. After following the getting started guide the brain is trained correctly and the platforms are balancing the balls as expected. The question is why the agent is giving as state only two elements of the rotation quaternion instead of the four:

state.Add(gameObject.transform.rotation.z);
state.Add(gameObject.transform.rotation.x);

Was it intended to use gameObject.transform.eulerAngles.z instead?

Again, when the action step takes place the code compares with the quaternion element but rotates along the Euler angles axis:

if ((gameObject.transform.rotation.z < 0.25f && action_z > 0f) ||
    (gameObject.transform.rotation.z > -0.25f && action_z < 0f))
{
    gameObject.transform.Rotate(new Vector3(0, 0, 1), action_z);
}

(transform.rotation returns a quaternion but transform.Rotate takes a Euler angles)

As I understand it the x and z elements of the rotation quaternion are not the x and z Euler angles, so I am getting confused by the mixing of the two. I have tried changing the agent code to use only Euler angles but the training is not good. What am I missing?

Thanks.

asolano avatar Nov 01 '17 09:11 asolano

Hi asolano, When you change the state to Euler angles, you will have to normalize the angle value. The range of rotation.z and rotation.x is between -1 ~ 1, but Euler angles is from 0 ~ 360. Everything passed to state should be normalized to a value between -1 ~ 1.

rotation.z and rotation.x is enough to represent the agent's rotation state in this case. And most importantly, the value is continuous. Instead, Euler angle will bounce from 360 to 0 when a full circle is completed, so it is not suitable to being used as state.

I think the reason that they use Euler angle in AgentStep to actually take the action is because Euler angle is easier to understand from human's view. And so it will make it easier to adjust the max rotation angle.

Hope that would helps :)

trulyspinach avatar Nov 01 '17 10:11 trulyspinach

Hi, SpinachCeo, and thanks for your quick reply.

Normalization, of course, I had completely forgotten about it. Once adjusted for it seems to work again, although the training takes longer and the behavior is a bit different (balls tend to reach equilibrium).

My knowledge of quaternions is not that deep, could you please explain why in this case only x and z are enough? (I would expect all four components are required to represent the rotation) I guess I find it suspicious that they happen to have the same name as the axis.

Appreciated.

asolano avatar Nov 02 '17 09:11 asolano

Thanks for reaching out to us. Hopefully you were able to resolve your issue. We are closing this due to inactivity, but if you need additional assistance, feel free to reopen the issue.

shihzy avatar Mar 28 '18 21:03 shihzy

@adason The reason we only have x and z instead of four components is due to x and z rotations having -1 ~ 1 range, so it can cover rotation in four components. Is you want to understand more about Quaternion you could refer to this link. https://www.3dgep.com/understanding-quaternions/

xiaomaogy avatar Mar 28 '18 22:03 xiaomaogy

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.

lock[bot] avatar Jan 03 '20 13:01 lock[bot]