Exemplo n.º 1
0
    public void StartNewTrainingMode()
    {
        Destroy(playerGO);

        // Create population brains
        agentGenomeList = new List <AgentGenome>();

        BodyGenome templateBodyGenome = new BodyGenome();

        templateBodyGenome.InitializeGenomeAsDefault();
        //BodyGenome bodyGenomeTemplate = new BodyGenome();
        //bodyGenomeTemplate.CopyBodyGenomeFromTemplate(bodyTemplate);

        for (int i = 0; i < trainingPopulationSize; i++)
        {
            AgentGenome newGenome = new AgentGenome(i);
            newGenome.InitializeBodyGenomeFromTemplate(templateBodyGenome);
            newGenome.InitializeRandomBrainFromCurrentBody(0.033f);
            agentGenomeList.Add(newGenome);
        }

        // CrossoverSettings:
        trainingSettingsManager = new TrainingSettingsManager(0.06f, 0.35f, 0.1f, 0.01f);
        GameObject dummyAgentGO = new GameObject("DummyAgent");

        dummyAgent       = dummyAgentGO.AddComponent <Agent>();
        dummyStartGenome = new StartPositionGenome(Vector3.zero, Quaternion.identity);

        ResetTrainingForNewGen();
        //Brain brain = new Brain()
        //Debug.Log(dummyAgent.testModule.);

        // Create Visible Display Agent to observe behavior

        /*displayStartPos = new StartPositionGenome(Vector3.zero, Quaternion.identity);
         * GameObject agentGO = Instantiate(Resources.Load("PredatorPrey/PredatorPrefab")) as GameObject; ; // GameObject.CreatePrimitive(PrimitiveType.Sphere);
         * //displayAgent
         * agentGO.name = "Guinea Pig";
         * //agentGO.transform.parent = gameObject.transform;
         * agentGO.transform.localPosition = displayStartPos.agentStartPosition;
         * agentGO.transform.localRotation = displayStartPos.agentStartRotation;
         * //agentGO.GetComponent<Collider>().enabled = false;
         * displayAgent = agentGO.AddComponent<Agent>();
         * displayAgent.isVisible = true;
         * displayAgent.InitializeAgentFromTemplate(agentGenomeList[0], displayStartPos);
         * // hook modules:
         * displayAgent.testModule.enemyTransform = targetGO.transform;
         * displayAgent.testModule.bias[0] = 1f;
         * displayAgent.testModule.enemyTestModule = displayAgent.testModule; // self as target, to zero out targetvel
         */
        ResetTrainingAgentAndEnvironment();
    }
Exemplo n.º 2
0
    // Representative system will be expanded later - for now, just defaults to Top # of performers
    public PlayerPopulation(int index, BodyGenome bodyTemplate, int numGenomes, int numPerfReps)
    {
        this.index = index;

        // Re-Factor:
        bodyGenomeTemplate = new BodyGenome();
        bodyGenomeTemplate.CopyBodyGenomeFromTemplate(bodyTemplate);
        //graphKing = new TheGraphKing();

        popSize = numGenomes;
        //this.numBaseline = numBaseline;

        // Create blank AgentGenomes for the standard population
        agentGenomeList    = new List <AgentGenome>();
        historicGenomePool = new List <AgentGenome>();
        //baselineGenomePool = new List<AgentGenome>();

        for (int j = 0; j < numGenomes; j++)
        {
            AgentGenome agentGenome = new AgentGenome(j);
            agentGenome.InitializeBodyGenomeFromTemplate(bodyGenomeTemplate);
            agentGenome.InitializeRandomBrainFromCurrentBody(0.2f);
            agentGenomeList.Add(agentGenome);
        }
        //RepopulateBaselineGenomes();
        //AppendBaselineGenomes();

        // Representatives:
        numPerformanceReps = numPerfReps;
        //Debug.Log("historicGenomePool count b4: " + historicGenomePool.Count.ToString());
        int numStartingHistoricalReps = 20;

        for (int h = 0; h < numStartingHistoricalReps; h++)
        {
            historicGenomePool.Add(agentGenomeList[h]); // init
        }
        //Debug.Log("historicGenomePool count after: " + historicGenomePool.Count.ToString());
        ResetRepresentativesList();

        fitnessManager = new FitnessManager();
        SetUpDefaultFitnessComponents(fitnessManager, this.index);
        //fitnessManager.ResetHistoricalData();
        //fitnessManager.ResetCurrentHistoricalDataLists();
        fitnessManager.InitializeForNewGeneration(agentGenomeList.Count);

        trainingSettingsManager = new TrainingSettingsManager(0.075f, 0.04f, 0.015f, 0.003f);
    }
Exemplo n.º 3
0
    public void RepopulateBaselineGenomes()
    {
        if (baselineGenomePool == null)
        {
            baselineGenomePool = new List <AgentGenome>();
        }
        else
        {
            baselineGenomePool.Clear();
        }


        for (int j = 0; j < numBaseline; j++)
        {
            AgentGenome baselineGenome = new AgentGenome(j);
            baselineGenome.InitializeBodyGenomeFromTemplate(bodyGenomeTemplate);
            float increment   = (float)j / (float)(numBaseline - 1);
            float weightScale = 0f; // how much to scale initial random weights
            baselineGenome.InitializeRandomBrainFromCurrentBody(increment * (float)j * weightScale);
            baselineGenomePool.Add(baselineGenome);
        }
    }
Exemplo n.º 4
0
    // Representative system will be expanded later - for now, just defaults to Top # of performers
    public PlayerPopulation(Challenge.Type challengeType, BodyGenome bodyTemplate, int numGenomes, int numBaseline, int numReps)
    {
        bodyGenomeTemplate = new BodyGenome();
        bodyGenomeTemplate.CopyBodyGenomeFromTemplate(bodyTemplate);

        graphKing = new TheGraphKing();

        popSize          = numGenomes;
        this.numBaseline = numBaseline;

        // Create blank AgentGenomes for the standard population
        agentGenomeList    = new List <AgentGenome>();
        historicGenomePool = new List <AgentGenome>();
        baselineGenomePool = new List <AgentGenome>();

        for (int j = 0; j < numGenomes; j++)
        {
            AgentGenome agentGenome = new AgentGenome(j);
            agentGenome.InitializeBodyGenomeFromTemplate(bodyGenomeTemplate);
            agentGenome.InitializeRandomBrainFromCurrentBody(0.0f);
            agentGenomeList.Add(agentGenome);
        }
        RepopulateBaselineGenomes();
        AppendBaselineGenomes();

        // Representatives:
        numPerformanceReps = numReps;
        ResetRepresentativesList();
        historicGenomePool.Add(agentGenomeList[0]); // init

        fitnessManager = new FitnessManager();
        SetUpDefaultFitnessComponents(challengeType, fitnessManager);
        fitnessManager.ResetHistoricalData();
        fitnessManager.ResetCurrentHistoricalDataLists();
        fitnessManager.InitializeForNewGeneration(agentGenomeList.Count);

        trainingSettingsManager = new TrainingSettingsManager(0.01f, 0.8f, 0.2f, 0.005f);
    }
Exemplo n.º 5
0
    public void ResetTrainingAgentAndEnvironment()
    {
        // Spawn Player & Target Randomly
        float   randomAngle  = UnityEngine.Random.Range(-Mathf.PI, Mathf.PI);
        float   randomRadius = UnityEngine.Random.Range(minSpawnRadius, maxSpawnRadius);
        Vector3 displayPos   = new Vector3(Mathf.Cos(randomAngle) * randomRadius, Mathf.Sin(randomAngle) * randomRadius, 0f);
        Vector3 targetPos    = new Vector3(Mathf.Cos(randomAngle + Mathf.PI) * randomRadius, Mathf.Sin(randomAngle + Mathf.PI) * randomRadius, 0f);

        BodyGenome templateBodyGenome = new BodyGenome();

        templateBodyGenome.InitializeGenomeAsDefault();
        AgentGenome newGenome = new AgentGenome(-1);

        newGenome.InitializeBodyGenomeFromTemplate(templateBodyGenome);
        newGenome.InitializeRandomBrainFromCurrentBody(0.0f);

        if (displayGO != null)
        {
            Destroy(displayGO); // NOT IDEAL
        }
        displayGO    = Instantiate(Resources.Load("PredatorPrey/PredatorPrefab")) as GameObject;
        displayAgent = displayGO.AddComponent <Agent>();
        displayAgent.humanControlled = false;
        StartPositionGenome displayStartPos = new StartPositionGenome(displayPos, Quaternion.identity);

        displayGO.transform.position = displayPos;

        if (targetGO != null)
        {
            Destroy(targetGO); // NOT IDEAL
        }
        targetGO    = Instantiate(Resources.Load("PredatorPrey/PreyPrefab")) as GameObject;
        targetAgent = targetGO.AddComponent <Agent>();
        targetAgent.humanControlled = false;
        StartPositionGenome targetStartPos = new StartPositionGenome(targetPos, Quaternion.identity);

        targetGO.transform.position = targetPos;

        // Hook-Ups PLAYER:
        displayAgent.isVisible = true;
        displayAgent.InitializeAgentFromTemplate(agentGenomeList[0], displayStartPos);  // creates TestModule
        // Hook-Ups TARGET:
        targetAgent.isVisible = true;
        targetAgent.InitializeAgentFromTemplate(newGenome, targetStartPos);  // creates TestModule

        // hook modules PLAYER:
        displayAgent.testModule.ownRigidBody2D  = displayGO.GetComponent <Rigidbody2D>();
        displayAgent.testModule.bias[0]         = 1f;
        displayAgent.testModule.enemyTestModule = targetAgent.testModule; // self as target, to zero out targetvel
        // hook modules TARGET:
        targetAgent.testModule.ownRigidBody2D  = targetGO.GetComponent <Rigidbody2D>();
        targetAgent.testModule.bias[0]         = 1f;
        targetAgent.testModule.enemyTestModule = displayAgent.testModule; // self as target, to zero out targetvel

        /*
         * // Spawn Player & Target Randomly
         * float randomAngle = UnityEngine.Random.Range(-Mathf.PI, Mathf.PI);
         * float randomRadius = UnityEngine.Random.Range(minSpawnRadius, maxSpawnRadius);
         * Vector3 playerPos = new Vector3(Mathf.Cos(randomAngle) * randomRadius, Mathf.Sin(randomAngle) * randomRadius, 0f);
         * Vector3 targetPos = new Vector3(Mathf.Cos(randomAngle + Mathf.PI) * randomRadius, Mathf.Sin(randomAngle + Mathf.PI) * randomRadius, 0f);
         * targetGO.transform.position = targetPos;
         *
         * displayStartPos = new StartPositionGenome(playerPos, Quaternion.identity);
         * GameObject agentGO;
         * if (displayAgent == null) {
         *  agentGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         *  displayAgent = agentGO.AddComponent<Agent>();
         * }
         * else {
         *  agentGO = displayAgent.gameObject;
         * }
         * //displayAgent
         * agentGO.name = "Guinea Pig";
         * agentGO.transform.parent = gameObject.transform;
         * agentGO.transform.localPosition = displayStartPos.agentStartPosition;
         * agentGO.transform.localRotation = displayStartPos.agentStartRotation;
         * //agentGO.GetComponent<Collider>().enabled = false;
         * displayAgent.isVisible = true;
         * displayAgent.InitializeAgentFromTemplate(agentGenomeList[0], displayStartPos);
         * // hook modules:
         * displayAgent.testModule.enemyTransform = targetGO.transform;
         * displayAgent.testModule.bias[0] = 1f;
         * displayAgent.testModule.enemyTestModule = displayAgent.testModule;
         */
    }
Exemplo n.º 6
0
    public void StartNewDataCollectionRound()
    {
        curTimeStep = 0;
        imitationUI.TogglePause();

        // Spawn Player & Target Randomly
        float   randomAngle  = UnityEngine.Random.Range(-Mathf.PI, Mathf.PI);
        float   randomRadius = UnityEngine.Random.Range(minSpawnRadius, maxSpawnRadius);
        Vector3 playerPos    = new Vector3(Mathf.Cos(randomAngle) * randomRadius, Mathf.Sin(randomAngle) * randomRadius, 0f);
        Vector3 targetPos    = new Vector3(Mathf.Cos(randomAngle + Mathf.PI) * randomRadius, Mathf.Sin(randomAngle + Mathf.PI) * randomRadius, 0f);

        BodyGenome templateBodyGenome = new BodyGenome();

        templateBodyGenome.InitializeGenomeAsDefault();
        AgentGenome newGenome = new AgentGenome(-1);

        newGenome.InitializeBodyGenomeFromTemplate(templateBodyGenome);
        newGenome.InitializeRandomBrainFromCurrentBody(0.0f);

        if (playerGO == null)
        {
            //ResetTrainingForNewGen();
            // Create Visible Display Agent to observe behavior
            //displayStartPos = new StartPositionGenome(Vector3.zero, Quaternion.identity);
            //playerGO.transform.localPosition = displayStartPos.agentStartPosition;
            //playerGO.transform.localRotation = displayStartPos.agentStartRotation;
        }
        else
        {
            //playerGO.transform.position = playerPos;
            Destroy(playerGO); // NOT IDEAL
        }
        // Instantiate Player (Predator) Human-Controlled:
        playerGO    = Instantiate(Resources.Load("PredatorPrey/PredatorPrefab")) as GameObject;
        playerAgent = playerGO.AddComponent <Agent>();
        playerAgent.humanControlled = true;
        StartPositionGenome playerStartPos = new StartPositionGenome(playerPos, Quaternion.identity);

        playerGO.transform.position = playerPos;

        if (targetGO == null)
        {
            //targetGO = Instantiate(Resources.Load("PredatorPrey/PreyPrefab")) as GameObject;
            //targetGO.transform.position = targetPos;
        }
        else
        {
            //targetGO.transform.position = targetPos;
            Destroy(targetGO); // NOT IDEAL
        }
        targetGO    = Instantiate(Resources.Load("PredatorPrey/PreyPrefab")) as GameObject;
        targetAgent = targetGO.AddComponent <Agent>();
        targetAgent.humanControlled = false;
        StartPositionGenome targetStartPos = new StartPositionGenome(targetPos, Quaternion.identity);

        targetGO.transform.position = targetPos;



        // Hook-Ups PLAYER:
        playerAgent.isVisible = true;
        playerAgent.InitializeAgentFromTemplate(newGenome, playerStartPos);  // creates TestModule
        // Hook-Ups TARGET:
        targetAgent.isVisible = true;
        targetAgent.InitializeAgentFromTemplate(newGenome, targetStartPos);  // creates TestModule

        // hook modules PLAYER:
        playerAgent.testModule.ownRigidBody2D  = playerGO.GetComponent <Rigidbody2D>();
        playerAgent.testModule.bias[0]         = 1f;
        playerAgent.testModule.enemyTestModule = targetAgent.testModule; // self as target, to zero out targetvel
        // hook modules TARGET:
        targetAgent.testModule.ownRigidBody2D  = targetGO.GetComponent <Rigidbody2D>();
        targetAgent.testModule.bias[0]         = 1f;
        targetAgent.testModule.enemyTestModule = playerAgent.testModule; // self as target, to zero out targetvel
    }