private TSPSpecimen InverseMutation(TSPSpecimen specimen) { int index1 = random.Next(2, dataLoaded.totalNumberOfCities); int index2 = random.Next(0, dataLoaded.totalNumberOfCities); while (index2 == index1) { index2 = random.Next(0, dataLoaded.totalNumberOfCities); } if (index2 < index1) { int temp = index2; index2 = index1; index1 = temp; } List <CityElement> citiesInRange = new List <CityElement>(); for (int i = index1; i < index2; i++) { citiesInRange.Add(specimen.citiesVisitedInOrder[i]); } citiesInRange.Reverse(); for (int i = index1; i < index2; i++) { specimen.citiesVisitedInOrder[i] = citiesInRange[i - index1]; } return(specimen); }
public TSPSpecimen GeneticCycle() { bestSolution = FindBest(oldPopulation).Clone(); statisticsHolder.AddNewGenerationStatistics(currentGeneration, oldPopulation); while (currentGeneration < NUMBER_OF_GENERATIONS && stagnationCounter < STAGNATION_FACTOR * NUMBER_OF_GENERATIONS) { currentGeneration++; newPopulation = Selection(); Crossover(newPopulation); Mutation(newPopulation); Evaluate(newPopulation); TSPSpecimen bestSolutionInNewPopolation = FindBest(newPopulation); if (bestSolutionInNewPopolation.objectiveFunction > bestSolution.objectiveFunction) { bestSolution = bestSolutionInNewPopolation.Clone(); stagnationCounter = 0; } else { stagnationCounter++; } statisticsHolder.AddNewGenerationStatistics(currentGeneration, newPopulation); oldPopulation = newPopulation; } return(bestSolution); }
public static void SaveKnapsackSolutionToFile(TSPSpecimen result, string filePath, string annotation) { using (var writer = new StreamWriter($"{filePath +PROBLEM_NAME+ DateTime.Now.ToFileTime()}{annotation}{CSV_FILE_EXTENSION}", true)) using (var csv = new CsvWriter(writer)) { csv.WriteComment("PLECAK"); csv.NextRecord(); csv.WriteHeader <ItemElement>(); csv.NextRecord(); csv.WriteRecords(result.thief.knapsack); } }
private TSPSpecimen[] Cross(TSPSpecimen parent1, TSPSpecimen parent2) { double chance = random.NextDouble(); if (chance <= PROBABILITY_OF_CROSSOVER) { return(OXCross(parent1, parent2)); } else { return new TSPSpecimen[] { parent1, parent2 } }; }
private TSPSpecimen Mutate(TSPSpecimen specimen) { double chance = random.NextDouble(); if (chance <= PROBABILITY_OF_MUTATION) { return(mutationFunction(specimen)); } else { return(specimen); } }
public TSPSpecimen Clone() { TSPSpecimen newSpecimen = new TSPSpecimen(); newSpecimen.citiesVisitedInOrder = new List <CityElement>(); newSpecimen.totalTravelTime = totalTravelTime; newSpecimen.objectiveFunction = objectiveFunction; newSpecimen.thief = thief.Clone(); for (int i = 0; i < citiesVisitedInOrder.Count; i++) { newSpecimen.citiesVisitedInOrder.Add(citiesVisitedInOrder[i].Clone()); } return(newSpecimen); }
private static void RunGeneticAlgorithm() { CsvStatisticsHolder.GetInstance().Reset(); GeneticAlgorithm g = new GeneticAlgorithm(); TSPSpecimen best = g.GeneticCycle(); WriteLine("FINISH: " + best.objectiveFunction); int b = best.objectiveFunction; double avg = CsvStatisticsHolder.GetInstance().results.Average(p => p.averageOfResults); double minavg = (CsvStatisticsHolder.GetInstance().results.Average(p => p.worstResult)); double maxavg = (CsvStatisticsHolder.GetInstance().results.Average(p => p.bestResult)); results.Add(new CsvTotalResult(b, maxavg, minavg, avg)); Utilities.SaveStatisticsToFile(); }
public static void SavePathSolutionToFile(TSPSpecimen result, string filePath, string annotation) { using (var writer = new StreamWriter($"{filePath +PROBLEM_NAME+ DateTime.Now.ToFileTime()}{annotation}{CSV_FILE_EXTENSION}", true)) using (var csv = new CsvWriter(writer)) { List <CityElement> cities = new List <CityElement>(); cities.AddRange(result.citiesVisitedInOrder); cities.Add(cities[0]); csv.WriteComment("MIASTA"); csv.NextRecord(); csv.WriteHeader <CityElement>(); csv.NextRecord(); csv.WriteRecords(cities); } }
private TSPSpecimen SwapMutation(TSPSpecimen specimen) { int index1 = random.Next(0, dataLoaded.totalNumberOfCities); int index2 = random.Next(0, dataLoaded.totalNumberOfCities); while (index2 == index1) { index2 = random.Next(0, dataLoaded.totalNumberOfCities); } CityElement city1 = specimen.citiesVisitedInOrder[index1]; CityElement city2 = specimen.citiesVisitedInOrder[index2]; specimen.citiesVisitedInOrder[index1] = city2; specimen.citiesVisitedInOrder[index2] = city1; return(specimen); }
private TSPSpecimen OXCrossBasedOnFirstParent(TSPSpecimen parent1, TSPSpecimen parent2, int crossPoint1, int crossPoint2) { TSPSpecimen child = new TSPSpecimen(); List <int> crossSection = new List <int>(); for (int i = crossPoint1; i < crossPoint2; i++) { crossSection.Add(parent1.citiesVisitedInOrder[i].index); } List <int> restOfGenes = new List <int>(); for (int i = crossPoint2; i < dataLoaded.totalNumberOfCities; i++) { if (!crossSection.Contains(parent2.citiesVisitedInOrder[i].index)) { restOfGenes.Add(parent2.citiesVisitedInOrder[i].index); } } for (int i = 0; i < crossPoint2; i++) { if (!crossSection.Contains(parent2.citiesVisitedInOrder[i].index)) { restOfGenes.Add(parent2.citiesVisitedInOrder[i].index); } } List <int> childCities = new List <int>(); int secondPartOfGenes = restOfGenes.Count - crossPoint1; childCities.AddRange(restOfGenes.Skip(secondPartOfGenes)); childCities.AddRange(crossSection); childCities.AddRange(restOfGenes.Take(secondPartOfGenes)); child.citiesVisitedInOrder.Clear(); for (int i = 0; i < dataLoaded.totalNumberOfCities; i++) { child.citiesVisitedInOrder.Add(dataLoaded.cities[childCities[i] - 1].Clone()); } return(child); }
private TSPSpecimen[] OXCross(TSPSpecimen parent1, TSPSpecimen parent2) { int crossPoint1 = random.Next(0, dataLoaded.totalNumberOfCities); int crossPoint2 = random.Next(0, dataLoaded.totalNumberOfCities); while (crossPoint2 == crossPoint1) { crossPoint2 = random.Next(0, dataLoaded.totalNumberOfCities); } if (crossPoint2 < crossPoint1) { int temp = crossPoint2; crossPoint2 = crossPoint1; crossPoint1 = temp; } TSPSpecimen child1 = OXCrossBasedOnFirstParent(parent1, parent2, crossPoint1, crossPoint2); TSPSpecimen child2 = OXCrossBasedOnFirstParent(parent2, parent1, crossPoint1, crossPoint2); return(new TSPSpecimen[] { child1, child2 }); }