Example #1
0
    /// <summary>
    /// Create the start genome
    /// </summary>
    private void SetStartGenome()
    {
        _nodeCounter       = new GeneCounter(0);
        _connectionCounter = new GeneCounter(0);

        _startGenome = new Genome();

        int amountPlayerInputs = PlayerController.GetAmountOfInputs(_levelViewWidht, _levelViewHeight);

        List <NodeGene> tmpInputNodes = new List <NodeGene>();

        //Crate input nodes
        for (int i = 0; i < amountPlayerInputs; i++)
        {
            NodeGene node = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.SIGMOID);
            _startGenome.AddNodeGene(node);
            tmpInputNodes.Add(node);
        }

        //Create output nodes
        NodeGene outputNode1 = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.SIGMOID);
        NodeGene outputNode2 = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.SIGMOID);

        _startGenome.AddNodeGene(outputNode1);
        _startGenome.AddNodeGene(outputNode2);

        //Create connections
        for (int i = 0; i < amountPlayerInputs; i++)
        {
            _startGenome.AddConnectionGene(new ConnectionGene(tmpInputNodes[i].ID, outputNode1.ID, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
            _startGenome.AddConnectionGene(new ConnectionGene(tmpInputNodes[i].ID, outputNode2.ID, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        }
    }
Example #2
0
        public static void Test4()
        {
            Genome        genome = new Genome();
            NeuralNetwork net;

            float[] input;
            float[] output;
            string  logs = "======================TEST 4===================================\n\n";

            genome = new Genome();
            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.INPUT, 0));
            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.INPUT, 1));
            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.INPUT, 2));
            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, 3));
            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 4));
            genome.AddConnectionGene(new ConnectionGene(0, 4, 0.4f, true, 0));
            genome.AddConnectionGene(new ConnectionGene(1, 4, 0.7f, true, 1));
            genome.AddConnectionGene(new ConnectionGene(2, 4, 0.1f, true, 2));
            genome.AddConnectionGene(new ConnectionGene(4, 3, 1f, true, 3));

            net   = new NeuralNetwork(genome, Neuron.ActivationType.SIGMOID, false);
            input = new float[] { 0.5f, 0.75f, 0.90f };
            for (int i = 0; i < 3; i++)
            {
                output = net.FeedForward(input);
                logs  += "output is of length=" + output.Length + " and has output[0]=" + output[0] + " expecting 0.9924\n";
            }

            Debug.Log(logs);
        }
Example #3
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);
    }
Example #4
0
    public Genome CreateNeuronFromID(int ID, Genome genome)
    {
        for (int i = 0; i < innovations.Count; i++)
        {
            if (innovations[i].percepID == ID)
            {
                genome.AddNodeGene(innovations[i].percepID, innovations[i].percepType, false, innovations[i].splitValues, Random.Range(-1, 1));
                return(genome);
            }
        }

        genome.AddNodeGene(0, NodeType.Hidden, false, Vector2.zero, Random.Range(-1, 1));
        return(genome);
    }
Example #5
0
    /// <summary>
    /// Create the start genome
    /// </summary>
    private void SetStartGenome()
    {
        _nodeCounter       = new GeneCounter(0);
        _connectionCounter = new GeneCounter(0);

        _startGenome = new Genome();

        //5 input nodes + 1 bias node
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));

        //OutputNodes
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));
        _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID));

        //Add connections to first output nde
        _startGenome.AddConnectionGene(new ConnectionGene(0, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(1, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(2, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(3, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(4, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(5, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));

        //Connection to second ouztputNode
        _startGenome.AddConnectionGene(new ConnectionGene(0, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(1, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(2, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(3, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(4, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
        _startGenome.AddConnectionGene(new ConnectionGene(5, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber()));
    }
Example #6
0
        public void CrossoverTest()
        {
            // Parent 1
            Genome parent1 = new Genome();

            for (int i = 0; i < 3; i++)
            {
                NodeGene node = new NodeGene(NodeGene.TYPE.INPUT, i + 1);
                parent1.AddNodeGene(node);
            }

            parent1.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, 4));
            parent1.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 5));

            parent1.AddConnectionGene(new ConnectionGene(1, 4, 1f, true, 1));
            parent1.AddConnectionGene(new ConnectionGene(2, 4, 1f, false, 2));
            parent1.AddConnectionGene(new ConnectionGene(3, 4, 1f, true, 3));
            parent1.AddConnectionGene(new ConnectionGene(2, 5, 1f, true, 4));
            parent1.AddConnectionGene(new ConnectionGene(5, 4, 1f, true, 5));
            parent1.AddConnectionGene(new ConnectionGene(1, 5, 1f, true, 8));

            // Parent 2 (More Fit)
            Genome parent2 = new Genome();

            for (int i = 0; i < 3; i++)
            {
                NodeGene node = new NodeGene(NodeGene.TYPE.INPUT, i + 1);
                parent2.AddNodeGene(node);
            }
            parent2.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, 4));
            parent2.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 5));
            parent2.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 6));

            parent2.AddConnectionGene(new ConnectionGene(1, 4, 1f, true, 1));
            parent2.AddConnectionGene(new ConnectionGene(2, 4, 1f, false, 2));
            parent2.AddConnectionGene(new ConnectionGene(3, 4, 1f, true, 3));
            parent2.AddConnectionGene(new ConnectionGene(2, 5, 1f, true, 4));
            parent2.AddConnectionGene(new ConnectionGene(5, 4, 1f, false, 5));
            parent2.AddConnectionGene(new ConnectionGene(5, 6, 1f, true, 6));
            parent2.AddConnectionGene(new ConnectionGene(6, 4, 1f, true, 7));
            parent2.AddConnectionGene(new ConnectionGene(3, 5, 1f, true, 9));
            parent2.AddConnectionGene(new ConnectionGene(1, 6, 1f, true, 10));

            // Crossover
            Genome child = Genome.Crossover(parent2, parent1, new System.Random(), 0.75f);

            // Return Results
            Debug.Log(String.Format("PARENT 1\n{0}\n\nPARENT 2\n{1}\n\nCROSSOVER CHILD\n{2}", parent1.ToString(), parent2.ToString(), child.ToString()));
        }
Example #7
0
    /// <summary>
    /// Crossover of two parent genomes to procuce one child
    /// </summary>
    /// <param name="parent1">the more fit parent!</param>
    /// <param name="parent2">the less fit parent</param>
    /// <returns>the newly breed child genome</returns>
    public static Genome CrossOver(Genome parent1, Genome parent2)
    {
        Genome childGenome = new Genome();

        //Copy nodes of the more fit parent
        foreach (NodeGene parent1Node in parent1.Nodes.Values)
        {
            childGenome.AddNodeGene(new NodeGene(parent1Node));
        }

        //Iterate through every connection
        foreach (ConnectionGene parent1Connection in parent1.Connections.Values)
        {
            if (parent2.Connections.ContainsKey(parent1Connection.InnovationNumber))
            {
                //Matching genes
                if (Random.Range(-1.0f, 1.0f) >= 0)
                {
                    childGenome.AddConnectionGene(new ConnectionGene(parent1Connection));
                }
                else
                {
                    childGenome.AddConnectionGene(new ConnectionGene(parent2.Connections[parent1Connection.InnovationNumber]));
                }
            }
            else
            {
                //Distjoint or excess gene
                childGenome.AddConnectionGene(new ConnectionGene(parent1Connection));
            }
        }

        return(childGenome);
    }
Example #8
0
        public void EvaluatorTestInit()
        {
            Genome genome = new Genome();
            int    n1     = _nodeInnovation.GetNewInnovationNumber();
            int    n2     = _nodeInnovation.GetNewInnovationNumber();
            int    n3     = _nodeInnovation.GetNewInnovationNumber();

            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.INPUT, n1));
            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.INPUT, n2));
            genome.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, n3));

            int c1 = _connectionInnovation.GetNewInnovationNumber();
            int c2 = _connectionInnovation.GetNewInnovationNumber();

            genome.AddConnectionGene(new ConnectionGene(n1, n3, 0.5f, true, c1));
            genome.AddConnectionGene(new ConnectionGene(n2, n3, 0.5f, true, c2));

            _eval = new EvalutorTestEvaluator(_config, genome, _nodeInnovation, _connectionInnovation);
        }
Example #9
0
    public Genome GetDeafultGenome()
    {
        Genome g = new Genome(gt);

        for (int i = 0; i < genome.GetNodeGenes().Count; i++)
        {
            g.AddNodeGene(genome.GetNodeGenes()[i]);
        }
        return(g);
    }
Example #10
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);
        }
Example #11
0
    void OnEnable()  //set up inital nodes and connections
    {
        gt      = new GeneTracker();
        genome  = new Genome(gt);
        nodes   = new List <NodeGene>();
        species = new List <Species>();
        for (int i = 0; i < inputSize; i++)
        {
            NodeGene nodeIn = new NodeGene(NodeGene.NODETYPE.INPUT, gt.NewId(), 0, false, NodeGene.ACTIVATION_FUNCTION.NULL);
            nodes.Add(nodeIn);
            genome.AddNodeGene(nodeIn.Copy());
        }
        for (int i = 0; i < outputSize; i++)
        {
            NodeGene nodeOut = new NodeGene(NodeGene.NODETYPE.OUTPUT, gt.NewId(), 1, true, outputActiationFunction);
            nodes.Add(nodeOut);
            genome.AddNodeGene(nodeOut.Copy());
        }


        connections = new List <ConnectionGene>();


        for (int i = 0; i < inputSize; i++)
        {
            for (int i2 = 0; i2 < outputSize; i2++)
            {
                //Debug.Log(genome.GetNodeGenes()[i]);
                ConnectionGene con = new ConnectionGene(genome.GetNodeGenes()[i], genome.GetNodeGenes()[inputSize + i2], gt.Innovate());
                connections.Add(con.Copy());
            }
        }
        for (int i = 0; i < connections.Count; i++)
        {
            //Debug.Log(connections[i]);
        }
    }
Example #12
0
    public static Genome Clone(Genome genome)
    {
        Genome child = new Genome();

        foreach (NodeGene node in genome.GetNodeGenes().Values)
        {
            child.AddNodeGene(node.Clone());
        }

        foreach (ConnectionGene con in genome.GetConnectionGenes().Values)
        {
            child.AddConnectionGene(con.Clone());
        }

        return(child);
    }
Example #13
0
    public Genome InitGenome()
    {
        Genome g1 = new Genome(gt);



        for (int i = 0; i < inputSize + outputSize; i++)
        {
            NodeGene node = GetDeafultGenome().GetNodeGenes()[i].Copy();
            if (node.GetNodeType() == NodeGene.NODETYPE.OUTPUT || node.GetNodeType() == NodeGene.NODETYPE.HIDDEN)
            {
                node.SetBias(Random.Range(-1.0f, 1.0f));
                //node.SetBias(1.0f);
            }
            g1.AddNodeGene(node);
            //g1.GetNodeGenes()[inputSize + i].SetBias(Random.Range(-1.0f, 1.0f));
        }
        for (int i = 0; i < startConnections; i++)//sample n connections
        {
            ConnectionGene con = connections[Random.Range(0, connections.Count)].Copy();
            //ConnectionGene con = connections[i].Copy();
            con.SetWeight(Random.Range(-weightRange, weightRange));
            //con.SetWeight(1.0f);
            int inId = con.GetInNode().GetId();
            con.SetInNode(g1.GetNodeGenes()[inId]);
            int outId = con.GetOutNode().GetId();
            //Debug.Log(con.GetOutNode());
            //Debug.Log(outId);
            con.SetOutNode(g1.GetNodeGenes()[outId]);
            g1.AddConnectionGene(con);
        }
        if (speciation)
        {
            InsertGenomeIntoSpecies(g1, species);
            impf.StatsSpecies(species.Count);
        }
        genomes.Add(g1);

        return(g1);
    }
Example #14
0
    public static Genome Crossover(Genome parent1, Genome parent2)
    {
        Genome child = new Genome();

        foreach (NodeGene parent1Node in parent1.GetNodeGenes().Values)
        {
            child.AddNodeGene(parent1Node.Clone());
        }

        foreach (ConnectionGene parent1Node in parent1.GetConnectionGenes().Values)
        {
            if (parent2.GetConnectionGenes().ContainsKey(parent1Node.innovation)) //matching gene
            {
                child.AddConnectionGene(Random.value < 0.5f ? parent1Node.Clone() : parent2.GetConnectionGenes()[parent1Node.innovation].Clone());
            }
            else
            {
                child.AddConnectionGene(parent1Node.Clone());
            }
        }

        return(child);
    }
Example #15
0
        public static void MutateAddNode(this Genome genome)
        {
            var connection = genome.ConnectionGenes.Values.Where(cg => cg.IsEnabled).GetRandomElement();

            if (connection == null)
            {
                return;
            }

            var newNode = new NodeGene();

            genome.AddNodeGene(newNode);

            var newPreviousConnection = new ConnectionGene(connection.PreviousNodeId, newNode.Id, 1);

            genome.AddConnectionGene(newPreviousConnection);

            var newNextConnection = new ConnectionGene(newNode.Id, connection.NextNodeId, connection.Weight);

            genome.AddConnectionGene(newNextConnection);

            connection.Toggle(false);
        }
Example #16
0
    public static Genome Crossover(Genome parent1, Genome parent2)
    { // parent1 always more fit parent - no equal fitness parents
        System.Random rand      = new System.Random();
        Genome        offspring = new Genome();

        foreach (NodeGene parent1Node in parent1.GetNodes().Values)
        {
            offspring.AddNodeGene(parent1Node.CopyNode());
        }

        foreach (ConnectionGene parent1Connection in parent1.GetConnections().Values)
        {
            if (parent2.GetConnections().ContainsKey(parent1Connection.getInnovation()))
            {
                offspring.AddConnectionGene((rand.NextDouble() > 0.5) ? parent1Connection.CopyConnection() : parent2.GetConnections()[parent1Connection.getInnovation()].CopyConnection());
            }
            else
            {
                offspring.AddConnectionGene(parent1Connection.CopyConnection());
            }
        }

        return(offspring);
    }
Example #17
0
    public Genome Copy()
    {
        Genome          g         = new Genome(track);
        List <NodeGene> nodesList = new List <NodeGene>();

        for (int i = 0; i < nodes.Count; i++)
        {
            nodesList.Add(nodes[i].Copy());
            g.AddNodeGene(nodesList[i]);
        }

        List <ConnectionGene> connectionsList = new List <ConnectionGene>();

        for (int i = 0; i < connections.Count; i++)
        {
            connectionsList.Add(connections[i].Copy());
            connectionsList[i].SetInNode(g.FindNode(connections[i].GetInNode().GetId()));
            connectionsList[i].SetOutNode(g.FindNode(connections[i].GetOutNode().GetId()));
            //Debug.Log(connectionsList[i]);
            g.AddConnectionGene(connectionsList[i]);
        }
        g.SetLayers(layers);
        return(g);
    }
Example #18
0
    public Genome CrossoverGenes(Genome genome1, Genome genome2, GeneTracker track)
    {
        Genome          child        = new Genome(track);
        List <NodeGene> newNodeGenes = new List <NodeGene>();

        //for (int i = 0; i < genome1.GetNodeGenes().Count; i++)
        //{
        //    commonNodeGenes.Add(genome1.GetNodeGenes()[i].Copy());

        //}
        //for (int i = 0; i < genome2.GetNodeGenes().Count; i++)
        //{
        //    if (!commonNodeGenes.Contains(genome2.GetNodeGenes()[i]))
        //    {
        //        commonNodeGenes.Add(genome2.GetNodeGenes()[i].Copy());

        //    }


        //}

        if (genome1.GetFitness() < genome2.GetFitness())
        {//swap so genome1 has the higher fitness
            Genome temp = genome2;
            genome2 = genome1;
            genome1 = temp;
        }
        //Debug.Log("g1 "+genome1+ "\ng2 " + genome2);

        child.SetLayers(genome1.GetLayers());

        /*
         * for (int i = 0; i < genome1.GetNodeGenes().Count; i++) {
         *  for (int i2 = 0; i2 < genome2.GetNodeGenes().Count; i2++)
         *  {
         *
         *
         *  }
         * }
         */

        for (int i = 0; i < genome1.GetNodeGenes().Count; i++)
        {
            newNodeGenes.Add(genome1.GetNodeGenes()[i].Copy());
        }

        for (int i = 0; i < genome2.GetNodeGenes().Count; i++)
        {
            if (newNodeGenes.Contains(genome2.GetNodeGenes()[i]))
            {
                if (Random.Range(0, 2) == 1)
                {
                    // newNodeGenes.RemoveAt(newNodeGenes.IndexOf(genome2.GetNodeGenes()[i]));
                    //newNodeGenes.Add(genome2.GetNodeGenes()[i].Copy());
                    newNodeGenes[newNodeGenes.IndexOf(genome2.GetNodeGenes()[i])].SetBias(genome2.GetNodeGenes()[i].GetBias());
                }
            }
        }
        for (int i = 0; i < genome1.GetNodeGenes().Count; i++)
        {
            child.AddNodeGene(newNodeGenes[i]);
        }

        List <ConnectionGene> newConnectionGenes = new List <ConnectionGene>();

        //if equal choose random
        for (int i = 0; i < genome1.GetConnectionGenes().Count; i++)  //copy all connections
        {
            newConnectionGenes.Add(genome1.GetConnectionGenes()[i].Copy());
            newConnectionGenes[i].SetInNode(child.FindNode(genome1.GetConnectionGenes()[i].GetInNode().GetId()));
            newConnectionGenes[i].SetOutNode(child.FindNode(genome1.GetConnectionGenes()[i].GetOutNode().GetId()));
        }
        for (int i = 0; i < genome2.GetConnectionGenes().Count; i++)
        {
            //Debug.Log(newConnectionGenes.Contains(genome2.GetConnectionGenes()[i]));
            if (newConnectionGenes.Contains(genome2.GetConnectionGenes()[i]))
            {
                //Debug.Log(Random.Range(0, 2) );
                if (Random.Range(0, 2) == 1)
                {
                    //newConnectionGenes.RemoveAt(newConnectionGenes.IndexOf(genome2.GetConnectionGenes()[i]));
                    //newConnectionGenes.Add(genome2.GetConnectionGenes()[i].Copy());

                    //newConnectionGenes[i].SetWeight(genome2.GetConnectionGenes()[i].GetWeight());
                    //Debug.Log(newConnectionGenes[newConnectionGenes.IndexOf(genome2.GetConnectionGenes()[i])].GetWeight());
                    newConnectionGenes[newConnectionGenes.IndexOf(genome2.GetConnectionGenes()[i])].SetWeight(genome2.GetConnectionGenes()[i].GetWeight());
                }
            }
        }
        for (int i = 0; i < genome1.GetConnectionGenes().Count; i++)
        {
            child.AddConnectionGene(newConnectionGenes[i]);
        }
        //Debug.Log("child "+child);
        return(child);
    }
Example #19
0
    private void Awake()
    {
        if (Manager.gameType == Manager.GameType.Train)
        {
            // add initial sensors for board
            for (int y = 1; y < 21; y++)
            {
                for (int x = 1; x < 11; x++)
                {
                    startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, (y - 1) * 10 + x));
                }
            }
            // add initial sensors for current tetromino
            for (int y = 1; y < 5; y++)
            {
                for (int x = 1; x < 5; x++)
                {
                    startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, (y - 1) * 4 + x + 200));
                }
            }
            // add initial sensor for next tetromino
            startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 217));
            // add tetromino position sensor
            startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 218));
            startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 219));
            // add initial sensor for previous frame DAS
            startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 220));
            // add initial BIAS SENSOR - SHOULD ALWAYS BE SET TO 1
            startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 221));

            //add initial out nodes
            // 222 - null
            // 223 - left
            // 224 - down
            // 225 - right
            // 226 - a
            // 227 - b
            for (int i = 222; i < 228; i++)
            {
                startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, i));
            }

            startingGenome.AddConnectionGene(new ConnectionGene(221, 223, 2f, true, History.Innovate()));

            startingGenome.AddConnectionMutation();
            startingGenome.AddNodeMutation();

            for (int y = 0; y < 20; y++)
            {
                startingGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 228 + y));
                startingGenome.AddConnectionGene(new ConnectionGene(228 + y, Random.Range(222, 228), Random.Range(-2f, 2f), true, History.Innovate()));
            }

            for (int y = 0; y < 20; y++)
            {
                for (int x = 0; x < 10; x++)
                {
                    startingGenome.AddConnectionGene(new ConnectionGene(y * 10 + x + 1, 228 + y, Random.Range(0.5f, 2f), true, History.Innovate()));
                }
            }
        }
    }
Example #20
0
    public void SetUp()
    {
        populationManager = new PopulationManager
        {
            Callback = this
        };

        //-----------------------------------------------------------------------------------
        // Parent 1
        //-----------------------------------------------------------------------------------

        parent1Genome = new Genome();

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

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

        //Create hidden node
        parent1Genome.AddNodeGene(new NodeGene(5, NodeGeneType.HIDDEN, 0.5f));

        //Create connections
        parent1Genome.AddConnectionGene(new ConnectionGene(1, 4, 1.0, true, 1));
        parent1Genome.AddConnectionGene(new ConnectionGene(2, 4, 1.0, false, 2));
        parent1Genome.AddConnectionGene(new ConnectionGene(3, 4, 1.0, true, 3));
        parent1Genome.AddConnectionGene(new ConnectionGene(2, 5, 1.0, true, 4));
        parent1Genome.AddConnectionGene(new ConnectionGene(5, 4, 1.0, true, 5));
        parent1Genome.AddConnectionGene(new ConnectionGene(1, 5, 1.0, true, 8));

        //-----------------------------------------------------------------------------------
        // Parent 2
        //-----------------------------------------------------------------------------------

        parent2Genome = new Genome();

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

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

        //Create hidden node
        parent2Genome.AddNodeGene(new NodeGene(5, NodeGeneType.HIDDEN, 0.4f));
        parent2Genome.AddNodeGene(new NodeGene(6, NodeGeneType.HIDDEN, 0.5f));

        //Create connections
        parent2Genome.AddConnectionGene(new ConnectionGene(1, 4, 1.0, true, 1));
        parent2Genome.AddConnectionGene(new ConnectionGene(2, 4, 1.0, false, 2));
        parent2Genome.AddConnectionGene(new ConnectionGene(3, 4, 1.0, true, 3));
        parent2Genome.AddConnectionGene(new ConnectionGene(2, 5, 1.0, true, 4));
        parent2Genome.AddConnectionGene(new ConnectionGene(5, 4, 1.0, false, 5));
        parent2Genome.AddConnectionGene(new ConnectionGene(5, 6, 1.0, true, 6));
        parent2Genome.AddConnectionGene(new ConnectionGene(6, 4, 1.0, true, 7));
        parent2Genome.AddConnectionGene(new ConnectionGene(3, 5, 1.0, true, 9));
        parent2Genome.AddConnectionGene(new ConnectionGene(1, 6, 1.0, true, 10));

        //-----------------------------------------------------------------------------------
        // Parent 3
        //-----------------------------------------------------------------------------------

        parent3GenomeSimple = new Genome();
        //Create the input nodes
        parent3GenomeSimple.AddNodeGene(new NodeGene(7, NodeGeneType.INPUT, 0f));
        parent3GenomeSimple.AddNodeGene(new NodeGene(8, NodeGeneType.INPUT, 0f));
        parent3GenomeSimple.AddNodeGene(new NodeGene(9, NodeGeneType.INPUT, 0f));

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

        //Create connections
        parent3GenomeSimple.AddConnectionGene(new ConnectionGene(7, 10, 1.0, true, 11));
        parent3GenomeSimple.AddConnectionGene(new ConnectionGene(8, 10, 1.0, true, 12));
        parent3GenomeSimple.AddConnectionGene(new ConnectionGene(9, 10, 1.0, true, 13));


        //Init the connection gene counter with 11
        connectionInnovationCounter = new GeneCounter(14);
        nodeInnovationCounter       = new GeneCounter(11);

        //Set result values from callback to null
        resultAgentsInitalizedAgentList    = null;
        resultAgentsInitializedSpeciesList = null;
        resultAgentKilled     = null;
        resultAllAgentsKilled = false;

        //Set fine tuning parameters
        PopulationManager._FACTOR_EXCESS_GENES       = 1;
        PopulationManager._FACTOR_DISJOINT_GENES     = 1;
        PopulationManager._FACTOR_AVG_WEIGHT_DIF     = 0.4f;
        PopulationManager._THRESHOLD_NUMBER_OF_GENES = 20;
        PopulationManager._COMPABILITY_THRESHOLD     = 3;
    }
Example #21
0
        private void CreateStartingGenomeAndEvaluator()
        {
            // Create New Genome
            Genome genome = new Genome(config.newWeightRange, config.perturbingProbability);

            // Check there is at least 1 Input & 1 Output Nodes
            if (config.inputNodeNumber == 0)
            {
                config.inputNodeNumber = 1;
            }
            if (config.outputNodeNumber == 0)
            {
                config.outputNodeNumber = 1;
            }

            // Create Input Nodes
            for (int i = 0; i < config.inputNodeNumber; i++)
            {
                genome.AddNodeGene(new NodeGene(NodeGene.TYPE.INPUT, _nodeInnovation.GetNewInnovationNumber()));
            }

            // Create Hidden Nodes
            foreach (int nb in config.hiddenNodeStartNumberByLayer)
            {
                for (int i = 0; i < nb; ++i)
                {
                    genome.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, _nodeInnovation.GetNewInnovationNumber()));
                }
            }

            // Create Output Nodes
            for (int i = 0; i < config.outputNodeNumber; i++)
            {
                genome.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, _nodeInnovation.GetNewInnovationNumber()));
            }

            // Create Connections
            if (config.addConnectionOnCreation)
            {
                if (config.hiddenNodeStartNumberByLayer.Count > 0)
                {
                    int inputStartId         = 0;
                    int previousHiddenStatId = 0;
                    int previousHiddenStopId = 0;
                    int hiddenStartId        = config.inputNodeNumber;
                    int hiddenStopId         = config.inputNodeNumber;
                    int outputStartId        = genome.Nodes.Count - config.outputNodeNumber;

                    // Loop through Hidden Layers
                    for (int hiddenLayer = 0; hiddenLayer < config.hiddenNodeStartNumberByLayer.Count; hiddenLayer++)
                    {
                        // Get Hidden Start & Stop Ids
                        if (hiddenLayer > 0)
                        {
                            hiddenStartId += config.hiddenNodeStartNumberByLayer[hiddenLayer - 1];
                        }
                        hiddenStopId += config.hiddenNodeStartNumberByLayer[hiddenLayer];

                        // Loop through Nodes of the current Layer
                        for (int hiddenNodeId = hiddenStartId; hiddenNodeId < hiddenStopId; ++hiddenNodeId)
                        {
                            // If current Hidden Layer is the first Layer
                            if (hiddenLayer == 0)
                            {
                                // Add Connections from Inputs to First Hidden Layer
                                for (int inputNodeId = inputStartId; inputNodeId < config.inputNodeNumber; ++inputNodeId)
                                {
                                    genome.AddConnectionGene(new ConnectionGene(inputNodeId, hiddenNodeId, Random.Range(config.newWeightRange.x, config.newWeightRange.y), true, _connectionInnovation.GetNewInnovationNumber()));
                                }
                            }
                            else
                            {
                                // Add Connections from previous Hidden Layer to current Hidden Layer
                                for (int previousHiddenNodeId = previousHiddenStatId; previousHiddenNodeId < previousHiddenStopId; ++previousHiddenNodeId)
                                {
                                    genome.AddConnectionGene(new ConnectionGene(previousHiddenNodeId, hiddenNodeId, Random.Range(config.newWeightRange.x, config.newWeightRange.y), true, _connectionInnovation.GetNewInnovationNumber()));
                                }

                                // If current Hidden Layer is the last Layer
                                if (hiddenLayer + 1 == config.hiddenNodeStartNumberByLayer.Count)
                                {
                                    // Add Connections from Last Hidden Layer to Outputs
                                    for (int outputNodeId = outputStartId; outputNodeId < genome.Nodes.Count; ++outputNodeId)
                                    {
                                        genome.AddConnectionGene(new ConnectionGene(hiddenNodeId, outputNodeId, Random.Range(config.newWeightRange.x, config.newWeightRange.y), true, _connectionInnovation.GetNewInnovationNumber()));
                                    }
                                }
                            }
                        }

                        // Save previous Hidden Layer Start & Stop Ids
                        previousHiddenStatId = hiddenStartId;
                        previousHiddenStopId = hiddenStopId;
                    }
                }
                else
                {
                    int outputStartId = config.inputNodeNumber;
                    int outputStopId  = config.inputNodeNumber + config.outputNodeNumber;

                    // Loop Input Node
                    for (int inputNodeId = 0; inputNodeId < outputStartId; ++inputNodeId)
                    {
                        // Loop Output Node & add Connection between Input Node & Hidden Node
                        for (int outputNodeId = outputStartId; outputNodeId < outputStopId; ++outputNodeId)
                        {
                            genome.AddConnectionGene(new ConnectionGene(inputNodeId, outputNodeId, Random.Range(config.newWeightRange.x, config.newWeightRange.y), true, _connectionInnovation.GetNewInnovationNumber()));
                        }
                    }
                }
            }

            _startingGenome = genome;
            Debug.Log("Starting Genome:\n" + genome.ToString());

            // Create Evaluator
            _evaluator = new CreatureEvaluator(config, genome, _nodeInnovation, _connectionInnovation);
        }
Example #22
0
    public void SetUp()
    {

        //-----------------------------------------------------------------------------------
        // Parent 1
        //-----------------------------------------------------------------------------------

        parent1Genome = new Genome();

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

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

        //Create hidden node
        parent1Genome.AddNodeGene(new NodeGene(5, NodeGeneType.HIDDEN, 0.5f));

        //Create connections
        parent1Genome.AddConnectionGene(new ConnectionGene(1, 4, 1.0, true, 1));
        parent1Genome.AddConnectionGene(new ConnectionGene(2, 4, 1.0, false, 2));
        parent1Genome.AddConnectionGene(new ConnectionGene(3, 4, 1.0, true, 3));
        parent1Genome.AddConnectionGene(new ConnectionGene(2, 5, 1.0, true, 4));
        parent1Genome.AddConnectionGene(new ConnectionGene(5, 4, 1.0, true, 5));
        parent1Genome.AddConnectionGene(new ConnectionGene(1, 5, 1.0, true, 8));

        //-----------------------------------------------------------------------------------
        // Parent 2
        //-----------------------------------------------------------------------------------

        parent2Genome = new Genome();

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

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

        //Create hidden node
        parent2Genome.AddNodeGene(new NodeGene(5, NodeGeneType.HIDDEN, 0.4f));
        parent2Genome.AddNodeGene(new NodeGene(6, NodeGeneType.HIDDEN, 0.6f));

        //Create connections
        parent2Genome.AddConnectionGene(new ConnectionGene(1, 4, 1.0, true, 1));
        parent2Genome.AddConnectionGene(new ConnectionGene(2, 4, 1.0, false, 2));
        parent2Genome.AddConnectionGene(new ConnectionGene(3, 4, 1.0, true, 3));
        parent2Genome.AddConnectionGene(new ConnectionGene(2, 5, 1.0, true, 4));
        parent2Genome.AddConnectionGene(new ConnectionGene(5, 4, 1.0, false, 5));
        parent2Genome.AddConnectionGene(new ConnectionGene(5, 6, 1.0, true, 6));
        parent2Genome.AddConnectionGene(new ConnectionGene(6, 4, 1.0, true, 7));
        parent2Genome.AddConnectionGene(new ConnectionGene(3, 5, 1.0, true, 9));
        parent2Genome.AddConnectionGene(new ConnectionGene(1, 6, 1.0, true, 10));

        //-----------------------------------------------------------------------------------
        // Feed forward network
        //-----------------------------------------------------------------------------------

        //Nodes
        feedForwardNetwork = new Genome();
        feedForwardNetwork.AddNodeGene(new NodeGene(0, NodeGeneType.INPUT, 0f));
        feedForwardNetwork.AddNodeGene(new NodeGene(1, NodeGeneType.INPUT, 0f));

        feedForwardNetwork.AddNodeGene(new NodeGene(2, NodeGeneType.OUTPUT, 1f));
        feedForwardNetwork.AddNodeGene(new NodeGene(3, NodeGeneType.OUTPUT, 1f));

        feedForwardNetwork.AddNodeGene(new NodeGene(4, NodeGeneType.HIDDEN, 0.5f));
        feedForwardNetwork.AddNodeGene(new NodeGene(5, NodeGeneType.HIDDEN, 0.5f));
        feedForwardNetwork.AddNodeGene(new NodeGene(6, NodeGeneType.HIDDEN, 0.5f));

        //Connections
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(0, 4, 0.2, true, 0));
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(0, 5, 0.3, true, 1));
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(1, 5, 0.4, true, 2));
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(1, 6, 0.5, true, 3));
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(4, 2, 0.6, true, 4));
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(5, 2, 0.7, true, 5));
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(5, 3, 0.8, true, 6));
        feedForwardNetwork.AddConnectionGene(new ConnectionGene(6, 3, 0.9, true, 7));

        //-----------------------------------------------------------------------------------
        // Recurrent network
        //-----------------------------------------------------------------------------------

        recurrentNetwork = new Genome(feedForwardNetwork);
        recurrentNetwork.AddConnectionGene(new ConnectionGene(2, 4, -2, true, 8));
        recurrentNetwork.AddConnectionGene(new ConnectionGene(6, 6, -2, true, 9));

    }
Example #23
0
    void Start()
    {
        History.nodeInnovation = 0;
        // FIRST PARENT
        firstParent.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 1));
        firstParent.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 2));
        firstParent.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 3));

        firstParent.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, 4));
        firstParent.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 5));


        // CONNECTIONS

        firstParent.AddConnectionGene(new ConnectionGene(1, 4, 1f, true, 1));
        firstParent.AddConnectionGene(new ConnectionGene(2, 4, 1f, false, 2));
        firstParent.AddConnectionGene(new ConnectionGene(3, 4, 1f, true, 3));
        firstParent.AddConnectionGene(new ConnectionGene(2, 5, 1f, true, 4));
        firstParent.AddConnectionGene(new ConnectionGene(5, 4, 1f, true, 5));
        firstParent.AddConnectionGene(new ConnectionGene(1, 5, 1f, true, 8));

        // SECOND PARENT
        secondParent.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 1));
        secondParent.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 2));
        secondParent.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, 3));

        secondParent.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, 4));
        secondParent.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 5));
        secondParent.AddNodeGene(new NodeGene(NodeGene.TYPE.HIDDEN, 6));

        // CONNECTIONS

        secondParent.AddConnectionGene(new ConnectionGene(1, 4, 1f, true, 1));
        secondParent.AddConnectionGene(new ConnectionGene(2, 4, 1f, false, 2));
        secondParent.AddConnectionGene(new ConnectionGene(3, 4, 1f, true, 3));
        secondParent.AddConnectionGene(new ConnectionGene(2, 5, 1f, true, 4));
        secondParent.AddConnectionGene(new ConnectionGene(5, 4, 1f, false, 5));

        secondParent.AddConnectionGene(new ConnectionGene(5, 6, -1f, true, 6));
        secondParent.AddConnectionGene(new ConnectionGene(6, 4, 1f, true, 7));
        secondParent.AddConnectionGene(new ConnectionGene(3, 5, 1f, true, 9));
        secondParent.AddConnectionGene(new ConnectionGene(1, 6, 1f, true, 10));

        // SIMPLE GENOME
        simpleGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, History.NodeInnovate()));
        simpleGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, History.NodeInnovate()));
        simpleGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.SENSOR, History.NodeInnovate()));
        simpleGenome.AddNodeGene(new NodeGene(NodeGene.TYPE.OUTPUT, History.NodeInnovate()));
        simpleGenome.AddConnectionGene(new ConnectionGene(1, 4, -1f, true, History.Innovate()));
        simpleGenome.AddConnectionGene(new ConnectionGene(2, 4, 1f, true, History.Innovate()));
        simpleGenome.AddConnectionGene(new ConnectionGene(3, 4, 1f, true, History.Innovate()));

        //simpleGenome.AddConnectionGene(new ConnectionGene(2, 3, 1f, true, History.Innovate()));

        Genome offspringGenome = Genome.Crossover(secondParent, firstParent);

        int          numToRender = 1003;
        int          scale       = 20;
        NEATRenderer renderer    = new NEATRenderer();

        for (int i = 0; i < numToRender; i += 100)
        {
            renderer.DrawGenome(TetrisNEAT.OpenGenome("/" + i + ".tetro"), new Vector3((scale + 1) * (i + 1), 0, 0), Vector2.one * scale);
        }

        // Un-comment the below line to conduct a test with the current evaluator after first removing the rendering instructions
        //StartCoroutine(SimpleTest());
    }
Example #24
0
    public void LoadGenome(string filename, bool all)
    {
        string path = filename;

        Genome genome = new Genome();

        using (StreamReader reader = new StreamReader(path))
        {
            bool   node       = false;
            bool   connection = false;
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Equals("Nodes"))
                {
                    node       = true;
                    connection = false;
                }
                else if (line.Equals("Connections"))
                {
                    node       = false;
                    connection = true;
                }
                else
                {
                    string[] info = line.Split(',');

                    if (node)
                    {
                        int                    id         = int.Parse(info[0]);
                        NodeGene.Type          type       = (NodeGene.Type)System.Enum.Parse(typeof(NodeGene.Type), info[1]);
                        GenomeUtils.Activation activation = (GenomeUtils.Activation)System.Enum.Parse(typeof(GenomeUtils.Activation), info[2]);
                        genome.AddNodeGene(new NodeGene(type, id, activation));
                        Counter.SetNodeCounter(id);
                    }
                    else if (connection)
                    {
                        int   innovation = int.Parse(info[0]);
                        bool  expressed  = bool.Parse(info[1]);
                        int   inNode     = int.Parse(info[2]);
                        int   outNode    = int.Parse(info[3]);
                        float weight     = float.Parse(info[4]);
                        genome.AddConnectionGene(new ConnectionGene(inNode, outNode, weight, expressed, innovation));
                        Counter.SetConnectionCounter(innovation);
                    }
                    else
                    {
                        Debug.LogError("Invalid genome file");
                    }
                }
            }
        }

        genomes.Clear();
        genomes.Add(genome);

        if (all)
        {
            for (int i = 1; i < GenomeUtils.POP_SIZE; i++)
            {
                genomes.Add(GenomeUtils.Clone(genome));
            }
        }
        else
        {
            for (int i = 1; i < GenomeUtils.POP_SIZE; i++)
            {
                genomes.Add(new Genome(inputNodes, outputNodes));
            }
        }

        SetSpecies();
        MakeNNets();
    }