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.

Question How to use Random.Range in every episode?

Discussion in 'ML-Agents' started by hyunsoo_unity, Sep 28, 2022.

  1. hyunsoo_unity

    hyunsoo_unity

    Joined:
    Mar 25, 2022
    Posts:
    1
    Hi, I want to have an agent created in a couple of places every episode. To this end, I created a function called RandomActivate function to create a randomValue using the Random.Range function, and to activate the desired agent through SetActive(false) or SetActive(true) through a conditional statement at a specific randomValue. The areas are set to 4 places, and it is a code that aims for the drone agent to go towards the goal. RandomActivate function is as follows:
    Code (CSharp):
    1. void RandomActivate()
    2.     {
    3.         randomValue = Random.Range(1, 5);
    4.         Debug.Log("randomValue in RandomActivate: " + randomValue);
    5.         if (randomValue == 1) //activate drone 1
    6.         {
    7.             Drone1.gameObject.SetActive(true);
    8.             Drone1Trans = Drone1.transform;
    9.             agentTrans = Drone1Trans;
    10.  
    11.             Goal1.gameObject.SetActive(true);
    12.             Goal1Trans = Goal1.transform;
    13.             goalTrans = Goal1Trans;
    14.  
    15.             Drone2.gameObject.SetActive(false);
    16.             Drone2Trans = null;
    17.             Drone3.gameObject.SetActive(false);
    18.             Drone3Trans = null;
    19.             Drone4.gameObject.SetActive(false);
    20.             Drone4Trans = null;
    21.  
    22.             Goal2.gameObject.SetActive(false);
    23.             Goal2Trans = null;
    24.             Goal3.gameObject.SetActive(false);
    25.             Goal3Trans = null;
    26.             Goal4.gameObject.SetActive(false);
    27.             Goal4Trans = null;
    28.  
    29.  
    30.         }
    31.  
    32.         else if (randomValue == 2) //activate drone 2
    33.         {
    34.             Drone2.gameObject.SetActive(true);
    35.             Drone2Trans = Drone2.transform;
    36.             agentTrans = Drone2Trans;
    37.  
    38.             Goal2.gameObject.SetActive(true);
    39.             Goal2Trans = Goal2.transform;
    40.             goalTrans = Goal2Trans;
    41.  
    42.             Drone1.gameObject.SetActive(false);
    43.             Drone1Trans = null;
    44.             Drone3.gameObject.SetActive(false);
    45.             Drone3Trans = null;
    46.             Drone4.gameObject.SetActive(false);
    47.             Drone4Trans = null;
    48.  
    49.             Goal1.gameObject.SetActive(false);
    50.             Goal1Trans = null;
    51.             Goal3.gameObject.SetActive(false);
    52.             Goal3Trans = null;
    53.             Goal4.gameObject.SetActive(false);
    54.             Goal4Trans = null;
    55.  
    56.         }
    57.  
    58.         else if (randomValue == 3) //activate drone 3
    59.         {
    60.             Drone3.gameObject.SetActive(true);
    61.             Drone3Trans = Drone3.transform;
    62.             agentTrans = Drone3Trans;
    63.  
    64.             Goal3.gameObject.SetActive(true);
    65.             Goal3Trans = Goal3.transform;
    66.             goalTrans = Goal3Trans;
    67.  
    68.             Drone1.gameObject.SetActive(false);
    69.             Drone1Trans = null;
    70.             Drone2.gameObject.SetActive(false);
    71.             Drone2Trans = null;
    72.             Drone4.gameObject.SetActive(false);
    73.             Drone4Trans = null;
    74.  
    75.  
    76.             Goal1.gameObject.SetActive(false);
    77.             Goal1Trans = null;
    78.             Goal2.gameObject.SetActive(false);
    79.             Goal2Trans = null;
    80.             Goal4.gameObject.SetActive(false);
    81.             Goal4Trans = null;
    82.  
    83.         }
    84.  
    85.         else if (randomValue == 4) //activate drone 4
    86.         {
    87.             Drone4.gameObject.SetActive(true);
    88.             Drone4Trans = Drone4.transform;
    89.             agentTrans = Drone4Trans;
    90.  
    91.             Goal4.gameObject.SetActive(true);
    92.             Goal4Trans = Goal4.transform;
    93.             goalTrans = Goal4Trans;
    94.  
    95.             Drone1.gameObject.SetActive(false);
    96.             Drone1Trans = null;
    97.             Drone2.gameObject.SetActive(false);
    98.             Drone2Trans = null;
    99.             Drone3.gameObject.SetActive(false);
    100.             Drone3Trans = null;
    101.  
    102.             Goal1.gameObject.SetActive(false);
    103.             Goal1Trans = null;
    104.             Goal2.gameObject.SetActive(false);
    105.             Goal2Trans = null;
    106.             Goal3.gameObject.SetActive(false);
    107.             Goal3Trans = null;
    108.         }
    109.     }
    I tried to call this function to OnEpisodeBegin() to select a random agent for every episode, but if I'm lucky, it works 2-3 times successfully, but it doesn't work consistently. I checked the log running through Debug.Log, the order of the first run and subsequent calls are quite different:

    When called for the first time:
    UnityEngine.Debug:Log (object)
    MyAgent:RandomActivate () (at Assets/MyProject/Scripts/MyAgent.cs:155)
    MyAgent:OnEpisodeBegin () (at Assets/MyProject/Scripts/MyAgent.cs:114)
    Unity.MLAgents.Agent:_AgentReset () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:1299)
    Unity.MLAgents.Academy:ForcedFullReset () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Academy.cs:548)
    Unity.MLAgents.Academy:EnvironmentStep () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Academy.cs:562)
    Unity.MLAgents.AcademyFixedUpdateStepper:FixedUpdate () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Academy.cs:43)

    The funny thing is, after the second time, the calling behavior changes again:

    (Case 1) When called after the second time:
    UnityEngine.Debug:Log (object)
    MyAgent:RandomActivate () (at Assets/MyProject/Scripts/MyAgent.cs:155)
    MyAgent:OnEpisodeBegin () (at Assets/MyProject/Scripts/MyAgent.cs:114)
    Unity.MLAgents.Agent:_AgentReset () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:1299)
    Unity.MLAgents.Agent:EndEpisodeAndReset (Unity.MLAgents.Agent/DoneReason) (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:798)
    Unity.MLAgents.Agent:EndEpisode () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:772)
    MyAgent:OnActionReceived (Unity.MLAgents.Actuators.ActionBuffers) (at Assets/MyProject/Scripts/MyAgent.cs:91)
    Unity.MLAgents.Actuators.VectorActuator:OnActionReceived (Unity.MLAgents.Actuators.ActionBuffers) (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Actuators/VectorActuator.cs:76)
    Unity.MLAgents.Actuators.ActuatorManager:ExecuteActions () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Actuators/ActuatorManager.cs:295)
    Unity.MLAgents.Agent:AgentStep () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:1344)
    Unity.MLAgents.Academy:EnvironmentStep () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Academy.cs:589)
    Unity.MLAgents.AcademyFixedUpdateStepper:FixedUpdate () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Academy.cs:43)

    (Case 2) When called after the second time:
    UnityEngine.Debug:Log (object)
    MyAgent:RandomActivate () (at Assets/MyProject/Scripts/MyAgent.cs:155)
    MyAgent:OnEpisodeBegin () (at Assets/MyProject/Scripts/MyAgent.cs:114)
    Unity.MLAgents.Agent:LazyInitialize () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:486)
    Unity.MLAgents.Agent:OnEnable () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:365)
    MyAgent:RandomActivate () (at Assets/MyProject/Scripts/MyAgent.cs:158)
    MyAgent:OnEpisodeBegin () (at Assets/MyProject/Scripts/MyAgent.cs:114)
    Unity.MLAgents.Agent:_AgentReset () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:1299)
    Unity.MLAgents.Agent:EndEpisodeAndReset (Unity.MLAgents.Agent/DoneReason) (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:798)
    Unity.MLAgents.Agent:EndEpisode () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:772)
    MyAgent:OnActionReceived (Unity.MLAgents.Actuators.ActionBuffers) (at Assets/MyProject/Scripts/MyAgent.cs:91)
    Unity.MLAgents.Actuators.VectorActuator:OnActionReceived (Unity.MLAgents.Actuators.ActionBuffers) (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Actuators/VectorActuator.cs:76)
    Unity.MLAgents.Actuators.ActuatorManager:ExecuteActions () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Actuators/ActuatorManager.cs:295)
    Unity.MLAgents.Agent:AgentStep () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Agent.cs:1344)
    Unity.MLAgents.Academy:EnvironmentStep () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Academy.cs:589)
    Unity.MLAgents.AcademyFixedUpdateStepper:FixedUpdate () (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Academy.cs:43)

    How can I fix this problem, my fellow developers?