public void SolveStringSearchProblem() { string passphrase = "GenticAlgorithmsWorks!"; var random = new RandomNumberGenerators.Default(); var genePool = new Chromosomes.GenePool<char>(); genePool.AddRange("abcdefghijklmnopqrstuvwxyz".ToCharArray()); genePool.AddRange("ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()); genePool.Add('!'); var chromosomeFactory = new Chromosomes.SequentialChromosomeFactory<char>(random, genePool, passphrase.Length); // chromosome as fixed lenght sequence with array of allowed characters var fitnessCalc = new FitnessCalculators.StringComparer(passphrase); var mutationStrategy = new MutationStrategies.SingleGeneMutation(random, 0.01); // mutation rate fixed to 1% var crossoverStrategy = new CrossoverStrategies.SingleCut(random); var selectionStrategy = new SelectionStrategies.KeepFittest(); var populationStrategy = new PopulationStrategies.SinglePopulation(100); // size fixed to 100 var evolution = new Evolution(random, chromosomeFactory, populationStrategy, mutationStrategy, crossoverStrategy, selectionStrategy); do { evolution.Step(); if (evolution.TopFitness == 1.0) { return; } } while (evolution.Generation < 1000); Assert.Fail(); }
public void SolveTravelingSalesmanProblem() { var citiesCoordinates = new List<TravelingSalesmanCity>() { new TravelingSalesmanCity('A', 0, 0), new TravelingSalesmanCity('B', 10, 100), new TravelingSalesmanCity('C', 20, 20), new TravelingSalesmanCity('D', 60, 20), new TravelingSalesmanCity('E', 30, 10), new TravelingSalesmanCity('F', 50, 70), new TravelingSalesmanCity('G', 70, 30), new TravelingSalesmanCity('H', 30, 30), new TravelingSalesmanCity('I', 50, 80) }; var random = new RandomNumberGenerators.Default(); var genePool = new Chromosomes.GenePool<char>(); genePool.AddRange(new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' }); var chromosomeFactory = new Chromosomes.PermutationChromosomeFactory<int>(random, genePool, 9); // chromosome as a permutation of 1-9 numbers var fitnessCalc = new TravelingSalesmanProblemFitnessCalculator(citiesCoordinates); var mutationStrategy = new MutationStrategies.SingleGeneMutation(random, 0.02); // mutation rate fixed to 2% var crossoverStrategy = new CrossoverStrategies.DoubleCut(random); var selectionStrategy = new SelectionStrategies.RouletteWheel(random); var migrationStrategy = new MigrationStrategies.Random(random, 0.05); // random migration with chance of occurence 5% var populationStrategy = new PopulationStrategies.MultiPopulation(10, 50, migrationStrategy); // 10 population with fixed size of 50 // need to pass something telling that fitness is a total distance so the shortest the better var evolution = new Evolution(random, chromosomeFactory, populationStrategy, mutationStrategy, crossoverStrategy, selectionStrategy); do { evolution.Step(); if (evolution.TopFitness == 1.0) { return; } } while (evolution.Generation < 1000); Assert.Fail(); }