public void Test_Evolve() { Population pop = new Population(1024, 0.8d, 0.1d, 0.05d); Chromosome[] oldArr = pop.GetPopulation(); // Evolve and get the new population pop.Evolve(); Chromosome[] newArr = pop.GetPopulation(); // Check the details on the resulting evolved population. Assert.AreEqual(80, (int) (pop.GetCrossover() * 100)); Assert.AreEqual(10, (int) (pop.GetElitism() * 100)); Assert.AreEqual(5, (int) (pop.GetMutation() * 100)); // Check to ensure that the elitism took. int elitismCount = (int)Math.Round(1024 * 0.1d); int counter = 0; for (int i = 0; i < oldArr.Length; i++) { if (Array.BinarySearch(newArr, oldArr[i]) >= 0) { ++counter; } } // Account for the fact that mating/mutation may cause more than // just the fixed number of chromosomes to be identical. Assert.IsTrue(counter >= elitismCount); Assert.IsTrue(counter < oldArr.Length); }
public void Test_GetMutation() { Population pop = new Population(1024, 0.8d, 0.1d, 0.05d); Assert.AreEqual(5, (int) (pop.GetMutation() * 100)); pop = new Population(1024, 0.8d, 0.1d, 0.0d); Assert.AreEqual(0, (int) (pop.GetMutation() * 100)); pop = new Population(1024, 0.8d, 0.1d, 1.0d); Assert.AreEqual(100, (int) (pop.GetMutation() * 100)); }
public void Test_GetElitism() { Population pop = new Population(1024, 0.8d, 0.1d, 0.05d); Assert.AreEqual(10, (int) (pop.GetElitism() * 100)); pop = new Population(1024, 0.8d, 0.0d, 0.05d); Assert.AreEqual(0, (int) (pop.GetElitism() * 100)); pop = new Population(1024, 0.8d, 0.99d, 0.05d); Assert.AreEqual(99, (int) (pop.GetElitism() * 100)); }
public void Test_GetCrossover() { Population pop = new Population(1024, 0.8d, 0.1d, 0.05d); Assert.AreEqual(80, (int) (pop.GetCrossover() * 100)); pop = new Population(1024, 0.0d, 0.1d, 0.05d); Assert.AreEqual(0, (int) (pop.GetCrossover() * 100)); pop = new Population(1024, 1.0d, 0.1d, 0.05d); Assert.AreEqual(100, (int) (pop.GetCrossover() * 100)); }
static void Main(string[] args) { // The size of the simulation population const int populationSize = 2048; // The maximum number of generations for the simulation. const int maxGenerations = 50000; // The probability of crossover for any member of the population, // where 0.0 <= crossoverRatio <= 1.0 const double crossoverRatio = 0.8d; // The portion of the population that will be retained without change // between evolutions, where 0.0 <= elitismRatio < 1.0 const double elitismRatio = 0.1d; // The probability of mutation for any member of the population, // where 0.0 <= mutationRatio <= 1.0 const double mutationRatio = 0.20d; // Get the current run time. Not very accurate, but useful for // some simple reporting. long startTime = DateTime.Now.Ticks; // Create the initial population Population population = new Population(populationSize, crossoverRatio, elitismRatio, mutationRatio); // Start evolving the population, stopping when the maximum number of // generations is reached, or when we find a solution. int i = 0; Chromosome best = population.GetPopulation()[0]; while ((i++ <= maxGenerations) && (best.GetFitness() != 0)) { Console.WriteLine("Generation " + i + ": " + best.GetGene()); population.Evolve(); best = population.GetPopulation()[0]; } // Get the end time for the simulation. long endTime = DateTime.Now.Ticks; // Print out some information to the console. Console.WriteLine("Generation " + i + ": " + best.GetGene()); Console.WriteLine("Total execution time: " + (endTime - startTime) + "ms"); Console.ReadLine(); }
public void Test_GetPopulation() { Population pop = new Population(1024, 0.8d, 0.1d, 0.05d); Chromosome[] arr = pop.GetPopulation(); Assert.AreEqual(1024, arr.Length); Chromosome[] newArr = new Chromosome[arr.Length]; arr.CopyTo(newArr, 0); Array.Sort(newArr); // Assert that the array is actually sorted. CollectionAssert.AreEqual(arr, newArr); }