def test_at_goal(self): test_board = [['0', 'R', '0'], ['R', '0', '0']] flow_game = flow_board.Flow(test_board) self.assertFalse(utilities.at_goal(flow_game)) flow_game.paths['R'].add_to_path((0, 0)) self.assertTrue(utilities.at_goal(flow_game))
def test_single_gp(self): flow_game = Flow(self.simple_board) solution = BFS().solve_single_gp(flow_game) self.assertTrue(utils.at_goal(solution), "Tests if BFS solves simple board with single GP method of adding to queue") flow_game = Flow(self.medium_flow) solution = BFS().solve_single_gp(flow_game) self.assertTrue(utils.at_goal(solution), "Tests if BFS solves medium board with single GP method of adding to queue")
def test_round_robin(self): flow_game = Flow(self.simple_board) solution = BFS().solve_rr(flow_game) self.assertTrue(utils.at_goal(solution), "Test if BFS solves the simple board with RR method of adding to queue") flow_game = Flow(self.medium_flow) solution = BFS().solve_single_gp(flow_game) self.assertTrue(utils.at_goal(solution), "Test if BFS solves the medium board with RR method of adding to queue")
def test_single_gp(self): flow_game = Flow(self.simple_board) solution = BFS().solve_single_gp(flow_game) self.assertTrue( utils.at_goal(solution), "Tests if BFS solves simple board with single GP method of adding to queue" ) flow_game = Flow(self.medium_flow) solution = BFS().solve_single_gp(flow_game) self.assertTrue( utils.at_goal(solution), "Tests if BFS solves medium board with single GP method of adding to queue" )
def test_round_robin(self): flow_game = Flow(self.simple_board) solution = BFS().solve_rr(flow_game) self.assertTrue( utils.at_goal(solution), "Test if BFS solves the simple board with RR method of adding to queue" ) flow_game = Flow(self.medium_flow) solution = BFS().solve_single_gp(flow_game) self.assertTrue( utils.at_goal(solution), "Test if BFS solves the medium board with RR method of adding to queue" )
def solve_game_dumb(self, flow_game): """ Solves the given flow game using backtracking. :type flow_game: Flow :rtype: (bool, Flow) """ # print flow_game if utils.at_goal(flow_game): return True, flow_game sorted_colors = self.order_colors(flow_game) # For each color that is not complete for color_path in sorted_colors: if color_path.is_complete(): continue # Complete its path bt_path = BackTrackingPath(color_path) completed_path = bt_path.get_next_path() # While there are still backtracking options while completed_path is not None: game_copy = copy.deepcopy(completed_path.flow_game) rv = self.solve_game_dumb(game_copy) if rv[0]: return rv completed_path = bt_path.get_next_path() return False, None
def solve_single_gp(self, initial_game): """ Creates the stack to be searched in a round robin manner - Each color gets to select 1 move until the solution is found. :type initial_game_state: Flow :return: Flow """ self.queue.append(initial_game) # For loop to go through queue while len(self.queue) > 0: game = self.queue.pop(0) if utils.at_goal(game): return game self.generate_possible_moves_single_gp(game) return None
def solve(self, initial_flow): """ Solve the initial flow game :type initial_flow: Flow :rtype: Flow """ self.frontier.append((0, initial_flow)) while len(self.frontier) > 0: chosen_node = self.get_next_to_explore() # print chosen_node[0], chosen_node[1] if utils.at_goal(chosen_node[1]): return chosen_node else: self.create_possibilites(chosen_node) return "No solution found"
def solve(self, initial_game_state): """ :type initial_game_state: Flow :return Flow: """ self.queue.put((astar().heuristic(initial_game_state), initial_game_state)) current_cost = 0 while self.queue._qsize() > 0: pop = self.queue.get() game = pop[1] current_cost += pop[0] # print current_cost if utils.at_goal(game): return game possible_moves = utils.generate_possible_moves_single_gp(game) for i, value in enumerate(possible_moves): # print possible_moves[i] self.queue.put((astar().heuristic(possible_moves[i]) + current_cost, possible_moves[i])) return None
def solve(self, initial_game_state): """ :type initial_game_state: Flow :return Flow: """ self.queue.put( (astar().heuristic(initial_game_state), initial_game_state)) current_cost = 0 while self.queue._qsize() > 0: pop = self.queue.get() game = pop[1] current_cost += pop[0] # print current_cost if utils.at_goal(game): return game possible_moves = utils.generate_possible_moves_single_gp(game) for i, value in enumerate(possible_moves): # print possible_moves[i] self.queue.put( (astar().heuristic(possible_moves[i]) + current_cost, possible_moves[i])) return None