public double GetShortPath(string startNodeId, string targetNodeId) { visitedNodeMarks = new Dictionary <string, double>(); double shortestPathCost = 0; Collection <Arc> nodeAdjacentArc; int numSettledNodes = 0; double distToAdjNode = 0; ActiveNode startNode; ActiveNode activeNode; ActiveNode currentNode; if (heuristics == null) { startNode = new ActiveNode(startNodeId, 0, 0, "-1"); } else { startNode = new ActiveNode(startNodeId, 0, heuristics[startNodeId], "-1"); } activeNodes = new List <ActiveNode>(); parents = new Dictionary <string, string>(); activeNodes.Add(startNode); while (activeNodes.Count() != 0) { Console.WriteLine("++++++++++++++"); activeNodes.Sort(new ActiveNodeCompare()); for (int i = 0; i < activeNodes.Count(); i++) { Console.WriteLine("pai xu hou :" + activeNodes[i].id); } currentNode = activeNodes[0]; activeNodes.RemoveAt(0); if (isvisited(currentNode.id)) { continue; } visitedNodeMarks.Add(currentNode.id, currentNode.dist); parents.Add(currentNode.id, currentNode.parent); numSettledNodes++; if (currentNode.id == targetNodeId) { shortestPathCost = currentNode.dist; break; } if (numSettledNodes > graph.Nodes.Count()) { Console.WriteLine("There is no short path between startNode and targetNode"); break; } nodeAdjacentArc = this.graph.AdjacentArcs[currentNode.id]; for (int i = 0; i < nodeAdjacentArc.Count(); i++) { Arc arc = nodeAdjacentArc[i]; if (!isvisited(arc.TailNode.Id)) { distToAdjNode = currentNode.dist + nodeAdjacentArc[i].Cost; if (heuristics == null) { activeNode = new ActiveNode(arc.TailNode.Id, distToAdjNode, 0, currentNode.id); } else { activeNode = new ActiveNode(arc.TailNode.Id, distToAdjNode, heuristics[currentNode.id], currentNode.id); } activeNodes.Add(activeNode); } } } return(shortestPathCost); }
public double GetShortPath(string startNodeId, string targetNodeId) { visitedNodeMarks = new Dictionary<string, double>(); double shortestPathCost = 0; Collection<Arc> nodeAdjacentArc; int numSettledNodes = 0; double distToAdjNode = 0; ActiveNode startNode; ActiveNode activeNode; ActiveNode currentNode; if (heuristics == null) { startNode = new ActiveNode(startNodeId, 0, 0, "-1"); } else { startNode = new ActiveNode(startNodeId,0,heuristics[startNodeId],"-1"); } activeNodes = new List<ActiveNode>(); parents = new Dictionary<string, string>(); activeNodes.Add(startNode); while (activeNodes.Count() != 0) { Console.WriteLine("++++++++++++++"); activeNodes.Sort(new ActiveNodeCompare()); for (int i = 0; i < activeNodes.Count(); i++) { Console.WriteLine("pai xu hou :"+activeNodes[i].id); } currentNode = activeNodes[0]; activeNodes.RemoveAt(0); if (isvisited(currentNode.id)) { continue; } visitedNodeMarks.Add(currentNode.id, currentNode.dist); parents.Add(currentNode.id, currentNode.parent); numSettledNodes++; if (currentNode.id == targetNodeId) { shortestPathCost = currentNode.dist; break; } if (numSettledNodes > graph.Nodes.Count()) { Console.WriteLine("There is no short path between startNode and targetNode"); break; } nodeAdjacentArc = this.graph.AdjacentArcs[currentNode.id]; for (int i = 0; i < nodeAdjacentArc.Count(); i++) { Arc arc = nodeAdjacentArc[i]; if (!isvisited(arc.TailNode.Id)) { distToAdjNode = currentNode.dist + nodeAdjacentArc[i].Cost; if (heuristics == null) { activeNode = new ActiveNode(arc.TailNode.Id, distToAdjNode,0, currentNode.id); } else { activeNode = new ActiveNode(arc.TailNode.Id, distToAdjNode,heuristics[currentNode.id], currentNode.id); } activeNodes.Add(activeNode); } } } return shortestPathCost; }