Esempio n. 1
0
        public override int CompareTo(object a)
        {
            Sudokufitness Gene1 = this;
            Sudokufitness Gene2 = (Sudokufitness)a;

            return(Math.Sign(Gene2.CurrentFitness - Gene1.CurrentFitness));
        }
Esempio n. 2
0
        public override void CopyGeneInfo(SudokuChromesome dest)
        {
            Sudokufitness theGene = (Sudokufitness)dest;

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

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


            Sudokufitness CrossingGene = (Sudokufitness)g;

            if (TheSeed.Next(2) == 1)
            {
                for (int j = 0; j < 9; j++)
                {
                    CrossoverPoint = TheSeed.Next(8) + 1;
                    for (int k = 0; k < CrossoverPoint; k++)
                    {
                        aGene1.TheArray[k, j] = CrossingGene.TheArray[k, j];
                        //						aGene1.TheArray[8 - k,j] = TheArray[8-k, j];
                        aGene2.TheArray[k, j] = TheArray[k, j];
                        //						aGene2.TheArray[8 - k ,j] = CrossingGene.TheArray[8 - k,j];
                    }

                    for (int k = CrossoverPoint; k < 9; k++)
                    {
                        aGene2.TheArray[k, j] = CrossingGene.TheArray[k, j];
                        //						aGene1.TheArray[8 - k,j] = TheArray[8-k, j];
                        aGene1.TheArray[k, j] = TheArray[k, j];
                        //						aGene2.TheArray[8 - k ,j] = CrossingGene.TheArray[8 - k,j];
                    }
                }
            }
            else
            {
                for (int j = 0; j < 9; j++)
                {
                    CrossoverPoint = TheSeed.Next(8) + 1;
                    for (int k = 0; k < CrossoverPoint; k++)
                    {
                        aGene1.TheArray[j, k] = CrossingGene.TheArray[j, k];
                        //						aGene1.TheArray[8 - k,j] = TheArray[8-k, j];
                        aGene2.TheArray[j, k] = TheArray[j, k];
                        //						aGene2.TheArray[8 - k ,j] = CrossingGene.TheArray[8 - k,j];
                    }

                    for (int k = CrossoverPoint; k < 9; k++)
                    {
                        aGene2.TheArray[j, k] = CrossingGene.TheArray[j, k];
                        //						aGene1.TheArray[8 - k,j] = TheArray[8-k, j];
                        aGene1.TheArray[j, k] = TheArray[j, k];
                        //						aGene2.TheArray[8 - k ,j] = CrossingGene.TheArray[8 - k,j];
                    }
                }
            }



/*
 *                      SudokuGenome CrossingGene = (SudokuGenome)g;
 *                      for (int i = 0; i < CrossoverPoint; i++)
 *                      {
 *                              for (int j = 0; j < CrossoverPoint; j++)
 *                              {
 *                                      aGene1.TheArray[i,j] = CrossingGene.TheArray[i,j];
 *                                      aGene2.TheArray[i,j] = TheArray[i, j];
 *                              }
 *                      }
 *
 *                      for (int i = CrossoverPoint; i < 9; i++)
 *                              for (int j = CrossoverPoint; j < 9; j++)
 *                              {
 *                                      aGene1.TheArray[i,j] =  TheArray[i,j];
 *                                      aGene2.TheArray[i,j] = CrossingGene.TheArray[i,j];
 *                              }
 *
 *                      for (int i = CrossoverPoint; i < 9; i++)
 *                              for (int j = 0; j < CrossoverPoint; j++)
 *                              {
 *                                      aGene1.TheArray[i,j] =  TheArray[i,j];
 *                                      aGene2.TheArray[i,j] = CrossingGene.TheArray[i,j];
 *                              }
 *
 *                      for (int i = 0; i < CrossoverPoint; i++)
 *                              for (int j = CrossoverPoint; j < 9; j++)
 *                              {
 *                                      aGene1.TheArray[i,j] =  CrossingGene.TheArray[i,j];
 *                                      aGene2.TheArray[i,j] = TheArray[i,j];
 *                              }
 */

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

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

            return(aGene);
        }