Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public virtual void mutate_link()
        {
            for (int i = 0; i < 100; i++)
            {
                NodeGene a = nodes.random_element();
                NodeGene b = nodes.random_element();

                if (a == null || b == null)
                {
                    continue;
                }
                if (a.X == b.X)
                {
                    continue;
                }

                ConnectionGene con;
                if (a.X < b.X)
                {
                    con = new ConnectionGene(a, b);
                }
                else
                {
                    con = new ConnectionGene(b, a);
                }

                if (connections.contains(con))
                {
                    continue;
                }

                con        = neat.getConnection(con.From, con.To);
                con.Weight = (GlobalRandom.NextDouble * 2 - 1) * neat.WEIGHT_RANDOM_STRENGTH;
                data_structures.RandomHashSet <Gene> hi = new data_structures.RandomHashSet <Gene>();
                hi.add_sorted(connections, con);
                return;
            }
        }