public void GA_Issue61_Solved() { const Int32 FinalAns = 4567213; var chromosome = new Issue61.GuessNumberChromosome(FinalAns.ToString().Length); var population = new Population(1000, 5000, chromosome); var fitness = new Issue61.GuessNumberFitness(FinalAns); var selection = new EliteSelection(); var crossover = new AlternatingPositionCrossover(); var mutation = new ReverseSequenceMutation(); var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation); ga.MutationProbability = 0.2f; ga.CrossoverProbability = 0.75f; ga.Termination = new OrTermination( new FitnessThresholdTermination(1.0), new FitnessStagnationTermination(200), new GenerationNumberTermination(1000)); ga.Population.GenerationStrategy = new TrackingGenerationStrategy(); ga.Start(); foreach (var gen in ga.Population.Generations) { foreach (var chromossome in gen.Chromosomes) { // Asserts if AlternatingPositionCrossover generated only ordered chromossomes. Assert.AreEqual(chromosome.Length, chromosome.GetGenes().Distinct().Count()); } } }
public void ItCanPerformCrossover() { var one = GATestHelper.GetNumericChromosomeOne(); var two = GATestHelper.GetNumericChromosomeTwo(); var cc = new AlternatingPositionCrossover(); var child = cc.Execute(one, two, GATestHelper.GetTravelingSalesmanDefaultConfiguration()); Assert.AreEqual("1,5,2,4,3,6,7", string.Join(",", (IEnumerable <Gene>)child.Genes)); }
public void Cross_TspChromosome_Child() { var target = new AlternatingPositionCrossover(); var chromosome1 = new TspChromosome(100); var chromosome2 = new TspChromosome(100); var actual = target.Cross(new TspChromosome[] { chromosome1, chromosome2 }); Assert.AreEqual(2, actual.Count); CollectionAssert.AllItemsAreUnique(chromosome1.GetGenes()); CollectionAssert.AllItemsAreUnique(chromosome2.GetGenes()); }
public void GA_WithAlternatingPositionCrossover_Evolve() { var chromosome = new TspChromosome(50); var population = new Population(50, 50, chromosome) { GenerationStrategy = new TrackingGenerationStrategy() }; var fitness = new TspFitness(chromosome.Length, 0, 1000, 0, 1000); var crossover = new AlternatingPositionCrossover(); var ga = new GeneticAlgorithm(population, fitness, new EliteSelection(), crossover, new ReverseSequenceMutation()) { Termination = new GenerationNumberTermination(100) }; ga.Start(); Assert.Less( population.Generations.First().BestChromosome.Fitness.Value, population.Generations.Last().BestChromosome.Fitness.Value); }
public void Cross_ParentWithNoOrderedGenes_Exception() { var target = new AlternatingPositionCrossover(); var chromosome1 = Substitute.For <ChromosomeBase>(8); chromosome1.ReplaceGenes(0, new Gene[] { new Gene(8), new Gene(2), new Gene(3), new Gene(4), new Gene(6), new Gene(5), new Gene(7), new Gene(1) }); // 3 7 5 1 6 8 2 4 var chromosome2 = Substitute.For <ChromosomeBase>(8); chromosome2.ReplaceGenes(0, new Gene[] { new Gene(1), new Gene(2), new Gene(3), new Gene(4), new Gene(5), new Gene(5), new Gene(6), new Gene(7) }); Assert.Catch <CrossoverException>(() => { target.Cross(new List <IChromosome>() { chromosome1, chromosome2 }); }, "The Alternating-position (AP) can be only used with ordered chromosomes. The specified chromosome has repeated genes."); }
private void EvolveGeneticStrategyButton_Click(object sender, RoutedEventArgs e) { OutputTextBlock.Text = "Evolving..."; Task.Run(() => { var chromosome = new BlackjackChromosome(); var fitness = new BlackjackFitness(); var population = new Population(Settings.GeneticSettings.MinPopulationSize, Settings.GeneticSettings.MaxPopulationSize, chromosome); ISelection selection; switch (Settings.GeneticSettings.SelectionType) { case SelectionType.Elite: selection = new EliteSelection(); break; case SelectionType.RouletteWheel: selection = new RouletteWheelSelection(); break; case SelectionType.StochasticUniversalSampling: selection = new StochasticUniversalSamplingSelection(); break; case SelectionType.Tournament: selection = new TournamentSelection(Settings.GeneticSettings.TournamentSize); break; default: throw new InvalidOperationException(); } ICrossover crossover; switch (Settings.GeneticSettings.CrossoverType) { case CrossoverType.AlternatingPosition: crossover = new AlternatingPositionCrossover(); break; case CrossoverType.CutAndSplice: crossover = new CutAndSpliceCrossover(); break; case CrossoverType.Cycle: crossover = new CycleCrossover(); break; case CrossoverType.OnePoint: crossover = new OnePointCrossover(); break; case CrossoverType.TwoPoint: crossover = new TwoPointCrossover(); break; case CrossoverType.OrderBased: crossover = new OrderBasedCrossover(); break; case CrossoverType.Ordered: crossover = new OrderedCrossover(); break; case CrossoverType.PartiallyMapped: crossover = new PartiallyMappedCrossover(); break; case CrossoverType.PositionBased: crossover = new PositionBasedCrossover(); break; case CrossoverType.ThreeParent: crossover = new ThreeParentCrossover(); break; case CrossoverType.Uniform: crossover = new UniformCrossover(Settings.Current.GeneticSettings.MixProbability); break; case CrossoverType.VotingRecombination: crossover = new VotingRecombinationCrossover(); break; default: throw new InvalidOperationException(); } var mutation = new UniformMutation(); var termination = new FitnessStagnationTermination(Settings.Current.GeneticSettings.NumStagnantGenerations); var taskExecutor = new ParallelTaskExecutor(); var ga = new GeneticAlgorithm( population, fitness, selection, crossover, mutation); ga.Termination = termination; ga.TaskExecutor = taskExecutor; ga.MutationProbability = Settings.GeneticSettings.MutationProbability; ga.CrossoverProbability = Settings.GeneticSettings.CrossoverProbability; var latestFitness = double.MinValue; ga.GenerationRan += (s, o) => { geneticStrategy = (IStrategy)ga.BestChromosome; var generationNumber = ga.GenerationsNumber; var bestFitness = ga.BestChromosome.Fitness.Value; var avgFitness = ga.Population.CurrentGeneration.Chromosomes.Average(c => c.Fitness.Value); Dispatcher.Invoke(() => { if (generationNumber == 1) { OutputTextBlock.Text = string.Empty; } OutputTextBlock.Text = $"Gen: {generationNumber}\tFit: {bestFitness}\tAvg: {avgFitness.ToString("0")}\n" + OutputTextBlock.Text; if (bestFitness != latestFitness) { latestFitness = bestFitness; var savedImageName = Settings.Current.GeneticSettings.SaveImagePerGeneration ? "gen" + generationNumber : null; StrategyViewer.Draw(GeneticStrategyCanvas, geneticStrategy, $"Best from generation {generationNumber}", savedImageName); } }, DispatcherPriority.Background); }; ga.TerminationReached += (s, o) => { Dispatcher.Invoke(() => { OutputTextBlock.Text = "Termination reached.\n" + OutputTextBlock.Text; TestGeneticStrategyButton.IsEnabled = true; }, DispatcherPriority.Background); }; ga.Start(); }); }
public void Cross_DocumentationSample_Child() { var target = new AlternatingPositionCrossover(); // 1 2 3 4 5 6 7 8 var chromosome1 = Substitute.For <ChromosomeBase>(8); chromosome1.ReplaceGenes(0, new Gene[] { new Gene(1), new Gene(2), new Gene(3), new Gene(4), new Gene(5), new Gene(6), new Gene(7), new Gene(8) }); var child1 = Substitute.For <ChromosomeBase>(8); chromosome1.CreateNew().Returns(child1); // 3 7 5 1 6 8 2 4 var chromosome2 = Substitute.For <ChromosomeBase>(8); chromosome2.ReplaceGenes(0, new Gene[] { new Gene(3), new Gene(7), new Gene(5), new Gene(1), new Gene(6), new Gene(8), new Gene(2), new Gene(4) }); var child2 = Substitute.For <ChromosomeBase>(8); chromosome2.CreateNew().Returns(child2); var actual = target.Cross(new List <IChromosome>() { chromosome1, chromosome2 }); Assert.AreEqual(2, actual.Count); // 1 3 2 7 5 4 6 8 var actualChild = actual[0]; Assert.AreEqual(8, actualChild.Length); Assert.AreEqual(1, actualChild.GetGene(0).Value); Assert.AreEqual(3, actualChild.GetGene(1).Value); Assert.AreEqual(2, actualChild.GetGene(2).Value); Assert.AreEqual(7, actualChild.GetGene(3).Value); Assert.AreEqual(5, actualChild.GetGene(4).Value); Assert.AreEqual(4, actualChild.GetGene(5).Value); Assert.AreEqual(6, actualChild.GetGene(6).Value); Assert.AreEqual(8, actualChild.GetGene(7).Value); // 3 1 7 2 5 4 6 8 actualChild = actual[1]; Assert.AreEqual(8, actualChild.Length); Assert.AreEqual(3, actualChild.GetGene(0).Value); Assert.AreEqual(1, actualChild.GetGene(1).Value); Assert.AreEqual(7, actualChild.GetGene(2).Value); Assert.AreEqual(2, actualChild.GetGene(3).Value); Assert.AreEqual(5, actualChild.GetGene(4).Value); Assert.AreEqual(4, actualChild.GetGene(5).Value); Assert.AreEqual(6, actualChild.GetGene(6).Value); Assert.AreEqual(8, actualChild.GetGene(7).Value); }
public IList <IChromosome> AlternatingPosition() { var target = new AlternatingPositionCrossover(); return(target.Cross(CreateTwoParents())); }