Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Question about branches

Discussion in 'ML-Agents' started by ilaydanil, May 21, 2020.

  1. ilaydanil

    ilaydanil

    Joined:
    May 17, 2020
    Posts:
    20
    Hey,
    I am confused about how to use branches. What is the difference between using multiple braches with different sizes and only one branch with a big size?
    When I use multiple branches for one agent, how the agent will reach those branches in the code?
     
  2. MrWetsnow

    MrWetsnow

    Joined:
    Jan 5, 2020
    Posts:
    60
    When the AI runs, it will pick one value in *each* branch and pass that to your code. It's useful when you need to have your agent make two decisions at the same time. For example, if you only need your agent to decide which way to walk, you would have 1 branch with 5 values. (don't move, move left, move right, move up, move down).

    If you also want your agent, in the same turn as walking, to do something else (for example, pick a direction to look in), you would add a second branch, with 4 values (up, down, right, left).

    Then your code would look something like this:

    Code (CSharp):
    1.  
    2. public override void OnActionReceived(float[] branches) {
    3.   int movement = Mathf.FloorToInt(branches[0])
    4.   int lookDirection = Mathf.FloorToInt(branches[1])
    5.  
    6.  // now use these values to make your agent do something
    7.  if (movement == 0) { row = -1; col = -1; };
    8.  if (movement == 1) { row = -1; col = 1; }
    9.  if (movement == 2) { row = 1; col = -1; }
    10.  if (movement == 3) { row = 1; col = 1; }
    11.  
    12.  if (lookDirection == 0) { angle = 0; };
    13.  if (lookDirection == 1) { angle = 90; }
    14.  if (lookDirection == 2) { angle = 180; }
    15.  if (lookDirection == 3) { angle = 270; }
    16.  
    17. // Now execute the actions
    18. DoMovement(row, col);
    19. SetLookDirection(lookDirection);
    20. }
    21.  
    On the other hand, if you want your agent to either move in a certain direction, *or* look in a certain direction, you would use one branch with 9 values: (don't move, move left, move right, move up, move down, look up, look down, look right, look left). And your code would execute only one of those in a single turn.

    Note that you can use CollectDiscreteActionMasks() to mask out the actions that are not possible. For example, if you are next to a wall on your left, you would add the index of moving left to the mask so it's not picked as a possible action.

    Hope that helps.
     
    heartingNinja likes this.