本文整理汇总了Python中pelita.graph.AdjacencyList.a_star方法的典型用法代码示例。如果您正苦于以下问题:Python AdjacencyList.a_star方法的具体用法?Python AdjacencyList.a_star怎么用?Python AdjacencyList.a_star使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pelita.graph.AdjacencyList
的用法示例。
在下文中一共展示了AdjacencyList.a_star方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pos_within
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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)
示例2: test_pos_within
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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
示例3: test_a_star2
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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))
示例4: test_a_star
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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)))
示例5: SmartEatingPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
class SmartEatingPlayer(AbstractPlayer):
def set_initial(self):
self.adjacency = AdjacencyList(self.current_uni.reachable([self.initial_pos]))
self.next_food = None
def goto_pos(self, pos):
return self.adjacency.a_star(self.current_pos, pos)[-1]
def get_move(self):
# check, if food is still present
if (self.next_food is None
or self.next_food not in self.enemy_food):
if not self.enemy_food:
# all food has been eaten? ok. i’ll stop
return datamodel.stop
self.next_food = self.rnd.choice(self.enemy_food)
try:
dangerous_enemy_pos = [bot.current_pos
for bot in self.enemy_bots if bot.is_destroyer]
next_pos = self.goto_pos(self.next_food)
# check, if the next_pos has an enemy on it
if next_pos in dangerous_enemy_pos:
# whoops, better wait this round and take another food next time
self.next_food = None
return datamodel.stop
move = diff_pos(self.current_pos, next_pos)
return move
except NoPathException:
return datamodel.stop
示例6: test_a_star
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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))))
示例7: test_path_to_same_position
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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)])
示例8: test_pos_within
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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)
示例9: OurPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
class OurPlayer(AbstractPlayer):
def set_initial(self):
self.adjacency = AdjacencyList(self.current_uni.reachable([self.initial_pos]))
self.next_food = None
def goto_pos(self, pos):
return self.adjacency.a_star(self.current_pos, pos)[-1]
def get_move(self):
# from SmartRandom
dangerous_enemy_pos = [bot.current_pos
for bot in self.enemy_bots if bot.is_destroyer]
killable_enemy_pos = [bot.current_pos
for bot in self.enemy_bots if bot.is_harvester]
# easy kill (please test)
for killable in killable_enemy_pos:
if killable in self.legal_moves.items():
move = diff_pos(self.current_pos, killable)
self.say("Easy kill!")
return move
#pdb.set_trace()
# panic
# for dangerous in dangerous_enemy_pos:
# if killable in self.legal_moves.items():
# move = diff_pos(self.current_pos, killable)
# self.say("Easy kill!")
# return move
# check, if food is still present
# if the nearest is not suitable, choose one at random!
if (self.next_food is None
or self.next_food not in self.enemy_food):
if not self.enemy_food:
# all food has been eaten? ok. i’ll stop
return datamodel.stop
# SUBOPTIMAL (chooses at random)
self.next_food = self.rnd.choice(self.enemy_food)
try:
next_pos = self.goto_pos(self.next_food)
move = diff_pos(self.current_pos, next_pos)
return move
except NoPathException:
return datamodel.stop
示例10: test_a_star_exceptions
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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))
示例11: FoodEatingPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
class FoodEatingPlayer(AbstractPlayer):
def set_initial(self):
self.adjacency = AdjacencyList(self.current_uni.reachable([self.initial_pos]))
self.next_food = None
def goto_pos(self, pos):
return self.adjacency.a_star(self.current_pos, pos)[-1]
def get_move(self):
# check, if food is still present
if self.next_food is None or self.next_food not in self.enemy_food:
if not self.enemy_food:
# all food has been eaten? ok. i’ll stop
return datamodel.stop
self.next_food = self.rnd.choice(self.enemy_food)
try:
next_pos = self.goto_pos(self.next_food)
move = diff_pos(self.current_pos, next_pos)
return move
except NoPathException:
return datamodel.stop
示例12: HungryPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
class HungryPlayer(AbstractPlayer):
""" Basically a clone of the FoodEatingPlayer. """
def __init__(self):
# Do some basic initialisation here. You may also accept additional
# parameters which you can specify in your factory.
# Note that any other game variables have not been set yet. So there is
# no ``self.current_uni`` or ``self.current_state``
pass
def set_initial(self):
# Now ``self.current_uni`` and ``self.current_state`` are known.
# ``set_initial`` is always called before ``get_move``, so we can do some
# additional initialisation here
# Initialize an AdjacencyList for all reachable positions
# This will help us find shortest paths
# see in graph.py for more details
self.adjacency = AdjacencyList(self.current_uni.reachable([self.initial_pos]))
# Once we have picked a food item to go to, we’ll note it here
# Otherwise we risk flapping between two states
self.next_food = None
def path_to(self, pos):
""" Given a position, this return a shortest path from the current position. """
return self.adjacency.a_star(self.current_pos, pos)
def get_move(self):
# check, if food is still present, otherwise go somewhere else
if (self.next_food is None
or self.next_food not in self.enemy_food):
if not self.enemy_food:
# all food has been eaten? ok. i’ll stop
self.say("I am hungry.")
return stop
# all the food is in self.enemy_food
# we just pick one to go to
# (of course, there may be a smarter choice than just going random)
self.next_food = self.rnd.choice(self.enemy_food)
try:
# figure out the path to take
shortest_path = self.path_to(self.next_food)
# our next position is the last element in the path
next_pos = shortest_path[-1]
# we are a little exited about eating
# (this does not account for any food we additionally eat on our way
# to the food we have picked.)
if len(shortest_path) == 1:
self.say("Yay. Food next.")
else:
self.say("Eating in {0} steps.".format(len(shortest_path)))
# should we check for the enemy at this position?
# self.enemy_bots ?
# Naah – we risk it :)
# the difference between here and there
# is the direction we need to go to
move = diff_pos(self.current_pos, next_pos)
return move
except NoPathException:
# whoops, there is no path possible
# we better wait
return stop
示例13: UniverseNoiser
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
class UniverseNoiser(object):
""" Class to make bot positions noisy.
Supports uniform noise in maze space. Can be extended to support other types
of noise. Noise will only be applied if the enemy bot is with a certain
threshold (`sight_distance`).
Parameters
----------
universe : CTFUniverse
the universe which will later be used
noise_radius : int, optional, default: 5
the radius for the uniform noise
sight_distance : int, optional, default: 5
the distance at which noise is no longer applied.
Attributes
----------
adjacency : AdjacencyList
adjacency list representation of the Maze
"""
def __init__(self, universe, noise_radius=5, sight_distance=5):
self.adjacency = AdjacencyList(universe)
self.noise_radius = noise_radius
self.sight_distance = sight_distance
def uniform_noise(self, universe, bot_index):
""" Apply uniform noise to the enemies of a Bot.
Given a `bot_index` the method looks up the enemies of this bot. It then
adds uniform noise in maze space to the enemy positions. If a position
is noisy or not is indicated by the `noisy` attribute in the Bot class.
The method will modify the reference, therefore it is important to use a
copy of the universe as an argument.
Parameters
----------
universe : CTFUniverse
the universe to add noise to
bot_index : int
the bot whose enemies should be noisy
Returns
-------
noisy_universe : CTFUniverse
universe with noisy enemy positions
"""
bot = universe.bots[bot_index]
bots_to_noise = universe.enemy_bots(bot.team_index)
for b in bots_to_noise:
# Check that the distance between this bot and the enemy is larger
# than `sight_distance`.
if len(self.adjacency.a_star(bot.current_pos, b.current_pos)) > self.sight_distance:
# If so then alter the position of the enemy
possible_positions = list(self.adjacency.pos_within(b.current_pos,
self.noise_radius))
b.current_pos = random.choice(possible_positions)
b.noisy = True
return universe
示例14: OurPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
class OurPlayer(AbstractPlayer):
b = "Bonnie"
c = "Clyde"
message_list = [\
(c, "This here's Miss Bonnie Parker. I'm Clyde Barrow. We rob banks."), \
(b, "Whooee! A man with a record!"), \
(c, "Hell, you might just be the best damn girl in Texas."), \
(b, "Go for it Clyde"), \
(b, "You're good!"), \
(c, "I ain't good. I'm the best!"), \
(b, "And modest."), \
(b, "Hey, that ain't ours!"), \
(c, "Sure it is."), \
(b, "But we come in this one."), \
(c, "That don't mean we have to go home in it!"), \
(c, "Next time, I'll aim a little lower!"), \
(b, "Tell them I don't smoke cigars."), \
(c, "'Least I ain't a liar."), \
(c, "We got a dollar ninety-eight, and you're laughing!"), \
]
talkcounter = 0
def __init__(self, name):
self.name = name
self.mess = ""
def set_initial(self):
self.current_strategy = 0
self.round_index = None
self.score_history = np.zeros([2, 300])
self.tracking_idx = None
self.path = []
self.memory = deque([], maxlen = 5)
self.chase_mode = False
self.border_mode = True
self.chase_count = 0
self.FOOD_MIN = len(self.enemy_food)/3
def find_path(self, thingslist):
""" finds the path to the nearest object
*thingslist* - list of tuples with objects positions
"""
self.adjacency = AdjacencyList(self.current_uni.free_positions())
try:
pathd = self.adjacency.bfs(self.current_pos, thingslist)
except NoPathException:
return None
return pathd
def read_score(self):
self.score_history[0, self.round_index] = self.current_uni.teams[0].score
self.score_history[1, self.round_index] = self.current_uni.teams[1].score
@property
def tracking_target(self):
""" Bot object we are currently tracking. """
return self.current_uni.bots[self.tracking_idx]
@property
def path_to_target(self):
""" Path to the target we are currently tracking. """
self.adjacency = AdjacencyList(self.current_uni.free_positions())
try:
return self.adjacency.a_star(self.current_pos,
self.tracking_target.current_pos)
except NoPathException:
return None
def talk(self):
mess = OurPlayer.message_list[0]
if mess[0] == self.name:
OurPlayer.message_list = OurPlayer.message_list[1:]
OurPlayer.message_list.append(mess)
string = mess[0] + ": " + mess[1]
self.mess = string
def start_chase(self):
#self.say("Chase him!")
self.chase_mode = True
self.chase_count += 1
if self.partner:
self.partner.chase_mode = True
self.partner.chase_count += 1
def stop_chase(self):
#self.say("Stopit!")
self.chase_mode = False
if self.partner:
self.partner.chase_mode = False
def go_for_border(self):
if (self.me.index==0 or self.me.index==1) and self.border_mode:
bor_u = [x for x in self.team_border if x[1]>x[0]//2 ]
border_path = self.find_path(bor_u)
elif (self.me.index==2 or self.me.index==3) and self.border_mode:
bor_d = [x for x in self.team_border if x[1]<=x[0]//2]
border_path = self.find_path(bor_d)
else:
border_path = self.find_path(self.team_border)
if len(border_path)==0:
#.........这里部分代码省略.........
示例15: len_of_shortest_path
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import a_star [as 别名]
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)