public bool extend() { if (citiesLeft.Count() != 0) { foreach (int i in citiesLeft) { double initialCost = matrix[path.Last()][i] + cost; double[][] newMatrix = this.matrix.Select(a => a.ToArray()).ToArray(); for (int j = 0; j < matrix.Count(); j++) { newMatrix[path.Last()][j] = Double.PositiveInfinity; newMatrix[j][i] = Double.PositiveInfinity; } List <int> newPath = path.Select(a => a).ToList(); newPath.Add(i); List <int> newCitiesLeft = citiesLeft.Select(a => a).ToList(); newCitiesLeft.Remove(i); BBState state = new BBState(newMatrix, newPath, newCitiesLeft, initialCost); PriorityQueue.getInstance().insert(state); } return(false); } else { cost = matrix[path.Last()][0] + cost; path.Add(0); return(true); } }
/// <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]; double bssf = Double.PositiveInfinity; // TODO: Add your implementation for a branch and bound solver here. //List<int> citiesLeft = new List<int>(); //double[][] matrix = new double[Cities.Length][]; //for (int i = 0; i < Cities.Length; i++) //{ // citiesLeft.Add(i); // matrix[i] = new double[Cities.Length]; // for (int j = 0; j < Cities.Length; j++) // { // if (i != j) // matrix[i][j] = Cities[i].costToGetTo(Cities[j]); // else // matrix[i][j] = Double.PositiveInfinity; // } //} //citiesLeft.RemoveAt(0); double[][] matrix = new double[4][]; matrix[0] = new double[4] { Double.PositiveInfinity, 7, 3, 12 }; matrix[1] = new double[4] { 3, Double.PositiveInfinity, 6, 14 }; matrix[2] = new double[4] { 5, 8, Double.PositiveInfinity, 6 }; matrix[3] = new double[4] { 9, 3, 5, Double.PositiveInfinity }; BBState state = new BBState(matrix, new List <int>() { 0 }, new List <int>() { 1, 2, 3 }, 0); //BBState state = new BBState(matrix, new List<int>() { 0 }, citiesLeft,0); PriorityQueue.getInstance().insert(state); BBState current; while (!PriorityQueue.getInstance().isEmpty()) { current = PriorityQueue.getInstance().deletemin(); if (state.extend()) { if (current.getCost() < bssf) { bssf = current.getCost(); PriorityQueue.getInstance().trim(bssf); } } } results[COST] = "not implemented"; // load results into array here, replacing these dummy values results[TIME] = "-1"; results[COUNT] = "-1"; return(results); }