Example #1
0
        private Genome Crossover()
        {
            var mother = Genomes.GetRandomElement();
            var father = Genomes.GetRandomElement();

            while (father == mother)
            {
                father = Genomes.GetRandomElement();
            }

            if (mother.Fitness < father.Fitness)
            {
                var tmp = father;
                father = mother;
                mother = tmp;
            }

            var childGenome = new Genome();

            foreach (var node in mother.NodeGenes.Values)
            {
                childGenome.AddNodeGene(new NodeGene(node));
            }

            foreach (var motherConnectionGene in mother.ConnectionGenes)
            {
                ConnectionGene newChildConnection;
                var            coinToss         = RandomGenerator.GetCoinToss();
                var            connectionGeneId = motherConnectionGene.Key;

                if (father.ConnectionGenes.ContainsKey(connectionGeneId))
                {
                    if (coinToss)
                    {
                        newChildConnection = new ConnectionGene(motherConnectionGene.Value);
                    }
                    else
                    {
                        newChildConnection = new ConnectionGene(father.ConnectionGenes[connectionGeneId]);
                    }
                }
                else
                {
                    newChildConnection = new ConnectionGene(motherConnectionGene.Value);
                }

                childGenome.AddConnectionGene(newChildConnection, connectionGeneId);
            }

            return(childGenome);
        }
Example #2
0
        public Genome Breed()
        {
            Genome childGenome;

            if (Genomes.Count > 1 && RandomGenerator.GetRandomResult(Config.CrossoverChance))
            {
                childGenome = Crossover();
            }
            else
            {
                childGenome = new Genome(Genomes.GetRandomElement());
            }

            childGenome.TryMutate();

            return(childGenome);
        }