Search Unity

Question Agent goes up the floor one floor at a time

Discussion in 'ML-Agents' started by Yuulis04, May 28, 2021.

  1. Yuulis04

    Yuulis04

    Joined:
    Apr 4, 2021
    Posts:
    28
    Hello. I created a simple building and I want agent to go up the floor one floor at a time.
    I made the code below, but agent goes up to the top floor at once. How should I solve this?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.MLAgents;
    5. using Unity.MLAgents.Sensors;
    6. using Unity.MLAgents.Actuators;
    7. public class AgentControl_3Floor_Lv0 : Agent
    8. {
    9.     Rigidbody rBody;
    10.     public Transform Water;
    11.     private int nowFloor;  // Agent's floor
    12.     private float AgentSpeed = 0.5f;  // Agent's speed
    13.     private float FirstWaterHeight = -10.0f;  // Water's height at Initializing
    14.     private bool onEpisode = false;  // During Episode?
    15.     // At Initializing
    16.     public override void Initialize()
    17.     {
    18.         this.rBody = GetComponent<Rigidbody>();
    19.     }
    20.     // Spawning Agent
    21.     public void SpawnAgent()
    22.     {
    23.         this.transform.localPosition = new Vector3(Random.Range(-19.5f, 19.5f), 1.5f, Random.Range(-14.5f, 14.5f));
    24.         nowFloor = 1;
    25.     }
    26.     // Episode begin
    27.     public override void OnEpisodeBegin()
    28.     {
    29.         onEpisode = false;
    30.         this.rBody.angularVelocity = Vector3.zero;
    31.         this.rBody.velocity = Vector3.zero;
    32.         this.transform.rotation = Quaternion.identity;
    33.         SpawnAgent();
    34.         Water.localPosition = new Vector3(-25.0f, FirstWaterHeight, -20.0f);
    35.         onEpisode = true;
    36.     }
    37.     // Collecting observations  size = 4
    38.     public override void CollectObservations(VectorSensor sensor)
    39.     {
    40.         sensor.AddObservation(this.transform.localPosition);  // x, y, z
    41.         sensor.AddObservation(Water.localPosition.y); // y
    42.     }
    43.     // Choosing Agent's action  size = 4
    44.     public override void OnActionReceived(float[] action)
    45.     {
    46.         Vector3 dirToGo = Vector3.zero;
    47.         Vector3 rotateDir = Vector3.zero;
    48.         int act = (int)action[0];
    49.         if (act == 1) dirToGo = transform.forward;  // forward
    50.         if (act == 2) dirToGo = transform.forward * -1.0f;  // back
    51.         if (act == 3) rotateDir = transform.up;  // right rotation
    52.         if (act == 4) rotateDir = transform.up * -1.0f;  // left rotation
    53.         this.transform.Rotate(rotateDir, Time.deltaTime * 100f);
    54.         this.rBody.AddForce(dirToGo * AgentSpeed, ForceMode.VelocityChange);
    55.         if (this.transform.localPosition.y < Water.localPosition.y)
    56.         {
    57.             AddReward(-1.0f);
    58.             EndEpisode();
    59.         }
    60.         // Reward step by step
    61.         if (nowFloor == 1) AddReward(0.6f / MaxStep);
    62.         if (nowFloor == 2) AddReward(0.8f / MaxStep);
    63.         if (nowFloor == 3) AddReward(1.0f / MaxStep);
    64.     }
    65.     // When touching something
    66.     public void OnTriggerEnter (Collider col)
    67.     {
    68.         // During Episode?
    69.         if (onEpisode)
    70.         {
    71.             // Touching UpStair
    72.             if (col.gameObject.tag == "upStair")
    73.             {
    74.                 this.rBody.angularVelocity = Vector3.zero;
    75.                 this.rBody.velocity = Vector3.zero;
    76.                 this.transform.rotation = Quaternion.identity;
    77.                 int xPosPuls = -1;
    78.                 int zPosPuls = -1;
    79.                 if (this.transform.localPosition.x > 0) xPosPuls = 1;
    80.                 if (this.transform.localPosition.z > 0) zPosPuls = 1;
    81.                 nowFloor++;
    82.                 this.transform.Translate(xPosPuls * -1.0f, (nowFloor - 1) * 7.0f + 1.5f, zPosPuls * 4.0f);
    83.                 this.transform.Rotate(0f, xPosPuls * -90.0f, 0f);
    84.             }
    85.             // Touching DownStair
    86.             else if (col.gameObject.tag == "downStair")
    87.             {
    88.                 this.rBody.angularVelocity = Vector3.zero;
    89.                 this.rBody.velocity = Vector3.zero;
    90.                 this.transform.rotation = Quaternion.identity;
    91.                 int xPosPuls = -1;
    92.                 int zPosPuls = -1;
    93.                 if (this.transform.localPosition.x > 0) xPosPuls = 1;
    94.                 if (this.transform.localPosition.z > 0) zPosPuls = 1;
    95.                 nowFloor--;
    96.                 this.transform.Translate(xPosPuls * -1.0f, (nowFloor - 1) * -7.0f + 1.5f, zPosPuls * -4.0f);
    97.                 this.transform.Rotate(0f, xPosPuls * -90.0f, 0f);
    98.             }
    99.             // Touching Obstacle
    100.             else if (col.gameObject.tag == "obstacle")
    101.             {
    102.                 AddReward(-0.6f);
    103.                 EndEpisode();
    104.             }
    105.         }
    106.         else
    107.         {
    108.             SpawnAgent();
    109.         }
    110.     }
    111.     // When touching Wall
    112.     public void OnCollisionEnter (Collision col)
    113.     {
    114.         // During Episode?
    115.         if (onEpisode)
    116.         {
    117.             if (col.gameObject.tag == "wall") AddReward(-0.6f);
    118.             EndEpisode();
    119.         }
    120.         else
    121.         {
    122.             SpawnAgent();
    123.         }
    124.     }
    125. }