private void ReinitializePopulation(List <Representation> oldGeneration, int membersToReplace) { // 1.) pick up unique elite members and remove them from current population List <Representation> newGeneration = new List <Representation>(); List <Representation> eliteMembers = UniqueEliteSelection(oldGeneration.Count / 5 < 6 ? 6 : oldGeneration.Count / 10, oldGeneration); foreach (Representation representation in eliteMembers) { Representation member = oldGeneration.Find(item => item.Values.SequenceEqual(representation.Values)); oldGeneration.Remove(member); } // 2.) randomly initialise new initialPopulationCount/2 members Initialisation initialisation = new Initialisation(membersToReplace < oldGeneration.Count - eliteMembers.Count ? membersToReplace : oldGeneration.Count - eliteMembers.Count); List <Path> newMembers = initialisation.GenerateInitialPopulation(); Decoder decoder = new Decoder(); List <Representation> switcherees = newMembers.Select(path => decoder.EncodePath(path)).ToList(); // 3.) shuffle remains of current population and replace the first half with new member oldGeneration.Shuffle(); oldGeneration.RemoveRange(0, switcherees.Count); oldGeneration.InsertRange(0, switcherees); // 4.) append elite members oldGeneration.InsertRange(0, eliteMembers); }
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 RealizeEvolution() { Random random = new Random(); 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++) { Console.Write("Epoch #" + i + "."); // Reinitialisation happens every 1/10th iteration and randomly resets half of population // Elite 10% is untouched by this process if ((i % 500) == 0 && i != 0) { ReinitializePopulation(representations, (int)(3 * initialPopulationCount / 4)); } // Remap fitness using exponential remapping //RemapFitness(representations, remapParameter); // Selection Selection selection = new Selection(parentsCount, representations); List <Representation> parents = selection.SelectParents(); //List<Representation> parents = selection.SelectCombinedParents(); // 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; } // Revaluate current population after fitness remapping //List<Path> currentPaths = new List<Path>(); //foreach (Representation representation in representations) //{ // Path path = decoder.DecodeRepresentation(representation); // currentPaths.Add(path); //} //evaluation.EvaluatePopulation(currentPaths); //for (int j = 0; j < representations.Count; j++) //{ // representations[j].Fitness = currentPaths[j].Fitness; //} // Replacement Replacement replacement = new Replacement(representations, descendants, initialPopulationCount); //representations = replacement.GenerationReplacement(); //representations = replacement.NextGeneration(); representations = replacement.DuplicationElimination(7, representations.Count / 20 < 3 ? representations.Count / 20 : 3, 20); Console.Write(" Maximum fitness: " + representations.Max(item => item.Fitness)); // Save to export file SaveSixBestMembers(representations); } }
public void RealiseEvolution() { // Initialisation Initialisation initialisation = new Initialisation(initialPopulationCount, scale); List <Individual> population = initialisation.GenerateInitialPopulation(); // Validation foreach (Individual representation in population) { if (StaticOperations.ValidateIndividual(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 = selection.SelectParents(2); // Genetic operators List <Individual> descendants = new List <Individual>(); GeneticOperators geneticOperators = new GeneticOperators(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); } // Validation foreach (Individual representation in population) { if (StaticOperations.ValidateIndividual(representation) == false) { throw new NotSupportedException(); } } // 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() { // 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(); }