当前位置: 首页>>代码示例>>Python>>正文


Python PriorityQueue.remove方法代码示例

本文整理汇总了Python中PriorityQueue.PriorityQueue.remove方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.remove方法的具体用法?Python PriorityQueue.remove怎么用?Python PriorityQueue.remove使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.remove方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: astar

# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import remove [as 别名]
def astar(start, successors, goal, g, h):
    path = []
    states = PriorityQueue()
    firststate = start
    states.put((-g(firststate) - h(firststate), firststate))

    i = 0
    nexti = 1
    while True:
        i += 1
        # if i == 40000:
        #    return (0, None)
        if i == nexti:
            nexti += 1  # 3*len(states)
            curbest = states.top()[1]
            print "%8d: # states: %5d, h of best state: %5d" % (i, len(states), h(curbest))
            print "\tcurrent top: score = %6d; %r" % (g(curbest) + h(curbest), curbest)
        try:
            if goal(states.top()[1]):
                print i
                return (states.top()[1], g(states.top()[1]))
        except IndexError:
            return (0, None)
        best = states.top()[1]
        states.pop()
        for s in successors(best):
            prev = [x for x in states if x[1] == s]
            # if len(prev)>0:print prev[0][1]
            # print s
            if len(prev) > 0 and g(s) < g(prev[0][1]):
                states.remove(prev[0])
            if len(prev) > 0:
                continue
            states.put((-g(s) - h(s), s))
开发者ID:cconnett,项目名称:euler,代码行数:36,代码来源:astarpathless.py

示例2: astar

# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import remove [as 别名]
def astar(start, successors, goal, g, h):
    path = []
    states = PriorityQueue()
    firststate = (start,None)
    states.put((-g(firststate) - h(firststate), firststate))
    
    i = 0
    while True:
        i += 1
        if i % 2500 == 0:
            print '%8d: # states: %5d, h of best state: %5d' % \
                  (i, len(states), min(h(state[1]) for state in states))
        try:
            if goal(states.top()[1]):
                return g(states.top()[1]), getpath(states.top()[1])
        except IndexError:
            return None
        best = states.top()[1]
        states.pop()
        for s in successors(best):
            prev = [x for x in states if x[1][0] == s]
            s = (s, best)
            #if len(prev)>0:print prev[0][1]
            #print s
            if len(prev) > 0 and g(s) < g(prev[0][1]):
                states.remove(prev[0])
            if len(prev) > 0:
                continue
            states.put((-g(s) - h(s), s))
开发者ID:cconnett,项目名称:euler,代码行数:31,代码来源:astarstateless.py

示例3: main

# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import remove [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." )
开发者ID:wish343,项目名称:Python,代码行数:34,代码来源:taskmaster.py

示例4: __init__

# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import remove [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
开发者ID:fimad,项目名称:TextToKill,代码行数:102,代码来源:Game.py


注:本文中的PriorityQueue.PriorityQueue.remove方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。