Ejemplo n.º 1
0
        public override int CompareTo(object a)
        {
            EquationGenome Gene1 = this;
            EquationGenome Gene2 = (EquationGenome)a;

            return(Math.Sign(Gene2.CurrentFitness - Gene1.CurrentFitness));
        }
Ejemplo n.º 2
0
        public override double CalculateFitness()
        {
            try
            {
                lock (this)
                {
                    // CurrentFitness = CalculateClosestProductSum();
                    //			CurrentFitness =  CalculateClosestSumTo10();
                    //			CurrentFitness = CalculateMazeDistanceFitness();
                    CurrentFitness = CalculateSeriesFitness();
                    if (CurrentFitness < 0.0f)
                    {
                        CurrentFitness = 0.01f;
                    }

                    if (CurrentFitness == 1)
                    {
                        PerfectFitness = true;
                        PerfectGenome  = this;
                    }
                }
            }
            catch (Exception ex)
            {
                CurrentFitness = .01f;                  // ignore occasional exception
            }
            return(CurrentFitness);
        }
Ejemplo n.º 3
0
        public override void CopyGeneInfo(Genome dest)
        {
            EquationGenome theGene = (EquationGenome)dest;

            theGene.Length = Length;
            theGene.TheMin = TheMin;
            theGene.TheMax = TheMax;
        }
Ejemplo n.º 4
0
        public override Genome Crossover2Point(Genome g)
        {
            EquationGenome aGene1 = new EquationGenome();
            EquationGenome aGene2 = new EquationGenome();

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

            // Pick a random crossover point
            int CrossoverPoint1 = TheSeed.Next(1, (int)Length);
            int CrossoverPoint2 = TheSeed.Next(CrossoverPoint1, (int)Length);

            // normalize
            if (CrossoverPoint1 > CrossoverPoint2)
            {
                int temp = CrossoverPoint1;
                CrossoverPoint1 = CrossoverPoint2;
                CrossoverPoint2 = temp;
            }

            EquationGenome CrossingGene = (EquationGenome)g;

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

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


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


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

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

            return(aGene);
        }
Ejemplo n.º 5
0
 public Population()
 {
     //
     // TODO: Add constructor logic here
     //
     for (int i = 0; i < kInitialPopulation; i++)
     {
         EquationGenome aGenome    = new EquationGenome(kLength, kMin, kMax);
         int            nCrossOver = EquationGenome.TheSeed.Next(1, (int)aGenome.Length);
         aGenome.SetCrossoverPoint(nCrossover);
         aGenome.CalculateFitness();
         Genomes.Add(aGenome);
     }
 }
Ejemplo n.º 6
0
 public Population()
 {
     //
     // TODO: Add constructor logic here
     //
     for  (int i = 0; i < kInitialPopulation; i++)
     {
         EquationGenome aGenome = new EquationGenome(kLength, kMin, kMax);
         int nCrossOver = EquationGenome.TheSeed.Next(1, (int)aGenome.Length);
         aGenome.SetCrossoverPoint(nCrossover);
         aGenome.CalculateFitness();
         Genomes.Add(aGenome);
     }
 }
Ejemplo n.º 7
0
        public override void CopyGeneFrom(Genome src)
        {
            EquationGenome theGene = (EquationGenome)src;

            for (int i = 0; i < TheArray.Count; i++)
            {
                TheArray[i] = (Gene)theGene.TheArray[i];
            }

            Length         = theGene.Length;
            TheMin         = theGene.TheMin;
            TheMax         = theGene.TheMax;
            CurrentFitness = theGene.CurrentFitness;
        }
Ejemplo n.º 8
0
 public Genome[] GetHighestScoreGenomes(int number)
 {
     Genomes.Sort();
     EquationGenome[] strongestGenomes = new EquationGenome[number];
     if (number > Genomes.Count)
     {
         number = Genomes.Count;
     }
     for (int i = 0; i < number; i++)
     {
         strongestGenomes[i] = new EquationGenome(kLength, kMin, kMax);
         strongestGenomes[i].SetCrossoverPoint(kCrossover);
         strongestGenomes[i].CopyGeneFrom((EquationGenome)Genomes[i]);
     }
     return(strongestGenomes);
 }
Ejemplo n.º 9
0
        EquationGenome GetBestGeneFromAllPopulations()
        {
            float          topFitness = -1;
            EquationGenome BestGenome = null;

            foreach (Population p in Populations)
            {
                EquationGenome g = (EquationGenome)p.GetHighestScoreGenome();
                if (g.CurrentFitness > topFitness)
                {
                    BestGenome = g;
                    topFitness = (float)g.CurrentFitness;
                }
            }

            return(BestGenome);
        }
Ejemplo n.º 10
0
        public override Genome UniformCrossover(Genome g)
        {
            EquationGenome aGene1 = new EquationGenome();
            EquationGenome aGene2 = new EquationGenome();

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

// swap genes randomly

            EquationGenome CrossingGene = (EquationGenome)g;

            for (int i = 0; i < Length; i++)
            {
                if (TheSeed.Next(100) % 2 == 0)
                {
                    aGene1.TheArray.Add(CrossingGene.TheArray[i]);
                    aGene2.TheArray.Add(TheArray[i]);
                }
                else
                {
                    aGene1.TheArray.Add(TheArray[i]);
                    aGene2.TheArray.Add(CrossingGene.TheArray[i]);
                }
            }



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

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

            return(aGene);
        }
Ejemplo n.º 11
0
        public override Genome Crossover(Genome g)
        {
            EquationGenome aGene1 = new EquationGenome();
            EquationGenome aGene2 = new EquationGenome();

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

            // Pick a random crossover point
            CrossoverPoint = TheSeed.Next(1, (int)Length);

            EquationGenome CrossingGene = (EquationGenome)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
            EquationGenome aGene = null;

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

            return(aGene);
        }
Ejemplo n.º 12
0
        public void RunPopulation(object thePopulationObject)
        {
            Population thePopulation       = (Population)thePopulationObject;
            int        NumberOfGenerations = 50000;

            for (int i = 0; i < NumberOfGenerations; i++)
            {
                thePopulation.NextGeneration();
                if (i % (NumberOfGenerations / 20) == 0)
                {
                    thePopulation.WriteNextGenerationTop(10);
                    if (EquationGenome.PerfectFitness == true)
                    {
                        NumberOfGenerations = i;
                        break;
                    }
                }

                if (i % (NumberOfGenerations / 4) == 0)
                {
                    EquationGenome[] EquationGenomesBest = (EquationGenome[])thePopulation.GetHighestScoreGenomes(Population.kNumberOfChoiceGenomes);
                    CopyHighestFitnessToOtherPopulations(EquationGenomesBest);
                }
            }

            thePopulation.FinishedRunning = true;

            thePopulation.CalculateFitnessForAll();

            EquationGenome EquationGenomeToIllustrate = (EquationGenome)thePopulation.GetHighestScoreGenome();

            Console.WriteLine("");
            Console.WriteLine("Best Genome in {1} after {0} Generations", NumberOfGenerations, thePopulation.Name);
            Console.WriteLine(EquationGenomeToIllustrate.ToString());
            EquationGenomeToIllustrate = GetBestGeneFromAllPopulations();
            Console.WriteLine("The next value in the series = {0}", EquationGenomeToIllustrate.CalculateNextValue());
            Class1.EquationGenomeToIllustrate = EquationGenomeToIllustrate;
            //			Console.ReadLine();
        }
Ejemplo n.º 13
0
 public Genome[] GetHighestScoreGenomes(int number)
 {
     Genomes.Sort();
     EquationGenome[] strongestGenomes = new EquationGenome[number];
     if (number > Genomes.Count)
         number = Genomes.Count;
     for (int i = 0; i < number; i++)
     {
         strongestGenomes[i] = new EquationGenome(kLength, kMin, kMax);
         strongestGenomes[i].SetCrossoverPoint(kCrossover);
         strongestGenomes[i].CopyGeneFrom((EquationGenome)Genomes[i]);
     }
     return strongestGenomes;
 }
Ejemplo n.º 14
0
        public override double CalculateFitness()
        {
            try
            {
                lock(this)
                {
                    // CurrentFitness = CalculateClosestProductSum();
                    //			CurrentFitness =  CalculateClosestSumTo10();
                    //			CurrentFitness = CalculateMazeDistanceFitness();
                    CurrentFitness = CalculateSeriesFitness();
                    if (CurrentFitness < 0.0f)
                        CurrentFitness = 0.01f;

                    if (CurrentFitness == 1)
                    {
                        PerfectFitness = true;
                        PerfectGenome = this;
                    }
                }
            }
            catch (Exception ex)
            {
                CurrentFitness = .01f;  // ignore occasional exception
            }
            return CurrentFitness;
        }
Ejemplo n.º 15
0
 void CopyHighestFitnessToOtherPopulations(EquationGenome[] BestGenomes)
 {
     // inject the genomes into all the populations
     foreach (Population p in Populations)
     {
         for (int i = 0; i < BestGenomes.Length; i++)
         {
             int randomnum = EquationGenome.TheSeed.Next(p.Count);
             p.InjectGenome(BestGenomes[i], randomnum);
         }
     }
 }
Ejemplo n.º 16
0
        public override Genome Crossover(Genome g)
        {
            EquationGenome aGene1 = new EquationGenome();
            EquationGenome aGene2 = new EquationGenome();
            g.CopyGeneInfo(aGene1);
            g.CopyGeneInfo(aGene2);

            // Pick a random crossover point
            CrossoverPoint = TheSeed.Next(1, (int)Length);

            EquationGenome CrossingGene = (EquationGenome)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
            EquationGenome aGene = null;
            if (TheSeed.Next(2) == 1)
            {
                aGene = aGene1;
            }
            else
            {
                aGene = aGene2;
            }

            return aGene;
        }
Ejemplo n.º 17
0
        public override Genome Crossover2Point(Genome g)
        {
            EquationGenome aGene1 = new EquationGenome();
            EquationGenome aGene2 = new EquationGenome();
            g.CopyGeneInfo(aGene1);
            g.CopyGeneInfo(aGene2);

            // Pick a random crossover point
            int CrossoverPoint1 = TheSeed.Next(1, (int)Length);
            int CrossoverPoint2 = TheSeed.Next(CrossoverPoint1, (int)Length);
            // normalize
            if (CrossoverPoint1 > CrossoverPoint2)
            {
                int temp = CrossoverPoint1;
                CrossoverPoint1 = CrossoverPoint2;
                CrossoverPoint2 = temp;
            }

            EquationGenome CrossingGene = (EquationGenome)g;

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

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

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

            // 50/50 chance of returning gene1 or gene2
            EquationGenome aGene = null;
            if (TheSeed.Next(2) == 1)
            {
                aGene = aGene1;
            }
            else
            {
                aGene = aGene2;
            }

            return aGene;
        }
Ejemplo n.º 18
0
 public void InjectGenome(EquationGenome g, int index)
 {
     ((EquationGenome)Genomes[index]).CopyGeneFrom(g);
 }
Ejemplo n.º 19
0
 public void InjectGenome(EquationGenome g, int index)
 {
     ((EquationGenome)Genomes[index]).CopyGeneFrom(g);
 }
Ejemplo n.º 20
0
        public override Genome UniformCrossover(Genome g)
        {
            EquationGenome aGene1 = new EquationGenome();
            EquationGenome aGene2 = new EquationGenome();
            g.CopyGeneInfo(aGene1);
            g.CopyGeneInfo(aGene2);

            // swap genes randomly
            EquationGenome CrossingGene = (EquationGenome)g;
            for (int i = 0; i < Length; i++)
            {
                if (TheSeed.Next(100) % 2 == 0)
                {
                    aGene1.TheArray.Add(CrossingGene.TheArray[i]);
                    aGene2.TheArray.Add(TheArray[i]);
                }
                else
                {
                    aGene1.TheArray.Add(TheArray[i]);
                    aGene2.TheArray.Add(CrossingGene.TheArray[i]);
                }

            }

            // 50/50 chance of returning gene1 or gene2
            EquationGenome aGene = null;
            if (TheSeed.Next(2) == 1)
            {
                aGene = aGene1;
            }
            else
            {
                aGene = aGene2;
            }

            return aGene;
        }
Ejemplo n.º 21
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 (EquationGenome.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
                EquationGenome babyGene1 = null;
                EquationGenome babyGene2 = null;

                int randomnum = EquationGenome.TheSeed.Next(100) % 3;
                if (randomnum == 0)
                {
                    babyGene1 = (EquationGenome)((EquationGenome)GeneDads[i]).Crossover((EquationGenome)GeneMoms[i]);
                    babyGene2 = (EquationGenome)((EquationGenome)GeneMoms[i]).Crossover((EquationGenome)GeneDads[i]);
                }
                else if (randomnum == 1)
                {
                    babyGene1 = (EquationGenome)((EquationGenome)GeneDads[i]).Crossover2Point((EquationGenome)GeneMoms[i]);
                    babyGene2 = (EquationGenome)((EquationGenome)GeneMoms[i]).Crossover2Point((EquationGenome)GeneDads[i]);
                }
                else
                {
                    babyGene1 = (EquationGenome)((EquationGenome)GeneDads[i]).UniformCrossover((EquationGenome)GeneMoms[i]);
                    babyGene2 = (EquationGenome)((EquationGenome)GeneMoms[i]).UniformCrossover((EquationGenome)GeneDads[i]);
                }

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


                CalculateFitnessForAll(GenomeFamily);

                for (int j = 0; j < 4; j++)
                {
                    CheckForUndefinedFitness((Genome)GenomeFamily[j]);
                }

                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);
                }

                // if population shrunk, you can add rest of genes
                if (Population.kPopulationLimit < Genomes.Count)
                {
                    GenomeResults.Add(GenomeFamily[3]);
                    GenomeResults.Add(GenomeFamily[4]);
                }
            }
        }