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

How to implement "AgentStep" function?

Open liuyihunter opened this issue 9 years ago • 7 comments

In the Ball3DAgent.cs.
For "AgentStep(float[] act)" function, What's the meaning of values inside in the "float[] act" . And why action_z can not greater than 2?

float action_z = act[0]; if (action_z > 2f) { action_z = 2f; }

Thanks

liuyihunter avatar Oct 09 '17 10:10 liuyihunter

Hi, thank you for your question. float[] act is the array of floats corresponding to the actions the brain chose for the agent.
The brain is the component that choses which action each agent must take given the state of the agent. The AgentStep() method determines what the actions correspond to.
In the Ball3DAgent.cs script, the first action float corresponds to rotation of the platform along the z axis and the second along the x axis.
We capped action_z to be smaller than 2 and bigger than -2 to avoid a too sudden rotation of the platform. It is like a rule we set up for this particular environment.

vincentpierre avatar Oct 09 '17 18:10 vincentpierre

Hi, Instead of changing the rotation, I tried to change the position of the platform. I changed the logic to this: gameObject.transform.position = gameObject.transform.position + new Vector3(0,0,action_z);

When the brain is set to player mode, actions are being properly reflected in the environment, but not in environment mode. Specifically in external mode, only Rotate is working, but not any other custom actions which I wanted to make.

Could you help me why this happens?

GEORGJOSE007 avatar Oct 10 '17 08:10 GEORGJOSE007

In the log file I am getting this error, due to which actions were not received by the Unity environment

JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List1[System.Single]]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'action', line 1, position 12. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00058] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String id) [0x00012] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x0007f] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.JsonConverter propertyConverter, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, System.Object target) [0x00061] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) [0x00220] in :0

(Filename: Line: 0)

GEORGJOSE007 avatar Oct 10 '17 09:10 GEORGJOSE007

If I only replace :
gameObject.transform.Rotate(new Vector3(0, 0, 1), action_z);
with
gameObject.transform.position = gameObject.transform.position + new Vector3(0,0,action_z); The environment still works for me with external brain. Are you sure you did not change anything else? Like you pointed, this looks like an error in the way Unity receives the JSON which the line 41 of 3DBallAgent.cs does not influence.

vincentpierre avatar Oct 10 '17 17:10 vincentpierre

gameObject.transform.Rotate(new Vector3(0, 0, 1), action_z) was also not working. It was a problem from the python side in the format in which we were sending actions. Now the issue is sorted

Thanks for the help. :)

GEORGJOSE007 avatar Oct 11 '17 05:10 GEORGJOSE007

@GEORGJOSE007,

Was there something confusing in our documentation that tripped you up around sending the right actions? If so, I'd be happy to change things so others in the future don't run into the same problem.

awjuliani avatar Oct 11 '17 16:10 awjuliani

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 04 '20 23:01 lock[bot]