//Check if the search algorithm is ready to terminate. private bool terminateSearch(Node endNode) { Console.WriteLine("openList.Count = " + openList.Count); if (openList.Count != 0) { ClosedListItem goal = null; //Find the goal node in the closedList foreach (ClosedListItem item in closedList) { if (item.Node.Index == endNode.Index) { goal = item; } } //If the smallest costSoFar element in the openList is greater than the goal item in the closedList if (goal != null && openList[0].CostSoFar > goal.CostSoFar) { return(true); } return(false); } return(true); }
public List <Node> GetPath(Node startNode, Node endNode, Heuristic heuristic) { closedList.Clear(); openList.Clear(); //Create the first entry in the openList openList.Add(new OpenListItem(startNode, 0.0f, new List <Node>(), getEstimatedCostSoFar(startNode, endNode, heuristic))); OpenListItem currentItem, childItem; List <Node> childItemConnections; bool addToClosedList = true; bool childItemUpdated = false; //Keep running until terminateSearch() returns true while (!terminateSearch(endNode)) { //Grab the first element in the openList currentItem = openList[0]; openList.RemoveAt(0); //Add all children to the openList ordered by increasing estimatedTotalCost foreach (Connection childConnection in TileGenerator.Graph.getConnectedNodes(currentItem.Node)) { if (!currentItem.ConnectionNodes.Contains(childConnection.ToNode)) { //Add the parent's connection chain to the child childItemConnections = new List <Node>(currentItem.ConnectionNodes); //Add the connection from the parent to the child childItemConnections.Add(childConnection.ToNode); //Create an OpenListItem for the child childItem = new OpenListItem(childConnection.ToNode, currentItem.CostSoFar + childConnection.Cost, childItemConnections, getEstimatedCostSoFar(childConnection.ToNode, endNode, heuristic)); //Check if the childItem's node already exists in the openList for (int i = 0; i < openList.Count; i++) { //Check if both items have the same nodes if (childItem.Node.Index == openList[i].Node.Index) { // Keep the more optimal path if (childItem.CostSoFar < openList[i].CostSoFar) { openList[i] = childItem; childItemUpdated = true; break; } } } if (!childItemUpdated) { insertIntoOpenList(openList, childItem); } childItemUpdated = false; //Reset flag } } //Check if it exists on the closed list for (int i = 0; i < closedList.Count; i++) { //Check if both items have the same nodes if (currentItem.Node.Index == closedList[i].Node.Index) { // Keep the more optimal path if (currentItem.CostSoFar < closedList[i].CostSoFar) { closedList[i] = new ClosedListItem(currentItem.Node, currentItem.CostSoFar, currentItem.ConnectionNodes); addToClosedList = false; break; } } } if (addToClosedList) { //Add currentItem to the closedList closedList.Add(new ClosedListItem(currentItem.Node, currentItem.CostSoFar, currentItem.ConnectionNodes)); } addToClosedList = true; //Reset flag } //Return the goalItem's connection list on the closedList foreach (ClosedListItem item in closedList) { if (item.Node.Index == endNode.Index) { return(item.ConnectionNodes); } } return(null); }