ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

N multiple agents using same brain

Open hsaikia opened this issue 8 years ago • 6 comments

I need to train N multiple agents using the same brain. Since the number of agents is variable, I instantiate them through a script S and an XYZ_Agent prefab. The agent prefab contains the XYZ_Agent script. Now the only thing that remains is to assign the XYZ_Brain GameObject in the scene to the Brain field of the individual XYZ_Agent scripts at runtime. How can I do this?

Right now, I have a public field for the XYZ_Brain GameObject in script S, where I assign it and while instantiating my agents, I use

XYZ_Clone.GetComponent<XYZ_Agent>().brain = XYZ_Brain.GetComponent<Brain>();

but this doesn't work. Do I need to create clones of the Brain as well?

Thank you!

hsaikia avatar Apr 17 '18 15:04 hsaikia

Hi, you must use the Agent.GiveBrain(Brain b) method on your XYZ_Clone. Let us know how it worked.

vincentpierre avatar Apr 17 '18 16:04 vincentpierre

Here's an example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnviroSpawn : MonoBehaviour {

    public GameObject prefab;
    public int Count = 1;
    public Vector3 Dimensions = Vector3.one; //Size of each prefab environment in Unity units
    public Brain Brain;

    private bool done = false;
    public int delay = 500;

    void Update(){
        if(!done){
            if(delay-- < 0){
                Populate();
                done = true;
            }
        }
    }

    public void Populate(){
        int sideCount = (int)Mathf.Pow(Count, 1.0f/3) + 1;

        Vector3 origin = new Vector3(Dimensions.x - (sideCount * Dimensions.x)/2,
                                     Dimensions.y - (sideCount * Dimensions.y)/2,
                                     Dimensions.z - (sideCount * Dimensions.z)/2);
        int created = 0;
        for(int i = 0; i < sideCount; i++){
            for(int j = 0; j < sideCount; j++){
                for(int k = 0; k < sideCount; k++){
                    if(created++ >= Count) break;

                    GameObject instance = Instantiate<GameObject>(prefab, origin + new Vector3(i * Dimensions.x, 
                                                                                               j * Dimensions.y, 
                                                                                               k * Dimensions.z), 
                                                                  Quaternion.identity);
                    Agent agent = instance.GetComponentInChildren<Agent>();
                    if(agent != null)
                        agent.GiveBrain(Brain);

                }
                if(created >= Count) break;
            }
            if(created >= Count) break;
        }
    }
}

(The delay is only there to give me time to switch to the profiler window.)

See also: https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Learning-Environment-Design-Agents.md#instantiating-an-agent-at-runtime

jo3w4rd avatar Apr 17 '18 16:04 jo3w4rd

It worked! thanks a lot everyone!

hsaikia avatar Apr 18 '18 09:04 hsaikia

Thanks for reaching out to us. Hopefully you were able to resolve your issue. We are closing this due to inactivity, but if you need additional assistance, feel free to reopen the issue.

xiaomaogy avatar May 17 '18 22:05 xiaomaogy

The docs' code reads "assign a Brain instance to the new Agent and initialize it by calling its AgentReset() method" However, several members of the ml agents dev team said, one shouldn't manually call AgentReset(). Could we get a clarification on this plz @xiaomaogy ? Link: https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Learning-Environment-Design-Agents.md#instantiating-an-agent-at-runtime

MentalGear avatar Aug 10 '19 10:08 MentalGear

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 31 '21 04:01 github-actions[bot]