public Track(Track PreviousTrack, Arc Transition) { if (_Target==null) throw new InvalidOperationException("You must specify a target Node for the Track class."); Queue = PreviousTrack; _Cost = Queue.Cost + Transition.Cost; _NbArcsVisited = Queue._NbArcsVisited + 1; EndNode = Transition.EndNode; }
public Track(Node GraphNode) { if ( _Target==null ) throw new InvalidOperationException("You must specify a target Node for the Track class."); _Cost = 0; _NbArcsVisited = 0; Queue = null; EndNode = GraphNode; }
private void Propagate(Track TrackToPropagate) { foreach ( Arc A in TrackToPropagate.EndNode.OutgoingArcs ) { if ( A.Passable && A.EndNode.Passable ) { Track Successor = new Track(TrackToPropagate, A); int PosNF = _Closed.IndexOf(Successor, SameNodesReached); int PosNO = _Open.IndexOf(Successor, SameNodesReached); if ( PosNF>0 && Successor.Cost>=((Track)_Closed[PosNF]).Cost ) continue; if ( PosNO>0 && Successor.Cost>=((Track)_Open[PosNO]).Cost ) continue; if ( PosNF>0 ) _Closed.RemoveAt(PosNF); if ( PosNO>0 ) _Open.RemoveAt(PosNO); _Open.Add(Successor); } } }
private Node[] GoBackUpNodes(Track T) { int Nb = T.NbArcsVisited; Node[] Path = new Node[Nb+1]; for (int i=Nb; i>=0; i--, T=T.Queue) Path[i] = T.EndNode; return Path; }
/// <summary> /// Use for a 'step by step' search only. This method is alternate to SearchPath. /// The algorithm must have been initialize before. /// </summary> /// <exception cref="InvalidOperationException">You must initialize AStar before using NextStep().</exception> /// <returns>'true' unless the search ended.</returns> public bool NextStep() { if ( !Initialized ) throw new InvalidOperationException("You must initialize AStar before launching the algorithm."); if ( _Open.Count==0 ) return false; _NbIterations++; int IndexMin = _Open.IndexOfMin(); Track BestTrack = (Track)_Open[IndexMin]; _Open.RemoveAt(IndexMin); if ( BestTrack.Succeed ) { _LeafToGoBackUp = BestTrack; _Open.Clear(); } else { Propagate(BestTrack); _Closed.Add(BestTrack); } return _Open.Count>0; }
/// <summary> /// Use for a 'step by step' search only. This method is alternate to SearchPath. /// Initializes AStar before performing search steps manually with NextStep. /// </summary> /// <exception cref="ArgumentNullException">StartNode and EndNode cannot be null.</exception> /// <param name="StartNode">The node from which the path must start.</param> /// <param name="EndNode">The node to which the path must end.</param> public void Initialize(Node StartNode, Node EndNode) { if ( StartNode==null || EndNode==null ) throw new ArgumentNullException(); _Closed.Clear(); _Open.Clear(); Track.Target = EndNode; _Open.Add( new Track(StartNode) ); _NbIterations = 0; _LeafToGoBackUp = null; }