예제 #1
0
 public void Crossover(Chromosome other)
 {
     int crossoverPosition = c_rnd.Next(c_length);
     //Console.WriteLine("Crossover at " + crossoverPosition);
     for( int i = crossoverPosition; i < c_length; ++i )
     {
         int tmp = Genes[i];
         Genes[i] = other.Genes[i];
         other.Genes[i] = tmp;
     }
 }
예제 #2
0
        public void Evolve(FitnessFunc f)
        {
            Chromosome[] newGen = new Chromosome[Chromosomes.Length];

            Fitness = 0f;
            foreach( Chromosome c in Chromosomes )
            {
                Fitness += c.UpdateFitness(f);
            }

            for( int i = 0; i < newGen.Length; )
            {
                Chromosome c0 = new Chromosome(Selection());
                Chromosome c1 = new Chromosome(Selection());
                //Console.WriteLine("New Gen - " + i);
                //Console.WriteLine(String.Format("Select chromosome {0} and {1}.", Array.IndexOf(Chromosomes, c0), Array.IndexOf(Chromosomes, c1)));

                if( (float)c_rnd.NextDouble() < CrossoverRate )
                {
                    c0.Crossover(c1);
                }

                c0.Mutate(MutationRate);
                c1.Mutate(MutationRate);

                newGen[i++] = c0;
                // If the population is odd, discard one member.
                // It could be done at random into the whole population instead of the last one.
                if( i < newGen.Length )
                {
                    newGen[i++] = c1;
                }
            }

            Chromosomes = newGen;
            ++Generation;

            Fitness = 0f;
            foreach( Chromosome c in Chromosomes )
            {
                Fitness += c.UpdateFitness(f);
            }
        }
예제 #3
0
 public Chromosome(Chromosome other)
 {
     Genes = new int[c_length];
     Array.Copy(other.Genes, Genes, c_length);
 }
예제 #4
0
        public Population(int nbChromosomes, float crossoverRate, float mutationRate)
        {
            CrossoverRate = crossoverRate;
            MutationRate  = mutationRate;
            Fitness       = 0f;

            Chromosomes = new Chromosome[nbChromosomes];
            for( int i = 0; i < nbChromosomes; ++i )
            {
                Chromosomes[i] = new Chromosome();
            }
        }