Unity-Technologies / ml-agents

The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinforcement learning and imitation learning.
https://unity.com/products/machine-learning-agents
Other
17.19k stars 4.16k forks source link

RayPerceptionSensor2D don't work #4151

Closed autoli closed 4 years ago

autoli commented 4 years ago

Describe the bug When I add the RayPerceptionSensor2D component to an agent, it report an error as follows: NullReferenceException: Object reference not set to an instance of an object.

and then I add the tag items except the agent to clear the error, but the ray can not be seen as if it detect itself, the cast sphere can only be seen inside agent.

I print the ray information and find out the rayInfo.rayOutput.HitFraction is always zero whatever I change the sensor settings. I change the HitFraction for a try, the ray can be seen actually but it can't detect anything. All rays are hit at the end of ray length.

However, I add the RayPerceptionSensor3D for a simple test, it seems true and the ray can be seen clearly witout the error above.

Environment:

chriselion commented 4 years ago

Could you please provide the full callstack from the NullReferenceException? I can't reproduce the error.

2D raycasts behave differently than 3D ones by default - they will "hit" objects that they start in. You can change this behavior in Project Settings > Physics 2D > Queries Start In Colliders. If that's too big of a change for your project, you can change the start position of the rays by adding another GameObject parented to the Agent's GameObject, adding the RayPerceptionSensorComponent2D to that, and adjusting it's transform (and make sure the Use Child Sensors checkbox is checked in the Agent component in the Inspector).

autoli commented 4 years ago

Thanks a lot ! The problem seems to be solved by changing Project Settings > Physics 2D > Queries Start In Colliders. The rays can normally hit the other objects. But when I add RayPerceptionSensor2D component, the error is still reported. It is like the detectable tag is null when added although it doesn't matter.

the error is: NullReferenceException: Object reference not set to an instance of an object Unity.MLAgents.Sensors.RayPerceptionSensor.PerceiveSingleRay (Unity.MLAgents.Sensors.RayPerceptionInput input, System.Int32 rayIndex, Unity.MLAgents.Sensors.DebugDisplayInfo+RayInfo& debugRayOut) (at E:/important data/ml-agents-release_2/ml-agents-release_2/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs:465) Unity.MLAgents.Sensors.RayPerceptionSensorComponentBase.OnDrawGizmosSelected () (at E:/important data/ml-agents-release_2/ml-agents-release_2/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs:287) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

The next error is also reported when I am typing the tag name. Tag: Ite is not defined. UnityEngine.GameObject:CompareTag(String) Unity.MLAgents.Sensors.RayPerceptionSensor:PerceiveSingleRay(RayPerceptionInput, Int32, RayInfo&) (at E:/important data/ml-agents-release_2/ml-agents-release_2/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs:467) Unity.MLAgents.Sensors.RayPerceptionSensorComponentBase:OnDrawGizmosSelected() (at E:/important data/ml-agents-release_2/ml-agents-release_2/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs:287) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

The above errors can be cleared when I finish the tag add.

chriselion commented 4 years ago

Thanks for the callstacks, I think I see the problems now (and I can reproduce them with the 3D Raycasts in our examples). The internal tracker IDs for these are MLA-1106 and MLA-1109.

chriselion commented 4 years ago

PR to fix the exceptions are here: https://github.com/Unity-Technologies/ml-agents/pull/4155 (unfortunately I don't think we can easily prevent the "is not defined" messages from spamming the console).

autoli commented 4 years ago

Thanks for your reply! The "is not defined" can be cleared manually in the console which . Can I ask for another help-wanted? Can the mlagent be trained correctly when I change the timescale in Unity for an acceleration? Because I use the ML to 2D game which contains too much trigger and initialization by scripts. As a layman, it's hard for me to modify the scripts to use multiple training areas within the same scene or launch scenes asynchronously by dragging. So it trains slowly and I try for increasing timescale that really makes the game speed up. it seems that I have to change the decision request period. I don't know clearly whether this method can work! Maybe I have to change some setting about the ML or there exsits other methods for this situation. Thanks again for your patience!

chriselion commented 4 years ago

Can you please post on the Unity forum (https://forum.unity.com/forums/ml-agents.453/) for the other question?

github-actions[bot] commented 3 years ago

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.