public IList <AStarNode> Expand(AStarNode state) { AddInitial(state); // dodaj startowy element do listy zamkniętych(odwiedzonych) AStarNode tmp; while (!IsGoal(ClosedList.Last())) { OpenList.OrderBy(p => p.F); //lista otwartych posortowana wg najmniejszej wagi współczynnika F tmp = OpenList.First(); //weź pierwszy element z otwartej listy(najlepiej prosperujący) RemoveFirst(OpenList); //przenieś do zamkniętej tmp.Opened = false; tmp.Closed = true; AddToClosed(tmp); ManageNeighbours(ClosedList.Last()); //dodaj sąsiadów do otwartej listy lub zaktualizuj wsp F i rodzica } AStarNode parent = Goal; //uzupełnij listę ze ścieżką poprzez wybranie elementu docelowego i rekursywnie dodaj wszystkie elementy połączone while (parent != null) { StackTrace.Insert(0, parent); parent = parent.Parent; } return(StackTrace); }
public bool FindTheShortesPath(Node Start, Node Target) { Node CurrentNode = Start; OpenList.Add(CurrentNode); for (; OpenList.Count > 0;) { CurrentNode = OpenList[0]; if (CurrentNode.Equals(Target)) { VisitedNodes.Add(Target); for (int Index = 0; Index < VisitedNodes.Count - 1; Index++) { ShortesPath.Add(InnerGraph.FindEdge(VisitedNodes[Index], VisitedNodes[Index + 1])); } return(true); } OpenList.Remove(CurrentNode); ClosedList.Add(CurrentNode); foreach (Node Inheritor in CurrentNode.Inheritors.Where(Node => Node != null && Node.Index != Node.Inheritors.Length - 1)) { if (!ClosedList.Contains(Inheritor)) { if (!OpenList.Contains(Inheritor)) { Inheritor[Inheritor.Index] = CurrentNode; Inheritor.HeuristicPathWeight = CalculateHeuristic(Inheritor, Target); Inheritor.GainedPathWeight = InnerGraph.FindEdge(CurrentNode, Inheritor).Weight; Inheritor.TotalPathWeight = Inheritor.GainedPathWeight + Inheritor.HeuristicPathWeight; OpenList.Add(Inheritor); OpenList = OpenList.OrderBy(Node => Node.TotalPathWeight).ToList <Node>(); } } } VisitedNodes.Add(CurrentNode); } return(true); }