Esempio n. 1
0
        public override void CopyGeneInfo(Genome dest)
        {
            MastermindGenome theGene = (MastermindGenome)dest;

            theGene.Length = Length;
            theGene.TheMin = TheMin;
            theGene.TheMax = TheMax;
        }
 public MasterMindPopulation(int numberOfGenomes)
 {
     Genomes.Clear();
     for (int i = 0; i < numberOfGenomes; i++)
     {
         MastermindGenome aGenome = new MastermindGenome(kLength, 1, 9);
         aGenome.SetCrossoverPoint(kCrossover);
         aGenome.CalculateFitness();
         Genomes.Add(aGenome);
     }
 }
Esempio n. 3
0
        public override Genome Crossover(Genome g)
        {
            MastermindGenome aGene1 = new MastermindGenome();
            MastermindGenome aGene2 = new MastermindGenome();

            g.CopyGeneInfo(aGene1);
            g.CopyGeneInfo(aGene2);


            MastermindGenome CrossingGene = (MastermindGenome)g;

            for (int i = 0; i < CrossoverPoint; i++)
            {
                aGene1.TheArray.Add(CrossingGene.TheArray[i]);
                aGene2.TheArray.Add(TheArray[i]);
            }

            for (int j = CrossoverPoint; j < Length; j++)
            {
                aGene1.TheArray.Add(TheArray[j]);
                aGene2.TheArray.Add(CrossingGene.TheArray[j]);
            }

            // 50/50 chance of returning gene1 or gene2
            MastermindGenome aGene = null;

            if (TheSeed.Next(2) == 1)
            {
                aGene = aGene1;
            }
            else
            {
                aGene = aGene2;
            }

            return(aGene);
        }
        public void DoCrossover(ArrayList genes)
        {
            ArrayList GeneMoms = new ArrayList();
            ArrayList GeneDads = new ArrayList();

            for (int i = 0; i < genes.Count; i++)
            {
                // randomly pick the moms and dad's
                if (MastermindGenome.TheSeed.Next(100) % 2 > 0)
                {
                    GeneMoms.Add(genes[i]);
                }
                else
                {
                    GeneDads.Add(genes[i]);
                }
            }

            //  now make them equal
            if (GeneMoms.Count > GeneDads.Count)
            {
                while (GeneMoms.Count > GeneDads.Count)
                {
                    GeneDads.Add(GeneMoms[GeneMoms.Count - 1]);
                    GeneMoms.RemoveAt(GeneMoms.Count - 1);
                }

                if (GeneDads.Count > GeneMoms.Count)
                {
                    GeneDads.RemoveAt(GeneDads.Count - 1);                     // make sure they are equal
                }
            }
            else
            {
                while (GeneDads.Count > GeneMoms.Count)
                {
                    GeneMoms.Add(GeneDads[GeneDads.Count - 1]);
                    GeneDads.RemoveAt(GeneDads.Count - 1);
                }

                if (GeneMoms.Count > GeneDads.Count)
                {
                    GeneMoms.RemoveAt(GeneMoms.Count - 1);                     // make sure they are equal
                }
            }

            // now cross them over and add them according to fitness
            for (int i = 0; i < GeneDads.Count; i++)
            {
                // pick best 2 from parent and children
                MastermindGenome babyGene1 = (MastermindGenome)((MastermindGenome)GeneDads[i]).Crossover((MastermindGenome)GeneMoms[i]);
                MastermindGenome babyGene2 = (MastermindGenome)((MastermindGenome)GeneMoms[i]).Crossover((MastermindGenome)GeneDads[i]);

                GenomeFamily.Clear();
                GenomeFamily.Add(GeneDads[i]);
                GenomeFamily.Add(GeneMoms[i]);
                GenomeFamily.Add(babyGene1);
                GenomeFamily.Add(babyGene2);
                CalculateFitnessForAll(GenomeFamily);
                GenomeFamily.Sort();

                if (Best2 == true)
                {
                    // if Best2 is true, add top fitness genes
                    GenomeResults.Add(GenomeFamily[0]);
                    GenomeResults.Add(GenomeFamily[1]);
                }
                else
                {
                    GenomeResults.Add(babyGene1);
                    GenomeResults.Add(babyGene2);
                }
            }
        }