public void solveProblemRandom() { //initialize BSSF with a greedy algorithm Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Algorithms algorithms = new Algorithms(); bssf = new TSPSolution(algorithms.random(Cities)); stopwatch.Stop(); Program.MainForm.tbCostOfTour.Text = " " + bssf.costOfRoute(); Program.MainForm.tbElapsedTime.Text = " " + stopwatch.Elapsed.TotalSeconds; // do a refresh. Program.MainForm.Invalidate(); }
//Our TSP internal void solveProblemTabu() { int bssfUpdates = 0; Algorithms algorithms = new Algorithms(); bssf = new TSPSolution(algorithms.greedy(Cities)); TSPSolution currentSolution = new TSPSolution(bssf.Route); int size = Convert.ToInt32(Program.MainForm.textBoxTabuSize.Text); TabuList tabuList = new TabuList(size);//set capacity of tabuList int timeSeconds = Convert.ToInt32(Program.MainForm.textBoxTime.Text); double totalTime = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while(stopwatch.Elapsed.TotalSeconds < timeSeconds)//run for 600 seconds, 10 minutes { currentSolution = findBestCandidate(currentSolution, tabuList); if(currentSolution.cost < bssf.cost) { bssf = new TSPSolution(currentSolution.Route); totalTime = stopwatch.Elapsed.TotalSeconds; bssfUpdates++; } tabuList.addSolution(currentSolution); } stopwatch.Stop(); Program.MainForm.tbCostOfTour.Text = " " + bssf.cost; Program.MainForm.tbElapsedTime.Text = " " + totalTime; //Program.MainForm.tbElapsedTime.Text = " " + stopwatch.Elapsed.TotalSeconds; Program.MainForm.toolStripTextBox1.Text = " " + bssfUpdates; //print bssf update number // do a refresh. Program.MainForm.Invalidate(); }
/// <summary> /// solve the problem. This is the entry point for the solver when the run button is clicked /// right now it just picks a simple solution. /// </summary> public void solveProblemBandB() { //initialize BSSF with a greedy algorithm Algorithms algorithms = new Algorithms(); bssf = new TSPSolution(algorithms.greedy(Cities)); Node.bssf = bssf.costOfRoute(); int maxQsize = 0; int totalStates = 0; int timeSeconds = Convert.ToInt32(Program.MainForm.textBoxTime.Text); //set up priority queue and stopwatch PriorityQueue PQ = new PriorityQueue(); PQ.insert(new Node(Cities)); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while(PQ.getSize() > 0 && stopwatch.Elapsed.TotalSeconds < timeSeconds) { //pop node off of queue and check lower bound against bssf Node node = PQ.deleteMin(); if(node.lowerBound > Node.bssf) { Node.prunes++; break; } Node include = null; Node exclude = null; double maxDif = Double.NegativeInfinity; //search for include/exclude edge that gives max difference in lower bound double[,] matrix = node.rCmatrix; for (int i = 0; i < node.matrixLength; i++) { if (node.exited[i] == -1) { for (int j = 0; j < node.matrixLength; j++) { if (matrix[i, j] == 0) { Node tempInclude = new Node(node, true, i, j); Node tempExclude = new Node(node, false, i, j); double potentialMaxDif = tempExclude.lowerBound - tempInclude.lowerBound; if (potentialMaxDif > maxDif) { maxDif = potentialMaxDif; include = tempInclude; exclude = tempExclude; } } } } } //check if found a bssf if(include.totalEdges == include.matrixLength && include.lowerBound < Node.bssf) { Node.bssfUpdates++; Node.bssf = include.lowerBound; Node.bssfNode = include; } else if(include.lowerBound < Node.bssf)//add include node to queue { PQ.insert(include); int currentQSize = PQ.getSize(); if(currentQSize > maxQsize) { maxQsize = currentQSize; } } else//prune include node { Node.prunes++; } if(exclude.lowerBound < Node.bssf)//add exclude node to queue { PQ.insert(exclude); int currentQSize = PQ.getSize(); if (currentQSize > maxQsize) { maxQsize = currentQSize; } } else//prune exclude node { Node.prunes++; } totalStates += 2;//2 states are created per while-loop iteration } stopwatch.Stop(); //if stopwatch is < 30, then we have found an optimal solution bool isOptimal = false; if(stopwatch.Elapsed.TotalSeconds < timeSeconds) { isOptimal = true; } //prune number of items left in the queue Node.prunes += PQ.getSize(); //if a bssf has been found better than the greedy solution if(Node.bssfNode != null) { Node solution = Node.bssfNode; ArrayList route = solution.getRoute(Cities); // call this the best solution so far. bssf is the route that will be drawn by the Draw method. bssf = new TSPSolution(route); } //display stats if (isOptimal) { Program.MainForm.tbCostOfTour.Text = " " + bssf.costOfRoute() + "*"; } else { Program.MainForm.tbCostOfTour.Text = " " + bssf.costOfRoute(); } Program.MainForm.tbElapsedTime.Text = " " + stopwatch.Elapsed.TotalSeconds; // do a refresh. Program.MainForm.Invalidate(); //print more stats Console.WriteLine(); Console.WriteLine("Max # of stored states: " + maxQsize); Console.WriteLine("# of BSSF updates: " + Node.bssfUpdates); Console.WriteLine("Total # of states created: " + totalStates); Console.WriteLine("Total # of states pruned: " + Node.prunes); Node.resetStaticVariables(); }
/// <summary> /// solve the problem. This is the entry point for the solver when the run button is clicked /// right now it just picks a simple solution. /// </summary> public void solveProblem(Solver solver) { Algorithms algo = new Algorithms(); Stopwatch timer = new Stopwatch(); timer.Restart(); switch (solver) { case Solver.DEFAULT: int x; Route = new ArrayList(); // this is the trivial solution. for (x = 0; x < Cities.Length; x++) { Route.Add(Cities[Cities.Length - x - 1]); } break; case Solver.RANDOM: Route = algo.RandomSolution(Cities); break; case Solver.GREEDY: Route = algo.GreedySolution(Cities); break; case Solver.BRANCH_BOUND: double BSSF = double.PositiveInfinity; Route = algo.GreedySolution(Cities); TSPSolution greedyRoute = new TSPSolution(Route); BSSF = greedyRoute.costOfRoute(); Route = algo.BranchAndBound(Cities, Route, BSSF); break; case Solver.SIMANNEAL: switch (Program.MainForm.saSource) { // Random case 1: //Route = algo.RandomSolution(Cities); solveProblem(Solver.RANDOM); break; // Greedy case 2: //Route = algo.GreedySolution(Cities); solveProblem(Solver.GREEDY); break; // Branch and Bound case 3: solveProblem(Solver.BRANCH_BOUND); break; default: solveProblem(Solver.DEFAULT); break; } Route = algo.SimulatedAnnealingSolution(Cities, Route); break; default: break; } timer.Stop(); bssf = new TSPSolution(Route); Program.MainForm.tbCostOfTour.Text = " " + bssf.costOfRoute(); Program.MainForm.tbElapsedTime.Text = timer.Elapsed.ToString(); // do a refresh. Program.MainForm.tbElapsedTime.Text = " " + timer.Elapsed.TotalSeconds; Program.MainForm.Invalidate(); }