コード例 #1
0
        public static Genome crossOver(Genome g1, Genome g2)
        {
            Neat neat = g1.Neat;

            Genome genome = neat.empty_genome();

            int index_g1 = 0;
            int index_g2 = 0;

            while (index_g1 < g1.Connections.size() && index_g2 < g2.Connections.size())
            {
                ConnectionGene gene1 = g1.Connections.get(index_g1);
                ConnectionGene gene2 = g2.Connections.get(index_g2);

                int in1 = gene1.Innovation_number;
                int in2 = gene2.Innovation_number;

                if (in1 == in2)
                {
                    if (GlobalRandom.NextDouble > 0.5)
                    {
                        genome.Connections.add(Neat.getConnection(gene1));
                    }
                    else
                    {
                        genome.Connections.add(Neat.getConnection(gene2));
                    }
                    index_g1++;
                    index_g2++;
                }
                else if (in1 > in2)
                {
                    index_g2++;
                }
                else
                {
                    genome.Connections.add(Neat.getConnection(gene1));
                    index_g1++;
                }
            }

            while (index_g1 < g1.Connections.size())
            {
                ConnectionGene gene1 = g1.Connections.get(index_g1);
                genome.Connections.add(Neat.getConnection(gene1));
                index_g1++;
            }

            foreach (ConnectionGene c in genome.Connections.Data)
            {
                genome.Nodes.add(c.From);
                genome.Nodes.add(c.To);
            }

            return(genome);
        }