示例#1
0
 public void Crossover(ref Genome genome2, out Genome child1, out Genome child2)
 {
     int pos = (int)(random.NextDouble() * (double)Length);
     child1 = new Genome(Length, false);
     child2 = new Genome(Length, false);
     for (int i = 0; i < Length; i++)
     {
         if (i < pos)
         {
             child1.Genes[i] = Genes[i];
             child2.Genes[i] = genome2.Genes[i];
         }
         else
         {
             child1.Genes[i] = genome2.Genes[i];
             child2.Genes[i] = Genes[i];
         }
     }
 }
示例#2
0
 /// <summary>
 /// Create the *initial* genomes by repeated calling the supplied fitness function
 /// </summary>
 private void CreateGenomes()
 {
     for (int i = 0; i < PopulationSize; i++)
     {
         Genome g = new Genome(GenomeSize);
         CurrentGeneration.Add(g);
     }
 }
示例#3
0
 public abstract Genome UniformCrossover(Genome g);
示例#4
0
 public abstract Genome Crossover2Point(Genome g);
示例#5
0
 public abstract Genome Crossover(Genome g);
示例#6
0
 public abstract void CopyGeneInfo(Genome g);
示例#7
0
 public abstract void CopyGeneFrom(Genome src);
示例#8
0
 private void Mutate(Genome aGene)
 {
     if (EquationGenome.TheSeed.Next(100) < (int)(kMutationFrequency * 100.0))
     {
       	aGene.Mutate();
     }
 }
示例#9
0
 public void CheckForUndefinedFitness(Genome g)
 {
     if (double.IsNaN(g.CurrentFitness))
         g.CurrentFitness = 0.01f;
 }
示例#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;
        }
示例#11
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;
        }
示例#12
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;
        }
示例#13
0
 public override void CopyGeneInfo(Genome dest)
 {
     EquationGenome theGene = (EquationGenome)dest;
     theGene.Length = Length;
     theGene.TheMin = TheMin;
     theGene.TheMax = TheMax;
 }
示例#14
0
 public Genome DeepCopy()
 {
     Genome g = new Genome(Length, false);
     Array.Copy(Genes, g.Genes, Length);
     return g;
 }
示例#15
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;
        }