Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

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?