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

Multiple scenes - multiple academies or just one? #3371

Closed lukemadera closed 4 years ago

lukemadera commented 4 years ago

Is your feature request related to a problem? Please describe. I receive this error:

NullReferenceException: Object reference not set to an instance of an object.
02-06 07:49:57.621 17938 17967 E Unity   :   at MLAgents.Brain.BrainDecideAction () [0x00000] in <00000000000000000000000000000000>:0 
02-06 07:49:57.621 17938 17967 E Unity   :   at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
02-06 07:49:57.621 17938 17967 E Unity   :   at MLAgents.Academy.EnvironmentStep ()

I currently have 3 scenes - 1 master (always loaded) and 2 different levels that are dynamically loaded based on the character's position in the world. I only receive this issue sometimes and I suspect it may be related to having multiple scenes. The documentation says to only have 1 Academy per scene but does not mention anything about multiple scenes (which I believe is common for many games). Do I need 1 academy per scene? Or one total (in the Master Scene)? Currently I have 1 academy in the master scene, and 1 in Scene A (level 1). I think the issue is that a game object (agent) in Scene A is being destroyed but uses the Master Academy, so then the academy tries to environment step but can't get the (now destroyed) agent. So I need to either properly destroy the agent (how would I do that?) so the Academy knows not to look for it anymore OR attach the scene A agents to the scene A academy and have 1 academy per scene so the agents and academy are both destroyed at the same time.

Describe the solution you'd like A description in the docs of how to work with multiple scenes. Specifically:

  1. how to destroy an agent properly (remove it from an Academy)
  2. how to reference / use multiple academies (e.g. attach agent A to academy A) If it is advised to use 1 academy per scene or 1 academy across all scenes.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Related to https://github.com/Unity-Technologies/ml-agents/issues/1666

chriselion commented 4 years ago

Hi @lukemadera, You're in luck - as of the next release (tentatively being released next week), Academy will be a singleton instead of a MonoBehavior. These changes are already on the master branch if you want to try them out.

lukemadera commented 4 years ago

Thanks for the prompt reply @chriselion and good to hear! I'm not quite sure what the answer to my question is though - so the suggested approach would then be to only have 1 academy for the entire game (rather than 1 per scene)? Performance wise I could see that getting quite heavy, unless we can activate / deactivate agents & brains at will. Or would there be a way to break things up?

chriselion commented 4 years ago

On the current release: I think you'll need to only have one Academy in the master scene. When you disable the Agent it should remove the Agent from the Academy: https://github.com/Unity-Technologies/ml-agents/blob/0.13.1/UnitySDK/Assets/ML-Agents/Scripts/Agent.cs#L290-L303 And I believe this happens when you destroy the GameObject that the Agent is attached to.

On the master branch / upcoming release: You don't need to add an Academy to the scene anymore (and in fact, you can't). The removal code is roughly the same, and called from Agent.OnDisable

lukemadera commented 4 years ago

Thanks! Yeah I removed the 2nd academy I had in the 2nd scene and that seemed to fix things. The docs say "1 academy in the scene" so I assumed it was 1 per scene, not 1 per game; but it errors if there's 1 per scene. Thanks for the help!

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.