Search Unity

Activating / changing models on runtime

Discussion in 'ML-Agents' started by Darian4, Feb 27, 2020.

  1. Darian4

    Darian4

    Joined:
    Nov 5, 2018
    Posts:
    3
    Let’s say I have a guard who follows a path. His movements are totally scripted so the player can anticipate his moves. But the player is clumsy and got detected, now I want the guard to attack the player, but with a trained brain this time so he’s a harder opponent.

    1: How can I switch from a scripted behavior to a ML one?

    Now the guard has been injured. I want him to try and escape, maybe grab an healthpack or call for help, also a trained behavior.

    2: How can I switch from the “fight” brain to the “flee” brain?

    And finally, let’s say I have guards in “fight” mode, others in “flee” mode. I want to train one brain while using an already trained for the other.

    3: How can I train on only one brain, using inference on the other?
     
  2. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    (answering these out of order)
    3. You can set some Agents to be "Inference Only", so that they'll use inference even if others are training. There's more details in the description of "Behavior Type" here: https://github.com/Unity-Technologi...Environment-Design-Agents.md#agent-properties

    2. If you have a NNModel loaded, you can set the model that the Agent will use with Agent.GiveModel() (https://github.com/Unity-Technologi...4.1/com.unity.ml-agents/Runtime/Agent.cs#L308) There's an example of loading an NNModel from disk here: https://github.com/Unity-Technologi...redAssets/Scripts/ModelOverrider.cs#L114-L128
    Our WallJump example also has several NNModels on the Agent, and assigns them with GiveModel (https://github.com/Unity-Technologi...amples/WallJump/Scripts/WallJumpAgent.cs#L318)

    1. This is a bit trickier depending on how your scripting is implemented. Probably the easiest way to do this is make your Agent.AgentAction() method do nothing during the times that you want the guard to follow the script, then handle the movement outside of the Agent implementation. If you know ahead of time that the guard is in "scripted" mode, you can avoid calling RequestDecision() on their Agent; this will avoid spending any time on collecting observations or doing inference. Depending on your setup, RequestDecision() might be getting called from the DecisionRequester script, so you'd want to replicate that logic somewhere else.
     
    Sab_Rango likes this.
  3. Darian4

    Darian4

    Joined:
    Nov 5, 2018
    Posts:
    3
    Thank you for your answers, it’s very helpful.

    3. Of course, now that you mentioned it I remember seeing it and wondering what was the purpose of that switch… and I forgot about it. So, that’s the purpose!

    2. Ok, using GiveModel() seems easy enough.

    1. Strangely I thought it would be the easiest, it turns out it’s the most difficult. While I was looking for a solution I imagined something like that, which led to the next question (but it’s just out of curiosity):

    I thought of giving each guard several brains, each brain being a component of an empty and each guard “subscribing” to the correct brain depending on the guard’s situation. For example:
    - A guard script on a guard prefab without any behavior parameter component.
    - A “flee brain” script associated with an empty and a behavior parameter component.
    - A “fight brain”, same thing.
    The guard sends his array of observations to the correct brain and gets an array of actions from it.

    Then I wondered, why giving each guard several brains? Why not having a pool of brains for all guards, each guard subscribing to the correct brain depending on his situation? Having 3 guards sending their observations to the same “flee” brain instance for example.

    1 Is it possible?
    2 Is there any advantage? For example, if there are thousands of guards (a very well-guarded place) maybe avoiding having thousands of instantiated neural networks in memory but only a few/one.


    Once again, it’s just a question out of curiosity.
     
  4. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    I think what you described is essentially what the WallJump example scene does - it has multiple NNModels, but they should be shared across the Agents.
     
    Sab_Rango likes this.
  5. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    so you cant set behaviour type to inference from script? thats bizarre and annoying, why not?