//returns number of updates to BSSF made during the exploration private void explore(SearchSpace current, PriorityQ q) { List <int> citiesRemaining = current.CitiesRemaining; bool leaf = true; //O() foreach (int city in citiesRemaining) { leaf = false; SearchSpace child = new SearchSpace(current, city);//O(n^2) statesCreated++; if (child.Bound < costOfBssf()) { q.Insert(child); } else { statesPruned++; } } if (leaf) { TSPSolution possibleSoln = new TSPSolution(current.Route, Cities); if (possibleSoln.costOfRoute() < costOfBssf()) { bssf = possibleSoln; solutionsFound++; } } }
/// <summary> /// performs a Branch and Bound search of the state space of partial tours /// stops when time limit expires and uses BSSF as solution /// </summary> /// <returns>results array for GUI that contains three ints: cost of solution, time spent to find solution, number of solutions found during search (not counting initial BSSF estimate)</returns> public string[] bBSolveProblem() { string[] results = new string[3]; Stopwatch timer = new Stopwatch(); timer.Start(); double[,] graphMatrix = buildGraphMatrix(); defaultGetBSSF(); //replace with greedy or however you want to initialize BSSF //build cityIndices list. Stupid but need it List <int> cityIndices = initCityIndices(); //O(n) //Build the q with the initial searchSpace having {0} as it's current route PriorityQ q = new PriorityQ(); q.Makequeue(); //O(1) q.Insert(new SearchSpace(new List <int>(), cityIndices, 0, graphMatrix, 0, _size)); //O(log|V|) statesCreated++; //Branch and bound. //O(n^2n!) while (q.NotEmpty() && timer.ElapsedMilliseconds < 60 * 1000) //could run O(n!) times { SearchSpace curr = q.Deletemin(); //O(log|V|) if (curr.Bound < costOfBssf()) { explore(curr, q); } else { statesPruned++; } } timer.Stop(); results[COST] = costOfBssf().ToString(); // load results array results[TIME] = timer.Elapsed.ToString(); results[COUNT] = solutionsFound.ToString(); statesStoredMax = q.MaxCount; statesPruned += q.Count; return(results); }