public static void Run() { var config = new EAConfiguration { PopulationSize = 13, OverproductionFactor = 1.5, MaximumGenerations = 10000, CrossoverType = CrossoverType.Uniform, AdultSelectionType = AdultSelectionType.Overproduction, ParentSelectionType = ParentSelectionType.Tournament, CrossoverRate = 0.21, MutationRate = 0.98, TournamentSize = 7, TournamentProbability = 0.895, TargetFitness = 1.0, Mode = EAMode.MaximizeFitness, Elites = 0, CalculateStatistics = true }; var lolzea = new LOLZEA(config, new DefaultRandomNumberGenerator()); var stopwatchtot = new Stopwatch(); var stopwatchgen = new Stopwatch(); PopulationStatistics currentStats = new PopulationStatistics(); lolzea.PopulationStatisticsCalculated += (stats) => { currentStats = stats; }; lolzea.NewGenerationEvent += (gen) => { //PrintProgressBar(gen, config.MaximumGenerations); var progress = (gen / (double)config.MaximumGenerations) * 100.0; var totruntime = stopwatchtot.Elapsed; var genruntime = stopwatchgen.Elapsed; Console.WriteLine(); Console.WriteLine(string.Format("G# {0} best_f: {1:F3} avg_f: {2:F3} SD: {3:F3} Progress: {4,5:F2} Gen: {5} Tot: {6}", gen, lolzea.Best.Fitness, currentStats.AverageFitness, currentStats.StandardDeviationFitness, progress, genruntime, totruntime)); Console.WriteLine($"Generation winner: {lolzea.Best.ToString()}"); stopwatchgen.Restart(); }; stopwatchtot.Start(); stopwatchgen.Start(); var res = lolzea.Evolve(); Console.WriteLine("\n\nDone!"); Console.WriteLine($"Winner: {res.Winner}"); Console.Read(); }
public static void Run() { var config = new EAConfiguration { PopulationSize = 100, OverproductionFactor = 2.0, MaximumGenerations = 100, CrossoverType = CrossoverType.OnePoint, AdultSelectionType = AdultSelectionType.GenerationalMixing, ParentSelectionType = ParentSelectionType.Tournament, CrossoverRate = 0.9, MutationRate = 0.01, TournamentSize = 10, TournamentProbability = 0.8, TargetFitness = 1.0, Mode = EAMode.MaximizeFitness, Elites = 1, CalculateStatistics = true }; var onemaxEA = new OneMaxEA(config, new DefaultRandomNumberGenerator()); var stopwatchtot = new Stopwatch(); var stopwatchgen = new Stopwatch(); PopulationStatistics currentStats = new PopulationStatistics(); onemaxEA.PopulationStatisticsCalculated += (stats) => { currentStats = stats; }; onemaxEA.NewGenerationEvent += (gen) => { //PrintProgressBar(gen, config.MaximumGenerations); double progress = (gen / (double)config.MaximumGenerations) * 100.0; var totruntime = stopwatchtot.Elapsed; var genruntime = stopwatchgen.Elapsed; Console.WriteLine(); Console.WriteLine(string.Format("G# {0} best_f: {1:F3} avg_f: {2:F3} SD: {3:F3} Progress: {4,5:F2} Gen: {5} Tot: {6}", gen, onemaxEA.Best.Fitness, currentStats.AverageFitness, currentStats.StandardDeviationFitness, progress, genruntime, totruntime)); Console.WriteLine("Generation winner: " + ((BinaryGenotype)onemaxEA.Best?.Genotype).ToBitString()); stopwatchgen.Restart(); }; stopwatchtot.Start(); stopwatchgen.Start(); var res = onemaxEA.Evolve(); Console.WriteLine("\n\nDone!"); Console.WriteLine($"Winner: {res.Winner}"); Console.Read(); }
public void TournamentParentSelection_VerifySelectionProbability() { // Test tournament selection by comparing it to the old "established" tournament selection implementation. var random = new DefaultRandomNumberGenerator("seed".GetHashCode()); for (int tournamentSize = 2; tournamentSize <= 10; tournamentSize++) { var population = new Population(11); var index = 0; population.Fill(() => new TestPhenotype(index++, index * index)); population.Sort(EAMode.MaximizeFitness); var config = new EAConfiguration { TournamentSize = 3, TournamentProbability = 0.5 }; #pragma warning disable CS0618 // Type or member is obsolete var oldselection = new TournamentParentSelectionOld(config); #pragma warning restore CS0618 // Type or member is obsolete var oldselected = oldselection.SelectParents(population, 275000, EAMode.MaximizeFitness, random); foreach (var(a, b) in oldselected) { ((TestPhenotype)a).Count2++; ((TestPhenotype)b).Count2++; } var selection = new TournamentParentSelection(config); var selected = selection.SelectParents(population, 275000, EAMode.MaximizeFitness, random); foreach (var(a, b) in selected) { ((TestPhenotype)a).Count++; ((TestPhenotype)b).Count++; } var counts = population.Cast <TestPhenotype>().Select(p => p.Count).Zip(population.Cast <TestPhenotype>().Select(p => p.Count2), (c1, c2) => (c1, c2)); foreach (var count in counts) { (count.c1 - count.c2).Should().BeLessThan(1000, $"tournament size = {tournamentSize}"); } } }
public void ReadConfigFromFileTest() { var path = "TestFiles/TestConfig.json"; var config = EAConfiguration.ReadConfigurationFromFile(path); config.PopulationSize.Should().Be(100); config.OverproductionFactor.Should().Be(1.5); config.MaximumGenerations.Should().Be(500); config.CrossoverType.Should().Be(CrossoverType.Uniform); config.AdultSelectionType.Should().Be(AdultSelectionType.Overproduction); config.ParentSelectionType.Should().Be(ParentSelectionType.FitnessProportionate); config.CrossoverRate.Should().Be(0.8); config.MutationRate.Should().Be(0.02); config.TournamentSize.Should().Be(10); config.TournamentProbability.Should().Be(0.5); config.TargetFitness.Should().Be(1.0); config.Mode.Should().Be(EAMode.MaximizeFitness); config.Elites.Should().Be(1); config.ReevaluateElites.Should().BeTrue(); }
public void ReadConfigFromJsonStringTest() { var json = @"{ ""PopulationSize"": 100, ""OverproductionFactor"": 1.5, ""MaximumGenerations"": 500, ""CrossoverType"": ""Uniform"", ""AdultSelectionType"": ""Overproduction"", ""ParentSelectionType"": ""FitnessProportionate"", ""CrossoverRate"": 0.8, ""MutationRate"": 0.02, ""TournamentSize"": 10, ""TournamentProbability"": 0.5, ""TargetFitness"": 1.0, ""Mode"": ""MaximizeFitness"", ""Elites"": 1, ""ReevaluateElites"": true, ""RankSelectionMinProbability"": 0.5, ""RankSelectionMaxProbability"": 1.5 }"; var config = EAConfiguration.ParseFromJsonString(json); config.PopulationSize.Should().Be(100); config.OverproductionFactor.Should().Be(1.5); config.MaximumGenerations.Should().Be(500); config.CrossoverType.Should().Be(CrossoverType.Uniform); config.AdultSelectionType.Should().Be(AdultSelectionType.Overproduction); config.ParentSelectionType.Should().Be(ParentSelectionType.FitnessProportionate); config.CrossoverRate.Should().Be(0.8); config.MutationRate.Should().Be(0.02); config.TournamentSize.Should().Be(10); config.TournamentProbability.Should().Be(0.5); config.TargetFitness.Should().Be(1.0); config.Mode.Should().Be(EAMode.MaximizeFitness); config.Elites.Should().Be(1); config.ReevaluateElites.Should().BeTrue(); config.RankSelectionMinProbability.Should().Be(0.5); config.RankSelectionMaxProbability.Should().Be(1.5); }
public void TournamentSelection_PerfTest() { var random = new DefaultRandomNumberGenerator(); var config = new EAConfiguration { TournamentSize = 20, TournamentProbability = 0.5 }; var popsize = 10000; var pop = new Population(popsize); pop.Fill(() => CreatePhenotypeMock(random.NextDouble() * 10).Object); pop.Sort(EAMode.MaximizeFitness); Console.WriteLine($"popsize = {popsize}, tournament size = {config.TournamentSize}"); var watch = new Stopwatch(); watch.Start(); #pragma warning disable CS0618 // Type or member is obsolete IParentSelection selection = new TournamentParentSelectionOld(config); #pragma warning restore CS0618 // Type or member is obsolete foreach (var selected in selection.SelectParents(pop, popsize, EAMode.MaximizeFitness, random)) { } watch.Stop(); Console.WriteLine($"old: {watch.ElapsedMilliseconds} ms"); watch.Restart(); selection = new TournamentParentSelection(config); foreach (var selected in selection.SelectParents(pop, popsize, EAMode.MaximizeFitness, random)) { } watch.Stop(); Console.WriteLine($"new: {watch.ElapsedMilliseconds} ms"); }
public void TestOneMaxEA() { var config = new EAConfiguration { PopulationSize = 100, OverproductionFactor = 2.0, MaximumGenerations = 100, CrossoverType = CrossoverType.OnePoint, AdultSelectionType = AdultSelectionType.GenerationalMixing, ParentSelectionType = ParentSelectionType.Tournament, CrossoverRate = 0.9, MutationRate = 0.01, TournamentSize = 10, TournamentProbability = 0.8, TargetFitness = 1.0, Mode = EAMode.MaximizeFitness, Elites = 1, CalculateStatistics = true }; var fitnesslimitCounter = 0; for (int i = 0; i < 100; i++) { var onemaxEA = new OneMaxEA(config, new DefaultRandomNumberGenerator()); onemaxEA.TerminationEvent += (r) => { if (r == TerminationReason.FitnessLimitReached) { fitnesslimitCounter++; } }; var res = onemaxEA.Evolve(); } fitnesslimitCounter.Should().BeGreaterOrEqualTo(99); Console.WriteLine($"Fitness limit reached {fitnesslimitCounter} times"); }
public static void Run() { var config = new EAConfiguration { PopulationSize = 10, OverproductionFactor = 1.0, MaximumGenerations = 100000, CrossoverType = CrossoverType.OnePoint, AdultSelectionType = AdultSelectionType.GenerationalReplacement, ParentSelectionType = ParentSelectionType.Tournament, CrossoverRate = 0.18, MutationRate = 0.99, TournamentSize = 19, TournamentProbability = 0.77, TargetFitness = 0.0, Mode = EAMode.MinimizeFitness, Elites = 1, CalculateStatistics = true, SnapshotFilename = "snapshot2.bin", SnapshotGenerationInterval = 100 }; var hammingEA = new HammingEA(config, new DefaultRandomNumberGenerator()); var stopwatchtot = new Stopwatch(); var stopwatchgen = new Stopwatch(); PopulationStatistics currentStats = new PopulationStatistics(); hammingEA.PopulationStatisticsCalculated += (stats) => { currentStats = stats; }; hammingEA.NewBestFitnessEvent += (pheno, gen) => { double progress = (gen / (double)config.MaximumGenerations) * 100.0; var totruntime = stopwatchtot.Elapsed; var genruntime = stopwatchgen.Elapsed; Console.WriteLine(); Console.WriteLine(string.Format("G# {0} best_f: {1:F2} avg_f: {2:F2} SD: {3:F2} Progress: {4,5:F2} Gen: {5} Tot: {6}", gen, hammingEA.Best.Fitness, currentStats.AverageFitness, currentStats.StandardDeviationFitness, progress, genruntime, totruntime)); Console.WriteLine("Generation winner: " + ((StringGenotype)hammingEA.Best?.Genotype)); stopwatchgen.Restart(); }; hammingEA.TerminationEvent += (r) => { if (r == TerminationReason.FitnessLimitReached) { Console.WriteLine($"Fitness limit reached: {hammingEA.Best.Fitness}"); } }; stopwatchtot.Start(); stopwatchgen.Start(); var res = hammingEA.Evolve(); Console.WriteLine("\n\nDone!"); Console.WriteLine($"Winner: {res.Winner}"); WriteResultToFile(hammingEA.PopulationStatistics); Console.Read(); }