/// <summary> /// Мутация при которой все особоби, кроме самой лучшей мутируют /// (мутируют около 1/3 битов). /// </summary> /// <param name="population">Популяция.</param> private void CatacliysmicMutation(Population population) { Chromosome bestChromosome = population.BestChromosome(); int bitsMutation = (int)(population.SizeChromosome * 0.7); AbstractMutation mutation = GeneticMethod.ChosenMutationMethod(mutationMethod, 100, bitsMutation); foreach (var chromosome in population.populationList) { if (!chromosome.Equals(bestChromosome)) { mutation.Mutation(chromosome); } } }
public override int GeneticAlgorithm(MainGraph graph, ProblemData problemData) { algorithmInfo = new AlgorithmInfo(); adjacencyList = AdjacencyList.GenerateList(graph); cost = Cost.GanerateCostArray(graph, problemData); problem = problemData; var crossover = GeneticMethod.ChosenCrossoverMethod(crossoverMethod, CrossoverProbability, dotCrossover); var mutation = GeneticMethod.ChosenMutationMethod(mutationMethod, MutationProbability, dotMutation); var selection = GeneticMethod.ChosenSelectionMethod(selectionMethod, CountTour, CountSelected); ReductionForClassicGA reduction = new ReductionForClassicGA(); Chromosome bestChromosome = null; Population startPopulation = new Population(PopulationSize, cost); var population = startPopulation; int stepGA = 0; double MediumFitness = Solution.MediumFitnessPopulation(population); RandomSelection randomSelection = new RandomSelection(); FitnessCalculation(population.populationList); stopwatch = new Stopwatch(); stopwatch.Start(); while (stepGA < IterateSize) { List <Chromosome> midPopulation = selection.Selection(population); List <Chromosome> parentPopulation = randomSelection.Selection(midPopulation, selection.indexSelectChrom); List <Chromosome> childList = crossover.Crossover(parentPopulation); if (childList.Count == 0) { continue; } mutation.Mutation(childList); FitnessCalculation(childList); reduction.Reduction(childList, randomSelection.indexTwoParant, population.populationList); double tempMediumFitness = Solution.MediumFitnessPopulation(population); double absFitness = Math.Abs(tempMediumFitness - MediumFitness); MediumFitness = tempMediumFitness; if (absFitness >= 0 && absFitness <= 1) { bestChromosome = population.BestChromosome(); var worstChromosome = population.WorstChromosome(); if (bestChromosome.fitness - worstChromosome.fitness <= 1 && bestChromosome.fitness - worstChromosome.fitness >= 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { stopwatch.Stop(); algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; break; } } } stepGA++; } stopwatch.Stop(); if (stepGA == IterateSize) { bool answer = false; bestChromosome = population.BestChromosome(); while (population.populationList.Count != 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; answer = true; break; } else { population.populationList.Remove(bestChromosome); if (population.populationList.Count == 0) { break; } bestChromosome = population.BestChromosome(); } } if (!answer) { bestChromosome = null; } } return(Solution.Answer(cost, bestChromosome, problemData, graph)); }
/// <summary> /// Реализация генетического алгоритма "Genitor". /// </summary> /// <param name="graph">Граф.</param> /// <param name="problemData">Параметры задачи</param> public override int GeneticAlgorithm(MainGraph graph, ProblemData problemData) { this.algorithmInfo = new AlgorithmInfo(); // Инициализация основных структур. adjacencyList = AdjacencyList.GenerateList(graph); cost = Cost.GanerateCostArray(graph, problemData); ProblemData problem = problemData; var crossover = GeneticMethod.ChosenCrossoverMethod(crossoverMethod, 100, dotCrossover); var mutation = GeneticMethod.ChosenMutationMethod(mutationMethod, MutationProbability, dotMutation); var selection = GeneticMethod.ChosenSelectionMethod(selectionMethod, CountTour, CountSelected); Chromosome bestChromosome = null; double MediumFitness = 0; Population startPopulation = new Population(PopulationSize, cost); var population = startPopulation; int stepGA = 0; // вычесление пригодности хромосом. for (int i = 0; i < PopulationSize; i++) { population.populationList[i].fitness = Fitness.FunctionTrue(cost, problemData, population.populationList[i]); } RandomSelection randomSelection = new RandomSelection(); MediumFitness = Solution.MediumFitnessPopulation(population); stopwatch = new Stopwatch(); stopwatch.Start(); while (stepGA < IterateSize) { List <Chromosome> midPopulation = selection.Selection(population); List <Chromosome> parentPopulation = randomSelection.Selection(midPopulation); Chromosome child = crossover.Crossover(parentPopulation[0], parentPopulation[1]); if (mutation != null) { mutation.Mutation(child); } // вычесление ранга хромосомы. //population.Sort(); Ranking2(population); // пригодность потомка child.fitness = Fitness.FunctionTrue(cost, problemData, child); // поиск самой худщей хромосомы Chromosome bedChrom = null; bedChrom = population.populationList.First(); // замена худшей хромосомы на потомка int index = population.populationList.IndexOf(bedChrom); population.populationList.Insert(index, child); population.populationList.RemoveAt(index + 1); double tempMediumFitness = Solution.MediumFitnessPopulation(population); double absFitness = Math.Abs(tempMediumFitness - MediumFitness); MediumFitness = tempMediumFitness; if (absFitness >= 0 && absFitness <= 1) { bestChromosome = population.BestChromosome(); var worstChromosome = population.WorstChromosome(); if (bestChromosome.fitness - worstChromosome.fitness <= 1 && bestChromosome.fitness - worstChromosome.fitness >= 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { stopwatch.Stop(); algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; break; } } } stepGA++; } stopwatch.Stop(); if (stepGA == IterateSize) { bool answer = false; bestChromosome = population.BestChromosome(); while (population.populationList.Count != 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; answer = true; break; } else { population.populationList.Remove(bestChromosome); if (population.populationList.Count == 0) { break; } bestChromosome = population.BestChromosome(); } } if (!answer) { bestChromosome = null; } } return(Solution.Answer(cost, bestChromosome, problemData, graph)); }