Exemplo n.º 1
0
    def test_noise_manhattan_failure(self):
        test_layout = (""" ##################
            ########## . 2   #
            ########## #####3#
            ###0###### .  . 1#
            ################## """)
        # noiser should not crash when it does not find a connection
        universe = CTFUniverse.create(test_layout, 4)

        positions = [b.current_pos for b in universe.bots]

        positions = [b.current_pos for b in universe.bots]
        team_positions = []
        enemy_positions = []

        # We try it a few times to avoid coincidental failure
        RANDOM_TESTS = 3
        for i in range(RANDOM_TESTS):
            noiser = ManhattanNoiser(universe.copy())
            new_uni = noiser.uniform_noise(universe.copy(), 0)
            new_positions = [b.current_pos for b in new_uni.bots]

            team_positions += new_positions[0::2]
            enemy_positions += new_positions[1::2]

        # assume not all bots (except 0 and 2) are in the original position anymore
        self.assertEqual(set(positions[0::2]), set(team_positions))
        self.assertNotEqual(
            set(positions[1::2]), set(enemy_positions),
            "Testing randomized function, may fail sometimes.")
Exemplo n.º 2
0
    def test_uniform_noise_4_bots_no_noise_a_star(self):
        test_layout = (
        """ ##################
            # #.  .  # . 2   #
            # #####    #####3#
            #  0  . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(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.values()))
        self.assertEqual(100, sum(position_bucket_2.values()))
        # 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.
        six.assertCountEqual(self, position_bucket_0, expected_0, position_bucket_0)

        # bots should never have been noised
        self.assertEqual(100, position_bucket_2[bot_2_pos])
Exemplo n.º 3
0
    def test_bot_ids(self):
        layout = (""" ####
                #01#
                #### """)
        dummy_universe = CTFUniverse.create(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, {})
Exemplo n.º 4
0
    def test_uniform_noise_4_bots_a_star(self):
        test_layout = (""" ##################
            # #. 2.  # .     #
            # #####    #####3#
            #  0  . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(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)

        expected_2 = [(7, 3), (8, 2), (7, 1), (8, 1), (6, 1), (3, 1), (5, 1),
                      (4, 1), (7, 2)]
        position_bucket_2 = collections.defaultdict(int)

        for i in range(100):
            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(100, sum(position_bucket_0.values()))
        self.assertEqual(100, sum(position_bucket_2.values()))
        # 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.assertCountEqual(position_bucket_0, expected_0,
                              sorted(position_bucket_0.keys()))
        self.assertCountEqual(position_bucket_2, expected_2,
                              sorted(position_bucket_2.keys()))
Exemplo n.º 5
0
    def test_pos_within(self):
        test_layout = (""" ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())
        free = {pos for pos, val in universe.maze.items() if not val}

        assert not ((0, 0) in al)
        with pytest.raises(NoPathException):
            al.pos_within((0, 0), 0)
        assert not ((6, 2) in al)
        with pytest.raises(NoPathException):
            al.pos_within((6, 2), 0)

        assert (1, 1) in al
        unittest.TestCase().assertCountEqual([(1, 1)], al.pos_within((1, 1),
                                                                     0))
        target = [(1, 1), (1, 2), (1, 3), (2, 3), (3, 3)]
        unittest.TestCase().assertCountEqual(target, al.pos_within((1, 1), 5))
        # assuming a_star is working properly
        for pos in target:
            assert len(al.a_star((1, 1), pos)) < 5
        for pos in free.difference(target):
            assert len(al.a_star((1, 1), pos)) >= 5
Exemplo n.º 6
0
    def test_uniform_noise_4_bots_manhattan(self):
        test_layout = (
        """ ##################
            # #. 2.  # .     #
            # #####    #####3#
            #   0  . # .  .#1#
            ################## """)
        universe = CTFUniverse.create(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.values()))
        self.assertEqual(200, sum(position_bucket_2.values()))
        # 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.
        six.assertCountEqual(self, position_bucket_0, expected_0, sorted(position_bucket_0.keys()))
        six.assertCountEqual(self, position_bucket_2, expected_2, sorted(position_bucket_2.keys()))
Exemplo n.º 7
0
    def test_pos_within(self):
        test_layout = (
        """ ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())
        free = {pos for pos, val in universe.maze.items() if not val}

        assert not ((0, 0) in al)
        with pytest.raises(NoPathException):
            al.pos_within((0, 0), 0)
        assert not ((6, 2) in al)
        with pytest.raises(NoPathException):
            al.pos_within((6, 2), 0)

        assert (1, 1) in al
        unittest.TestCase().assertCountEqual([(1, 1)], al.pos_within((1, 1), 0))
        target = [(1, 1), (1, 2), (1,3), (2, 3), (3, 3)]
        unittest.TestCase().assertCountEqual(target, al.pos_within((1, 1), 5))
        # assuming a_star is working properly
        for pos in target:
            assert len(al.a_star((1, 1), pos)) < 5
        for pos in free.difference(target):
            assert len(al.a_star((1, 1), pos)) >= 5
Exemplo n.º 8
0
    def test_uniform_noise_manhattan(self):
        test_layout = """ ##################
            # #.  .  # .     #
            # #####    ##### #
            #  0  . #  .  .#1#
            ################## """
        universe = CTFUniverse.create(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)
Exemplo n.º 9
0
    def test_a_star(self):
        test_layout = (""" ##################
            #02.# .  # .  #  #
            #   ###    ####1 #
            # ### . #  .  ##3#
            #                #
            ################## """)

        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())

        #Test distance to middle from both sides
        assert 11 == len(al.a_star((1, 1), (7, 2)))
        assert 12 == len(al.a_star((2, 1), (7, 2)))
        assert 14 == len(al.a_star((16, 1), (7, 2)))
        assert 15 == len(al.a_star((15, 1), (7, 2)))

        # Test basic assertions
        assert 0 == len(al.a_star((1, 1), (1, 1)))
        assert 1 == len(al.a_star((1, 1), (2, 1)))
        assert 1 == len(al.a_star((2, 1), (1, 1)))

        # Test distance to middle from both sides
        assert 11 == len(al.a_star((1, 1), (7, 2)))
        assert 12 == len(al.a_star((2, 1), (7, 2)))
        assert 14 == len(al.a_star((16, 1), (7, 2)))
        assert 15 == len(al.a_star((15, 1), (7, 2)))
Exemplo n.º 10
0
    def test_a_star(self):
        test_layout = (
        """ ##################
            #02.# .  # .  #  #
            #   ###    ####1 #
            # ### . #  .  ##3#
            #                #
            ################## """)

        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())

        #Test distance to middle from both sides
        assert 11 == len(al.a_star((1, 1), (7, 2)))
        assert 12 == len(al.a_star((2, 1), (7, 2)))
        assert 14 == len(al.a_star((16, 1), (7, 2)))
        assert 15 == len(al.a_star((15, 1), (7, 2)))    

        # Test basic assertions
        assert 0 == len(al.a_star((1, 1), (1, 1)))
        assert 1 == len(al.a_star((1, 1), (2, 1)))
        assert 1 == len(al.a_star((2, 1), (1, 1)))

        # Test distance to middle from both sides
        assert 11 == len(al.a_star((1, 1), (7, 2)))
        assert 12 == len(al.a_star((2, 1), (7, 2)))
        assert 14 == len(al.a_star((16, 1), (7, 2)))
        assert 15 == len(al.a_star((15, 1), (7, 2)))
Exemplo n.º 11
0
    def test_pos_within(self):
        test_layout = (
        """ ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())
        free = set(universe.maze.pos_of(Free))

        self.assertFalse((0, 0) in al)
        self.assertRaises(NoPathException, al.pos_within, (0, 0), 0)
        self.assertFalse((6, 2) in al)
        self.assertRaises(NoPathException, al.pos_within, (6, 2), 0)

        self.assertTrue((1, 1) in al)
        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)
Exemplo n.º 12
0
    def test_pos_within(self):
        test_layout = (
        """ ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())
        free = set(pos for pos, val in universe.maze.items() if not val)

        self.assertFalse((0, 0) in al)
        self.assertRaises(NoPathException, al.pos_within, (0, 0), 0)
        self.assertFalse((6, 2) in al)
        self.assertRaises(NoPathException, al.pos_within, (6, 2), 0)

        self.assertTrue((1, 1) in al)
        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)
Exemplo n.º 13
0
    def test_uniform_noise_4_bots_no_noise_manhattan(self):
        test_layout = (
        """ ##################
            # #.  .  # . 2   #
            # #####    #####3#
            #  0  . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        noiser = ManhattanNoiser(universe.copy())

        expected_0 = [ (1, 1), (3, 1), (4, 1), (5, 1), (6, 1),
                       (1, 2), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3),
                       (6, 3), (7, 3), (7, 2) ]
        position_bucket_0 = collections.defaultdict(int)

        bot_2_pos = (13, 1)
        position_bucket_2 = {bot_2_pos : 0}

        for i in range(200):
            new = noiser.uniform_noise(universe.copy(), 1)
            assert new.bots[0].noisy
            assert not new.bots[2].noisy
            position_bucket_0[new.bots[0].current_pos] += 1
            position_bucket_2[new.bots[2].current_pos] += 1
        assert 200 == sum(position_bucket_0.values())
        assert 200 == sum(position_bucket_2.values())
        # 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.
        unittest.TestCase().assertCountEqual(position_bucket_0, expected_0, position_bucket_0)

        # bots should never have been noised
        assert 200 == position_bucket_2[bot_2_pos]
Exemplo n.º 14
0
 def test_bfs_to_self(self):
     test_layout = (""" ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     self.assertEqual([], al.bfs((1, 1), [(1, 1), (2, 1)]))
Exemplo n.º 15
0
    def test_noise_manhattan_failure(self):
        test_layout = (
        """ ##################
            ########## . 2   #
            ########## #####3#
            ###0###### .  . 1#
            ################## """)
        # noiser should not crash when it does not find a connection
        universe = CTFUniverse.create(test_layout, 4)

        positions = [b.current_pos for b in universe.bots]

        positions = [b.current_pos for b in universe.bots]
        team_positions = []
        enemy_positions = []

        # We try it a few times to avoid coincidental failure
        RANDOM_TESTS = 3
        for i in range(RANDOM_TESTS):
            noiser = ManhattanNoiser(universe.copy())
            new_uni = noiser.uniform_noise(universe.copy(), 0)
            new_positions = [b.current_pos for b in new_uni.bots]

            team_positions += new_positions[0::2]
            enemy_positions += new_positions[1::2]

        # assume not all bots (except 0 and 2) are in the original position anymore
        assert set(positions[0::2]) == set(team_positions)
        assert set(positions[1::2]) != set(enemy_positions), \
                            "Testing randomized function, may fail sometimes."
Exemplo n.º 16
0
    def test_uniform_noise_4_bots_no_noise_manhattan(self):
        test_layout = (""" ##################
            # #.  .  # . 2   #
            # #####    #####3#
            #  0  . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        noiser = ManhattanNoiser(universe.copy())

        expected_0 = [(1, 1), (3, 1), (4, 1), (5, 1), (6, 1), (1, 2), (1, 3),
                      (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (7, 2)]
        position_bucket_0 = collections.defaultdict(int)

        bot_2_pos = (13, 1)
        position_bucket_2 = {bot_2_pos: 0}

        for i in range(200):
            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(200, sum(position_bucket_0.values()))
        self.assertEqual(200, sum(position_bucket_2.values()))
        # 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.assertCountEqual(position_bucket_0, expected_0, position_bucket_0)

        # bots should never have been noised
        self.assertEqual(200, position_bucket_2[bot_2_pos])
Exemplo n.º 17
0
    def test_too_few_players(self):
        layout = (""" ######
                #0123#
                ###### """)
        dummy_universe = CTFUniverse.create(layout, 4)
        team1 = SimpleTeam(TestPlayer('^'))

        self.assertRaises(ValueError, team1.set_initial, 0, dummy_universe, {})
Exemplo n.º 18
0
 def test_bfs_to_self(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     assert [] == al.bfs((1,1), [(1, 1), (2, 1)])
Exemplo n.º 19
0
 def test_bfs_to_self(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = Graph(universe.free_positions())
     assert [] == al.bfs((1,1), [(1, 1), (2, 1)])
Exemplo n.º 20
0
 def test_a_star_exceptions(self):
     test_layout = (""" ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     self.assertRaises(NoPathException, al.a_star, (1, 1), (10, 1))
     self.assertRaises(NoPathException, al.a_star, (0, 1), (10, 1))
     self.assertRaises(NoPathException, al.a_star, (1, 1), (11, 1))
Exemplo n.º 21
0
 def test_a_star(self):
     test_layout = (""" ##################
         #0#.  .  # .     #
         #2#####    #####1#
         #     . #  .  .#3#
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = AdjacencyList(universe.free_positions())
     # just a simple smoke test
     self.assertEqual(14, len(al.a_star((1, 1), (3, 1))))
Exemplo n.º 22
0
    def test_too_few_players(self):
        layout = (
            """ ######
                #0123#
                ###### """
        )
        dummy_universe = CTFUniverse.create(layout, 4)
        team1 = SimpleTeam(TestPlayer('^'))

        self.assertRaises(ValueError, team1.set_initial, 0, dummy_universe, {})
Exemplo n.º 23
0
 def test_path_to_same_position(self):
     test_layout = (""" ##################
         #0#.  .  # .     #
         #2#####    #####1#
         #     . #  .  .#3#
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = AdjacencyList(universe.free_positions())
     self.assertEqual([], al.a_star((1, 1), (1, 1)))
     self.assertEqual([], al.bfs((1, 1), [(1, 1)]))
Exemplo n.º 24
0
 def test_a_star_exceptions(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     self.assertRaises(NoPathException, al.a_star, (1, 1), (10, 1))
     self.assertRaises(NoPathException, al.a_star, (0, 1), (10, 1))
     self.assertRaises(NoPathException, al.a_star, (1, 1), (11, 1))
Exemplo n.º 25
0
    def test_too_few_players(self):
        layout = (
            """ ######
                #0123#
                ###### """
        )
        dummy_universe = CTFUniverse.create(layout, 4)
        team1 = SimpleTeam(TestPlayer('^'))

        with pytest.raises(ValueError):
            team1.set_initial(0, dummy_universe, {})
Exemplo n.º 26
0
 def test_path_to_same_position(self):
     test_layout = (
     """ ##################
         #0#.  .  # .     #
         #2#####    #####1#
         #     . #  .  .#3#
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = AdjacencyList(universe.free_positions())
     assert [] == al.a_star((1, 1), (1, 1))
     assert [] == al.bfs((1, 1), [(1, 1)])
Exemplo n.º 27
0
 def test_path_to_same_position(self):
     test_layout = (
     """ ##################
         #0#.  .  # .     #
         #2#####    #####1#
         #     . #  .  .#3#
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = Graph(universe.free_positions())
     assert [] == al.a_star((1, 1), (1, 1))
     assert [] == al.bfs((1, 1), [(1, 1)])
Exemplo n.º 28
0
 def test_a_star(self):
     test_layout = (
     """ ##################
         #0#.  .  # .     #
         #2#####    #####1#
         #     . #  .  .#3#
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = AdjacencyList(universe.free_positions())
     # just a simple smoke test
     self.assertEqual(14, len(al.a_star((1, 1), (3, 1))))
Exemplo n.º 29
0
 def test_basic_adjacency_list(self):
     test_layout = (
     """ ######
         #    #
         ###### """)
     universe = CTFUniverse.create(test_layout, 0)
     al = AdjacencyList(universe.free_positions())
     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.assertDictEqual(target, al)
Exemplo n.º 30
0
 def test_a_star_exceptions(self):
     test_layout = (""" ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     with pytest.raises(NoPathException):
         al.a_star((1, 1), (10, 1))
     with pytest.raises(NoPathException):
         al.a_star((0, 1), (10, 1))
     with pytest.raises(NoPathException):
         al.a_star((1, 1), (11, 1))
Exemplo n.º 31
0
 def test_basic_adjacency_list(self):
     test_layout = (
     """ ######
         #    #
         ###### """)
     universe = CTFUniverse.create(test_layout, 0)
     al = AdjacencyList(universe.free_positions())
     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)
Exemplo n.º 32
0
    def test_extended_adjacency_list(self):
        test_layout = (""" ##################
            #0#.  .  # .     #
            # #####    ##### #
            #     . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(test_layout, 2)
        al = AdjacencyList(universe.free_positions())

        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)]
        }

        for val in al.values():
            val.sort()

        for val in adjacency_target.values():
            val.sort()

        self.assertEqual(adjacency_target, al)
Exemplo n.º 33
0
    def test_extended_adjacency_list(self):
        test_layout = (
        """ ##################
            #0#.  .  # .     #
            # #####    ##### #
            #     . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(test_layout, 2)
        al = AdjacencyList(universe.free_positions())

        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)]}

        for val in al.values():
            val.sort()

        for val in adjacency_target.values():
            val.sort()

        assert adjacency_target == al
Exemplo n.º 34
0
 def test_a_star_exceptions(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     with pytest.raises(NoPathException):
         al.a_star((1, 1), (10, 1))
     with pytest.raises(NoPathException):
         al.a_star((0, 1), (10, 1))
     with pytest.raises(NoPathException):
         al.a_star((1, 1), (11, 1))
Exemplo n.º 35
0
 def test_a_star2(self):
     test_layout = (
         """ ########
             #1#    #
             # # #0 #
             #      #
             ######## """ )
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     #Test distance to middle from both sides
     print(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
     print(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
     assert 7 == len(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
     assert 7 == len(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
Exemplo n.º 36
0
 def create_TestUniverse(layout, black_score=0, white_score=0):
     initial_pos = [(1, 1), (4, 2)]
     universe = CTFUniverse.create(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 (1, 2) in universe.food_list:
         universe.teams[1].score += 1
     if not (2, 2) in universe.food_list:
         universe.teams[1].score += 1
     if not (3, 1) in universe.food_list:
         universe.teams[0].score += 1
     return universe
Exemplo n.º 37
0
 def test_a_star2(self):
     test_layout = (
         """ ########
             #1#    #
             # # #0 #
             #      #
             ######## """ )
     universe = CTFUniverse.create(test_layout, 2)
     al = Graph(universe.free_positions())
     #Test distance to middle from both sides
     print(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
     print(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
     assert 7 == len(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
     assert 7 == len(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
Exemplo n.º 38
0
 def create_TestUniverse(layout, black_score=0, white_score=0):
     initial_pos = [(1, 1), (4, 2)]
     universe = CTFUniverse.create(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
Exemplo n.º 39
0
 def create_TestUniverse(layout, black_score=0, white_score=0):
     initial_pos = [(1, 1), (4, 2)]
     universe = CTFUniverse.create(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 (1, 2) in universe.food_list:
         universe.teams[1].score += 1
     if not (2, 2) in universe.food_list:
         universe.teams[1].score += 1
     if not (3, 1) in universe.food_list:
         universe.teams[0].score += 1
     return universe
Exemplo n.º 40
0
 def create_TestUniverse(layout, black_score=0, white_score=0):
     initial_pos = [(1, 1), (4, 2)]
     universe = CTFUniverse.create(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
Exemplo n.º 41
0
 def test_bfs_exceptions(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = Graph(universe.free_positions())
     with pytest.raises(NoPathException):
         al.bfs((1, 1), [(10, 1)])
     with pytest.raises(NoPathException):
         al.bfs((1, 1), [(10, 1), (9, 1)])
     with pytest.raises(NoPathException):
         al.bfs((0, 1), [(10, 1)])
     with pytest.raises(NoPathException):
         al.bfs((1, 1), [(11, 1)])
Exemplo n.º 42
0
    def test_basic_adjacency_list(self):
        test_layout = (
        """ ######
            #    #
            ###### """)
        universe = CTFUniverse.create(test_layout, 0)
        al = AdjacencyList(universe.free_positions())
        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)]}

        for val in al.values():
            val.sort()

        for val in target.values():
            val.sort()

        assert target == al
Exemplo n.º 43
0
    def test_basic_adjacency_list(self):
        test_layout = (
        """ ######
            #    #
            ###### """)
        universe = CTFUniverse.create(test_layout, 0)
        al = Graph(universe.free_positions())
        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)]}

        for val in al.values():
            val.sort()

        for val in target.values():
            val.sort()

        assert target == al
Exemplo n.º 44
0
 def test_a_star3(self):
     test_layout = (
         """
         ################################################################
         #0#                #    #         #                     #   #  #
         # ######### ######           #              #           ###    #
         # #            #   ######## ## ## #  #      #           #   #  #
         #   ############   # # #  #  #         ## ###############      #
         # #            # ### # #     # ###  ##        #         #### ###
         # ####### #### #   #   #  #  #       #                         #
         # #   1      #     ###   ##### ##      ############# ###########
         # #   #      # #   #   #     # ##    #   #                     #
         #    ######################### ##    ## ######### ##############"""
     )
     universe = CTFUniverse.create(test_layout, 2)
     al = Graph(universe.free_positions())
     #Test distance to middle from both sides
     assert 15 == len(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
     assert 15 == len(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
Exemplo n.º 45
0
    def test_uniform_noise_manhattan(self):
        test_layout = (""" ##################
            # #.  .  # .     #
            # #####    ##### #
            #  0  . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(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.values()))
        # 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.assertCountEqual(position_bucket, expected, position_bucket)
Exemplo n.º 46
0
 def test_a_star3(self):
     test_layout = ("""
         ################################################################
         #0#                #    #         #                     #   #  #
         # ######### ######           #              #           ###    #
         # #            #   ######## ## ## #  #      #           #   #  #
         #   ############   # # #  #  #         ## ###############      #
         # #            # ### # #     # ###  ##        #         #### ###
         # ####### #### #   #   #  #  #       #                         #
         # #   1      #     ###   ##### ##      ############# ###########
         # #   #      # #   #   #     # ##    #   #                     #
         #    ######################### ##    ## ######### ##############"""
                    )
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     #Test distance to middle from both sides
     assert 15 == len(
         al.a_star(universe.bots[0].current_pos,
                   universe.bots[1].current_pos))
     assert 15 == len(
         al.a_star(universe.bots[1].current_pos,
                   universe.bots[0].current_pos))
Exemplo n.º 47
0
    def test_bot_ids(self):
        layout = (
            """ ####
                #01#
                #### """
        )
        dummy_universe = CTFUniverse.create(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, {})
Exemplo n.º 48
0
 def len_of_shortest_path(layout):
     uni = CTFUniverse.create(layout, 2)
     al = AdjacencyList(uni.free_positions())
     path = al.a_star(uni.bots[0].current_pos, uni.bots[1].current_pos)
     return len(path)
Exemplo n.º 49
0
 def len_of_shortest_path(layout):
     uni = CTFUniverse.create(layout, 2)
     al = AdjacencyList(uni.free_positions())
     path = al.a_star(uni.bots[0].current_pos, uni.bots[1].current_pos)
     return len(path)