public void Evolve_ManyGenerations_Fast() { int numberOfCities = 40; var selection = new EliteSelection(); var crossover = new OrderedCrossover(); var mutation = new TworsMutation(); var chromosome = new TspChromosome(numberOfCities); var fitness = new TspFitness (numberOfCities, 0, 1000, 0, 1000); var population = new Population (40, 40, chromosome); var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation); ga.Start(); var firstDistance = ((TspChromosome)ga.Population.BestChromosome).Distance; ga.Termination = new GenerationNumberTermination (1001); TimeAssert.LessThan(3000, () => { ga.Start(); }); var lastDistance = ((TspChromosome)ga.Population.BestChromosome).Distance; Assert.Less(lastDistance, firstDistance); }
public void Evolve_ManyGenerations_Fast() { var selection = new EliteSelection(); var crossover = new UniformCrossover(); var mutation = new UniformMutation(true); var chromosome = new AutoConfigChromosome(); var targetChromosome = new TspChromosome(10); var targetFitness = new TspFitness(10, 0, 100, 0, 100); var fitness = new AutoConfigFitness(targetFitness, targetChromosome); fitness.PopulationMinSize = 20; fitness.PopulationMaxSize = 20; fitness.Termination = new TimeEvolvingTermination(TimeSpan.FromSeconds(5)); var population = new Population(10, 10, chromosome); var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation); ga.TaskExecutor = new SmartThreadPoolTaskExecutor() { MinThreads = 10, MaxThreads = 20 }; ga.Termination = new GenerationNumberTermination(2); ga.Start(); Assert.NotNull(ga.BestChromosome); }
public void Evaluate_FitnessLowerThanZero_Zero() { var target = new TspFitness(10, 0, 10000000, 0, 10000000); var chromosome = new TspChromosome(10); var actual = target.Evaluate(chromosome); Assert.AreEqual(0, actual); }
public void Evaluate_ChromosomeWithLowerCities_FitnessDividedByDiff() { var target = new TspFitness(10, 0, 10, 0, 10); var chromosome = new TspChromosome(9); var actual = target.Evaluate(chromosome); Assert.AreNotEqual(0, actual); }
public void Clone_NoArgs_Cloned() { var target = new TspChromosome (10); target.Distance = 123; var actual = target.Clone () as TspChromosome; Assert.IsFalse (Object.ReferenceEquals (target, actual)); Assert.AreEqual(123, actual.Distance); }
public override IFitness CreateFitness() { var targetChromosome = new TspChromosome(10); var targetFitness = new TspFitness(10, 0, 100, 0, 100); var fitness = new AutoConfigFitness(targetFitness, targetChromosome); fitness.Termination = new FitnessStagnationTermination(500); fitness.PopulationMinSize = 20; fitness.PopulationMaxSize = 20; return fitness; }
public void GenerateGene_FitnessLowerThanZero_Zero () { var target = new TspChromosome (10); var cityIndex = Convert.ToDouble (target.GenerateGene (0).Value); Assert.IsTrue (cityIndex >= 0 && cityIndex < 10); }
/// <summary> /// Updates the sample. /// </summary> public override void Update() { var population = Context.Population; if (population != null && population.CurrentGeneration != null) { m_bestChromosome = population.BestChromosome as TspChromosome; } }
/// <summary> /// Resets the sample. /// </summary> public override void Reset() { m_bestChromosome = null; }