Esempio n. 1
0
        /// <summary>
        /// Solves the Searchable using DFS algorithm - Finds a path from the InitialState to the GoalState
        /// </summary>
        /// <param name="searchable"></param>
        /// <returns> Solution </returns>
        public override Solution Solve(ISearchable searchable)
        {
            clear();
            m_openListStates.Clear();
            m_stopWatch.Restart();
            Solution solution = new Solution();
            AState   current  = searchable.getInitialState();
            AState   goal     = searchable.getGoalState();

            m_closedList.Add(current.getState(), current);
            while (!goal.Equals(current))
            {
                List <AState> successors = searchable.getAllSuccessors(current);
                addToOpenList(successors);
                //Pops the next state to evaluate - Skips states that where already evaluated (exist in the closedList)
                while (m_openListStates.Count > 0 && m_closedList.ContainsKey(current.getState()))
                {
                    current = m_openListStates.Pop();
                }
                //Adds the current state (thats currently being evaluated) to the closed list so that it wont be evaluated again
                if (!m_closedList.ContainsKey(current.getState()))
                {
                    m_closedList.Add(current.getState(), current);
                    m_countGeneratedNodes++;
                }
            } //While loop is done - means the current state is the goal state

            solution = searchable.createSolution(current);
            m_stopWatch.Stop();
            return(solution);
        }
Esempio n. 2
0
        /// <summary>
        /// solving searchable problem acording to dfs algorithm
        /// </summary>
        /// <param name="searchDomain"> searcheble problem </param>
        /// <returns> solution to the problem </returns>
        public override Solution Solve(ISearchable searchDomain)
        {
            StartMeasureTime();
            Solution sol = new Solution();

            AState goal  = searchDomain.GetGoalState();
            AState state = searchDomain.GetStartState();
            IEnumerable <AState> stateSuccessors;

            stack.Push(state);

            while (stack.Count > 0)
            {
                state = stack.Pop();

                if (state.Equals(goal))
                {
                    sol = makeSolotion(state);
                    break;
                }

                else
                {
                    stateSuccessors = searchDomain.GetAllSuccessors(state);

                    foreach (AState successor in stateSuccessors)
                    {
                        stack.Push(successor);
                        m_countGeneratedNodes++;
                    }
                }
            }
            searchDomain.recoverProblem();
            StopMeasureTime();
            return(sol);
        }