本文整理汇总了Python中PriorityQueue.PriorityQueue.get方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.get方法的具体用法?Python PriorityQueue.get怎么用?Python PriorityQueue.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import get [as 别名]
def search(self, startHex, goalHex):
startNode = HexNode(startHex, None)
goalNode = HexNode(goalHex, None)
frontier = PriorityQueue()
frontier.put(startNode, 0)
cameFrom = {}
currCost = {}
cameFrom[startNode] = None
currCost[startNode] = 0
while not frontier.empty():
currNode = frontier.get()
if currNode.h == goalNode.h:
break
for nextNode in self.getNeighborNodes(currNode.h):
newCost = currCost[currNode] + self.cost(currNode, nextNode)
if nextNode not in currCost or newCost < currCost[nextNode]:
currCost[nextNode] = newCost
priority = newCost + HexMap.heuristic(goalNode, nextNode)
frontier.put(nextNode, priority)
cameFrom[nextNode] = currNode
return cameFrom, currCost
示例2: a_star_search
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import get [as 别名]
def a_star_search(self, start, goal):
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = start
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current.position_x == goal.position_x and current.position_y == goal.position_y:
break
for next in self.neighbors(current):
next_cell = Cell(next[0], next[1])
if next_cell.position_x == goal.position_x and next_cell.position_y == goal.position_y:
next_cell = goal
new_cost = cost_so_far[current] + self.heuristic(current, next_cell)
if next_cell not in cost_so_far or new_cost < cost_so_far[next_cell]:
cost_so_far[next_cell] = new_cost
priority = new_cost + self.heuristic(goal, next_cell)
frontier.put(next_cell, priority)
came_from[next_cell] = current
return came_from # , cost_so_far
示例3: dijkstra
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import get [as 别名]
def dijkstra(self, G, start, end=None):
D = {} # dictionary of final distances
P = {} # dictionary of predecessors
Q = PriorityQueue() # estimated distances of non-final vertices
Q[start] = 0 # add zero cost
for v in Q:
D[v] = Q[v]
if v == end:
break
for w in G.getVertex(v).adjacencies:
vwLength = D.get(v) + G.vertexAdjacencies(v).get(w).getcost()
if w in D:
if vwLength < D.get(w):
raise ValueError("Dijkstra: found better path to already-final vertex")
elif w not in Q or vwLength < Q.get(w):
Q[w] = vwLength
P[w] = v
return D, P
示例4: __init__
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import get [as 别名]
class Game:
""" Defines a running instance of a Murder Mystery.
Handles the initialization of the various game states.
"""
def __init__(self, abilities, players):
self.gm = players[0]
self.errorAbility = ErrorAbility()
self.eventQueue = PriorityQueue()
self.parser = Parser()
self.inbox = Inbox()
self.outbox = Outbox()
self.players = {}
for player in players[1:]:
self.players[player.getName().lower()] = player
self.abilities = {}
for ability in abilities:
self.abilities[ability.getName().lower()] = ability
def getGameMaster(self):
return self.gm
def addEvent(self, event):
""" Schedules an event to be run.
"""
self.eventQueue.put(event)
def addEvents(self, events):
""" Schedules a list of events to be run.
"""
if(events):
for event in events:
self.eventQueue.put(event)
def removeEvent(self, event):
""" Removes a specific event from the priority queue
"""
self.eventQueue.remove(event)
def getOutbox(self):
return self.outbox
def isValidAbility(self, name):
return name.lower() in self.abilities
def getAbility(self, name):
return self.abilities[name.lower()]
def getAbilityNames(self):
return self.abilities.keys()
def isValidPlayer(self, name):
return name.lower() in self.players
def getPlayer(self, name):
return self.players[name.lower()]
def getPlayerNames(self):
return self.players.keys()
def removePlayer(self, name):
if( self.isValidPlayer(name) ):
del self.players[name.lower()]
def run(self):
""" Run the game, and Don't stop.
Ever.
"""
print "Starting the main loop!"
while( True ):
self.step()
time.sleep(5)
def step(self):
""" Perform one step of the game logic.
You need to call this repeatedly to make the game run.
"""
#process incoming messages
newMessages = self.inbox.poll()
commands = self.parser.parse(self.abilities.values(), newMessages, self.errorAbility)
for (sender,ability,args) in commands:
print "Handling '"+ability.getName()+"' for '"+sender+"'"
for player in self.players.values():
if( player.getContact() == sender ):
print "\tRunning the ability!"
self.addEvents(ability.getEventsFor(self, player, args))
break
#Process the queue of events
while( not self.eventQueue.empty() ):
event = self.eventQueue.get()
if( event.when() < datetime.now() ):
print "Performing an event"
self.addEvents(event.perform(self))
else:
#Doesn't support peeking, so shove it back in the queue if it
#shouldn't happen yet
self.eventQueue.put(event)
break
示例5: PathFinder
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import get [as 别名]
class PathFinder(object):
def __init__(self, bzrc, tank_index):
self.bzrc = bzrc
self.points = []
self.edges = []
self.visibility_graph = None
self.frontier = []
self.visited = []
self.path = []
self.search_snapshots = []
self.create_visibility_graph(tank_index)
def get_path(self):
return self.get_a_star_path()
########################
### VISIBILITY GRAPH ###
########################
def create_visibility_graph(self, tank_index):
print "Generating Visibility Graph for Tank", tank_index
tank = self.bzrc.get_mytanks()[tank_index]
self.points = []
# append tank position to points, this MUST be the first point in this list (we're relying on that for
# our search algorithms below)
self.points.append((tank.x, tank.y))
# append goal flag position to points
flags = self.bzrc.get_flags()
for flag in flags:
if tank.flag != '-' and flag.color in tank.callsign: # if the tank has the flag, go home
self.points.append((flag.x, flag.y)) # TODO: this line only works if the other team hasn't already taken our flag and run with it. We should probably use our base instead...
break
elif flag.color not in tank.callsign: # if the tank has no flag, go for one
self.points.append((flag.x, flag.y))
break
# append obstacle corners to points, and obstacle edges to our list of edges
for obstacle in self.bzrc.get_obstacles():
i = 0
for point in obstacle:
self.points.append(point)
self.edges.append([point, obstacle[(i+1) % len(obstacle)]])
i += 1
# initialize the visibility graph to all -1's
length = len(self.points)
self.visibility_graph = [[-1 for _ in range(length)] for _ in range(length)]
# figure out the visibility between each pair of points
for col in range(length):
for row in range(length):
if self.visibility_graph[row][col] == -1: # we haven't considered this pair of points yet
if self.is_visible(self.points[col], self.points[row]):
# "if you are visible to me, than I am visible to you" mentality
self.visibility_graph[row][col] = 1
self.visibility_graph[col][row] = 1
else:
# "if you are not visible to me, than I am not visible to you" mentality
self.visibility_graph[row][col] = 0
self.visibility_graph[col][row] = 0
# remove any edges that are on the very edge of the map (and therefore couldn't be traversed by the tank)
self.remove_visibility_on_world_edges()
def update_visibility_graph(self, tank_index):
tank = self.bzrc.get_mytanks()[tank_index]
self.points[0] = (tank.x, tank.y) # update the tank position
# update goal flag position in points
flags = self.bzrc.get_flags()
for flag in flags:
if tank.flag != '-' and flag.color in tank.callsign: # if the tank has the flag, go home
self.points[1] = (flag.x, flag.y) # TODO: this line only works if the other team hasn't already taken our flag and run with it. We should probably use our base instead...
break
elif tank.flag == '-' and flag.color not in tank.callsign: # if the tank has no flag, go for one
self.points[1] = (flag.x, flag.y)
break
# update the first two rows and columns in our visibility graph (these are the only points that have changed)
# initialize the visibility graph to all -1's
length = len(self.points)
# figure out the visibility between each pair of points
for col in range(length):
for row in range(length):
if col > 1 and row > 1:
continue
if self.is_visible(self.points[col], self.points[row]):
# "if you are visible to me, than I am visible to you" mentality
self.visibility_graph[row][col] = 1
self.visibility_graph[col][row] = 1
else:
# "if you are not visible to me, than I am not visible to you" mentality
#.........这里部分代码省略.........