def test_uniform_noise_4_bots_no_noise(self): test_layout = ( """ ################## # #. . # . 2 # # ##### #####3# # 0 . # . .#1# ################## """) universe = create_CTFUniverse(test_layout, 4) noiser = UniverseNoiser(universe.copy()) position_bucket_0 = dict(((i, 0) for i in [(1, 2), (7, 3), (1, 3), (3, 3), (6, 3), (2, 3), (4, 3), (1, 1), (5, 3)])) bot_2_pos = (13, 1) position_bucket_2 = {bot_2_pos : 0} for i in range(100): new = noiser.uniform_noise(universe.copy(), 1) self.assertTrue(new.bots[0].noisy) self.assertFalse(new.bots[2].noisy) position_bucket_0[new.bots[0].current_pos] += 1 position_bucket_2[new.bots[2].current_pos] += 1 self.assertEqual(100, sum(position_bucket_0.itervalues())) self.assertEqual(100, sum(position_bucket_2.itervalues())) # Since this is a randomized algorithm we need to be a bit lenient with # our tests. We check that each position was selected at least once and # check that it was selected a minimum of five times. for v in position_bucket_0.itervalues(): self.assertTrue(v != 0) self.assertTrue(v >= 5, 'Testing randomized function, may fail sometimes.') # bots should never have been noised self.assertEqual(100, position_bucket_2[bot_2_pos])
def test_uniform_noise_4_bots_no_noise_a_star(self): test_layout = """ ################## # #. . # . 2 # # ##### #####3# # 0 . # . .#1# ################## """ universe = create_CTFUniverse(test_layout, 4) noiser = AStarNoiser(universe.copy()) expected_0 = [(1, 2), (7, 3), (1, 3), (3, 3), (6, 3), (2, 3), (4, 3), (1, 1), (5, 3)] position_bucket_0 = collections.defaultdict(int) bot_2_pos = (13, 1) position_bucket_2 = {bot_2_pos: 0} for i in range(100): new = noiser.uniform_noise(universe.copy(), 1) self.assertTrue(new.bots[0].noisy) self.assertFalse(new.bots[2].noisy) position_bucket_0[new.bots[0].current_pos] += 1 position_bucket_2[new.bots[2].current_pos] += 1 self.assertEqual(100, sum(position_bucket_0.itervalues())) self.assertEqual(100, sum(position_bucket_2.itervalues())) # Since this is a randomized algorithm we need to be a bit lenient with # our tests. We check that each position was selected at least once. self.assertItemsEqual(position_bucket_0, expected_0, position_bucket_0) # bots should never have been noised self.assertEqual(100, position_bucket_2[bot_2_pos])
def test_uniform_noise_manhattan(self): test_layout = """ ################## # #. . # . # # ##### ##### # # 0 . # . .#1# ################## """ universe = create_CTFUniverse(test_layout, 2) noiser = ManhattanNoiser(universe.copy()) position_bucket = collections.defaultdict(int) for i in range(200): new = noiser.uniform_noise(universe.copy(), 1) self.assertTrue(new.bots[0].noisy) position_bucket[new.bots[0].current_pos] += 1 self.assertEqual(200, sum(position_bucket.itervalues())) # Since this is a randomized algorithm we need to be a bit lenient with # our tests. We check that each position was selected at least once. expected = [ (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (7, 2), (6, 1), (5, 1), (4, 1), (3, 1), ] self.assertItemsEqual(position_bucket, expected, position_bucket)
def __init__(self, layout, number_bots, game_time, noise=True): self.universe = datamodel.create_CTFUniverse(layout, number_bots) self.number_bots = number_bots self.game_time = game_time self.noiser = UniverseNoiser(self.universe) if noise else None self.player_teams = [] self.viewers = []
def test_uniform_noise_4_bots_manhattan(self): test_layout = ( """ ################## # #. 2. # . # # ##### #####3# # 0 . # . .#1# ################## """) universe = create_CTFUniverse(test_layout, 4) noiser = ManhattanNoiser(universe.copy()) expected_0 = [ (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (7, 2), (7, 1), (6, 1), (5, 1), (4, 1), (3, 1), (8, 2), (8, 3)] position_bucket_0 = collections.defaultdict(int) expected_2 = [ (1, 1), (1, 2), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 2), (8, 1), (7, 1), (6, 1), (5, 1), (4, 1), (3, 1), (9, 2), (8, 3), (7, 2)] position_bucket_2 = collections.defaultdict(int) for i in range(200): new = noiser.uniform_noise(universe.copy(), 1) self.assertTrue(new.bots[0].noisy) self.assertTrue(new.bots[2].noisy) position_bucket_0[new.bots[0].current_pos] += 1 position_bucket_2[new.bots[2].current_pos] += 1 self.assertEqual(200, sum(position_bucket_0.itervalues())) self.assertEqual(200, sum(position_bucket_2.itervalues())) # Since this is a randomized algorithm we need to be a bit lenient with # our tests. We check that each position was selected at least once. self.assertItemsEqual(position_bucket_0, expected_0, sorted(position_bucket_0.keys())) self.assertItemsEqual(position_bucket_2, expected_2, sorted(position_bucket_2.keys()))
def test_bfs_to_self(self): test_layout = ( """ ############ #0. #.1# ############ """) universe = create_CTFUniverse(test_layout, 2) al = AdjacencyList(universe) self.assertEqual([], al.bfs((1,1), [(1, 1), (2, 1)]))
def test_a_star_exceptions(self): test_layout = ( """ ############ #0. #.1# ############ """) universe = create_CTFUniverse(test_layout, 2) al = AdjacencyList(universe) self.assertRaises(NoPathException, al.a_star, (1, 1), (10, 1)) self.assertRaises(NoPositionException, al.a_star, (0, 1), (10, 1)) self.assertRaises(NoPositionException, al.a_star, (1, 1), (11, 1))
def test_a_star(self): test_layout = ( """ ################## #0#. . # . # #2##### #####1# # . # . .#3# ################## """) universe = create_CTFUniverse(test_layout, 4) al = AdjacencyList(universe) # just a simple smoke test self.assertEqual(14, len(al.a_star((1, 1), (3, 1))))
def test_basic_adjacency_list(self): test_layout = ( """ ###### # # ###### """) universe = create_CTFUniverse(test_layout, 0) al = AdjacencyList(universe) target = { (4, 1): [(4, 1), (3, 1)], (1, 1): [(2, 1), (1, 1)], (2, 1): [(3, 1), (2, 1), (1, 1)], (3, 1): [(4, 1), (3, 1), (2, 1)]} self.assertEqual(target, al)
def create_TestUniverse(layout): initial_pos = [(1, 1), (4, 2)] universe = create_CTFUniverse(layout, number_bots) for i, pos in enumerate(initial_pos): universe.bots[i].initial_pos = pos if not universe.maze.has_at(Food, (1, 2)): universe.teams[1]._score_point() if not universe.maze.has_at(Food, (2, 2)): universe.teams[1]._score_point() if not universe.maze.has_at(Food, (3, 1)): universe.teams[0]._score_point() return universe
def create_TestUniverse(layout, black_score=0, white_score=0): initial_pos = [(1, 1), (4, 2)] universe = create_CTFUniverse(layout, number_bots) universe.teams[0].score = black_score universe.teams[1].score = white_score for i, pos in enumerate(initial_pos): universe.bots[i].initial_pos = pos if not Food in universe.maze[1, 2]: universe.teams[1]._score_point() if not Food in universe.maze[2, 2]: universe.teams[1]._score_point() if not Food in universe.maze[3, 1]: universe.teams[0]._score_point() return universe
def test_extended_adjacency_list(self): test_layout = ( """ ################## #0#. . # . # # ##### ##### # # . # . .#1# ################## """) universe = create_CTFUniverse(test_layout, 2) al = AdjacencyList(universe) adjacency_target = {(7, 3): [(7, 2), (7, 3), (6, 3)], (1, 3): [(1, 2), (2, 3), (1, 3)], (12, 1): [(13, 1), (12, 1), (11, 1)], (16, 2): [(16, 3), (16, 1), (16, 2)], (15, 1): [(16, 1), (15, 1), (14, 1)], (5, 1): [(6, 1), (5, 1), (4, 1)], (10, 3): [(10, 2), (11, 3), (10, 3), (9, 3)], (7, 2): [(7, 3), (7, 1), (8, 2), (7, 2)], (1, 2): [(1, 3), (1, 1), (1, 2)], (3, 3): [(4, 3), (3, 3), (2, 3)], (13, 3): [(14, 3), (13, 3), (12, 3)], (8, 1): [(8, 2), (8, 1), (7, 1)], (16, 3): [(16, 2), (16, 3)], (6, 3): [(7, 3), (6, 3), (5, 3)], (14, 1): [(15, 1), (14, 1), (13, 1)], (11, 1): [(12, 1), (11, 1), (10, 1)], (4, 1): [(5, 1), (4, 1), (3, 1)], (1, 1): [(1, 2), (1, 1)], (12, 3): [(13, 3), (12, 3), (11, 3)], (8, 2): [(8, 1), (9, 2), (8, 2), (7, 2)], (7, 1): [(7, 2), (8, 1), (7, 1), (6, 1)], (9, 3): [(9, 2), (10, 3), (9, 3)], (2, 3): [(3, 3), (2, 3), (1, 3)], (10, 1): [(10, 2), (11, 1), (10, 1)], (5, 3): [(6, 3), (5, 3), (4, 3)], (13, 1): [(14, 1), (13, 1), (12, 1)], (9, 2): [(9, 3), (10, 2), (9, 2), (8, 2)], (6, 1): [(7, 1), (6, 1), (5, 1)], (3, 1): [(4, 1), (3, 1)], (11, 3): [(12, 3), (11, 3), (10, 3)], (16, 1): [(16, 2), (16, 1), (15, 1)], (4, 3): [(5, 3), (4, 3), (3, 3)], (14, 3): [(14, 3), (13, 3)], (10, 2): [(10, 3), (10, 1), (10, 2), (9, 2)]} self.assertEqual(adjacency_target, al)
def test_noise_manhattan_failure(self): test_layout = """ ################## ########## . 2 # ########## #####3# ###0###### . . 1# ################## """ # noiser should not find a connection universe = create_CTFUniverse(test_layout, 4) positions = [b.current_pos for b in universe.bots] noiser = ManhattanNoiser(universe.copy()) new_uni = noiser.uniform_noise(universe.copy(), 0) new_positions = [b.current_pos for b in new_uni.bots] # assume not all bots (except 0 and 2) are in the original position anymore self.assertEqual(positions[0::2], new_positions[0::2]) self.assertNotEqual(positions[1::2], new_positions[1::2], "Testing randomized function, may fail sometimes.")
def test_bot_ids(self): layout = """ #### #01# #### """ dummy_universe = create_CTFUniverse(layout, 2) team1 = SimpleTeam(TestPlayer([north]), TestPlayer([east])) self.assertRaises(ValueError, team1._set_bot_ids, [1, 5, 10]) team1._set_bot_ids([1, 5]) team1._set_initial(dummy_universe) self.assertEqual(team1._get_move(1, dummy_universe), north) self.assertEqual(team1._get_move(5, dummy_universe), east) self.assertRaises(KeyError, team1._get_move, 6, dummy_universe) team2 = SimpleTeam(TestPlayer([north]), TestPlayer([east])) team2._set_bot_ids([1]) team2._set_initial(dummy_universe) self.assertEqual(team2._get_move(1, dummy_universe), north) self.assertRaises(KeyError, team2._get_move, 0, dummy_universe) self.assertRaises(KeyError, team2._get_move, 2, dummy_universe)
def test_pos_within(self): test_layout = ( """ ################## #0#. . # . # #2##### #####1# # . # . .#3# ################## """) universe = create_CTFUniverse(test_layout, 4) al = AdjacencyList(universe) free = set(universe.maze.pos_of(Free)) self.assertRaises(NoPositionException, al.pos_within, (0, 0), 0) self.assertRaises(NoPositionException, al.pos_within, (6, 2), 0) self.assertEqual(set([(1, 1)]), al.pos_within((1, 1), 0)) target = set([(1, 1), (1, 2), (1,3), (2, 3), (3, 3), (3, 3)]) self.assertEqual(target, al.pos_within((1, 1), 5)) # assuming a_star is working properly for pos in target: self.assertTrue(len(al.a_star((1, 1), pos)) < 5) for pos in free.difference(target): self.assertTrue(len(al.a_star((1, 1), pos)) >= 5)
def test_bot_ids(self): layout = ( """ #### #01# #### """ ) dummy_universe = create_CTFUniverse(layout, 2) team1 = SimpleTeam(TestPlayer('^'), TestPlayer('>')) dummy_universe.teams[0].bots = [1, 5, 10] self.assertRaises(ValueError, team1.set_initial, 0, dummy_universe, {}) dummy_universe.teams[0].bots = [1, 5] team1.set_initial(0, dummy_universe, {}) self.assertEqual(team1.get_move(1, dummy_universe, {}), {"move": north, "say": ""}) self.assertEqual(team1.get_move(5, dummy_universe, {}), {"move": east, "say": ""}) self.assertRaises(KeyError, team1.get_move, 6, dummy_universe, {}) team2 = SimpleTeam(TestPlayer('^'), TestPlayer('>')) team2.set_initial(1, dummy_universe, {}) self.assertEqual(team2.get_move(1, dummy_universe, {}), {"move": north, "say": ""}) self.assertRaises(KeyError, team2.get_move, 0, dummy_universe, {}) self.assertRaises(KeyError, team2.get_move, 2, dummy_universe, {})