Example #1
0
        public override Genome Crossover(Genome g)
        {
            ListGenome aGene1 = new ListGenome();
            ListGenome aGene2 = new ListGenome();

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

            ListGenome CrossingGene = (ListGenome)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]);
            }

            ListGenome aGene = null;

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

            return(aGene);
        }
Example #2
0
        public override int CompareTo(object a)
        {
            ListGenome Gene1 = this;
            ListGenome Gene2 = (ListGenome)a;

            return(Math.Sign(Gene2.CurrentFitness - Gene1.CurrentFitness));
        }
Example #3
0
        public override void CopyGeneInfo(Genome dest)
        {
            ListGenome theGene = (ListGenome)dest;

            theGene.Length = Length;
            theGene.TheMin = TheMin;
            theGene.TheMax = TheMax;
        }
Example #4
0
 public Population()
 {
     //
     // TODO: Add constructor logic here
     //
     for (int i = 0; i < kInitialPopulation; i++)
     {
         ListGenome aGenome = new ListGenome(kLength, kMin, kMax);
         aGenome.SetCrossoverPoint(kCrossover);
         aGenome.CalculateFitness();
         Genomes.Add(aGenome);
     }
 }
Example #5
0
        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 (ListGenome.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
                ListGenome babyGene1 = (ListGenome)((ListGenome)GeneDads[i]).Crossover((ListGenome)GeneMoms[i]);
                ListGenome babyGene2 = (ListGenome)((ListGenome)GeneMoms[i]).Crossover((ListGenome)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);
                }
            }
        }