public void Run() { // var populations = new int[]{30,50,100,200,500,1000,3000}; // Population and tournament size box plot data made with following alogorithm var populationSizes = new int[] { 30, 50, 100, 200, 500, 1000, 3000 }; for (int j = 0; j < 10; j++) { string result = ""; for (int i = 0; i < populationSizes.Length; i++) { var populationSize = populationSizes[i]; var problem = new FunctionMinimizationFloatingPointProblem(new F6(), 2, -50, 150); // var populationSize = 500; var tournamentSize = 3; var selectionOperator = new TournamentSelectionOperator <FloatingPointChromosome>(tournamentSize, problem); var crossoverOperator = new HeuristicCrossoverOperator(0.8); var mutationOperator = new GaussianMutationOperator(0.05); var elitismRate = 1.0 / populationSize; var ga = new GeneticAlgorithmBuilder <FloatingPointChromosome>( problem, crossoverOperator, mutationOperator ) .SetGeneticAlgorithmVariant(GeneticAlgorithmVariant.Generational) .SetPopulationSize(populationSize) .SetElitismRate(1.0 / populationSize) .Build(); // new GeneticAlgorithmRunner<FloatingPointChromosome>(ga).Run(); ga.Run(); result += ga.BestIndividual.Fitness; if (i != populationSizes.Length - 1) { result += ","; } } Console.WriteLine(result); } }
public void Run() { string fKey = null; Console.WriteLine("Enter function number [6, 7]:"); try { switch (Int32.Parse(Console.ReadLine())) { case 6: fKey = "F6"; break; case 7: fKey = "F7"; break; default: Console.WriteLine("Invalid input. Try again with numbar from 6, 7."); break; } } catch (Exception) { Console.WriteLine("Invalid input. Try again."); return; } Console.WriteLine("Enter tournament size > 2:"); int tsize = 0; try { var dim = Int32.Parse(Console.ReadLine()); if (dim <= 0) { Console.WriteLine("Invalid input. Try again."); return; } tsize = dim; } catch (Exception) { Console.WriteLine("Invalid input. Try again."); return; } Function f = fKey.Equals("F6") ? (Function) new F6() : new F7(); if (fKey != null) { Console.WriteLine("Optimization of function " + fKey + ":"); IProblem <FloatingPointChromosome> problem = new FunctionMinimizationFloatingPointProblem(f, 2, -50, 150); new GeneticAlgorithmRunner <FloatingPointChromosome>( new GeneticAlgorithmBuilder <FloatingPointChromosome>( problem, new HeuristicCrossoverOperator(0.75), new UniformMutationOperator(0.1) ) .SetGeneticAlgorithmVariant(GeneticAlgorithmVariant.SteadyState) .SetSelectionOperator(new TournamentSelectionOperator <FloatingPointChromosome>(tsize, problem)) .SetFitnessValueTreshold(10e-6) .Build()).Run(); Console.WriteLine(); } }
public void Evaluate(GAParametersChromosome chromosome) { // if (chromosome.IsEvaluated) return; _evaluations++; GAParameters parameters = chromosome.GetValue(0); _function.Clear(); int iters = 10; int childEvaluations = 10000; int maxGenerations = 100; double[] childGAFitnesses = new double[iters]; var chromosomeRepresentation = GAParameters.ChromosomeRepresentation[parameters.ChromosomeRepresentationIndex]; for (int i = 0; i < iters; i++) { if (chromosomeRepresentation == GAParameters.ChromosomeRepresentationType.Binary) { IProblem <BinaryChromosome> problem = new FunctionMinimizationBinaryProblem(_function, _functionDimension, 4, -50, 150); int populationSize = GAParameters.PopulationSize[parameters.PopulationSizeIndex]; double crossoverRate = GAParameters.CrossoverRate[parameters.CrossoverRateIndex]; ICrossoverOperator <BinaryChromosome> crossoverOperator; ISelectionOperator <BinaryChromosome> selectionOperator; int tournamentSize = GAParameters.TournamentSize[parameters.TournamentSizeIndex]; double selectivePressure = GAParameters.SelectivePressure[parameters.SelectivePressureIndex]; IMutationOperator <BinaryChromosome> mutationOperator; double mutationRate = GAParameters.MutationRate[parameters.MutationRateIndex]; GeneticAlgorithmVariant variant = GAParameters.GAVariant[parameters.GAVariantIndex]; double elitismRate = GAParameters.ElitismRate[parameters.ElitismRateIndex]; double generationGap = GAParameters.GenerationGap[parameters.GenerationGapIndex]; var crossOperatorType = GAParameters.CrossoverOperator[parameters.CrossoverOperatorIndex]; if (crossOperatorType == GAParameters.CrossoverOperatorType.OnePoint) { crossoverOperator = new OnePointCrossoverOperator(crossoverRate); } else { crossoverOperator = new TwoPointCrossoverOperator(crossoverRate); } var mutationOperatorType = GAParameters.MutationOperator[parameters.MutationOperatorIndex]; if (mutationOperatorType == GAParameters.MutationOperatorType.BinaryBoundary) { mutationOperator = new BinaryBoundaryMutationOperator(mutationRate); } else if (mutationOperatorType == GAParameters.MutationOperatorType.BitFlip) { mutationOperator = new BitFlipMutationOperator(mutationRate); } else { mutationOperator = new SimpleMutationOperator(mutationRate); } if (GAParameters.SelectionOperator[parameters.SelectionOperatorIndex] == GAParameters.SelectionOperatorType.Tournament) { selectionOperator = new TournamentSelectionOperator <BinaryChromosome>(tournamentSize, problem); } else { selectionOperator = new RouletteWheelSelectionOperator <BinaryChromosome>(selectivePressure, problem); } IGeneticAlgorithm <BinaryChromosome> ga; if (variant == GeneticAlgorithmVariant.Generational) { ga = new GenerationalGeneticAlgorithm <BinaryChromosome>( problem, populationSize, selectionOperator, crossoverOperator, mutationOperator, new CompositeTerminationCondition( new MaxEvaluationsTerminationCondition(childEvaluations), new MaxGenerationsTerminationCondition(maxGenerations) ), elitismRate ); } else { ga = new SteadyStateGeneticAlgorithm <BinaryChromosome>( problem, populationSize, selectionOperator, crossoverOperator, mutationOperator, new CompositeTerminationCondition( new MaxEvaluationsTerminationCondition(childEvaluations), new MaxGenerationsTerminationCondition(maxGenerations) ), generationGap, new WorstFitnessReplacementOperator <BinaryChromosome>(problem) ); } // new GeneticAlgorithmRunner<BinaryChromosome>(ga).Run(); ga.Run(); childGAFitnesses[i] = ga.BestIndividual.Fitness; } else { IProblem <FloatingPointChromosome> problem = new FunctionMinimizationFloatingPointProblem(_function, _functionDimension, -50, 150); int populationSize = GAParameters.PopulationSize[parameters.PopulationSizeIndex]; double crossoverRate = GAParameters.CrossoverRate[parameters.CrossoverRateIndex]; ICrossoverOperator <FloatingPointChromosome> crossoverOperator; ISelectionOperator <FloatingPointChromosome> selectionOperator; int tournamentSize = GAParameters.TournamentSize[parameters.TournamentSizeIndex]; double selectivePressure = GAParameters.SelectivePressure[parameters.SelectivePressureIndex]; IMutationOperator <FloatingPointChromosome> mutationOperator; double mutationRate = GAParameters.MutationRate[parameters.MutationRateIndex]; GeneticAlgorithmVariant variant = GAParameters.GAVariant[parameters.GAVariantIndex]; double elitismRate = GAParameters.ElitismRate[parameters.ElitismRateIndex]; double generationGap = GAParameters.GenerationGap[parameters.GenerationGapIndex]; var crossOperatorType = GAParameters.CrossoverOperator[parameters.CrossoverOperatorIndex]; if (crossOperatorType == GAParameters.CrossoverOperatorType.Arithmetic) { crossoverOperator = new ArithmeticCrossoverOperator(crossoverRate); } else { crossoverOperator = new HeuristicCrossoverOperator(crossoverRate); } var mutationOperatorType = GAParameters.MutationOperator[parameters.MutationOperatorIndex]; if (mutationOperatorType == GAParameters.MutationOperatorType.FloatingPointBoundary) { mutationOperator = new FloatingPointBoundaryMutationOperator(mutationRate); } else if (mutationOperatorType == GAParameters.MutationOperatorType.Uniform) { mutationOperator = new UniformMutationOperator(mutationRate); } else { mutationOperator = new GaussianMutationOperator(mutationRate); } if (GAParameters.SelectionOperator[parameters.SelectionOperatorIndex] == GAParameters.SelectionOperatorType.Tournament) { selectionOperator = new TournamentSelectionOperator <FloatingPointChromosome>(tournamentSize, problem); } else { selectionOperator = new RouletteWheelSelectionOperator <FloatingPointChromosome>(selectivePressure, problem); } IGeneticAlgorithm <FloatingPointChromosome> ga; if (variant == GeneticAlgorithmVariant.Generational) { ga = new GenerationalGeneticAlgorithm <FloatingPointChromosome>( problem, populationSize, selectionOperator, crossoverOperator, mutationOperator, new CompositeTerminationCondition( new MaxEvaluationsTerminationCondition(childEvaluations), new MaxGenerationsTerminationCondition(maxGenerations) ), elitismRate ); } else { ga = new SteadyStateGeneticAlgorithm <FloatingPointChromosome>( problem, populationSize, selectionOperator, crossoverOperator, mutationOperator, new CompositeTerminationCondition( new MaxEvaluationsTerminationCondition(childEvaluations), new MaxGenerationsTerminationCondition(maxGenerations) ), generationGap, new WorstFitnessReplacementOperator <FloatingPointChromosome>(problem) ); } // new GeneticAlgorithmRunner<FloatingPointChromosome>(ga).Run(); ga.Run(); childGAFitnesses[i] = ga.BestIndividual.Fitness; } } chromosome.Fitness = childGAFitnesses.Average(); }