當前位置: 首頁>>代碼示例>>Python>>正文


Python PriorityQueue.add方法代碼示例

本文整理匯總了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)
開發者ID:cyj907,項目名稱:snake,代碼行數:62,代碼來源:AIBDFS.py

示例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
開發者ID:cyj907,項目名稱:snake,代碼行數:52,代碼來源:Astar.py

示例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]
開發者ID:cyj907,項目名稱:MagicCube,代碼行數:73,代碼來源:MagicCubeAI.py


注:本文中的PriorityQueue.PriorityQueue.add方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。