本文整理汇总了Python中pelita.graph.AdjacencyList.bfs方法的典型用法代码示例。如果您正苦于以下问题:Python AdjacencyList.bfs方法的具体用法?Python AdjacencyList.bfs怎么用?Python AdjacencyList.bfs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pelita.graph.AdjacencyList
的用法示例。
在下文中一共展示了AdjacencyList.bfs方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BorderPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [as 别名]
class BorderPlayer(AbstractPlayer):
""" A player that makes moves at random. """
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
def get_move(self):
border_path = self.find_path(self.team_border)
self.say("Border!!!!")
if len(border_path)==0:
return stop
if border_path==None:
return stop
return diff_pos(self.current_pos, border_path.pop())
示例2: test_bfs_to_self
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [as 别名]
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)])
示例3: test_bfs_to_self
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [as 别名]
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)]))
示例4: test_path_to_same_position
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [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)])
示例5: test_bfs_exceptions
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [as 别名]
def test_bfs_exceptions(self):
test_layout = (
""" ############
#0. #.1#
############ """)
universe = CTFUniverse.create(test_layout, 2)
al = AdjacencyList(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)])
示例6: BFSPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [as 别名]
class BFSPlayer(AbstractPlayer):
""" This player uses breadth first search to always go to the closest food.
This player uses an adjacency list [1] to store the topology of the
maze. It will then do a breadth first search [2] to search for the
closest food. When found, it will follow the determined path until it
reaches the food. This continues until all food has been eaten or the
enemy wins.
The adjacency lits representation (`AdjacencyList`) and breadth first search
(`AdjacencyList.bfs`) are imported from `pelita.graph`.
* [1] http://en.wikipedia.org/wiki/Adjacency_list
* [2] http://en.wikipedia.org/wiki/Breadth-first_search
"""
def set_initial(self):
# Before the game starts we initialise our adjacency list.
self.adjacency = AdjacencyList(self.current_uni)
self.current_path = self.bfs_food()
def bfs_food(self):
""" Breadth first search for food.
Returns
-------
path : a list of tuples (int, int)
The positions (x, y) in the path from the current position to the
closest food. The first element is the final destination.
"""
try:
return self.adjacency.bfs(self.current_pos, self.enemy_food)
except NoPathException:
return [self.current_pos]
def get_move(self):
if self.current_pos == self.initial_pos:
# we have probably been killed
# reset the path
self.current_path = None
if not self.current_path:
self.current_path = self.bfs_food()
new_pos = self.current_path.pop()
return diff_pos(self.current_pos, new_pos)
示例7: OurPlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [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:
#.........这里部分代码省略.........
示例8: BasicDefensePlayer
# 需要导入模块: from pelita.graph import AdjacencyList [as 别名]
# 或者: from pelita.graph.AdjacencyList import bfs [as 别名]
class BasicDefensePlayer(AbstractPlayer):
""" A crude defensive player.
Will move towards the border, and as soon as it notices enemies in its
territory, it will start to track them. When it kills the enemy it returns to
the border and waits there for more.
"""
def set_initial(self):
self.adjacency = AdjacencyList(self.current_uni)
self.path = self.path_to_border
self.tracking = None
@property
def path_to_border(self):
""" Path to the closest border position. """
return self.adjacency.bfs(self.current_pos, self.team_border)
@property
def path_to_target(self):
""" Path to the target we are currently tracking. """
return self.adjacency.a_star(self.current_pos,
self.tracking_target.current_pos)
@property
def tracking_target(self):
""" Bot object we are currently tracking. """
return self.current_uni.bots[self.tracking]
def get_move(self):
# if we were killed, for whatever reason, reset the path
if self.current_pos == self.initial_pos:
self.current_path = self.path_to_border
# if we are not currently tracking anything
if not self.tracking:
# check the enemy positions
possible_targets = [enemy for enemy in self.enemy_bots
if self.team.in_zone(enemy.current_pos)]
if possible_targets:
# get the path to the closest one
closest_enemy = min([(len(self.adjacency.a_star(self.current_pos,
enemy.current_pos)),enemy) for enemy in possible_targets])
# track that bot by using its index
self.tracking = closest_enemy[1].index
else:
# otherwise keep going if we aren't already underway
if not self.path:
self.path = self.path_to_border
elif self.tracking:
# if the enemy is no longer in our zone
if not self.team.in_zone(self.tracking_target.current_pos):
self.tracking = None
self.path = self.path_to_border
# otherwise update the path to the target
else:
self.path = self.path_to_target
# if something above went wrong, just stand still
if not self.path:
return stop
else:
return diff_pos(self.current_pos, self.path.pop())