Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question CollectObservations() called twice every time. custom RequestDecision()

Discussion in 'ML-Agents' started by dorinon, Feb 22, 2022.

  1. dorinon

    dorinon

    Joined:
    Dec 29, 2021
    Posts:
    1
    like i said every time i request decision CollectObservations fires twice call requset decision in OnEpisodeBegin, and im pretty sure thats why

    version: 1.60

    my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.MLAgents;
    5. using Unity.MLAgents.Actuators;
    6. using Unity.MLAgents.Sensors;
    7.  
    8. public class boxPlacerAgent : Agent
    9. {
    10.  
    11.     private const float minScale = 0.005F;
    12.     private const float maxScale = 0.015F;
    13.     private int generation = 0;
    14.     public int quickFix = 0;
    15.  
    16.     public static List<GameObject> cubes = new List<GameObject>();
    17.  
    18.     public override void OnEpisodeBegin()
    19.     {
    20.         Debug.Log("start");
    21.         foreach (GameObject cube in cubes){
    22.             Destroy(cube);
    23.         }
    24.  
    25.  
    26.         cubes.Clear();
    27.         if (cubes.Count == 0) { RequestDecision(); }
    28.  
    29.  
    30.         generation++;
    31.     }
    32.  
    33.     public override void CollectObservations(VectorSensor sensor)
    34.     {
    35.  
    36.         Debug.Log("called");
    37.  
    38.         if (quickFix%2 == 0)
    39.         {
    40.             for (int y = 0; y < 5; y++) {
    41.                 NewCube();
    42.             }
    43.  
    44.             foreach (GameObject cube in cubes)
    45.             {
    46.                 sensor.AddObservation(cube.transform.localScale);
    47.             }
    48.             quickFix++;
    49.         }
    50.     }
    51.     public override void OnActionReceived(ActionBuffers actions)
    52.     {
    53.         for (int o = 0; o < 5; o++)
    54.         {
    55.             cubes[o].transform.localPosition = new Vector3(actions.ContinuousActions[o * 3] / 100, actions.ContinuousActions[o * 3 + 1] / 100, actions.ContinuousActions[o * 3 + 2] / 100);
    56.         }
    57.  
    58.         for(int i = 0; cubes.Count > i; i++)
    59.         {
    60.  
    61.             if (!(1.219 < cubes[i].transform.localPosition.y * 100f * 1.219f && 1.2955 < cubes[i].transform.localPosition.z * 100 * 1.2955 && 6.058 < cubes[i].transform.localPosition.x * 100 * 6.058))
    62.             {
    63.                 if (!(-1.219 > cubes[i].transform.localPosition.y * 100f * 1.219f && -1.2955 > cubes[i].transform.localPosition.z * 100 * 1.2955 && 0 > cubes[i].transform.localPosition.x * 100 * 6.058))
    64.                 {
    65.                 }
    66.                 else
    67.                 {
    68.                     AddReward(cubes[i].transform.localScale.y * cubes[i].transform.localScale.z * cubes[i].transform.localScale.x);
    69.                 }
    70.             }
    71.             Debug.Log("end");
    72.             EndEpisode();
    73.         }
    74.     }
    75.  
    76.  
    77.     private GameObject NewCube()
    78.     {
    79.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    80.         cube.transform.SetParent(gameObject.transform);
    81.         cube.transform.localScale = new Vector3(RandomScale(), RandomScale(), RandomScale());
    82.         cubes.Add(cube);
    83.  
    84.  
    85.         return cube;
    86.  
    87.     }
    88.  
    89.     private static float RandomScale()
    90.     {
    91.         return Random.Range(minScale, maxScale);
    92.     }
    93.  
    94.     private void OnTriggerEnter(Collider other)
    95.     {
    96.         if (other.gameObject == gameObject)
    97.         {
    98.  
    99.             Destroy(other.gameObject);
    100.         }
    101.  
    102.         if (other.gameObject.name == "Cube")
    103.         {
    104.  
    105.             Debug.Log("end");
    106.             AddReward(-0.1f);
    107.             EndEpisode();
    108.  
    109.  
    110.  
    111.         }
    112.     }
    113. }
    114.  
    115.  
     
    Last edited: Feb 22, 2022