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 Iterating through objects to observe with a foreach loop

Discussion in 'ML-Agents' started by GeorgGrech, Mar 4, 2023.

  1. GeorgGrech


    Nov 26, 2021
    Good day,

    I'm working on a project using Unity ML as part of a dissertation, and I'm wondering whether the way I have my observations set up makes sense.

    In my project, an agent needs to go around the map collecting resources of different types, filling up their inventory, then "selling" them at its base in exchange for points.

    I want the agent to observe the properties of the resources around it, then make a decision on the best one, taking account things like distances and resource type. My agent "scans" an area around it to get the properties of the closest resources, then requests a decision.

    Since the agent has to observe multiple objects, I've used a foreach loop in CollectObservations.

    Code (CSharp):
    1.     public override void CollectObservations(VectorSensor sensor)
    2.     {
    3.         try
    4.         {
    5.             if (resourcesTrackingList != null && resourcesTrackingList.Count > 0)
    6.             {
    7.                 foreach (ResourceData resourceData in resourcesTrackingList)
    8.                 {
    9.                     sensor.AddObservation(resourceData.distanceFromBase);
    10.                     sensor.AddObservation(resourceData.distanceFromPlayer);
    11.                     sensor.AddOneHotObservation((int)resourceData.type, resourceData.numOfTypes);
    12.                     sensor.AddObservation(resourceData.takenAmount);
    13.                 }
    14.             }
    16.             sensor.AddObservation(enemyPlayer.inventoryAmountFree); //Keep track of inventory
    17.         }
    19.         catch
    20.         {
    21.             Debug.Log("Exception caught in observations");
    22.         }
    23.     }
    Does this system make sense? Or there another way I should go about it?