public void EasyGaTest() { MultipointCrossover<ListGenotype<BitGene>, BitGene, double> crossover = new MultipointCrossover<ListGenotype<BitGene>, BitGene, double>(1); EasyGa<ListGenotype<BitGene>, double> algorithm = new EasyGa<ListGenotype<BitGene>, double>.Builder() .WithElitismPercentage(0.5) .WithFitnessFunction(g => g.Count(b => b.Value)) .WithStopCriteria( StopCriteriaBuilder.StopAtGeneration<ListGenotype<BitGene>, double>(2500) .Or(StopCriteriaBuilder.StopAtFitness<ListGenotype<BitGene>, double>(20))) .RegisterBreeder(new BitBreeder(20, 20)) .Register(new RouletteWheelSelector<ListGenotype<BitGene>>(20)) .Register(new Recombinator<ListGenotype<BitGene>, double>(crossover, 2, 10, Recombinator<ListGenotype<BitGene>, double>.RecombinatioNumberType.Absolute)) .Register(new BitBaseMutator<ListGenotype<BitGene>, double>(0.05)) .Build(); Engine<ListGenotype<BitGene>, double> engine = new Engine<ListGenotype<BitGene>, double>.Builder() .WithAlgorithm(algorithm) .Build(); while (!engine.HasReachedStopCriteria) { //if (engine.World != null) //{ // TestContext.WriteLine($"Generation {engine.World.Generation} (Size: {engine.World.Population.Count})"); // foreach (Individual<ListGenotype<BitGene>, double> individual in engine.World.Population) // { // TestContext.WriteLine(individual); // } //} engine.NextGeneration(); } TestContext.WriteLine($"{engine.CurrentWorld.Generation} generations reached"); TestContext.WriteLine($"{engine.Statistics.BestIndividualGeneration} is best indivual's generation"); TestContext.WriteLine($"{engine.Statistics.BestFitness} is best Fitness"); TestContext.WriteLine($"{engine.Statistics.BestGenotype} is best Genotype"); }
private void BuildEngine() { GenotypeGeneratorBreeder<ListGenotype<FloatGene>>.BreederDelegateWithIndex generateNewGenotype = index => new ListGenotype<FloatGene>( Enumerable.Range(0, cities.Count).ToList().RandomSort().Select(i => new FloatGene(i))); EasyGa<ListGenotype<FloatGene>, double> algorithm = new EasyGa<ListGenotype<FloatGene>, double>.Builder() .WithElitismPercentage(0.01) .WithFitnessFunction(FitnessFunction) .WithStopCriteria(world => world.Generation == 1200) .RegisterBreeder(new GenotypeGeneratorBreeder<ListGenotype<FloatGene>>(generateNewGenotype, 200)) .Register(new RouletteWheelSelector<ListGenotype<FloatGene>>(200)) .Register( new Recombinator<ListGenotype<FloatGene>, double>( new PartiallyMatchedCrossover<ListGenotype<FloatGene>, FloatGene, double>(), 2, 100, Recombinator<ListGenotype<FloatGene>, double>.RecombinatioNumberType.Absolute)) .Register(new ReverseAlterer<ListGenotype<FloatGene>, FloatGene, double>(0.3)) .Register(new SwapAlterer<ListGenotype<FloatGene>, FloatGene, double>(0.3)) .Build(); Engine = new Engine<ListGenotype<FloatGene>, double>.Builder() .WithAlgorithm(algorithm) .Build(); }