Ejemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        Genome parent3GenomeSimple = new Genome();

        //Create the input nodes
        parent3GenomeSimple.AddNodeGene(new NodeGene(1, NodeGeneType.INPUT, 0f));
        parent3GenomeSimple.AddNodeGene(new NodeGene(2, NodeGeneType.INPUT, 0f));
        parent3GenomeSimple.AddNodeGene(new NodeGene(3, NodeGeneType.INPUT, 0f));

        //Create the output node
        parent3GenomeSimple.AddNodeGene(new NodeGene(4, NodeGeneType.OUTPUT, 1f));

        //Create connections
        parent3GenomeSimple.AddConnectionGene(new ConnectionGene(1, 4, 1.0, true, 1));
        parent3GenomeSimple.AddConnectionGene(new ConnectionGene(2, 4, 1.0, true, 2));
        parent3GenomeSimple.AddConnectionGene(new ConnectionGene(3, 4, 1.0, true, 3));


        //Init the connection gene counter with 11
        connectionCounter = new GeneCounter(4);
        nodeCounter       = new GeneCounter(5);

        manager.Callback = this;

        manager.CreateInitialPopulation(parent3GenomeSimple, nodeCounter, connectionCounter, 100);
    }
Ejemplo n.º 2
0
    public void CreateInitialPopulation_Test()
    {
        //Test the the reusult values
        Assert.Null(resultAgentsInitalizedAgentList);
        Assert.Null(resultAgentsInitializedSpeciesList);

        //Init population
        populationManager.CreateInitialPopulation(parent3GenomeSimple, nodeInnovationCounter, connectionInnovationCounter, 100);

        Assert.NotNull(resultAgentsInitalizedAgentList);
        Assert.NotNull(resultAgentsInitializedSpeciesList);

        //Test the amount of agents
        Assert.AreEqual(populationManager.PopulationSize, resultAgentsInitalizedAgentList.Count);
        Assert.AreEqual(1, resultAgentsInitializedSpeciesList.Count);

        int loopCounter = 0;

        foreach (AgentObject agent in resultAgentsInitalizedAgentList)
        {
            Assert.AreEqual(100, agent.GetFitness());
            Genome agentGenome = agent.Genome;

            //Should not be the same objects but contain the same values
            Assert.AreNotEqual(parent3GenomeSimple, agentGenome);
            Assert.AreEqual(parent3GenomeSimple.Nodes.Count, agentGenome.Nodes.Count);
            Assert.AreEqual(parent3GenomeSimple.Connections.Count, agentGenome.Connections.Count);

            //Test if the object in the species list is the same as the object in the agents list
            Assert.AreEqual(agent, resultAgentsInitializedSpeciesList[0].Members[loopCounter++]);

            //Test all nodes
            foreach (int nodeKey in parent3GenomeSimple.Nodes.Keys)
            {
                Assert.True(agentGenome.Nodes.ContainsKey(nodeKey));
                Assert.True(CompareNodeGenes(parent3GenomeSimple.Nodes[nodeKey], agentGenome.Nodes[nodeKey]));
            }

            //Test all connections
            foreach (int connectionKey in parent3GenomeSimple.Connections.Keys)
            {
                Assert.True(agentGenome.Connections.ContainsKey(connectionKey));
                Assert.True(CompareConnectionGenes(parent3GenomeSimple.Connections[connectionKey], agentGenome.Connections[connectionKey]));
            }
        }
    }
Ejemplo n.º 3
0
 /// <summary>
 /// Start and stop the evaluation
 /// </summary>
 public void StartStopEvaluation()
 {
     if (_evaluationRunning)
     {
         _evaluationRunning = !_evaluationRunning;
         KillAllPlayerManually();
     }
     else
     {
         _evaluationRunning = !_evaluationRunning;
         SetStartGenome();
         _manager.CreateInitialPopulation(_startGenome, _nodeCounter, _connectionCounter, _generationSize, true);
     }
 }
Ejemplo n.º 4
0
        public void AllAgentsKilledCallback(List <AgentObject> agents, List <Species> species, int generation)
        {
            //Destry all player
            foreach (GameObject gameObject in GameObject.FindGameObjectsWithTag("Player"))
            {
                Destroy(gameObject);
            }

            GenerationEvaluator eval      = new GenerationEvaluator(agents, species, generation);
            AgentObject         bestAgent = eval.BestAgent;

            Debug.Log("All agents dead. Best fitness: " + bestAgent.GetFitness() + ", Average: " + eval.AverageFitness);

            //Fill the screen
            FillXorScreen(bestAgent.Genome, _screen, _screenObjPrefab);

            SetDisplay(bestAgent.Genome);

            if (CanAgentSolveXOR(bestAgent) || eval.Generation > 500)
            {
                foreach (NodeGene node in bestAgent.Genome.Nodes.Values)
                {
                    Debug.Log("Node: " + node.ID + ", Type: " + node.Type);
                }

                foreach (ConnectionGene connection in bestAgent.Genome.Connections.Values)
                {
                    if (connection.Expressed)
                    {
                        Debug.Log("Connection: In: " + connection.InNode + " Out: " + connection.OutNode + ", Weight: " + connection.Weight + ", InnovationNumber: " + connection.InnovationNumber);
                    }
                }

                result.Add(eval.Generation);

                if (result.Count < amountRuns)
                {
                    _manager.CreateInitialPopulation(startingGenome, new GeneCounter(6), new GeneCounter(9), 400);
                }
                else
                {
                    Debug.Log("FINIIIISHED");
                }
            }
            else
            {
                _manager.GenerateNextGeneration();
            }
        }
Ejemplo n.º 5
0
        // Use this for initialization
        void Start()
        {
            startingGenome = new Genome();
            startingGenome.AddNodeGene(new NodeGene(0, NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
            startingGenome.AddNodeGene(new NodeGene(1, NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
            startingGenome.AddNodeGene(new NodeGene(2, NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
            startingGenome.AddNodeGene(new NodeGene(3, NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));


            startingGenome.AddConnectionGene(new ConnectionGene(0, 3, Random.Range(0f, 1f), true, 0));
            startingGenome.AddConnectionGene(new ConnectionGene(1, 3, Random.Range(0f, 1f), true, 1));
            startingGenome.AddConnectionGene(new ConnectionGene(2, 3, Random.Range(0f, 1f), true, 2));



            //Nodes for a complete network

            /*
             * startingGenome.AddNodeGene(new NodeGene(4, NodeGeneType.HIDDEN, 0.5f));
             * startingGenome.AddNodeGene(new NodeGene(5, NodeGeneType.HIDDEN, 0.5f));
             *
             * startingGenome.AddConnectionGene(new ConnectionGene(0, 4, Random.Range(0f, 1f), true, 0));
             * startingGenome.AddConnectionGene(new ConnectionGene(0, 5, Random.Range(0f, 1f), true, 1));
             * startingGenome.AddConnectionGene(new ConnectionGene(1, 4, Random.Range(0f, 1f), true, 2));
             * startingGenome.AddConnectionGene(new ConnectionGene(1, 5, Random.Range(0f, 1f), true, 3));
             *
             * startingGenome.AddConnectionGene(new ConnectionGene(4, 3, Random.Range(0f, 1f), true, 4));
             * startingGenome.AddConnectionGene(new ConnectionGene(5, 3, Random.Range(0f, 1f), true, 5));
             *
             * startingGenome.AddConnectionGene(new ConnectionGene(2, 3, Random.Range(0f, 1f), true, 6));
             * startingGenome.AddConnectionGene(new ConnectionGene(2, 4, Random.Range(0f, 1f), true, 7));
             * startingGenome.AddConnectionGene(new ConnectionGene(2, 5, Random.Range(0f, 1f), true, 8));
             */

            result = new List <int>();

            _manager          = new PopulationManager();
            _manager.Callback = this;
            _manager.CreateInitialPopulation(startingGenome, new GeneCounter(6), new GeneCounter(9), 400, true);
        }