If actions = ActionType.UP and tmp = ActionType.DOWN:Įlif actions = ActionType.DOWN and tmp = ActionType.UP:Įlif actions = ActionType.LEFT and tmp = ActionType.RIGHT:Įlif actions = ActionType.RIGHT and tmp = ActionType.LEFT:įor ii, sublist in enumerate(self.board):ĭef _init_ (self, n, startState = None, goalState = None): Self.board, self.board = self.board, self.board State = epcopy(self) #PuzzleState(self.n) Self.blankCellPos = self.getBlankCellPos() class ActionType:Īctions = [ActionType.UP, ActionType.DOWN, ActionType.LEFT, Return hasattr(other,'state') and self.state = other.stateĪnd then there's the file that defines NPuzzle and Puzzle state. Return Solution (start_node, numExplored, memoryRequired)ĭef _init_(self, state, action, cost, parent): Print ('Frontier size = %d, Explored size = %d, Total size = %d ,Successor Length= %d' %(len(frontier.list), len(explored), len(frontier.list)+len(explored), len(successors))) If sc_node not in explored and sc_node not in frontier.list: # insert successor node into frontier list (if not found in explored and frontier) Return Solution (sc_node, numExplored, memoryRequired) If problem.isGoalState(sc_node.state): # if found solution # check for goal state immediately after generate Sc_node = Node(sc_state, sc_action, sc_cost, fr_node) Successors = problem.getSuccessors(fr_node.state)įor sc_state, sc_action, sc_cost in successors: # get the first-in frontier node from frontier list If problem.isGoalState(start_node.state): # return immediately if start node is the goal state Start_node = Node(problem.getStartState(), None, 0, None) """Search the shallowest nodes in the search tree first.""" I have these code in Python: def breadthFirstSearch(problem): To return Solution (fr_node, numExplored, memoryRequired)Īnd it always return an solution for 2x2, 3x3 is still very slow though I changed the return Solution (start_node, numExplored, memoryRequired) When I set the size to be 3(3x3) it never complete the search, and when I set the size to be 2 it solves but sometimes it will exhaust the frontier list and returns nothing, which should not be possible. Then I'll do some random moves on it to get the goal board state: I'm trying to do 8-puzzle using pycharm, I will first generate a 3x3 list and then shuffle it to get the initial board state example: I moved this from code review because folks over there suggest this should be posted here
0 Comments
Leave a Reply. |