public override Solution Solve(ISearchable searchDomain) { StartMeasureTime(); ClearOpenClosedLists(); AState startingState = searchDomain.GetStartState(); AddToOpenList(startingState); // add initial state to openList while (!IsEmptyOpenList()) // as long openList isnt empty { AState state = PopOpenList(); // get a state from the queue if (state.State.Equals(searchDomain.GetGoalState().State)) // check if it is a goalState { StopMeasureTime(); getBackTrace(state); // get the traceBack of the solution Solution.RevereseSolution(); return(Solution); } SUCCESORS = searchDomain.GetAllSuccessors(state); // find all the neighbors of the state AddToClosedList(state); foreach (AState astate in SUCCESORS) { if (!IsStateInOpenList(astate) && !IsStateInClosedList(astate)) // if it doesnt exist in the OpenList and CloseList { astate.ParentState = state; // update his parent AddToOpenList(astate); // add it to openList } } } return(null);// if there isnt solution }
/// <summary> /// get all the Succesors for the current state /// </summary> /// <param name="state"></param> /// <returns>list of Succesors</returns> public List <AState> GetAllSuccessors(AState state) { List <AState> successors = new List <AState>(); MazeState m = state as MazeState; Position down = new Position(m.Position.X + 1, m.Position.Y, m.Position.Z); // create down position checkDown(down, successors, m, state); // check for the down position Position up = new Position(m.Position.X - 1, m.Position.Y, m.Position.Z); // create up position checkUp(up, successors, m, state); // check for the up position Position right = new Position(m.Position.X, m.Position.Y + 1, m.Position.Z); // create right position checkRight(right, successors, m, state); // check for the right position Position left = new Position(m.Position.X, m.Position.Y - 1, m.Position.Z); // create left position checkLeft(left, successors, m, state); // check for the up position Position UpinZ = new Position(m.Position.X, m.Position.Y, m.Position.Z + 1); if (checkIfInLimits(UpinZ) == true && checkIfThereIsAWall(UpinZ) == false) // up in layers in maze { AState n_Astate = new MazeState(state, UpinZ); // add to succersos if it in limits and if not wall successors.Add(n_Astate); } Position DowninZ = new Position(m.Position.X, m.Position.Y, m.Position.Z - 1); if (checkIfInLimits(DowninZ) == true && checkIfThereIsAWall(DowninZ) == false) // down in layers in maze { AState n_Astate = new MazeState(state, DowninZ); // add to succersos if it in limits and if not wall successors.Add(n_Astate); } return(successors); }
public override Solution Solve(ISearchable searchDomain) { StartMeasureTime();// start measure the time ClearOpenClosedLists(); AState stratingState = searchDomain.GetStartState(); addToStack(stratingState); // add the initial state to stack while (!IfStackEmpty()) { AState state = popStack(); // pop from the stack if (state.State.Equals(searchDomain.GetGoalState().State)) // check if it is a goalState { StopMeasureTime(); // stop measure the time getBackTrace(state); // get the traceBack of the solution Solution.RevereseSolution(); return(Solution); } if (!isDiscovered(state)) // if state is not discovered { m_discovered.Add(state); // add it to DiscoverdLIst SUCCESORS = searchDomain.GetAllSuccessors(state); // foreach state in succersors, add it to stack foreach (AState Astate in SUCCESORS) { addToStack(Astate); } } } return(null); }
/// <summary> /// get the way in the maze to solve it /// </summary> /// <param name="state">state in the maze</param> public void getBackTrace(AState state) { Solution.AddState(state); while (state.ParentState != null) { Solution.AddState(state.ParentState); //add to SolutionList state = state.ParentState; // update the fatherState } }
/// <summary> /// check if the state is in the openList /// </summary> /// <param name="state">state in the maze</param> /// <returns>return true if the state exist in the list</returns> protected bool IsStateInOpenList(AState state) { foreach (AState s in m_openList) { if (state.State.Equals(s.State)) { return(true); } } return(false); }
public bool isDiscovered(AState state) // check if the state was discovered { foreach (AState s in m_discovered) // for each state in discovered list { if (s.State.Equals(state.State)) // check if equals { return(true); } } return(false); }
/// <summary> /// check if the Mase States are equals /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(AState obj) { //if ((obj.State == State) && (obj.M_G == M_G) && obj.ParentState.State == ParentState.State && obj.ParentState.M_G == ParentState.M_G) // return true; //return false; if (obj.State == State) { return(true); } return(false); }
/// <summary> /// compare the MazeState /// </summary> /// <param name="state"></param> /// <returns></returns> public override int CompareTo(AState state) { if (M_G > state.M_G) { return(1); } else { if (M_G < state.M_G) { return(-1); } return(0); } }
/// <summary> /// check if there is a way in down position /// </summary> /// <param name="down"> position in maze</param> /// <param name="successors">list of succesors</param> /// <param name="m">maze state</param> /// <param name="state"> state</param> public void checkDown(Position down, List <AState> successors, MazeState m, AState state) { if (checkIfInLimits(down) == true) // check if in limits of the maze { if (checkIfThereIsAWall(down) == false) // check if it is a wall { Position downCell = new Position(m.Position.X + 2, m.Position.Y, m.Position.Z); // cell that it is part of the options in the maze if (checkIfInLimits(downCell) == true) { AState n_Astate = new MazeState(state, downCell); // add to succersos if it in limits and if not wall successors.Add(n_Astate); } } } }
/// <summary> /// add state to ClosedList /// </summary> /// <param name="state">state in the maze</param> protected void AddToClosedList(AState state) { m_closedList.Enqueue(state); }
/// <summary> /// add State to OpenList /// </summary> /// <param name="state">state in the maze</param> protected void AddToOpenList(AState state) { m_openList.Enqueue(state); m_countGeneratedNodes++; }
/// <summary> /// add state to stack /// </summary> /// <param name="state">state in the maze</param> public void addToStack(AState state) { m_stack.Push(state); m_countGeneratedNodes++; }
/// <summary> /// inisialize the MazeSate /// </summary> /// <param name="Parent_state">get the ParentState</param> /// <param name="position">get the Position</param> public MazeState(AState Parent_state, Position position) : base(Parent_state) { m_position = position; m_state = position.ToString(); }
/// <summary> /// add State to Solution /// </summary> /// <param name="state">get the state to add</param> public void AddState(AState state) { m_solutionPath.Add(state); }