// Copy constructor for making children (Josh) // Reduces matrix based on whether edge is included // Adds state to queue public State(State parent, Boolean include, int from, int to) { matrix = (double[,])parent.matrix.Clone(); route = new ArrayList(parent.route); visited = new ArrayList(parent.visited); bound = parent.bound; this.parent = parent; depth = parent.depth + 1; if (include) { bound += matrix[from, to]; pathsLeft = parent.pathsLeft - 1; visited.Add(from); visited.Add(to); for (int i = 0; i < cityCount; i++) { matrix[from, i] = double.PositiveInfinity; matrix[i, to] = double.PositiveInfinity; } // If there's more than one path left, delete paths to used vertices (Josh) if (pathsLeft > 2) { for (int i = 0; i < visited.Count; i++) { matrix[to, (int)visited[i]] = double.PositiveInfinity; // Shouldn't be necessary, but who knows? matrix[(int)visited[i], from] = double.PositiveInfinity; } } else if (pathsLeft == 2) { //this should keep the path from the final city back to the first one for (int i = 1; i < visited.Count; i++) { matrix[to, (int)visited[i]] = double.PositiveInfinity; // Shouldn't be necessary, but who knows? matrix[(int)visited[i], from] = double.PositiveInfinity; } } matrix[to, from] = double.PositiveInfinity; } else { matrix[from, to] = double.PositiveInfinity; pathsLeft = parent.pathsLeft; } reduceMatrix(); // set BSSF if necessary (Josh) // If not, set BSSF if necessary (Josh) if (pathsLeft == 1) { for (int i = 0; i < cityCount; i++) { for (int j = 0; j < cityCount; j++) { if (matrix[i, j] != double.PositiveInfinity) { includeChild = new State(this, true, i, j); Console.WriteLine("Found solution"); for (int k = 0; k < includeChild.visited.Count; k++) { Console.WriteLine(includeChild.visited[k]); } if (includeChild.bound < BSSF.bound) { // Calculate route from visited nodes includeChild.calculateRoute(); BSSF = includeChild; // Prune states starting with root root.prune(); } else { return; } } } } } // Add to queue if (pathsLeft > 0 && bound < BSSF.bound) { queue.Enqueue(this, Priority); } }