//While processing, when a complete path is found that touches all cities once, this method is called. //If it's the best solution so far, we update our account of what the best path is. public void ProposeSolution(float totalLength) { //Get the missing edge to connect the first and last nodes. Edge finalEdge = TspSolver.FirstVertex.GetMissingEdge(); if (finalEdge != null) { //Update the total path length accordingly. totalLength += finalEdge.Length; //If it's a better solution than our best so far... if (totalLength < TspSolver.MinPath) { finalEdge.IsIncluded = true; TspSolver.MinPath = totalLength; TspSolver.MinPathText = new StringBuilder(); //Build a text representation of the path that is ready for export to CSV. foreach (Edge e in TspSolver.Edges) { if (e.IsIncluded) { TspSolver.MinPathText.AppendLine(e.ExportFriendlyPath); } } finalEdge.IsIncluded = false; //Write to file. TspSolver.WriteSolutionToFile(); } } }
static void Main(string[] args) { //TspSolver solver = new TspSolver(@"../../../fifteen.txt", 1000, "fifteen"); //TspSolver solver = new TspSolver(@"../../../fifteen.txt", 5, "fifteen"); //TspSolver solver = new TspSolver(@"../../../wi29.tsp.txt", 5, "wsahara"); //TspSolver solver = new TspSolver(@"../../../qa194.tsp.txt", 2, "qatar"); TspSolver solver = new TspSolver(@"../../../uy734.tsp.txt", 4, "uruguay"); //TspSolver solver = new TspSolver(@"../../../nu3496.tsp.txt", 3, "oman"); //TspSolver solver = new TspSolver(@"../../../ja9847.tsp.txt", 3, "japan"); var solution = solver.Solve(); TspSolver.WriteSolutionToFile(); Console.WriteLine("BEST PATH"); Console.WriteLine(TspSolver.MinPathText); Console.ReadLine(); }