本文整理汇总了Python中pelita.datamodel.CTFUniverse类的典型用法代码示例。如果您正苦于以下问题:Python CTFUniverse类的具体用法?Python CTFUniverse怎么用?Python CTFUniverse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CTFUniverse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_noise_manhattan_failure
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."
示例2: test_uniform_noise_manhattan
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)
示例3: test_uniform_noise_4_bots_manhattan
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()))
示例4: test_uniform_noise_4_bots_no_noise_a_star
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])
示例5: test_pos_within
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)
示例6: test_uniform_noise_4_bots_no_noise_manhattan
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]
示例7: test_a_star_exceptions
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))
示例8: test_too_few_players
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, {})
示例9: test_a_star
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))))
示例10: test_basic_adjacency_list
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)
示例11: create_TestUniverse
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
示例12: create_TestUniverse
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
示例13: test_bfs_exceptions
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)])
示例14: test_a_star3
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))
示例15: test_bot_ids
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, {})