public NodeState MakeChild(ref NodeState parentNode, int i) { //FROM and TO city indices int cityCount = Cities.Length; int fromCity = parentNode.GetLastCityInRouteIndex(); int toCity = i; //Make a child matrix from parent double[,] copyMatrix = CopyMatrix(parentNode.GetMatrix(), cityCount); double[,] childMatrix = MakeChildMatrix(copyMatrix, fromCity, toCity); //Inherit and add to lower bound for the child double childLB = parentNode.GetLowerBound() + parentNode.GetMatrix()[toCity, fromCity]; //Inherit and add to route for the child ArrayList childRoute = (ArrayList)parentNode.GetRoute().Clone(); childRoute.Add(Cities[toCity]); //Inherit the cities already visited by the parent and add the city the child is going to be in. List <bool> childCitiesVisited = CopyCitiesVisited(parentNode.GetCitiesVisited()); childCitiesVisited[toCity] = true; return(new NodeState(childMatrix, childLB, childRoute, toCity, childCitiesVisited)); }
public NodeState PruneTheQueue(ref List <NodeState> queue, NodeState currentNodeState, ref int TotalNodeStatesPrunned) { while (currentNodeState.GetLowerBound() >= bssf.costOfRoute() && queue.Count != 1) { currentNodeState = DeletePriorityNode(ref queue); TotalNodeStatesPrunned++; } return(currentNodeState); }
public void GenerateChildren(ref List <NodeState> queue, ref int MaxNodeStateAmount, ref int BSSFupdates, ref int TotalNodeStates, ref int TotalNodeStatesPrunned, int startCity) { int cityCount = Cities.Length; //Get the first node in the priority NodeState currentNodeState = DeletePriorityNode(ref queue); //If the currentNodeState should be prunned if (currentNodeState.GetLowerBound() >= bssf.costOfRoute()) { currentNodeState = PruneTheQueue(ref queue, currentNodeState, ref TotalNodeStatesPrunned); } //Generate a child for each city the currentNodeState can go to. for (int i = 0; i < cityCount; i++) { if (!currentNodeState.AlreadyVisited(i)) { //Make a child NodeState child = MakeChild(ref currentNodeState, i); child.ReduceMatrixAndUpdateLowerBound(cityCount); // Add to node states because a child was generated TotalNodeStates++; //If LB is less BSSF then add to queue, TNS++ if (child.GetLowerBound() < bssf.costOfRoute()) { //Before adding to the queue, see if you have a new BSSF if (BetterBSSFExists(ref child, startCity)) { bssf = new TSPSolution(child.GetRoute()); BSSFupdates++; } else { //add node to the queue if (!child.HasVisitedAllCities()) { //Add AddNodeToPriorityQueue(ref queue, child); } } } else { //Prune. We don't need to even visit it. TotalNodeStatesPrunned++; } } } //Check the MaxNodeStates CheckQueueSize(ref MaxNodeStateAmount, queue.Count - 1); }
public void SiftDown(ref List <NodeState> queue, NodeState last, int i, int queueSize) { //put node last in position 1 of the queue and let it sift down. int min = SmallestChild(ref queue, i, queueSize); while ((min != 0) && (queue[min].GetLowerBound() < last.GetLowerBound())) { queue[i] = queue[min]; i = min; min = SmallestChild(ref queue, i, queueSize); } if (queue[i] != last) { queue[i] = last; } //Remove last node in the heap because we must have one less queue.RemoveAt(queueSize); }