public TSPResult Run() { var result = _genetic.OptimizeForBestIndividual(); if (_use2opt) { result = Optimizer2Opt.Optimize(result); } var fitness = _tspModule.GetFitnessHistory(); return(new TSPResult(fitness, result) { ResolveInEpoch = _tspModule.ResolveCountInEpoch, RandomizedResolveInEpoch = _tspModule.RandomizedResolveCountInEpoch, ResolvePercentInEpoch = _tspModule.ConflictResolvesPercent, RandomizedResolvePercentInEpoch = _tspModule.RandomResolvesPercent, DifferencesInEpoch = _tspModule.DifferencesInEpoch.ToArray() }); }
public static PathFindingResult GetPathAndFitness(int[] order, OptimizationParameters optimizationParameters, CancellationToken ct, Random random) { IPathFinder algorithmPathFinding = optimizationParameters.OptimizationMethod switch { OptimizationMethod.Permutations => new Permutations(optimizationParameters), OptimizationMethod.NearestNeighbor => new NearestNeighbor(optimizationParameters), OptimizationMethod.GeneticAlgorithm => new GeneticPathFinding(order, optimizationParameters, (population) => { double[] fitness = new double[population.Length]; for (int i = 0; i < population.Length; i++) { fitness[i] = Fitness.CalculateFitness(population[i]); } return(fitness); }, ct, random), _ => throw new ArgumentException("Incorrect optimization method in config file") }; int[] objectOrder = algorithmPathFinding.FindShortestPath(order); if (optimizationParameters.Use2opt) { objectOrder = Optimizer2Opt.Optimize(objectOrder); } double pathLength = Fitness.CalculateFitness(objectOrder); if (optimizationParameters.ResultToFile) { Log log = new Log(optimizationParameters); log.SaveResult(objectOrder, pathLength); } return(new PathFindingResult() { Path = objectOrder, Fitness = pathLength }); } }