public void EndCurrentGeneration_BestChromosomeChanged_ChangeEventRaise() { var target = new TplPopulation(2, 2, new ChromosomeStub()); var eventRaise = false; target.BestChromosomeChanged += (e, a) => { eventRaise = true; }; target.CreateInitialGeneration(); target.CurrentGeneration.Chromosomes.Each(c => c.Fitness = 1); target.EndCurrentGeneration(); Assert.IsTrue(eventRaise); }
public void CreateInitialGeneration_AdamChromosomeCreateNewNull_Exception() { var c = Substitute.For <ChromosomeBase>(4); c.CreateNew().Returns((IChromosome)null); var population = new TplPopulation(2, 2, c); Assert.Catch <InvalidOperationException>(() => { try { population.CreateInitialGeneration(); } catch (AggregateException e) { throw e.InnerException; } }, "The Adam chromosome's 'CreateNew' method generated a null chromosome. This is a invalid behavior, please, check your chromosome code."); }
public static FloatingPointChromosome EvolveGeneticAlgorithm(FloatingPointChromosome chromosome, IOthelloAgent agent, string chromosomeLabel = "") { IPopulation population = new TplPopulation(30, 60, chromosome); IFitness fitness = new EvaluationFitness(agent); ISelection selection = new RouletteWheelSelection(); //Guess ICrossover crossover = new UniformCrossover(); //Guess IMutation mutation = new UniformMutation(); //Guess ITermination stagnation = new FitnessStagnationTermination(500); ITermination threshold = new FitnessThresholdTermination(.9); ITaskExecutor taskExecutor = new ParallelTaskExecutor() { MaxThreads = Environment.ProcessorCount, MinThreads = Environment.ProcessorCount / 2 }; GeneticAlgorithm algorithm = new GeneticAlgorithm(population, fitness, selection, crossover, mutation) { TaskExecutor = new TplTaskExecutor(), MutationProbability = .2f }; algorithm.TaskExecutor = taskExecutor; algorithm.Termination = stagnation; algorithm.Start(); SaveChromosome((FloatingPointChromosome)algorithm.BestChromosome, chromosomeLabel); Debug.WriteLine("finished Training, with {0} time spent on evolving", algorithm.TimeEvolving); Debug.WriteLine("fitness of this generation vs the last : {0}", algorithm.Fitness); return((FloatingPointChromosome)algorithm.BestChromosome); }