Exemple #1
0
        // =============== CROSSOVER ================
        public Genome Crossover(Genome partner)
        {
            Genome child = new Genome(Inputs, Outputs);

            child.Connections.Clear();
            child.Nodes.Clear();
            child.Layers   = Layers;
            child.NextNode = NextNode;
            child.BiasNode = BiasNode;
            List <ConnectionGene> childConnections = new List <ConnectionGene>();
            List <bool> isEnabled = new List <bool>();

            //CONNECTIONS
            foreach (ConnectionGene connection in Connections)
            {
                bool setEnabled       = true;
                int partnerConnection = GetMatchingGene(partner, connection.InnovationNumber);
                if (partnerConnection != -1) //if the other parent has the same gene
                {
                    //If one of the connections is disabled, there is 75% chance that the gene will be disabled
                    if ((!connection.IsEnabled || !partner.Connections[partnerConnection].IsEnabled) && randomGenerator.NextDouble() < 0.75)
                    {
                        setEnabled = false;
                    }
                    //The gene is transmitted by one of the parents
                    if (randomGenerator.NextDouble() < 0.5)
                    {
                        childConnections.Add(connection);
                    }
                    else
                    {
                        childConnections.Add(partner.Connections[partnerConnection]);
                    }
                }
                else //disjoint or excess gene -> add it
                {
                    childConnections.Add(connection);
                    setEnabled = connection.IsEnabled;
                }
                isEnabled.Add(setEnabled);
            }
            //the child has as many nodes as the fittest parent (this one)
            foreach (Node node in Nodes)
            {
                child.Nodes.Add(node.Clone());
            }
            //clone all connections
            for (int i = 0; i < childConnections.Count; i++)
            {
                child.Connections.Add(childConnections[i].Clone(child.GetNode(childConnections[i].FromNode.Number), child.GetNode(childConnections[i].ToNode.Number)));
                child.Connections[i].IsEnabled = isEnabled[i];
            }
            child.ConnectNodes();
            return(child);
        }
Exemple #2
0
        // ============== USEFUL METHODS =================
        public Genome ToGenome()
        {
            Genome clone = new Genome(Inputs, Outputs);

            clone.Nodes.Clear();
            clone.Connections.Clear();

            foreach (Node n in Nodes)
            {
                clone.Nodes.Add(n.Clone());
            }

            foreach (ConnectionGene c in Connections)
            {
                clone.Connections.Add(c.Clone(clone.GetNode(c.FromNode.Number), clone.GetNode(c.ToNode.Number)));
            }

            clone.Layers   = Layers;
            clone.NextNode = NextNode;
            clone.BiasNode = BiasNode;
            clone.ConnectNodes();
            clone.networkChanged = true;
            return(clone);
        }