本文整理汇总了Python中PriorityQueue.PriorityQueue.add方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.add方法的具体用法?Python PriorityQueue.add怎么用?Python PriorityQueue.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.add方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FindDirections
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import add [as 别名]
def FindDirections(self, curState):
self.directions = []
pq = PriorityQueue(["dist"])
maxDepth = 5
pq.add({"state": curState, "dist": self.heuristics(curState), "step": 0, "prev": None})
cnt = 0
while not pq.IsEmpty():
elem = pq.pop()
state = elem["state"]
stack = []
stack.append({"state": state, "step": 0, "prev": elem["prev"]})
"""
if cnt >= SCREEN_WITH + SCREEN_HEIGHT - 4 * SNAKE_WITH_HALH - 2:
print "size of queue: ", len(pq.queue)
cnt = 0
"""
"""
maxSize = max(len(pq.queue) / 2, len(pq.queue) - randint(10, 20))
reduceSize = min(maxSize, len(pq.queue) - 5)
for i in range(reduceSize):
pq.pop()
"""
if len(pq.queue) > 700:
# TODO: if we cannnot find solution for so long a time, the size of queue should
# be reduced to a pretty small number
for i in range(randint(0,len(pq.queue)-1)):
pq.pop()
#print "size of queue: ", len(pq.queue)
#print curState.snake.body
#print curState.snake.GetBodyRects()
continue
while len(stack) > 0:
el = stack.pop()
stat = el["state"]
if el["step"] >= maxDepth:
pq.add({"state": stat, "dist": self.heuristics(stat), "step": el["step"], "prev": el["prev"]})
continue
if stat.IsAppleEaten():
# guess if the snake can be dead by instinct
stat.AddSnakeLen()
if self._is_possible_dead_(stat):
continue
while el is not None:
self.directions.append(el["state"].snake.curDir)
el = el["prev"]
self.directions.pop()
return
ok_dirs = self._arange_dirs_(stat, self._get_ok_dirs_(stat))
for d in ok_dirs:
nextStat = stat.GetNextState(d)
nextEl = {"state": nextStat, "step": el["step"]+1, "prev": el}
stack.append(nextEl)
self.directions.append(Direction.Stop)
示例2: FindDirections
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import add [as 别名]
def FindDirections(self, curState):
pq = PriorityQueue(["dist", "changeDir"])
pq.add({"state": curState, "prev": None, "dist": 0 + self.heuristics(curState), "changeDir": 0, "step": 0})
cnt = 0
while True:
elem = pq.pop()
#print("Dir:", elem["state"].snake.curDir, "step:", elem["step"], "cnt: ", cnt)
ok_dirs = self._get_ok_dirs_(elem["state"])
#print ok_dirs
for d in ok_dirs:
nextState = elem["state"].GetNextState(d)
step = elem["step"] + 1
changeDir = 0
if d != elem["state"].snake.curDir:
changeDir = 1
dist = step + self.heuristics(nextState)
pq.add({"state": nextState, "prev": elem, "dist": dist, "changeDir":changeDir, "step": step} )
#if elem["step"] % 10 == 0:
# print elem["step"]
if pq.IsEmpty():
print "EMPTY!!!!", elem["step"]
if len(pq.storage) > 0:
elem = pq.storage.pop(0)
pq.add(elem)
"""
while len(pq.storage) > 0:
other = pq.storage.pop()
print other["step"], elem["step"]
if abs(other["step"] - elem["step"]) < elem["step"] * 0.1:
break
"""
print elem["state"].IsAppleEaten(), elem["state"].apple.GetApplePos()
#pq.add(other)
cnt += 1
if cnt >= 2000 or self._is_goal(elem["state"]) and elem["step"] > 0:
#print("step:", elem["step"], "cnt:", cnt, "empty:", pq.IsEmpty())
pq.clean()
while True:
self.directions.append(elem["state"].snake.curDir)
elem = elem["prev"]
if elem is None:
break
self.directions.pop() # remove the first direction (the state that already take a step)
return self.directions
示例3: IDA
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import add [as 别名]
def IDA(self, maxDepth=5):
from random import randint
pq = PriorityQueue(['h'])
stack = [{"state": self.state, "depth": 0, "act": None, "actions": deepcopy(MagicCubeConst.availActions), "prev":None}]
while True:
aaa = 0
while len(stack) > 0:
aaa += 1
print aaa
obj = stack[len(stack)-1]
state = obj["state"]
depth = obj["depth"]
actions = obj["actions"]
# if reach goal?
if state.IsGoal():
path = []
while True:
act = obj["act"]
prev = obj["prev"]
if prev != None:
obj = prev
else:
break
path.append(act)
return path
if depth == maxDepth:
pq.add({"obj": obj, "h": MagicCubeAI.heuristic(state)})
stack.pop()
continue
if len(actions) == 0:
stack.pop()
continue
act = actions.pop(randint(0,len(actions)-1))
# prevent actions cancelling out
if MagicCubeConst.pair_orients[act] == obj["act"]:
if len(actions) == 0:
stack.pop()
continue
act = actions.pop(randint(0,len(actions)-1))
# prevent the same consecutive actions
if act == obj["act"]:
prev = obj["prev"]
if prev != None and prev["act"] == act:
if len(actions) == 0:
stack.pop()
continue
else:
act = actions.pop(randint(0,len(actions)-1))
nextState = MagicCubeAI.GetNextState(state, act)
nextObj = {"state": nextState,
"depth": depth+1,
"act": act,
"actions": deepcopy(MagicCubeConst.availActions),
"prev": obj}
stack.append(nextObj)
d = 12*maxDepth
if len(pq.queue) > d:
left = randint(3, min(d,len(pq.queue)))
while len(pq.queue) > left:
pq.pop()
print len(pq.queue)
elem = pq.pop()
obj = elem["obj"]
obj["depth"] = 0
stack = [obj]