public List <Node> AStar() { // Pop off lowest F int? min = OpenList.Min(n => n.F); Node current = OpenList.Where(n => n.F == min).First(); OpenList.Remove(current); // Check for goal if (current.Equals(Goal)) { ClosedList.Push(current); return(ClosedList.Reverse().ToList()); } else { //possibleMoves = GetPassibleNodes(current); OpenList = GetPassibleNodes(current); List <Node> toRemove = new List <Node>(); OpenList.ForEach(n => { if (ClosedList.Contains(n)) { toRemove.Add(n); } else { GetF(n); } }); toRemove.ForEach(n => OpenList.Remove(n)); //possibleMoves.ForEach(n => //{ // if (!ClosedList.Contains(n)) // Dont move to an already visited Node // { // GetF(n); // if (!OpenList.Contains(n)) // { // n.Parent = current; // OpenList.Add(n); // } // } //}); } ClosedList.Push(current); return(null); }
public bool Find(int x0, int y0, int x1, int y1) { if (!Ready(x0, y0, x1, y1)) { return(false); } Node current = null; do { current = GetBestFromOpenList(); if (current == null) { break; } if (current.Equals(goal_)) { break; } if (!ProcessNeigbhors(current)) { break; } } while (true); if (goal_.parent != null) { current = goal_; } if (current != null && current.Equals(goal_)) { GeneratePath(current); return(true); } return(false); }
public List <Node> findPath(Node startNode, Node finishNode, WeightedGraph graph) { Dictionary <Node, int> pricesToVisitedNodes = new Dictionary <Node, int>(); Dictionary <Node, Node> paths = new Dictionary <Node, Node>(); QueueWithPriority queue = new QueueWithPriority(); queue.Put(startNode, 0); pricesToVisitedNodes.Add(startNode, 0); while (!queue.IsEmpty()) { Node current = queue.PopMostPriorityElement(); if (finishNode.Equals(current)) { break; } foreach (var neighbor in graph.GetNeighbors(current)) { int costToNeighbor = pricesToVisitedNodes[current] + graph.getWeight(current, neighbor); bool condition = false; // если соседний узел еще не посещался или новая цена перехода из старта до него меньше чем была, то if (!pricesToVisitedNodes.ContainsKey(neighbor)) { condition = true; } else if (pricesToVisitedNodes[neighbor] > costToNeighbor) { condition = true; } if (condition) { // обновляем цену перехода до соседнего узла pricesToVisitedNodes[neighbor] = costToNeighbor; queue.Put(neighbor, costToNeighbor + heuristic(current, neighbor)); if (!paths.ContainsKey(neighbor)) { paths.Add(neighbor, current); } else { paths[neighbor] = current; } } } } // выбираем цепочку , которая привела к цели Node next = finishNode; List <Node> path = new List <Node>(); path.Add(next); while (!startNode.Equals(next)) { next = paths[next]; path.Add(next); } return(path); }