예제 #1
0
        /// <summary>
        /// Breeds the elites. This automatically preserves their genetic makeup to the next generation.
        /// </summary>
        /// <param name="rand">The random object.</param>
        /// <param name="newPopulation">The new population.</param>
        /// <param name="codes">The genetic codes.</param>
        private void BreedElites(Random rand, List<Rat> newPopulation, String[] codes)
        {
            //Automatically add the top rat into the next population
            Rat rat1 = new Rat(MainForm.START_POSITION, 1, 1, 1);
            rat1.code = codes[0];
            rat1.decoded = Decode(codes[0]);
            newPopulation.Add(rat1);

            //Automatically add the second top rat into the next population

            Rat rat2 = new Rat(MainForm.START_POSITION, 1, 1, 1);
            rat2.code = codes[1];
            rat2.decoded = Decode(codes[1]);
            newPopulation.Add(rat2);
            int ct = 2;
            //Breed the top two rats
            if (numOfElites >= 4)
            {
                Breed(rand, newPopulation, codes[0], codes[1]);
                ct += 2;
            }

            //Randomly select the rest of the elites in pairs
            while (ct < codes.Length)
            {
                Breed(rand, newPopulation, codes[rand.Next(codes.Length)], codes[rand.Next(codes.Length)]);
                ct += 2;
            }
        }
예제 #2
0
        /// <summary>
        /// Breed two parent rats to produce offspring.
        /// </summary>
        /// <param name="rand">The random object.</param>
        /// <param name="newPopulation">The new population.</param>
        /// <param name="parent1">The genes of parent1.</param>
        /// <param name="parent2">The genes of parent2.</param>
        private void Breed(Random rand, List<Rat> newPopulation, String parent1, String parent2)
        {
            String baby1 = string.Empty, baby2 = string.Empty;
            Rat babyRat1, babyRat2;
            //apply cross over to the offspring
            CrossOver(rand, parent1, parent2, ref baby1, ref baby2);
            //mutate the bits for the offspring
            Mutate(rand, ref baby1);
            Mutate(rand, ref baby2);

            //create the rat objects
            babyRat1 = new Rat(MainForm.START_POSITION, 1, 1, 1);
            babyRat1.code = baby1;
            babyRat1.decoded = Decode(baby1);
            babyRat2 = new Rat(MainForm.START_POSITION, 1, 1, 1);
            babyRat2.code = baby2;
            babyRat2.decoded = Decode(baby2);
            newPopulation.Add(babyRat1);
            newPopulation.Add(babyRat2);
        }
예제 #3
0
 /// <summary>
 /// Return the current rats in the population to be tested.
 /// </summary>
 /// <returns></returns>
 public Rat[] CurrentRats()
 {
     Rat[] rats = new Rat[MainForm.current_rats_per_trial];
     int ct = 0;
     while (ct < MainForm.current_rats_per_trial)
     {
         rats[ct] = population[currentRat + ct];
         ++ct;
     }
     return rats;
 }
예제 #4
0
파일: MainForm.cs 프로젝트: ajrod/Ratatat
        /// <summary>
        /// Gets the max score of the rats.
        /// </summary>
        /// <param name="rats">The rats.</param>
        /// <returns></returns>
        public double getMaxScore(Rat[] rats)
        {
            double max = 0;
            foreach (Rat rat in rats)
            {
                if (rat.score > max)
                {
                    max = rat.score;
                }
            }

            return max;
        }
예제 #5
0
파일: MainForm.cs 프로젝트: ajrod/Ratatat
 /// <summary>
 /// Return true if all rats are finished.
 /// </summary>
 /// <param name="rats">The rats.</param>
 /// <returns></returns>
 public bool finished(Rat[] rats)
 {
     foreach (Rat rat in rats)
     {
         if (!rat.finished)
         {
             return false;
         }
     }
     return true;
 }
예제 #6
-1
        /// <summary>
        /// Gets the genetic codes of all elite rats.
        /// </summary>
        /// <param name="numbOfElites">The total number of elites.</param>
        /// <returns></returns>
        private String[] GetElites(int numbOfElites)
        {
            Rat[] elites = new Rat[numbOfElites];
            String[] elite_codes = new String[numbOfElites];
            int current_elite = 0, ct, current_index;
            double current_elite_score;

            while (current_elite < numbOfElites)
            {
                current_elite_score = population[0].score;
                elites[current_elite] = population[0];
                elite_codes[current_elite] = population[0].code;
                current_index = 0;
                ct = 1;
                while (ct < POPULATION_SIZE - current_elite)
                {
                    if (population[ct].score > current_elite_score)
                    {
                        current_elite_score = population[ct].score;
                        elites[current_elite] = population[ct];
                        elite_codes[current_elite] = population[ct].code;
                        current_index = ct;
                    }
                    ++ct;
                }
                ++current_elite;
                population.RemoveAt(current_index);
            }

            ct = 0;
            while (ct < numbOfElites)
            {
                population.Add(elites[ct]);
                Debug.Assert(elites[ct].code.Length > 0);
                ++ct;
            }
            Debug.Assert(elite_codes.Length == numbOfElites);
            return elite_codes;
        }
예제 #7
-1
 /// <summary>
 /// Creates the starting population of rats with randomly generated chromosomes.
 /// </summary>
 private void CreateStartingPop()
 {
     int ct = 0;
     Random r = new Random((int)DateTime.Now.Ticks);
     while (ct < POPULATION_SIZE)
     {
         Rat tmp = new Rat(MainForm.START_POSITION, 1, 1, 1);
         tmp.code = this.GetRandomChromosome(r);
         tmp.decoded = this.Decode(tmp.code);
         population.Add(tmp);
         ++ct;
     }
 }