/// <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); }
/// <summary> /// comper the state to other state /// </summary> /// <param name="other"> state to compare</param> /// <returns> 0 if equal -1 otherwise</returns> public override int CompareTo(AState other) { if (this.Equals(other)) { return(0); } return(-1); }
/// <summary> /// check if the state equal to other state /// </summary> /// <param name="obj"> state to compare </param> /// <returns> true if equal false otherwise</returns> public override bool Equals(AState obj) { if (obj is MazeState) { return(this.position.Equals(((MazeState)obj).position)); } return(false); }
/// <summary> /// Constructor /// </summary> /// <param name="maze3d"></param> public SearchableMaze3D(AMaze maze3d) { m_maze3d = (Maze3d)maze3d; Position3D statrPosition = (Position3D)maze3d.getStartPosition(); m_initialState = new MazeState(null, statrPosition.Col, statrPosition.Row, statrPosition.Level); Position3D goalPosition = (Position3D)maze3d.getGoalPosition(); m_goalState = new MazeState(null, goalPosition.Col, goalPosition.Row, goalPosition.Level); }
/// <summary> /// Backtracking a solution from a gives state (Using parent states) /// </summary> /// <param name="end"></param> /// <returns> Solution </returns> public Solution createSolution(AState end) { Solution solution = new Solution(); AState current = end; while (null != current) { solution.addState(current); current = current.getParentState(); } solution.reverese(); return(solution); }
/// <summary> /// Returns a List containing all the possible (valid) successors of a gives AState /// </summary> /// <param name="state"></param> /// <returns> List of successors </returns> public List <AState> getAllSuccessors(AState state) { List <AState> successors = new List <AState>(); MazeState mState = (MazeState)state; int col = mState.getX(); int row = mState.getY(); int floor = mState.getZ(); //Right (Col ++) if (col + 1 < m_maze3d.getWidth() && m_maze3d.isPath(col + 1, row, floor)) { successors.Add(new MazeState(state, col + 1, row, floor)); } //Left (Col --) if (col - 1 >= 0 && m_maze3d.isPath(col - 1, row, floor)) { successors.Add(new MazeState(state, col - 1, row, floor)); } //Up (Row ++) if (row + 1 < m_maze3d.getHeight() && m_maze3d.isPath(col, row + 1, floor)) { successors.Add(new MazeState(state, col, row + 1, floor)); } //Down (Row --) if (row - 1 >= 0 && m_maze3d.isPath(col, row - 1, floor)) { successors.Add(new MazeState(state, col, row - 1, floor)); } //Ascend (Floor ++) if (floor + 1 < m_maze3d.getLevels() && m_maze3d.isPath(col, row, floor + 1)) { successors.Add(new MazeState(state, col, row, floor + 1)); } //Descend (Floor --) if (floor - 1 >= 0 && m_maze3d.isPath(col, row, floor - 1)) { successors.Add(new MazeState(state, col, row, floor - 1)); } return(successors); }
/// <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); }
/// <summary> /// Constructor /// </summary> /// <param name="parentState"></param> public AState(AState parentState) { m_parentState = parentState; }
/// <summary> /// Adds a INode to the SortedList. /// </summary> /// <param name="sortedList">SortedList to add the node to.</param> /// <param name="node">Node to add to the sortedList.</param> internal static void Add(this SortedList <int, AState> sortedList, AState state) { sortedList.Add(state.TotalCost, state); }
/// <summary> /// constractor /// </summary> /// <param name="pos"> position of the new state</param> /// <param name="parentState"> the new state parent</param> public MazeState(Position pos, AState parentState) : base(parentState) { position = pos; }
/// <summary> /// Constructor /// </summary> /// <param name="parentState"></param> /// <param name="pointX"></param> /// <param name="pointY"></param> /// <param name="pointZ"></param> public MazeState(AState parentState, int pointX, int pointY, int pointZ) : base(parentState) { m_state = "(" + pointX + "," + pointY + "," + pointZ + ")"; m_position = new Position3D(pointZ, pointY, pointX); }
/// <summary> /// add a new state /// </summary> /// <param name="state">the new state to add</param> public void AddState(AState state) { m_solutionPath.Add(state); }
/// <summary> /// Adds a state to the Solution /// </summary> /// <param name="state"></param> public void addState(AState state) { m_solution.Add(state); }