public BuildGeneticAlgorithm(int countGenes, int minSizePopulation = 2, int maxSizePopulation = 100) { _chromosome = new BinaryChromosome(countGenes); _population = new GeneticSharp.Domain.Populations.Population(minSizePopulation, maxSizePopulation, _chromosome); }
public static void BuildTestGA() { //Create chromosome length = 20 BinaryChromosome chrom = new BinaryChromosome(20); //Create population = [2,100] var population = new Population(2, 100, chrom); //Console.WriteLine(chrom.Length); //Console.WriteLine(chrom.ToString()); //create Fitness Function (функция приспособления) var fitness = new FuncFitness( (c) => { var fc = c as BinaryChromosome; double result = 0.0; foreach (Gene gene in fc.GetGenes()) { result += Convert.ToDouble(gene.Value.ToString()); } return(result); } ); //create selection //var selection = new EliteSelection(); var selection = new TournamentSelection(); //var selection = new RouletteWheelSelection(); //var selection = new StochasticUniversalSamplingSelection(); //create crossover var crossover = new UniformCrossover(0.5f); //var crossover = new CutAndSpliceCrossover(); //только с EliteSelection() //var crossover = new OnePointCrossover(); //var crossover = new TwoPointCrossover(); //var crossover = new CycleCrossover(); // new OrderBasedCrossover(); new OrderedCrossover(); new PositionBasedCrossover(); new PartiallyMappedCrossover(); //может использоваться только с упорядоченными хромосомами. Указанная хромосома имеет повторяющиеся гены //var crossover = new ThreeParentCrossover(); //ОДНУ Итерацию выполняет //create mutation var mutation = new FlipBitMutation(); //var mutation = new UniformMutation(); //1 перегрузка принимает индексы генов для мутации, 2-я все гены мутируют //var mutation = new TworsMutation(); //Слабая //var mutation = new ReverseSequenceMutation(); //Слабая //create termination (Количество итераций) var termination = new GenerationNumberTermination(100); //var termination = new FitnessStagnationTermination(50); // var termination = new FitnessThresholdTermination(50); //Постоянно зацикливается //TimeSpan time = new TimeSpan(0, 0, 10); //10 секунд //var termination = new TimeEvolvingTermination(time); //Сам генетический алгоритм var ga = new GeneticAlgorithm( population, fitness, selection, crossover, mutation); ga.Termination = termination; Console.WriteLine("Generation: = distance"); var latestFitness = 0.0; ga.GenerationRan += (sender, e) => { var bestChromosome = ga.BestChromosome as BinaryChromosome; var bestFitness = bestChromosome.Fitness.Value; if (bestFitness != latestFitness) { latestFitness = bestFitness; var phenotype = bestChromosome.GetGenes(); //Console.WriteLine( // "Generation {0,2}: ({1},{2}),({3},{4}) = {5}", // ga.GenerationsNumber, // phenotype[0], // phenotype[1], // phenotype[2], // phenotype[3], // bestFitness //); Console.WriteLine("Generation {0,2}. Best Fitness = {1}", ga.GenerationsNumber, bestFitness); Console.Write("Chromosome: "); foreach (Gene g in phenotype) { Console.Write(g.Value.ToString() + ""); } Console.WriteLine(); } }; ga.Start(); }