本文整理汇总了Python中PriorityQueue.PriorityQueue.isEmpty方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.isEmpty方法的具体用法?Python PriorityQueue.isEmpty怎么用?Python PriorityQueue.isEmpty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.isEmpty方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def main():
tasks = Dispenser(why_not)
time = 0
cmd, args = get_cmd( COMMANDS )
while cmd != QUIT_CMD:
if cmd == TICK_CMD:
time += 1
if not tasks.isEmpty():
current = tasks.peek()
current.time_left -= 1
if current.time_left == 0:
print( "\nTask '" + current.name + \
"' completed at time " + str( time ) + "." )
tasks.remove()
if not tasks.isEmpty():
print( "New task is '" + tasks.peek().name + "'." )
else:
print( "Nothing else to do." )
else:
print( "Nothing to do." )
elif cmd == ADD_CMD:
new_task = Task( args[ 0 ], int( args[ 1 ] ) )
tasks.insert( new_task )
print( "\nAdded. Current task is '" + tasks.peek().name + \
"'." )
else:
assert True, "PROGRAM ERROR"
cmd, args = get_cmd( COMMANDS )
print( "\nTerminating the simulation." )
示例2: primsAlgorithm
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def primsAlgorithm(graph, start, surface):
for v in graph:
v.setStatus(0)
v.setParent(None)
pq = PriorityQueue()
#heapq.heappush(pq, (0, start))
pq.enQueue((0, start))
while (not pq.isEmpty()):
current = pq.deQueue()[1]
current.setStatus(2)
if(current.getParent() != None):
graph.drawEdge(current.getParent(), current, surface, "yellow")
time.sleep(0.5)
#print(str(current.getParent().getId()) + "->" + str(current.getId()))
for neighbour in current.getNeighbours():
if (neighbour.getStatus() == 0):
neighbour.setStatus(1)
neighbour.setParent(current)
neighbour.setpqWeight(current.neighbours[neighbour])
#print(str(neighbour.getpqWeight()))
pq.enQueue((neighbour.getpqWeight(), neighbour))
elif (neighbour.getStatus() == 1):
if (neighbour.getpqWeight() > current.neighbours[neighbour]):
old = (neighbour.getpqWeight(), neighbour)
neighbour.setpqWeight(current.neighbours[neighbour])
pq.updatePriority(old, (neighbour.getpqWeight(), neighbour))
neighbour.setParent(current)
"""
示例3: prims
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def prims(self, graph, start, canvas):
for v in graph:
v.setStatus(0)
v.setParent(None)
pq = PriorityQueue()
pq.enQueue((0, start))
while (not pq.isEmpty()):
current = pq.deQueue()[1]
current.setStatus(2)
if(current.getParent() != None):
graph.drawEdge(current.getParent(), current, canvas, "yellow")
canvas.update()
time.sleep(0.5)
for neighbour in current.getNeighbours():
if (neighbour.getStatus() == 0):
# Encountered a new vertex
neighbour.setStatus(1)
neighbour.setParent(current)
neighbour.setpqWeight(current.neighbours[neighbour])
pq.enQueue((neighbour.getpqWeight(), neighbour))
elif (neighbour.getStatus() == 1):
if (neighbour.getpqWeight() > current.neighbours[neighbour]):
# Found smaller weight, update accordingly
old = (neighbour.getpqWeight(), neighbour)
neighbour.setpqWeight(current.neighbours[neighbour])
pq.updatePriority(old, (neighbour.getpqWeight(), neighbour))
neighbour.setParent(current)
示例4: dijkstrasAlgorithm
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def dijkstrasAlgorithm(graph, start):
for v in graph:
v.setStatus(0)
#start.setStatus(2)
start.setDistance(0)
pq = PriorityQueue()
#heapq.heappush(pq, (0, start))
pq.enQueue((0, start))
while (not pq.isEmpty()):
current = pq.deQueue()[1]
current.setStatus(2)
#if(current.getParent() != None):
# print(str(current.getParent().getId()) + "->" + str(current.getId()))
for neighbour in current.getNeighbours():
if (neighbour.getStatus() == 0):
neighbour.setStatus(1)
neighbour.setParent(current)
neighbour.setpqWeight(current.neighbours[neighbour])
neighbour.setDistance(current.getDistance() + current.neighbours[neighbour])
pq.enQueue((neighbour.getpqWeight(), neighbour))
elif (neighbour.getStatus() == 1):
if (neighbour.getDistance()) > (current.getDistance() + current.neighbours[neighbour]):
#old = (neighbour.getpqWeight(), neighbour)
#neighbour.setpqWeight(current.neighbours[neighbour])
#pq.updatePriority(old, (neighbour.getpqWeight(), neighbour))
neighbour.setParent(current)
neighbour.setDistance(current.getDistance() + current.neighbours[neighbour])
示例5: run
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def run(self, gridMap, defStartNode=None, defEndNode=None, distanceType=PFA.DIS_TYPE_MANHATTAN):
if not gridMap:
return (PFA.RSLT_GRIDMAP_ERR,)
startNode = defStartNode
if not startNode:
startNode = gridMap.getStartGridNode()
if not startNode:
return (PFA.RSLT_NO_START_NODE,)
endNode = defEndNode
if not endNode:
endNode = gridMap.getEndGridNode()
if not endNode:
return (PFA.RSLT_NO_END_NODE,)
hdis = self.getDistance(startNode, endNode, distanceType)
if hdis < 0:
return (PFA.RSLT_DIS_INVALID,)
self.gridMap = gridMap
self.initPathMap(gridMap, distanceType)
openSet = PQ()
startPathNode = self.pMap[startNode.x][startNode.y]
openSet.push(startPathNode)
endPathNode = self.pMap[endNode.x][endNode.y]
ret = (PFA.RSLT_NONE,)
jumpPoint = []
while not openSet.isEmpty() and ret[0] == PFA.RSLT_NONE:
currNode = openSet.pop()
jumpPoint.append(currNode)
currNode.isInClose = True
currGridNode = currNode.gridNode
if gridMap.isEndGridNode(currGridNode.x, currGridNode.y):
ret = (PFA.RSLT_OK, self.genValidPath(gridMap), self.genAllVisNodeSet(gridMap), jumpPoint)
break
for i in range(len(self.visMap)):
for j in range(len(self.visMap[i])):
self.visMap[i][j] = False
for dv in PFA.DIR_VECTOR:
if not self.isOkPos(currGridNode, dv):
continue
jumpNode = self.findJumpNode(currNode, currNode, self.getPathNode(currGridNode, dv), openSet)
if jumpNode:
self.updateJumpNode(currNode, jumpNode, openSet)
return ret
示例6: dijkstra
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def dijkstra(aGraph,start):
pq = PriorityQueue()
start.setDistance(0)
pq.buildHeap([(v.getDistance(),v) for v in aGraph])
while not pq.isEmpty():
currentVert = pq.delMin()
for nextVert in currentVert.getConnections():
newDist = currentVert.getDistance() + currentVert.getWeight(nextVert)
if newDist < nextVert.getDistance():
nextVert.getDistance( newDist )
nextVert.setPred(currentVert)
pq.decreaseKey(nextVert,newdist)
开发者ID:ErikRHanson,项目名称:Problem-Solving-with-Algorithms-and-Data-Structures-Using-Python,代码行数:14,代码来源:dijkstra.py
示例7: astarSearch_Q
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def astarSearch_Q( self, start, goal, cSpace, imgsurface=None ):
def backtrace( node, pardict ):
path = []
path.append( node )
while( pardict.has_key(str(node))):
path.append( pardict[str(node)] );
node = pardict[str(node)];
path.reverse();
return path;
openList = PriorityQueue();
closedList = PriorityQueue();
parentDict = defaultdict();
sphereDict = defaultdict();
GDict = defaultdict();
ownerShpere = self.findOwnerSphere( start[0],start[1], cSpace.mScaledWidth, cSpace.mScaledHeight);
startNode = AstarNode( start[0], start[1], None, ownerShpere );
start_mF = self.distance( start, goal, cSpace.mScaledWidth, cSpace.mScaledHeight );
openList.push( startNode.mPosition, start_mF );
sphereDict[str(startNode.mPosition)] = ownerShpere;
GDict[str(startNode.mPosition)] = 0;
while( not openList.isEmpty() ):
current, curr_mF = openList.pop();
if current == goal:
return backtrace( current, parentDict );
if( imgsurface is not None ):
for event in pygame.event.get():
pass;
pygame.draw.circle( imgsurface, (0,255,0), (int(current[0]), int(current[1])), 2 );
pygame.display.update();
#openList.remove_task( current );
currOwnerSphere = sphereDict[str(current)];
successors = self.getSphereBoundaries(currOwnerSphere, goal, cSpace.mScaledWidth, cSpace.mScaledHeight);
closedList.push( current, curr_mF );
for suc in successors:
sucSamp = suc[0];
if( closedList.find( sucSamp ) ):
continue;
sucOwnerSphere = suc[1];
sucNode = AstarNode( sucSamp[0], sucSamp[1], current, sucOwnerSphere )
sphereDict[str(sucNode.mPosition)] = sucOwnerSphere;
#if sucSamp == goal:
# parentDict[str(sucNode.mPosition)] = current;
# return backtrace( sucSamp, parentDict );
sucNode.mG = GDict[str(current)] + self.distance( sucSamp, current, cSpace.mScaledWidth, cSpace.mScaledHeight );
sameOpen = openList.find( sucNode.mPosition );
if( sameOpen is None or GDict[str(sucNode.mPosition)] > sucNode.mG):
parentDict[str(sucNode.mPosition)] = sucNode.mParentNode;
GDict[str(sucNode.mPosition)] = sucNode.mG;
sucNode_mH = self.distance( sucSamp, goal, cSpace.mScaledWidth, cSpace.mScaledHeight );
sucNode_mF = sucNode.mG + sucNode_mH;
openList.push( sucNode.mPosition, sucNode_mF );
pass
pass
return None;
示例8: PriorityQueue
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PriorityQueue import PriorityQueue
s = PriorityQueue()
s.enQueue(2,1)
s.enQueue(3,1)
s.enQueue("sxw1",3)
s.enQueue("sxw2",3)
s.enQueue("sxw3",3)
s.enQueue("sxw4",3)
s.enQueue(1.14,2)
s.enQueue(2.14,2)
s.enQueue(3.14,2)
s.enQueue(4.14,2)
while not s.isEmpty():
item = s.deQueue()
print("length:", len(s),"value:", item.value)
示例9: run
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import isEmpty [as 别名]
def run(self, gridMap, defStartNode=None, defEndNode=None, distanceType=PFA.DIS_TYPE_MANHATTAN):
if not gridMap:
return (PFA.RSLT_GRIDMAP_ERR,)
startNode = defStartNode
if not startNode:
startNode = gridMap.getStartGridNode()
if not startNode:
return (PFA.RSLT_NO_START_NODE,)
endNode = defEndNode
if not endNode:
endNode = gridMap.getEndGridNode()
if not endNode:
return (PFA.RSLT_NO_END_NODE,)
self.initPathMap(gridMap, distanceType)
closeSet = PQ()
startPathNode = self.pMap[startNode.x][startNode.y]
closeSet.push(startPathNode)
endPathNode = self.pMap[endNode.x][endNode.y]
endPathNode.isInClose = True
ret = (PFA.RSLT_NONE,)
while not closeSet.isEmpty() and ret[0] == PFA.RSLT_NONE:
currNode = closeSet.pop()
currNode.isInClose = True
# print("%d,%d,%d" % (currNode.gridNode.x, currNode.gridNode.y,currNode.fv))
currGridNode = currNode.gridNode
for dv in PFA.DIR_VECTOR:
nx = currGridNode.x + dv[0]
ny = currGridNode.y + dv[1]
if not gridMap.isValidPos(nx, ny):
continue
if gridMap.isThroughTheWall(currGridNode.x, currGridNode.y, dv):
continue
gCost = self.getGCost(dv)
if gridMap.isEndGridNode(nx, ny):
endPathNode.updatePrev(currNode, gCost)
ret = (PFA.RSLT_OK, self.genValidPath(gridMap), self.genAllVisNodeSet(gridMap), None)
break
newNode = self.pMap[nx][ny]
if newNode:
if not newNode.isInClose:
newNode.isInClose = True
closeSet.push(newNode)
if currNode.gv + gCost < newNode.gv:
newNode.updatePrev(currNode, gCost)
closeSet.update(newNode)
# if newNode.isInClose:
# if currNode.gv + gCost < newNode.gv:
# newNode.isInClose = False
# newNode.updatePrev(currNode, gCost)
# openSet.push(newNode)
# newNode.isInOpen = True
# else:
# if currNode.gv + gCost < newNode.gv:
# newNode.updatePrev(currNode, gCost)
# if not newNode.isInOpen:
# openSet.push(newNode)
# newNode.isInOpen = True
# else:
# openSet.update(newNode)
return ret