def run_dfs(self): self.stack.append(self.initial_state) #self.stack_set.append(self.initial_state.puzzle) while(len(self.stack) != 0): current_state = self.stack.pop() #self.stack_set.pop() self.set.append(current_state.puzzle) if(self.is_final_state(current_state)): self.final_state = current_state return True else: movement = Movment(current_state) self.expanded_nodes.append(current_state) movement.right_movment() movement.left_movment() movement.up_movment() movement.down_movment() childs = [movement.right_state , movement.left_state , movement.up_state , movement.down_state] for child in childs: if (child != None): if (child.puzzle not in self.set): if (child not in self.stack): self.stack.append(child) #self.stack_set.append(child.puzzle) if (self.max_depth < child.depth): self.max_depth = child.depth return False
def search(self): queue = [] queue.append(self.initial_state) queue_set = [self.initial_state.puzzle] set = [] i=0 while(len(queue) != 0): state = queue.pop(0) queue_set.pop(0) set.append(state.puzzle) if (self.is_final_state(state)): self.final_state = state self.max_depth = state.depth return True movement = Movment(state) self.expanded_nodes.append(state) print(i) movement.right_movment() movement.left_movment() movement.up_movment() movement.down_movment() i = i + 1 if (movement.up_state != None): if (self.check_redundant_state(queue_set, set, movement.up_state) == False): queue.append(movement.up_state) queue_set.append(movement.up_state.puzzle) if (movement.down_state != None): if (self.check_redundant_state(queue_set, set, movement.down_state) == False): queue.append(movement.down_state) queue_set.append(movement.down_state.puzzle) if (movement.left_state != None): if (self.check_redundant_state(queue_set, set, movement.left_state) == False): queue.append(movement.left_state) queue_set.append(movement.left_state.puzzle) if (movement.right_state != None): if (self.check_redundant_state(queue_set, set, movement.right_state) == False): queue.append(movement.right_state) queue_set.append(movement.right_state.puzzle) return False
def run_manhattan_a_star(self): self.calculate_state_heuristic(self.initial_state) heapq.heappush(self.heap, (self.initial_state.cost, self.initial_state)) while (len(self.heap) != 0): (key, current_state) = heapq.heappop(self.heap) self.set.append(current_state.puzzle) if (self.is_final_state(current_state)): self.final_state = current_state return True else: movement = Movment(current_state) self.expanded_nodes.append(current_state) movement.right_movment() movement.left_movment() movement.up_movment() movement.down_movment() childs = [ movement.right_state, movement.left_state, movement.up_state, movement.down_state ] for child in childs: if (child != None): if (child.puzzle not in self.set): self.calculate_state_heuristic(child) if (self.check_if_not_exits(child)): heapq.heappush(self.heap, (child.cost, child)) else: heapq.heapify(self.heap) if (self.max_depth < child.depth): self.max_depth = child.depth return False