public void RealizeEvolution() { Initialisation initialisation = new Initialisation(initialPopulationCount); // Initialisation - validated List <Path> population = initialisation.GenerateInitialPopulation(); for (int i = 0; i < population.Count; i++) { if (StaticOperations.ValidatePath(population[i]) == false) { throw new NotSupportedException(); } } // Evaluation Evaluation evaluation = new Evaluation(); evaluation.EvaluatePopulation(population); // Encoding List <Representation> representations = new List <Representation>(); Decoder decoder = new Decoder(); foreach (Path path in population) { Representation representation = decoder.EncodePath(path); representations.Add(representation); } // Evolution cycle for (int i = 0; i < evolutionCycles; i++) { // Selection Selection selection = new Selection(parentsCount, representations); List <Representation> parents = selection.SelectParents(); // Genetic operator - validated GeneticOperator geneticOperator = new GeneticOperator(descendantsCount, parents); List <Representation> descendants = geneticOperator.GenerateDescendants(); // Decoding List <Path> descendantPaths = new List <Path>(); foreach (Representation representation in descendants) { Path path = decoder.DecodeRepresentation(representation); if (StaticOperations.ValidatePath(path) == false) { throw new NotSupportedException(); } descendantPaths.Add(path); } // Evaluation evaluation.EvaluatePopulation(descendantPaths); for (int j = 0; j < descendants.Count; j++) { descendants[j].Fitness = descendantPaths[j].Fitness; } // Replacement Replacement replacement = new Replacement(parents, descendants, initialPopulationCount); representations = replacement.NextGeneration(); // Save to export file SaveTwoBestMembers(representations); } }
public void RealiseGroupEvolution() { // Initialisation GroupInitialisation initialisation = new GroupInitialisation(initialPopulationCount, scale); List <Individual> population = null; if (mode == 1) { population = initialisation.GenerateGroupInitialPopulation(); } if (mode == 2) { population = initialisation.GenerateRandomGroupInitialPopulation(); } // Validation foreach (Individual representation in population) { if (StaticOperations.ValidateIndividualWithGroups(representation) == false) { throw new NotSupportedException(); } } // Evaluation Evaluation evaluation = new Evaluation(scale); foreach (Individual representation in population) { evaluation.EvaluateIndividual(representation); } // Evolution cycles for (int i = 0; i < evolutionCycles; i++) { Console.Write("Epoch #" + i + " "); // Selection Selection selection = new Selection(initialPopulationCount, population); List <Individual> parents = null; if (mode == 1) { parents = selection.SelectParents(2); } if (mode == 2) { parents = selection.SelectDiverseParents(2, 5); } // Genetic operators List <Individual> descendants = new List <Individual>(); GroupGeneticOperators geneticOperators = new GroupGeneticOperators(scale); for (int j = 0; j < parents.Count; j = j + 2) { descendants.AddRange(geneticOperators.GenerateDescendants(parents[j], parents[j + 1])); } // Evaluation foreach (Individual representation in descendants) { evaluation.EvaluateIndividual(representation); } // Replacement Replacement replacement = new Replacement(population, descendants, initialPopulationCount); population = replacement.NextGeneration(); List <Individual> orderedPopulation = population.OrderBy(item => item.Fitness).ToList(); Console.WriteLine("Best individual has fitness: " + orderedPopulation[0].Fitness); } SaveBestIndividualIntoFile(population[0]); }
public void RealiseEvolution() { // Initialisation ReferenceList referenceList = new ReferenceList("tsp.riesenia"); Initialisation initialisation = new Initialisation(initialPopulationCount, referenceList); List <Individual> population = initialisation.InitialisePopulation(); // Evaluation Evaluation evaluation = new Evaluation(referenceList); foreach (Individual individual in population) { evaluation.EvaluateIndividual(individual); } // Validation foreach (Individual individual in population) { if (StaticOperations.ValidateIndividual(individual) == false) { throw new NotSupportedException(); } } // Evolution cycles for (int i = 0; i < evolutionCycles; i++) { Console.Write("Epoch #" + i); // Selection Selection selection = new Selection(population, population.Count); List <Individual> parents = selection.SelectParents(2); // Genetic operators GeneticOperators geneticOperators = new GeneticOperators(); List <Individual> descendants = new List <Individual>(); for (int j = 0; j < parents.Count; j = j + 2) { descendants.AddRange(geneticOperators.GenerateDescendants(parents[j], parents[j + 1])); } // Validation foreach (Individual individual in descendants) { if (StaticOperations.ValidateIndividual(individual) == false) { throw new NotSupportedException(); } } // Evaluation foreach (Individual individual in descendants) { evaluation.EvaluateIndividual(individual); } // Replacement Replacement replacement = new Replacement(population, descendants, population.Count); population = replacement.NextGeneration(); // Save best individual List <Individual> orderedPopulation = population.OrderBy(item => item.Fitness).ToList(); bestIndividualPerGeneration.Add(orderedPopulation[0]); Console.WriteLine(" Minimum fitness: " + orderedPopulation[0].Fitness); } SaveBestIndividualsToFile(referenceList); }
public void RealiseEvolution() { // Initialise population - validated Initialisation initialisation = new Initialisation(initialPopulationCount); List <Individual> population = initialisation.GenerateInitialPopulation(); foreach (Individual individual in population) { if (StaticOperations.ValidateIndividual(individual) == false) { throw new NotSupportedException(); } } // Evaluate synthethic fitness of population Evaluation evaluation = new Evaluation(firstCriteria, secondCriteria, thirdCriteria); foreach (Individual individual in population) { // Only criteria with true value will be considered evaluation.EvaluateIndividual(individual); } //Evolution cycles for (int i = 0; i < evolutionCycles; i++) { Console.Write("Epoch #" + i); // Selection - q-tournament Selection selection = new Selection(population, initialPopulationCount); List <Individual> parents = selection.SelectParents(2); // Genetic operators List <Individual> descendants = new List <Individual>(); GeneticOperators geneticOperators = new GeneticOperators(); for (int j = 0; j < parents.Count; j = j + 2) { descendants.AddRange(geneticOperators.GenerateDescendants(parents[j], parents[j + 1])); } // Evaluation foreach (Individual individual in descendants) { // Only criteria with true value will be considered evaluation.EvaluateIndividual(individual); } // Replacement Replacement replacement = new Replacement(population, descendants, initialPopulationCount); population = replacement.NextGeneration(); // Save best individual if (firstCriteria) { List <Individual> paretSetSurvivors = population.Where(ind => ind.Fitness == 1 && ind.FitnessVector[0] == 1).ToList(); if (paretSetSurvivors.Count == 0) { bestIndividuals.Add(population[0]); } else { paretSetSurvivors.Shuffle(); bestIndividuals.Add(paretSetSurvivors[0]); } Console.WriteLine(" Paret set count: " + EvaluateParetSet(population)); } else { List <Individual> paretSetSurvivors = population.Where(ind => ind.Fitness == 1).ToList(); paretSetSurvivors.Shuffle(); bestIndividuals.Add(paretSetSurvivors[0]); Console.WriteLine(" Paret set count: " + EvaluateParetSet(population)); } } SaveBestIndividualsInFile(); }
public void RealiseEvolution() { // Initialise population Initialisation initialisation = new Initialisation(initialPopulationCount, goldFieldCount); List <Individual> population = initialisation.InitialisePopulation(); // Validate population for (int i = 0; i < population.Count; i++) { if (StaticOperations.ValidateIndividual(population[i]) == false) { throw new NotSupportedException(); } } // Evaluate population Evaluation evaluation = new Evaluation(); for (int i = 0; i < population.Count; i++) { evaluation.EvaluateIndividual(population[i]); } // Evolution cycle for (int i = 0; i < evolutionCycles; i++) { Console.Write("# Epoch " + (i + 1)); // Selection Selection selection = new Selection(population, population.Count); // Q tournament List <Individual> parents = selection.SelectParents(4); // Genetic operators List <Individual> descendants = new List <Individual>(); GeneticOperators geneticOperators = new GeneticOperators(); for (int j = 0; j < parents.Count; j = j + 2) { descendants.AddRange(geneticOperators.GenerateDescendants(parents[j], parents[j + 1])); } // Evaluation for (int j = 0; j < descendants.Count; j++) { evaluation.EvaluateIndividual(descendants[j]); } // Replacement Replacement replacement = new Replacement(population, descendants, population.Count); if (i - bestFitnessEpoch < 100) { population = replacement.NextGeneration(); } else { population = replacement.KillBestIndividuals(); bestFitness = double.MaxValue; } foreach (Individual individual in population) { if (StaticOperations.ValidateIndividual(individual) == false) { throw new NotSupportedException(); } } // Save best member List <Individual> orderedPopulation = population.OrderBy(ind => ind.Fitness).ToList(); bestIndividualsPerGeneration.Add(orderedPopulation[0]); Console.WriteLine(" Minimum fitness: " + orderedPopulation[0].Fitness + "."); if (orderedPopulation[0].Fitness < bestFitness) { bestFitness = orderedPopulation[0].Fitness; bestFitnessEpoch = i; } if (orderedPopulation[0].Fitness == 0) { break; } } SaveDataToFile(); }