示例#1
0
        /// <summary>
        /// The AStar algorithm implementation
        /// </summary>
        /// <param name="p_stateSpace">The state space to explore</param>
        /// <returns>TRUE if a path was found, FALSE if not</returns>
        public IStateNode GeneratePath(IStateSpace p_stateSpace)
        {
            if (p_stateSpace == null)
            {
                throw new InvalidOperationException("Cannot generate a path with a NULL state space");
            }

            p_stateSpace.ResetPath();
            m_openList.Clear();

            var node = p_stateSpace.GetStartState();

            if (node == null)
            {
                throw new InvalidOperationException("The state space did not return a valid Start State");
            }

            node.ActualCostFromStart = 0;
            m_openList.Add(node);

            while (m_openList.Count > 0)
            {
                var state = m_openList.RemoveLowest();

                state.IsClosed = true;
                if (p_stateSpace.IsGoalState(state))
                {
                    return(state);
                }

                foreach (var sp in p_stateSpace.Successors(state))
                {
                    if (!sp.IsClosed)
                    {
                        UpdateCell(state, sp, p_stateSpace);
                    }
                }
            }

            return(null); // no path found
        }