public override ISolverResult Solve(IAlgorithm tspSolvingAlgorithm) { var context = SolvingTimeContext.Instance; Statistics = new BasicSolverStatistics(); var resultAccumulator = new SolverResult(); var bestPath = new Path(new List <int>(), new ConstCostCalculationStrategy(int.MaxValue)); for (var i = 0; i < MslsRepeatAmount; i++) { using (context) { for (var j = 0; j < InsideAlgorithmRepeatAmount; j++) { var startNode = _randomGenerator.Next(0, CompleteGraph.NodesCount - 1); var pathAccumulator = _internalSolver.Solve(_internalAlgorithm, startNode); var localPath = pathAccumulator.Paths[0]; localPath = tspSolvingAlgorithm.Solve(localPath.Nodes.First(), CompleteGraph, localPath); if (localPath.Cost < bestPath.Cost) { bestPath = localPath; } } Statistics.UpdateSolvingResults(bestPath, context.Elapsed); resultAccumulator.AddPath(bestPath); } } return(resultAccumulator); }
public override ISolverResult Solve(IAlgorithm tspSolvingAlgorithm, ISolverResult solverResult) { Statistics = new BasicSolverStatistics(); ISet <Path> initialPopulation = new HashSet <Path>(solverResult.Paths); var optimalChilderens = new SolverResult(); _solverStopwatch.Start(); var localStopwatch = new Stopwatch(); while (_solverStopwatch.ElapsedMilliseconds < _solvingTime) { var parents = _selector.Select(initialPopulation); var child = _recombinator.Recombine(parents.Item1, parents.Item2); localStopwatch.Start(); var optimalChild = tspSolvingAlgorithm.Solve(StartNode(child), CompleteGraph, child); localStopwatch.Stop(); optimalChilderens.AddPath(optimalChild); initialPopulation = EnhancePopulation(optimalChild, initialPopulation); Statistics.UpdateSolvingResults(optimalChild, localStopwatch.Elapsed); localStopwatch.Reset(); } _solverStopwatch.Reset(); return(optimalChilderens); }
public override ISolverResult Solve(IAlgorithm tspSolvingAlgorithm) { var context = SolvingTimeContext.Instance; Statistics = new BasicSolverStatistics(); var resultPathAccumulator = new SolverResult(); for (var i = 0; i < IlsRepeatAmount; i++) { Path bestPath; using (context) { var pathAccumulator = _initializationSolver.Solve(_initializationAlgorithm, StartNode); bestPath = pathAccumulator.Paths[0]; var timer = new Stopwatch(); timer.Start(); var newPath = bestPath; while (timer.ElapsedMilliseconds < AlgorithmSolveTimeMs) { for (var j = 0; j < PerturbanceLength; j++) { var move = GetRandomMove(newPath, CompleteGraph); newPath = move.Move(bestPath); } var solvedPath = tspSolvingAlgorithm.Solve(bestPath.Nodes.First(), CompleteGraph, bestPath); if (solvedPath.Cost >= bestPath.Cost) { continue; } bestPath = newPath; } } resultPathAccumulator.AddPath(bestPath); Statistics.UpdateSolvingResults(bestPath, context.Elapsed); Console.WriteLine(i + " / 10"); } return(resultPathAccumulator); }
public override ISolverResult Solve(IAlgorithm tspSolvingAlgorithm, ISolverResult solverResult) { Statistics = new BasicSolverStatistics(); var newAccumulator = new SolverResult(); foreach (var path in solverResult.Paths) { var context = SolvingTimeContext.Instance; Path localyBestPath; using (context) { localyBestPath = tspSolvingAlgorithm.Solve(SelectStartNode(path), CompleteGraph, path); } newAccumulator.AddPath(localyBestPath); Statistics.UpdateSolvingResults(localyBestPath, context.Elapsed); } return(newAccumulator); }
public static void Main(string[] args) { var buildConfig = BuildConfiguration(); var config = buildConfig.GetSection(nameof(AppConfiguration)); var dataPath = config["data"]; var resultPath = config["results"]; var graphEdges = System.IO.Path.Combine(dataPath, config["graphEdges"]); var graphCoordinates = System.IO.Path.Combine(dataPath, config["graphCoordinates"]); var dataLoader = new GraphLoader(graphEdges, 100); var graph = dataLoader.Load(); var calculationStrategies = new ISimilarityCalculationStrategy[] { new EdgeSimillarityStrategy(), new NodeSimilarityStrategy() }; var simpleSolver = new TspSolver(graph); var localSearchSolver = new TspLocalSearchSolver(graph); var edgeFinder = new GraspEdgeFinder(3); var evolutinarySolver = new EvolutionarySolver(graph, new Recombinator(new SimilarityFinder(calculationStrategies), Steps, graph), new Selector(), 41000); var localSearch = new LocalSearchAlgorithm(Steps, edgeFinder); var stats = new BasicSolverStatistics(); var bestCost = int.MaxValue; ISolverStatistics bestStatistics = new BasicSolverStatistics(); for (var i = 0; i < 10; i++) { var generatedPaths = simpleSolver.Solve(new RandomPathAlgorithm(Steps, edgeFinder)); generatedPaths = localSearchSolver.Solve(localSearch, generatedPaths); evolutinarySolver.Solve(localSearch, generatedPaths); if (evolutinarySolver.SolvingStatistics.BestPath.Cost < bestCost) { bestCost = evolutinarySolver.SolvingStatistics.BestPath.Cost; bestStatistics = evolutinarySolver.SolvingStatistics; } stats.UpdateSolvingResults(evolutinarySolver.SolvingStatistics.BestPath, evolutinarySolver.SolvingStatistics.MeanSolvingTime); } var statsPrinter = new FilePrinter(resultPath, "evo_stats.res"); statsPrinter.Print(stats.ToString()); var output = new StringBuilder(); output.AppendLine($"{"Id".PadRight(4)}\tCost\tTime"); for (var i = 0; i < bestStatistics.Costs.Count; i++) { output.AppendLine($"{i.ToString().PadRight(4)}\t{bestStatistics.Costs[i].ToString().PadRight(4)}\t{bestStatistics.SolvingTimes[i].Milliseconds:D}"); } var evoResultsPrinter = new FilePrinter(resultPath, "evolutionary_results.res"); evoResultsPrinter.Print(output.ToString()); Console.WriteLine("Evolutionary solver ended his work!"); //SimilaritySolver(resultPath, graph, calculationStrategies, edgeFinder); }