def test_equals(self): game1 = Flow(self.simple_board) game2 = Flow(self.simple_board) self.assertTrue(game1 == game2) changed_board = copy.deepcopy(self.simple_board) changed_board[1][0] = 'r' game2 = Flow(changed_board) self.assertFalse(game1 == game2)
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 heuristic(self, game): F = Flow(game.board) """ Gives a cost to the board state by measuring how far between growth points this also makes the cost max value when one path can't be moved :type game: Flow :return: double """ cost = len(game.paths) * 2 for color in game.paths: path = game.paths[color] if path.is_complete(): cost -= 2 continue gp1, gp2 = path.get_grow_points() # print abs((gp1[0] - gp2[0]) + (gp1[1] - gp2[1])), gp1, gp2 cost += abs((gp1[0] - gp2[0]) + (gp1[1] - gp2[1])) adjecent_points = utils.get_adjacent_points(gp1) if not F.is_valid(adjecent_points[0][0], adjecent_points[0][1]): if not F.is_valid(adjecent_points[1][0], adjecent_points[1][1]): if not F.is_valid(adjecent_points[2][0], adjecent_points[2][1]): if not F.is_valid(adjecent_points[3][0], adjecent_points[3][1]): cost = sys.maxint return cost # print game.board[1][1] # for i, value in enumerate(game.board): # for j in enumerate(game.board[i]): # if j[1] == '0': # cost += 1 return cost
def test_adjacent_points(self): test_game = Flow(self.simple_board) self.assertTrue(test_game.adjacent_points((1, 0), (2, 0))) self.assertTrue(test_game.adjacent_points((2, 0), (1, 0))) self.assertFalse(test_game.adjacent_points((0, 3), (0, 2))) self.assertFalse(test_game.adjacent_points((0, 0), (1, 1)))
def test_find_end_points(self): test_flow = Flow(self.simple_board) red_path = test_flow.paths['R'].path_from1 red_path += test_flow.paths['R'].path_from2 orange_path = test_flow.paths['O'].path_from1 orange_path += test_flow.paths['O'].path_from2 self.assertTrue((0, 0) in red_path, "There is a R end point at position 0, 0 in simple board") self.assertTrue((2, 2) in orange_path, "There is a O end point at 2,2 in simple board")
def test_get_board_copy(self): test_board = [['0', 'R', '0'], ['R', '0', '0']] flow = Flow(test_board) original_board = flow.get_board_copy() self.assertTrue(utils.equal_boards(test_board, original_board), "Returned board should be the same as the test board") original_board[0][0] = 'T' self.assertFalse(utils.equal_boards(test_board, original_board), "Change made to the returned board. Test board should not be the same as it") self.assertTrue(utils.equal_boards(test_board, flow.get_board_copy()), "Change made to the returned board should not effect the new copy")
current_queue.append() was_added = True # Attempt to add to path 2 if route_num == 0 or route_num == 2: if self.can_be_added_to_path(point, 2): self.path_from2.append(point) was_added = True if was_added: self.flow_game.board[point[0]][point[1]] = self.color.lower() if __name__ == '__main__': first_board = [['R', '0', 'G', '0', '0'], ['0', '0', 'B', '0', '0'], ['0', '0', '0', '0', '0'], ['0', 'G', '0', '0', '0'], ['0', 'R', 'B', '0', '0']] medium_flow = [['R', 'Y', '0'], ['0', '0', '0'], ['G', '0', '0'], ['0', 'R', '0'], ['0', 'G', 'Y']] simple_board = [['R', 'Y', '0'], ['0', '0', '0'], ['R', '0', 'Y']] first_flow = Flow(first_board) # first_flow = Flow(simple_board) # first_flow = Flow(medium_flow) start_time = time.time() solution = astar().solve(first_flow) end_time = time.time() print solution print "Time:", end_time - start_time # print astar().heuristic(first_flow)
adj_points = utils.get_adjacent_points(gp1) for point in adj_points: if path.can_be_added_to_path(point): copy_path = copy.deepcopy(path) """:type:Path""" copy_path.add_to_path(point) rv = self.finish_path(copy_path) if rv[0]: return rv return False, None if __name__ == '__main__': simple_board = [['R', 'Y', '0'], ['0', '0', '0'], ['R', '0', 'Y']] simple_flow = Flow(simple_board) start = time.time() print BackTrackSolver().solve(simple_flow) print "Back Track simple:", str(time.time() - start) medium_flow = [['R', 'Y', '0'], ['0', '0', '0'], ['G', '0', '0'], ['0', 'R', '0'], ['0', 'G', 'Y']] medium_flow = Flow(medium_flow) start = time.time() print BackTrackSolver().solve(medium_flow) print "Back Track medium:", str(time.time() - start) first_board = [['R', '0', 'G', '0', 'Y'], ['0', '0', 'B', '0', 'M'], ['0', '0', '0', '0', '0'], ['0', 'G', '0', 'Y', '0'], ['0', 'R', 'B', 'M', '0']]
def setUp(self): simple_board = [['R', 'O', '0'], ['0', '0', '0'], ['R', '0', 'O']] self.simple_board = Flow(simple_board)
# print "Attempting" # print game # print possible, path.can_be_added_to_path(possible, 1) if path.can_be_added_to_path(possible, 1): copy_game = deepcopy(game) """:type: Flow""" copy_game.paths[color].add_to_path(possible, 1) self.queue.append(copy_game) # print "after attempt" # print copy_game if __name__ == '__main__': simple_board = [['R', 'Y', '0'], ['0', '0', '0'], ['R', '0', 'Y']] simple_flow = Flow(simple_board) # print BFS().solve_rr(simple_flow) start = time.time() print BFS().solve_single_gp(simple_flow) print "Simple board time:", str(time.time() - start) medium_flow = [['R', 'Y', '0'], ['0', '0', '0'], ['G', '0', '0'], ['0', 'R', '0'], ['0', 'G', 'Y']] medium_flow = Flow(medium_flow) # print BFS().solve_rr(medium_flow) start = time.time() print BFS().solve_single_gp(medium_flow) print "Medium flow time:", str(time.time() - start) first_board = [['R', '0', 'G', '0', '0'], ['0', '0', 'B', '0', '0'], ['0', '0', '0', '0', '0'], ['0', 'G', '0', '0', '0'],