Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Resolved Reset enviroment

Discussion in 'ML-Agents' started by mateolopezareal, Jun 8, 2020.

  1. mateolopezareal

    mateolopezareal

    Joined:
    Jun 4, 2020
    Posts:
    54
    I am training multiple agents with the same brain. When they reach the goal, all of them has to restart, however if I call EndEpisode() method only the agent that did called this method will restart.
    I want to find a way that whenever an agent call the EndEpisode() method all the other agents also restart. I have found this thread where they talk about it: https://forum.unity.com/threads/resetting-environment.876439/
    I also read the documentation of how
    Code (CSharp):
    1. Academy.Instance.OnEnvironmentReset += ResetMethod
    works, but I did not understand it. How do I implement this Academy.Instance.OnEnvironmentReset, what do I write in the ResetMethod?
     
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    If you just need to reset your agents, you could handle that in a central agent controller script. Similar to the DecisionRequester component, but not attached to any specific agent instance. Something like this:

    Code (CSharp):
    1. using Unity.MLAgents;
    2. using UnityEngine;
    3.  
    4. public class AgentController : MonoBehaviour
    5. {
    6.     private MyAgent[] agents;
    7.  
    8.     private void Awake()
    9.     {
    10.         agents = FindObjectsOfType<MyAgent>();
    11.         Academy.Instance.AgentPreStep += OnAgentPreStep;
    12.     }
    13.  
    14.     private void OnDestroy()
    15.     {
    16.         if (Academy.IsInitialized)
    17.         {
    18.             Academy.Instance.AgentPreStep -= OnAgentPreStep;
    19.         }
    20.     }
    21.  
    22.     private void OnAgentPreStep(int academyStepCount)
    23.     {
    24.         bool resetAll = false;
    25.  
    26.         foreach (MyAgent agent in agents)
    27.         {
    28.             resetAll = resetAll || agent.MustReset; // your custom reset bool
    29.         }
    30.  
    31.         foreach (MyAgent agent in agents)
    32.         {
    33.             if (resetAll)
    34.             {
    35.                 agent.EndEpisode();
    36.             }
    37.             else
    38.             {
    39.                 agent.RequestDecision();
    40.             }
    41.         }
    42.     }
    43. }