def findpath(self): # Temp = init_state Temp = [ copy.deepcopy(self.init_state), copy.deepcopy(self.init_state), copy.deepcopy(self.init_state), copy.deepcopy(self.init_state) ] print("INIT = ", self.init_state) cur_node = EightPuzzleNode(self.init_state, action='INIT') new_state = cur_node.state.successor('u') eightPuzzleH1(new_state, self.goal_state) # print("Temp = ",Temp) cur_node = EightPuzzleNode(Temp[0], action='INIT') new_state = cur_node.state.successor('d') eightPuzzleH1(new_state, self.goal_state) print("Downnn 1111 = ", new_state) cur_node = EightPuzzleNode(Temp[1], action='INIT') new_state = cur_node.state.successor('l') eightPuzzleH1(new_state, self.goal_state) print("left 1111 = ", new_state)
def getnode(currentNode: EightPuzzleNode): Nodes = [] def check(state: EightPuzzleState, i, j): board = copy.deepcopy(state.action_space) if i - 1 < 0: board.remove('u') if i + 1 > len(state.board) - 1: board.remove('d') if j - 1 < 0: board.remove('l') if j + 1 > len(state.board[0]) - 1: board.remove('r') return board pass moveAction = check(currentNode.state, currentNode.state.y, currentNode.state.x) for i in moveAction: movement: EightPuzzleState = currentNode.state.successor(i) Nodes.append(EightPuzzleNode(movement, currentNode, i)) return Nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node = EightPuzzleNode(init_state, action='INIT') num_nodes += 1 # TODO: 5 return solution, num_nodes
def getnode(currentNode: EightPuzzleNode): Nodes = [] getmove = Action(currentNode.state, currentNode.state.y, currentNode.state.x) for i in getmove: movement: EightPuzzleState = currentNode.state.successor(i) Nodes.append(EightPuzzleNode(movement, currentNode, i)) return Nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node = EightPuzzleNode(init_state, action='INIT') frontier.add(root_node) exploreNodes = set() num_nodes += 1 # TODO: 5 while not frontier.is_empty(): currentNode = frontier.next() if currentNode.state.is_goal(): solutionNode = currentNode break if currentNode.state not in exploreNodes: exploreNodes.add(currentNode.state) num_nodes += 1 for node in getNeighborNodes(currentNode): frontier.add(node) paths = solutionNode.trace() paths.pop(0) for path in paths: solution.append(path.action) return solution, num_nodes
def getNeighborNodes(currentNode: EightPuzzleNode): neighborNodes = [] actionSpace = getActionSpace(currentNode.state, currentNode.state.y, currentNode.state.x) for action in actionSpace: successorBoard: EightPuzzleState = currentNode.state.successor(action) neighborNodes.append( EightPuzzleNode(successorBoard, currentNode, action)) return neighborNodes
def test_by_hand(verbose=True): """Run a graph-search.""" """Run a graph-search.""" goal_state = EightPuzzleState([[1, 2, 3], [4, 5, 6], [7, 8, 0]]) init_state = EightPuzzleState.initializeState() while not _is_reachable(goal_state.board, init_state.board): init_state = EightPuzzleState.initializeState() # frontier = GreedyFrontier(eightPuzzleH2,goal_state) # Change this to your own implementation. # frontier = AStarFrontier(eightPuzzleH2,goal_state) frontier = DFSFrontier() # frontier.stack if verbose: print(init_state) # print(frontier.stack.board[0]) plan, num_nodes = graph_search(init_state, goal_state, frontier) # print(frontier.stack[0]) if verbose: print(f'A solution is found after generating {num_nodes} nodes.') if verbose: for action in plan: print(f'- {action}') pass # ========================== checking solution paet ========================== # print(init_state) cur_node = EightPuzzleNode(init_state, action="INIT") for i in plan: new_state = cur_node.state.successor(i) cur_node = EightPuzzleNode(new_state, cur_node, i) # print(new_state) # print() if new_state.is_goal(): # print('Congratuations!') break return len(plan), num_nodes
def getLeafNodes(currentNode: EightPuzzleNode): """ Find the possible Move of the current node, apply those moves then return new states as an array :param currentNode: EightPuzzleNode Node that we are dealing with :return: Next possible states """ leafNodes = [] possibleMoves = getPossibleActions(currentNode.state, currentNode.state.y, currentNode.state.x) for i in possibleMoves: transitionedState: EightPuzzleState = currentNode.state.successor(i) leafNodes.append(EightPuzzleNode(transitionedState, currentNode, i)) return leafNodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search # frontier.__hash__() # Temp = copy.deepcopy(init_state) # root_node = EightPuzzleNode(init_state, action='INIT') # num_nodes += 1 # new = root_node.state.successor('u') # print("After U") # print(new) # eightPuzzleH1(new, goal_state) # # print("ORIginal = ",Temp) # # new_state = root_node.state.successor('d') # print("After D") # print(new_state) # eightPuzzleH1(new_state, goal_state) round =1 i =0 Temp = [copy.deepcopy(init_state), copy.deepcopy(init_state), copy.deepcopy(init_state), copy.deepcopy(init_state)] VeryTemp = copy.deepcopy(init_state) actionList = ['u', 'd', 'l', 'r'] trace = [100,100,100,100] min = 99 traceFinal = [] while min != 0: # while round < 10: for i in range(4): Temp = copy.deepcopy(VeryTemp) print("Round = ",round/4) # print("Min = ",min) print(Temp) root_node = EightPuzzleNode(Temp, action='INIT') # num_nodes += 1 new = root_node.state.successor(actionList[i]) if new is not None: print("NONEeeeee++===========") print("After ", actionList[i]) print(new) trace[i] = eightPuzzleH1(new, goal_state)+round eightPuzzleH1(new, goal_state) i += 1 # round += 1 print("R = ",round) roundAction = 0 r = 0 print("Min before check = ",min) for r in range (4): if trace[r] <= min: min = trace[r] roundAction = r print(trace) print("Choose Action = ",actionList[roundAction]) # for r in range(3): # print(trace[r]) print(min,roundAction) Temp = copy.deepcopy(VeryTemp) root_node = EightPuzzleNode(Temp, action='INIT') num_nodes += 1 new = root_node.state.successor(actionList[roundAction]) print("Move ==>",actionList[roundAction]) print(new) traceFinal.append(actionList[roundAction]) solution.append(actionList[roundAction]) VeryTemp = copy.deepcopy(new) Sto = eightPuzzleH1(new, goal_state) r = 0 if Sto == 0: print("==Finish==") print(new) break print("Finish") print(Temp) print(round) trace = [100,100,100,100] # if round += 1 # round += 1 # TODO: 5 return solution, num_nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node = EightPuzzleNode(init_state, action='INIT') frontier.add(root_node) num_nodes += 1 # TODO: 5 passed = set() while not frontier.is_empty(): current_node: EightPuzzleNode = frontier.next() if current_node.state.is_goal(): "set new goal" goal_node = current_node break if current_node.state not in passed: passed.add(current_node.state) num_nodes += 1 acts = set() if current_node.state.y - 1 >= 0: acts.add('u') if current_node.state.y + 1 < 3: acts.add('d') if current_node.state.x - 1 >= 0: acts.add('l') if current_node.state.x + 1 < 3: acts.add('r') for i in acts: new_state = current_node.state.successor(i) new_node = EightPuzzleNode(new_state, current_node, i) frontier.add(new_node) print("\n") print(current_node.state) path = goal_node.trace() path.pop(0) for node in path: solution.append(node.action) return solution, num_nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node = EightPuzzleNode(init_state, action='INIT') frontier.add(root_node) num_nodes += 1 # TODO: 5 "passed is a set to keep the node that are passed" print("test Out") list = [] countList = 0 ActionList = ['u', 'd', 'l', 'r'] frontier.add(root_node) check = 1 checkSame = 0 while not frontier.is_empty(): cur_node = frontier.next() state = cur_node.state list.append(cur_node) countList += 1 # if state.board == goal_state.board: # break for i in range(3): for j in range(3): if state.board[i][j] != goal_state.board[i][j]: checkSame = checkSame + 1 if checkSame == 0: break num_nodes = num_nodes + 1 for i in range(4): temp = cur_node.state.successor(ActionList[i]) if temp: next_node = EightPuzzleNode(temp, cur_node, ActionList[i]) for i in range(countList): if next_node.state == list[i].state: check = 0 solution.append(cur_node.action) if check == 1: frontier.add(next_node) check = 1 checkSame = 0 return solution, num_nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node: EightPuzzleNode = EightPuzzleNode(init_state, action='INIT') frontier.add(root_node) num_nodes += 1 # TODO: 5 set_node = set() while frontier.is_empty() == False: current = frontier.next() if current.state.is_goal() == True: result = current break if current.state not in set_node: set_node.add(current.state) num_nodes += 1 Nodes = [] board = copy.deepcopy(init_state.action_space) if current.state.y - 1 < 0: board.remove('u') if current.state.y + 1 > len(init_state.board) - 1: board.remove('d') if current.state.x - 1 < 0: board.remove('l') if current.state.x + 1 > len(init_state.board[0]) - 1: board.remove('r') for i in board: EightPuzzleState2 = current.state.successor(i) Nodes.append(EightPuzzleNode(EightPuzzleState2, current, i)) for i in Nodes: frontier.add(i) path = result.trace() path.pop(0) # Remove -INIT for i in path: solution.append(i.action) return solution, num_nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node = EightPuzzleNode(init_state, action='INIT') num_nodes += 1 cur_node = root_node # TODO: 5 list = [] frontier.add(root_node) while not frontier.is_empty(): cur_node = frontier.next() state = cur_node.state list.append(cur_node) if state.board == goal_state.board: #print("\n",state,"\n") break check = True #print("\n",state) num_nodes = num_nodes + 1 actionList = ['u', 'd', 'l', 'r'] for i in actionList: temp = cur_node.state.successor(i) if temp: next_node = EightPuzzleNode(temp, cur_node, i) for i in list: if next_node.state == i.state: check = False solution.append(cur_node.action) if check: frontier.add(next_node) check = True return solution, num_nodes
def graph_search(init_state, goal_state, frontier): if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node = EightPuzzleNode(init_state, action='INIT') # print(root_node.state) num_nodes += 1 # ======================================================= cur_node = root_node round = 10 history = [] actionList = ['u', 'd', 'l', 'r'] move = 10 frontier.add(root_node) while not frontier.is_empty(): # print(" Round ", round) for i in range(4): new_State = cur_node.state.successor(actionList[i]) # if new_State != None: # for checkDup in range(len(history)): # if new_State == history[checkDup]: # same += 1 if new_State != None: if new_State.board not in history: move = 0 if move == 0 and new_State != None: new_node = EightPuzzleNode(new_State, cur_node, actionList[i]) history.append(new_State.board) # solution.append(actionList[i]) frontier.add(new_node) move = 10 num_nodes += 1 next = frontier.next() if type(next) == tuple: next_node = next[2] else: next_node = next print(next_node.state) if next_node.state.is_goal(goal_state.board) == True: check = 0 else: check = 1 i = 0 if check == 0: list = next_node.trace() for i in list: if i.action != "INIT": solution.append(i.action) break cur_node = next_node round += 1 return solution, num_nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploreation. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] exploded_state = set() # Perform graph search node = EightPuzzleNode(init_state, action='INIT') frontier.add(node) num_nodes += 1 while not frontier.is_empty(): current_node = frontier.next() current_state = current_node.state if current_state.is_goal(goal_state.board): break exploded_state.add(current_state) actions = {'u', 'd', 'l', 'r'} for action in actions: new_state = current_state.successor(action) if new_state is not None: if new_state not in exploded_state: num_nodes += 1 frontier.add( EightPuzzleNode(new_state, current_node, action)) node = current_node for x, element in enumerate(node.trace()): action = element.action if action is not 'INIT': solution.append(action) # Test solution # st = init_state # print("Test solution") # print(st) # for x, action in enumerate(solution): # st = st.successor(action) # if st.is_goal(): # print('Congratuations!') # TODO: 5 return solution, num_nodes
def graph_search(init_state, goal_state, frontier): """ Search for a plan to solve problem. Parameters ---------- init_state : EightPuzzleState an initial state goal_state : EightPuzzleState a goal state frontier : Frontier an implementation of a frontier which dictates the order of exploration. Returns ---------- plan : List[string] or None A list of actions to reach the goal, None if the search fails. Your plan should NOT include 'INIT'. num_nodes: int A number of nodes generated in the search. """ if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node: EightPuzzleNode = EightPuzzleNode(init_state, action='INIT') exploredNodeSet = set() frontier.add(root_node) num_nodes += 1 # TODO: 5 while not frontier.is_empty(): currentNode = frontier.next() # print() # print(currentNode.state) # frontier.remove(currentNode) if currentNode.state.is_goal(): solutionNode = currentNode # print("Current node is Goal") break if currentNode.state not in exploredNodeSet: exploredNodeSet.add(currentNode.state) num_nodes += 1 for child in getLeafNodes(currentNode): # print("Added\n" + str(child.state)) frontier.add(child) # print(num_nodes) # print() # print(solutionNode.state) tracePath = solutionNode.trace() tracePath.pop(0) # for i in tracePath: # print(i.state) # print() for elem in tracePath: solution.append(elem.action) # print(str(len(tracePath)) + " Levels") return solution, num_nodes
def graph_search(init_state, goal_state, frontier): if not _is_reachable(init_state.board, goal_state.board): return None, 0 if init_state.is_goal(goal_state.board): return [], 0 num_nodes = 0 solution = [] # Perform graph search root_node = EightPuzzleNode(init_state, action='INIT') # print(root_node.state) num_nodes += 1 # if current_node.state.x != 2: # acts.add('u') # if current_node.state.x != 0: # acts.add('d') # if current_node.state.y != 2: # acts.add('l') # if current_node.state.y != 0: # acts.add('r') # aom tryyyyyyy======================================================= print("test Out") cur_node = root_node round = 10 history = [] trace = [100,100,100,100] actionList = ['u','d','l','r'] same = 0 while round != 0: for i in range(4): print("round i = ",round ,i) new_State = cur_node.state.successor(actionList[i]) if new_State != None and new_State.board not in history: same = 0 print("After Move ",actionList[i]) new_node = EightPuzzleNode(new_State, cur_node, actionList[i]) history.append(new_State.board) solution.append(actionList[i]) print(new_State) frontier.add(new_node) eightPuzzleH1(new_State, goal_state) num_nodes += 1 temp = frontier.next() print(temp) if type(temp) == tuple: next_node = temp[2] else: next_node = temp print("Test") check = 0 for i in range(3): for j in range(3): if next_node.state.board[i][j] != goal_state.board[i][j]: check = check+1 i=0 if check==0: temp = next_node.trace() for i in range(len(temp)): if i != 0: solution.append(temp[i].action) break else: print("\033[H\033[J") cur_node = next_node return solution, num_nodes