private static void Main(string[] args) { QueuePopulation testPopulation; IFitnessFunction testFitnessFunction = new ExampleFitnessFunction(); if (_useDirect) { testPopulation = new QueuePopulation(100, new MazeDirectChromosome(_mazeSideLength, 0.05), testFitnessFunction, new RouletteWheelSelection()); for (int loopCnt = 0; loopCnt < 1000; loopCnt++) { // Run the epoch testPopulation.RunEpoch(); // Output the maze structure every 20 epochs if (loopCnt%20 == 0) { WriteImage_DirectEncoding("Maze_DirectEncoding_Generation" + loopCnt + ".bmp", ((MazeDirectChromosome) testPopulation.BestChromosome).GeneArray); } } } testPopulation = new QueuePopulation(100, new MazePositiveIndirectChromosome(_mazeSideLength, 80, 100), testFitnessFunction, new RouletteWheelSelection()); for (int loopCnt = 0; loopCnt < 1000; loopCnt++) { testPopulation.RunEpoch(); if (loopCnt%20 == 0) { WriteImage_IndirectPositiveEncoding("Maze_IndirectPositiveEncoding_Generation" + loopCnt + ".bmp", ((MazePositiveIndirectChromosome) testPopulation.BestChromosome).GeneArray); } } }
/// <summary> /// Perform migration between two populations. /// </summary> /// /// <param name="anotherPopulation">Population to do migration with.</param> /// <param name="numberOfMigrants">Number of chromosomes from each population to migrate.</param> /// <param name="migrantsSelector">Selection algorithm used to select chromosomes to migrate.</param> /// /// <remarks><para>The method performs migration between two populations - current and the /// <paramref name="anotherPopulation">specified one</paramref>. During migration /// <paramref name="numberOfMigrants">specified number</paramref> of chromosomes is choosen from /// each population using <paramref name="migrantsSelector">specified selection algorithms</paramref> /// and put into another population replacing worst members there.</para></remarks> /// public void Migrate(QueuePopulation anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector) { int currentSize = this.size; int anotherSize = anotherPopulation.Size; // create copy of current population List<IChromosome> currentCopy = new List<IChromosome>(); for (int i = 0; i < currentSize; i++) { currentCopy.Add(population[i].Clone()); } // create copy of another population List<IChromosome> anotherCopy = new List<IChromosome>(); for (int i = 0; i < anotherSize; i++) { anotherCopy.Add(anotherPopulation.population[i].Clone()); } // apply selection to both populations' copies - select members to migrate migrantsSelector.ApplySelection(currentCopy, numberOfMigrants); migrantsSelector.ApplySelection(anotherCopy, numberOfMigrants); // sort original populations, so the best chromosomes are in the beginning population.Sort(); anotherPopulation.population.Sort(); // remove worst chromosomes from both populations to free space for new members population.RemoveRange(currentSize - numberOfMigrants, numberOfMigrants); anotherPopulation.population.RemoveRange(anotherSize - numberOfMigrants, numberOfMigrants); // put migrants to corresponding populations population.AddRange(anotherCopy); anotherPopulation.population.AddRange(currentCopy); // find best chromosomes in each population FindBestChromosome(); anotherPopulation.FindBestChromosome(); }