コード例 #1
0
ファイル: astar.py プロジェクト: nielsmadan/dojo
    def __call__(self, graph, start_node, target_node):
        frontier = PriorityQueue()

        current_node = start_node
        distance_dict = defaultdict(lambda: infinity)
        distance_dict[current_node] = 0
        ancestors_dict = {}
        visited_set = set()

        while True:
            neighbors = graph.get_neighbors(current_node)

            current_distance = distance_dict[current_node]

            for neighbor in neighbors:
                if neighbor not in visited_set and (current_distance + 1) < distance_dict[neighbor]:
                    distance_dict[neighbor] = current_distance + 1
                    ancestors_dict[neighbor] = current_node
                    frontier.put((self.cost_function(distance_dict[neighbor], neighbor, target_node), neighbor))
                    self.nodes_expanded += 1

            visited_set.add(current_node)
            self.nodes_visited += 1

            if current_node == target_node:
                return list(reversed(find_ancestors(ancestors_dict, current_node, start_node)))
            else:
                try:
                    current_node = frontier.get_nowait()[1]
                except Empty:
                    break
コード例 #2
0
ファイル: __init__.py プロジェクト: Licorn/licorn
	def __init__(self, name, directory_scenarii,
									clean_func, state_file, cmd_display_func):
		self.name               = name
		self.list_scenario      = []
		self.selected_scenario  = []
		self.directory_scenarii = directory_scenarii
		self.clean_system       = clean_func
		self.cmd_display_func   = cmd_display_func
		self.state_file         = state_file
		# save the current context to restaure it at the end of the testsuite
		backends = [ line for line in process.execute(['get', 'config',
								'backends'])[0].split('\n') if 'U' in line
															and line != '' ]
		self.user_context    = backends[0].split('(')[0]
		self.current_context = self.user_context

		self.to_run     = PriorityQueue()
		self.failed     = PriorityQueue()
		self.passed     = []
		self.best_state = 1
		self.working    = Event()

		# used to modify the best state behaviour when running one one test
		# or the whole TS.
		self.best_state_only_one = 0

		# used to build initial testsuite data
		self.batch_run   = False
		self.interactive = False
コード例 #3
0
ファイル: game.py プロジェクト: yogeshg/small-projects
def getAlphaBeta( game, player, other, reward=AlphaBeta(-100,100), depth=10, tab=0 ):
    '''
    alpha: minimum bound of the outcome -- currently, evaluate of best move possible by other
    beta: maximum bound of the outcome -- currently, evaluate of best move possible by player
    
    returns: (alpha, beta, evaluation, move)
    '''
    # evaluation is the current value of board, assuming no more moves in future
    # alpha == beta == finalvalue if we figure out the outcome.
    reward.evaluation = game.evaluate_cached( player )
    move = Move(-1,-1)
    # base case: can't play further, lost
    if (reward.evaluation==reward.beta):
#         print 'Player', player, 'won!!!'
        return AlphaBetaOfMove(AlphaBeta(reward.beta, reward.beta, reward.beta), move)
    # base case: can't evaluate further
    if (depth == 0):
        return AlphaBetaOfMove(reward, move )
    q = PriorityQueue()
    for m in game.next_moves( ):
            # g2 = copy.deepcopy( game )
            assert game.move( player, m)
            try:
                oponent_reward = AlphaBeta(-reward.beta, -reward.alpha, -reward.evaluation)
                oponent_reward = getAlphaBeta(game, other, player, reward=oponent_reward, depth=depth-1, tab=tab+1 ).alphabeta
                player_reward = AlphaBeta(-oponent_reward.beta, -oponent_reward.alpha, -oponent_reward.evaluation)
                q.put_nowait( AlphaBetaOfMove( player_reward, m ) )
            finally:
                assert game.unmove(player, m)
#             if( tab < 1 ):
#                 print ("\t"*tab), (player,row,col), (other,other_r,other_c), (other_alpha,other_beta,other_evaluation), (next_alpha,next_beta,evaluation), update
    if( q.empty() ):
        return AlphaBetaOfMove( reward, Move(-1,-1) )
    else:
        return q.get_nowait()
コード例 #4
0
 def longestIncreasingPath(self, matrix):
     """
     :type matrix: List[List[int]]
     :rtype: int
     """
     m = len(matrix)
     if m == 0: return 0
     n = len(matrix[0])
     queue = PriorityQueue()
     res = 0
     mem = [[0] * n for _ in range(m)]
     dirs = [(-1,0),(1,0),(0,-1),(0,1)]
     def next_smalles(i,j):
         results = [0]
         for di, dj in dirs:
             ni, nj = di + i, dj + j
             if ni >= 0 and ni < m and nj >= 0 and nj < n and matrix[i][j] > matrix[ni][nj]:
                 results += mem[ni][nj],
         return results
     for i in range(m):
         for j in range(n):
             queue.put((matrix[i][j],i,j))
     while not queue.empty():
         v, i, j = queue.get()
         mem[i][j] = max(next_smalles(i,j)) + 1
         res = max(res, mem[i][j])
     return res
コード例 #5
0
class EventQueue(object):
    def __init__(self):
        """Event queue for executing events at 
        specific timepoints.

	In current form it is NOT thread safe."""
        self.q = PriorityQueue()
    
    def schedule(self, f, ts):
        """Schedule f to be execute at time ts"""
        self.q.put(EqItem(ts, f))
        
    def schedule_recurring(self, f, interval):
        """Schedule f to be run every interval seconds.

	It will be run for the first time interval seconds
        from now"""
        def recuring_f():
            f()
            self.schedule(recuring_f, time.time() + interval)
        self.schedule(recuring_f, time.time() + interval)
        
        
    def run(self):
        """Execute events in the queue as timely as possible."""
        while True:
            event = self.q.get()
            now = time.time()
            if now < event.ts:
                time.sleep(event.ts - now)
            event.f()
コード例 #6
0
def prepPriorityQueue(game):
    global sudokuQueue 
    sudokuQueue = PriorityQueue()
    for i in range(0,N):
        for j in range(0,N):
            if((not game[i][j].domain==None) and (len(game[i][j].domain )!=0)):
                sudokuQueue.put((len(game[i][j].domain),game[i][j]))
コード例 #7
0
def conflicts(board):

    conlist=PriorityQueue()
    clist=[]
    global conflictval,n,m,k
    #consistency check along col,row and grid
    for row in range(int(n)):
        for col in range(int(n)):
            recurval=0
            for c in range(int(n)):
                     #print "row check",board[row][c]
                     if board[row][c] == board[row][col]:
                         recurval=recurval+1
            for r in range(int(n)):
                     if board[r][col] == board[row][col]:
                         recurval=recurval+1
            for r1 in range(int(m)):
                for  c1 in range(int(k)):

                    if (board[r1+row-(row%int(m))][c1+col-(col%int(k))] == board[row][col]):
                        recurval=recurval+1
            if(recurval>3):
                clist.append((row,col))
                conlist.put((recurval,(row,col)))
                #conflictval[(row,col)]=recurval

    return clist
コード例 #8
0
ファイル: data.py プロジェクト: csferng/HashSGD
class PoolStreamData(StreamData):
    def __init__(self, feat_path, feature_maker, label_path=None, fold_in_cv=None):
        self.pool = PriorityQueue(100000)
        super(PoolStreamData, self).__init__(feat_path, feature_maker, label_path, fold_in_cv)
        self.fill_pool()

    def fill_pool(self):
        while not self.pool.full():
            try:
                ins = super(PoolStreamData,self).next()
                self.pool.put((random.random(),ins))    # insert ins with random priority --> kind of random shuffle
            except StopIteration:
                break

    def rewind(self):
        self.pool = PriorityQueue(100000)
        super(PoolStreamData,self).rewind()
        self.fill_pool()

    def next(self):
        try:
            (_,ins) = self.pool.get(False)
        except Empty:
            raise StopIteration
        self.fill_pool()
        return ins
    def __buildFromDB(self):
        try:
            dbConnection = self.pool.connection()
            cursor = dbConnection.cursor()
            cursor.execute("""select request_spec, id from priority_queue""")
            moreDBElement = True
            while moreDBElement:
                item = cursor.fetchone()
                if item:
                    request_spec = json.loads(item[0])
                    idRecord = item[1]
                    instance_properties = request_spec.get("instance_properties")
                    userId = instance_properties.get("user_id")
                    projectId = instance_properties.get("project_id")
                    timestamp = instance_properties.get("created_at")

                    requestPQ = {
                        "priority": 0,
                        "userId": userId,
                        "projectId": projectId,
                        "timestamp": timestamp,
                        "retryCount": 0,
                        "idRecord": idRecord,
                    }
                    PriorityQueue.put(self, (0, requestPQ))
                else:
                    moreDBElement = False

        except MySQLdb.Error, e:
            if dbConnection:
                dbConnection.rollback()
                LOG.info("Error %d: %s" % (e.args[0], e.args[1]))
コード例 #10
0
class MinimumWeightMatchingWithEdges:
    """Find a minimum weight matching using a greedy method.
    
    Attributes
    ----------
    graph : input undirected graph
    mate : dict with nodes (values are edges or None)
    cardinality : number
    """
    # Bedzie potrzebne do problemu chinskiego listonosza.

    def __init__(self, graph):
        """The algorithm initialization."""
        if graph.is_directed():
            raise ValueError("the graph is directed")
        self.graph = graph
        self.mate = dict((node, None) for node in self.graph.iternodes())
        self.cardinality = 0
        self._pq = PriorityQueue()

    def run(self):
        """Executable pseudocode."""
        for edge in self.graph.iteredges():
            self._pq.put((edge.weight, edge))
        while not self._pq.empty():
            _, edge = self._pq.get()
            if (self.mate[edge.source] is None and 
                self.mate[edge.target] is None):
                    self.mate[edge.source] = edge
                    self.mate[edge.target] = ~edge
                    self.cardinality += 1
コード例 #11
0
ファイル: astar_planner.py プロジェクト: awtreth/a_stars
	def run(self):
		"""
		basically calculate the shortest path with A* search algorithm
		"""

		origin = Node(self.startPose)
		
		#PriorityQueue of nodes
		pq = PriorityQueue()
		currentNode = origin
		
		# Compute the AStar search algorithm
		while not currentNode.pos.equals(self.goalPose) and not rospy.is_shutdown():
			children = self.expandNode(currentNode)
			
			for node in children:
				pq.put((node.cost(), node)) #it is ranked by the cost
			
			if(pq.qsize() == 0): #no more nodes to be expanded
				print "didn't find a path"
				currentNode = origin
				break
				
			# pop the "closest" node to the goal
			currentNode = pq.get()[1]
		
		self.calcPathWayPoints(currentNode) #lastNode
コード例 #12
0
    def find_corners_for_room(self, room_center):
        """
        returns the 4 corners of given room center
        :param room_center:
        :return: [right_up, left_up, left_down, right_down]
        """
        [x, y] = room_center
        prio_q = PriorityQueue()
        [right_up, left_up, left_down, right_down] = [None, None, None, None]
        # find the 4 closest corners
        for corner in self.found_corners:
            dist = Calc.get_dist_from_point_to_point(room_center, corner)
            prio_q.put([dist, corner])
        for i in xrange(4):
            [x_c, y_c] = prio_q.get()[1]
            if x_c < x and y_c > y:
                left_up = [x_c, y_c]
            elif x_c < x and y_c < y:
                left_down = [x_c, y_c]
            elif x_c > x and y_c > y:
                right_up = [x_c, y_c]
            elif x_c > x and y_c < y:
                right_down = [x_c, y_c]

        return [right_up, left_up, left_down, right_down]
コード例 #13
0
ファイル: segment.py プロジェクト: hwp/hw2012ws
def segment(img) :
	g = numpy.array(img.getdata())
	g = g.reshape(img.size)
	gx = g[:-1,1:] - g[:-1,:-1]
	gy = g[1:,:-1] - g[:-1,:-1]
	gms = gx * gx + gy * gy

	seg = Image.new('L', gms.shape)
	taken = numpy.zeros(seg.size)
	#print 'original local mininum'
	for i in range(seg.size[0]) :
		for j in range(seg.size[1]) :
			if localmin(gms, i, j) :
				taken[i][j] = 1
				seg.putpixel((j, i), img.getpixel((j, i)))
				#print (j, i), img.getpixel((j, i))
	q = PriorityQueue(-1)
	#print 'expand'
	for i in range(seg.size[0]) :
		for j in range(seg.size[1]) :
			if taken[i][j] > 0 :
				addNeighbor(seg, gms, taken, i, j, q)
	#print q.qsize()
	while not q.empty() :
		(i, j) = q.get()[1]
		addNeighbor(seg, gms, taken, i, j, q)
		#print q.qsize()
	return seg
コード例 #14
0
ファイル: event.py プロジェクト: nyc/netsim
class EventManager(object):
    """ Keeps track of and triggers events in our network simulation

    Instance Properties:
        Q - A priority queue of events ordered by time
    """

    def __init__(self):
        self._Q = PriorityQueue()
        self.t = 0

    def add_event(self, t, event, args):
        """ Add an event onto the event queue

        :param t: The time of the event
        :param event: The event to add
        :param args: A list of args for the event
        """
        self._Q.put((t, event, args))

    def pop_event(self):
        """ Getting the first event off of the event queue.
        
        :return: Returns a tuple consisting of the time, event, and the args
            for the event
        """
        return self._Q.get()

    def has_events(self):
        """ Whether or not the manager has any more events on its queue

        :return: True or False based on whether its empty
        """
        return not self._Q.empty()
コード例 #15
0
ファイル: Scheduler.py プロジェクト: DavidCorrea/UNQ-SO-2014
class PriorityScheduler():

    def __init__(self):
        self._readyq = PriorityQueue()
        self._pcbsQ = []
        self._counter = 0
        self._quantum = -1

    def add(self, pcb):
        self._readyq.put((pcb._priority, self._counter, pcb))
        self._pcbsQ.append(pcb)
        self._counter += 1

    def get_pcb(self):
        #El [2] es porque el next() me devuelve la tupla que guarda la PQ, por lo que le pido el objeto.
        self.pcb_to_give = self._readyq.get()[2]
        self._pcbsQ.remove(self.pcb_to_give)
        for pcb in self._pcbsQ:
            pcb.increase_priority()
        return self.pcb_to_give

    def set_scheduler(self, scheduler):
        scheduler.set_as_pq()

    def get_quantum(self):
        return self._quantum
コード例 #16
0
ファイル: client.py プロジェクト: sephii/ruzzle
def consume_solution_queue(q):
    priority_queue = PriorityQueue()
    proposed_solutions = set()

    try:
        while True:
            fetch = True
            while fetch:
                try:
                    item = q.get_nowait()

                    if item[1] not in proposed_solutions:
                        priorized_item = (-1 * (item[0] / len(item[1])), item[0], item[1])
                        priority_queue.put(priorized_item)
                        proposed_solutions.add(item[1])
                except Empty:
                    fetch = False

            try:
                solution = priority_queue.get_nowait()
                print_solution(solution[1], solution[2])
            except Empty:
                pass

            time.sleep(2)
    except KeyboardInterrupt:
        pass
コード例 #17
0
 def get(self, time, *args, **kwargs):
         a, b, item = PriorityQueue.get(self, *args, **kwargs)
         if a == time:
             return item
         else:
             PriorityQueue.put(self, (a, 0, item))
             raise NameError("No more tasks scheduled!")
コード例 #18
0
ファイル: food_agent.py プロジェクト: jaronhalt/cs156
	def solve(self):
		nexts = PriorityQueue()
		while True:
			self.step += 1
			self.current.visited = True
			possibilities = visitable_points(self.current, self.maze)
			for node in possibilities:
				nexts.put((node.get_cost(), node))
			next = nexts.get()
			try:
				while next[1].visited or (not are_neighbors(next[1].coords(), self.current.coords())):
					next = nexts.get(False)
				self.current = next[1]
				self.solution.append((self.step,self.current, str(self)))

			except Queue.Empty:
				try:
					self.current = self.solution.pop()[1]
					self.step -= 1

				except IndexError:
					print "No solution"
					exit(1)

			self.update()
コード例 #19
0
ファイル: base.py プロジェクト: ahmadassaf/DCAT-Tools
class CrawlManager(object):
    
    def __init__(self, config):
        try: self.queue_size = int(config.get('queue_size'))
        except: self.queue_size = 1000
            
        try: self.num_threads = int(config.get('num_threads'))
        except: self.num_threads = 10
        
        self.queue = PriorityQueue(maxsize=self.queue_size)
        self.processors = ProcessorManager(config)

        for i in  range(self.num_threads):
            t = Thread(target=self.queue_target, 
                       name="CrawlDaemon-%s" % i)
            t.daemon = True
            t.start()
    
    def queue_target(self):
        while True:
            try:
                queue_entry = self.queue.get(True)
                self.handle(queue_entry[1])
                self.queue.task_done()
            except Exception, e:
                log.exception(e)
コード例 #20
0
class MaxHeap(object):
  """
    Max Heap for the greedy algorithms
  """
  def __init__(self):
    self.popped = set()
    self.pq = PriorityQueue()

  def pop(self):
    """
      returns next item and its priority on the priority queue and removes item from the priority queue. 
      Returns (-1, 0) if queue is empty
    """
    while not self.pq.empty():
      a = self.pq.get()
      if a[1] not in self.popped:
        self.popped.add(a[1])
        return a[1], -a[0], a[2]
    return (-1, 0, None)

  def update(self, x, v, aux = None):
    """
      updates key x with value v. If x is currently not in the priority queue, x is inserted
    """
    if x in self.popped:
      self.popped.remove(x)
    self.pq.put((-v, x, aux))
コード例 #21
0
def __main__():
    #check aruguments
    if len(sys.argv) == 1:
        details()
        sys.exit()
    
    if len(sys.argv) < 2:
        usage()
    
    try: 
        opts, args = getopt.getopt(sys.argv[2:],"l:")
    except getopt.GetoptError:
        usage()
    
    for opt, arg in opts:
        if opt == "-l":
            global _log 
            _log = str(arg)
        else:
            print ("Unrecognized option: "+opt+"\n")
            usage()
    
    annot = open(sys.argv[1],"r")
    
    if (annot == None):
        print ("Bad annotation name: "+sys.argv[1])
        sys.exit()
        
        
    logfile = open(_log,"w")
    
    if (logfile == None):
        print ("Bad logfile name: "+_log)
        sys.exit()
        
    #reg ex
    annotpat = re.compile("\w+_(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(.*)")
    
    negQueue = PriorityQueue()
    
    #read the vcf file...
    for line in annot:
        line = line.rstrip()
        m = annotpat.match(line)
        
        if (m == None):# if we find a weird line...
            sys.stderr.write("Non-standard line (annot):\n\t"+line+"\n")
            continue
        else:
            scaf = int(m.group(1))
            start = int(m.group(2))
            direction = m.group(5)
            
            if direction == "-":
                negQueue.put_nowait((start, line))
                continue
            else:
                while not negQueue.empty():
                    print(negQueue.get()[1])
                print(line)
コード例 #22
0
def expand_tree(node):

  priorityqueue = PriorityQueue()
  while True:
    # Check if current node is the goal node
    if node.pixels == goal:
      break

    surrounding_area = get_surrounding_pixels(node.pixels)
    
    # Iterate through the all 8 pixels around the current x, y point
    for current_pixel in surrounding_area:
      
      # Only add pixel is a child if it isn't visisted already
      if current_pixel[0] not in visited and isFree(current_pixel[0]):
        # Create new node
        #weight = edge_weight_distance(node.value)
        next = Node(node, current_pixel[0])
        next.set_value(current_pixel[1])
        visited.add(next.pixels)
        #nodesToExplore.append(next)
        next = priorityqueue.put((next, next.value))

    #node = nodesToExplore.popleft()
    node = priorityqueue.get()[0]

  # Once breaking, return the full path of the goal node
  return node.return_full_path()
コード例 #23
0
ファイル: reliable_sock.py プロジェクト: VictorJuliani/Redes
	def __init__ (self, sock, addr, cwnd, ploss, pcorr):
		self.init = False
		self.end = False
		self.requesting = False
		self.sock = sock
		self.addr = addr
		self.ploss = ploss
		self.pcorr = pcorr
		self.cwnd = cwnd

		if self.cwnd <= 0:
			self.cwnd = WINDOW_SIZE

		self.attempt = 0
		self.timer = None

		self.lock = threading.Lock()

		# PQueue will sort packets by segnum, so when inserting in queue, packets will be like:
		# Acks - segnum = 0
		# Failed packets (for their segnum is securely < unset packets)
		# Unsent packets
		# So ordering is guaranteed and ACKs should be sent immediately
		self.buff = PQueue() # packets to send
		self.waiting = PQueue(cwnd) # packets wating for ACK

		self.seg = 0 # current packet
		self.ack = 0 # last valid ack
		self.nextSeg = random.randint(1,1000) # next expected packet
コード例 #24
0
ファイル: pr_graph.py プロジェクト: Simon0xzx/tampy
def p_mod_abs(domain_config, problem_config, solvers_config, max_iter=100):
    hl_solver, ll_solver = ParseSolversConfig.parse(solvers_config, domain_config)
    domain = ParseDomainConfig.parse(domain_config)
    problem = ParseProblemConfig.parse(problem_config, domain)
    if problem.goal_test():
        return False, "Goal is already satisfied. No planning done."
    n0 = HLSearchNode(hl_solver.translate_problem(problem), domain, problem)

    Q = PriorityQueue()
    Q.put((0, n0))
    for _ in range(max_iter):
        n = Q.get()[1]
        if n.is_hl_node():
            c_plan = n.plan(hl_solver)
            c = LLSearchNode(c_plan)
            Q.put((-n.heuristic(), n))
            Q.put((-c.heuristic(), c))
        elif n.is_ll_node():
            n.plan(ll_solver)
            if n.solved():
                return n.curr_plan, None
            Q.put((-n.heuristic(), n))
            if n.gen_child():
                fail_step, fail_pred = n.get_failed_pred()
                n_problem = n.get_problem(fail_step, fail_pred)
                c = HLSearchNode(hl_solver.translate_problem(n_problem), domain, n_problem, prefix=n.curr_plan.prefix(fail_step))
                Q.put((-c.heuristic(), c))

    return False, "Hit iteration limit, aborting."
コード例 #25
0
ファイル: updater.py プロジェクト: faliev/clinical-trials
	def update(self):
		#Queues to manage downloading
		dateQ = Queue(0)
		xmlTrialQ = PriorityQueue(100)

		# Create the single SQLAlchemy thread to update DB records
		sqlaThread = threading.Thread(target=self.updateTrials, args=(dateQ, xmlTrialQ))
		sqlaThread.daemon = True
		sqlaThread.start()

		startDate = self.fetchStartDate()
		delta = dt.date.today() - startDate
		dateList = [startDate + dt.timedelta(days=x) for x in range(0, delta.days)]

		print dateList

		# Add relevant dates to the list to be fetched
		for currentDate in dateList:
			dateQ.put(currentDate)

		print dateQ.queue

		numWorkerThreads = min(10, dateQ.qsize())

		# Create n worker threads to fetch the zip files of updated content and produce xmlTrial objects
		for i in range(numWorkerThreads):
			t = threading.Thread(target=self.fetchUpdatedTrials, args=(dateQ, xmlTrialQ))
			t.daemon = True
			t.start()

		dateQ.join() 	 # block until all dates have been fetched
		xmlTrialQ.join() # block until all xmlTrials have been created
コード例 #26
0
ファイル: __init__.py プロジェクト: echlebek/15puzz
def _solve(board, searchfun):
    """
    This is the top level function of the program. `board` is given to the
    search function by placing it into an empty `PriorityQueue`.

    A path to the result is returned.
    """
    cost = manhattan_distance(board)
    start = Node(0, cost, board, None)
    queue = PriorityQueue()
    queue.put(start)
    visited = set()

    result = searchfun(queue, visited)

    def getpath(node, path):
        path.append(node)
        if node.parent is not None:
            return getpath(node.parent, path)
        else:
            return path

    if queue.empty():
        # Something went wrong with the search algorithm.
        raise ValueError("Bad search")

    return getpath(result, [])
コード例 #27
0
ファイル: p5_ordering.py プロジェクト: cseoreos/Homework3
def order_domain_values(csp, variable):
    """Returns a list of (ordered) domain values for the given variable.

    This method implements the least-constraining-value (LCV) heuristic; that is, the value
    that rules out the fewest choices for the neighboring variables in the constraint graph
    are placed before others.
    """
    rating = 0 #To prioritize
    queue = PriorityQueue()
    lister = [] #List that is going to be returned
    
    for value in variable.domain:
        rating = 0 #Reset back to 0 for rating of a new value in the domain of the variable
        for constraint in csp.constraints[variable]: #Get the constraints of the variable
            for dim in constraint.var2.domain: #Check with all the value of the constraint varaibles domain to check if the value can be satisfied with these domain values
                if (constraint.is_satisfied(value, dim)):
                    rating = rating + 1
        queue.put((rating, value)) # Ordered by rating
    
    while not queue.empty(): #Putting into a list
        i = 0
        holder = queue.get()
        lister.insert(i, holder[1])
        i = i + 1
    return lister
コード例 #28
0
def dijkstraQ(graph, initial):
    visited = {initial: 0}
    path = {}

    nodes = PriorityQueue()
    for node in graph.nodes:
        nodes.put()

    while nodes:
        min_node = None
        for node in nodes:
            if node in visited:
                if min_node is None:
                    min_node = node
                elif visited[node] < visited[min_node]:
                    min_node = node
        if min_node is None:
            break

        nodes.remove(min_node)
        current_weight = visited[min_node]

        for edge in graph.edges[min_node]:
            try:
                weight = current_weight + graph.distances[(min_node, edge)]
            except:
                continue
            if edge not in visited or weight < visited[edge]:
                visited[edge] = weight
                path[edge] = min_node

    return visited, path
コード例 #29
0
ファイル: long_posts.py プロジェクト: mbhushan/bigdata
def mapper():
    reader = csv.reader(sys.stdin, delimiter='\t')
    writer = csv.writer(sys.stdout, delimiter='\t',
                        quotechar='"', quoting=csv.QUOTE_ALL)
    pq = PriorityQueue()
    topsize = 10
    result = []

    for line in reader:
        # YOUR CODE HERE
        post = line[4]
        if pq.qsize() < topsize:
            pq.put((len(post), line))
        else:
            minpost = pq.get()
            if minpost[0] >= post[0]:
                pq.put((len(minpost), line))
            else:
                pq.put((len(post), line))
    for i in range(topsize):
        result.append(pq.get())
        # writer.writerow(line)
        result.sort(key=lambda x: x[0])
    for r in result:
        writer.writerow(r[1])
コード例 #30
0
	def neighbors(self):
		aux = PriorityQueue()
		neighbors = []
		dx = [0, 0, 1, -1 ]
		dy = [1, -1, 0, 0 ]
		posx = 0
		posy = 0
		for i in range(self.size()):
			for j in range(self.size()):
				if self.blocks[i][j] == 0:
					posx = i
					posy = j
					break

		for k in range(4):
			nx = posx + dx[k]
			ny = posy + dy[k]
			if nx >= 0 and nx < self.size() and ny >= 0 and ny < self.size():
				tmp = copy.deepcopy(self.blocks)
				tmp[posx][posy] = tmp[nx][ny]
				tmp[nx][ny] = 0
				b = Board(tmp)
				aux.put((b.hamming(),b))

		while not aux.empty():
			neighbors.append(aux.get()[1])
		return neighbors
コード例 #31
0
 def put(self, tup):
     newtup = tup[0] * -1, tup[1], tup[2]
     PriorityQueue.put(self, newtup)
コード例 #32
0
def findOptimalPath(ghostCrd, pManCrd, scene):
    global graph 

    drc = isAdjacent(Node(pManCrd),Node(ghostCrd),scene)
    if drc:
        return [(Node(ghostCrd),drc),(Node(ghostCrd),None)]
    
    graph = generateStateGraph(ghostCrd, pManCrd, scene)
    
    frontier = PriorityQueue()
    
    # create start node
    start = graph[repr(Node(ghostCrd))]
    start.g = 0
    
    # create frontier
    frontier.put(start)
    
    # keep track of visited states and previous min cost with 
        # dictionary
    visited = {repr(start):start}
    
    while not frontier.empty():
        current_node = frontier.get()
        
        # if node has already been pruned, ignore
        if current_node.pruned:
            continue

        # check if node represents solution
        if current_node == Node(pManCrd):
            # reconstruct path using parent attribute
            path = []
            dest = current_node
            while dest.parent:
                path.append((dest.parent,[e.drc for e in dest.parent.edges if e.adjNode == dest][0]))
                dest = path[-1][0]
                
            return list(reversed(path))
        # expand node by generating child nodes from legal moves
        for edge in current_node.edges:
            c = edge.adjNode
            c.edges = graph[str(c)].edges
            if any(c == edge.adjNode for edge in graph[repr(Node(pManCrd))].edges):
                
                if c.crd[1] == pManCrd[1]:
                    minDist = min((abs(c.crd[0]-pManCrd[0]),'LEFT'),
                              (len(scene[0])-abs(c.crd[0]-pManCrd[0]),'RIGHT'))
                    
                else:
                    if c.crd[1] < pManCrd[1]:
                        minDist = (pManCrd[1]-c.crd[1],'DOWN')
                    
                    else:
                        minDist = (c.crd[1]-pManCrd[1],'UP')
                
                c.edges.add(Edge(graph[repr(Node(pManCrd))], *minDist))
                
            c.g = current_node.g+edge.weight
            c.h = heuristic(c, pManCrd,scene)

            c.f = c.g+c.h
            
            c.parent = current_node
            
            # ignore if visited or path is not more efficient
                # if path is more efficient, prune previous node
            if repr(c) in visited:
                if c.g >= visited[repr(c)].g:
                    continue
                else:
                    visited[repr(c)].pruned = True
            # add to visited nodes
            visited[repr(c)] = c
            
            # add to frontier
            frontier.put(c)
コード例 #33
0
 def __init__(self):
     self._timers = PriorityQueue()
     self._map = {}
コード例 #34
0
 def __init__(self):
     PriorityQueue.__init__(self)
     self.counter = 0
コード例 #35
0
 def getItemAndPriority(self, *args, **kwargs):
     _, _, item = PriorityQueue.get(self, *args, **kwargs)
     return item, self.ppp[item]
コード例 #36
0
 def get(self, *args, **kwargs):
     _, _, item = PriorityQueue.get(self, *args, **kwargs)
     return item
コード例 #37
0
 def put(self, item, priority):
     PriorityQueue.put(self, (priority, self.counter, item))
     self.counter += 1
コード例 #38
0
ファイル: utility.py プロジェクト: rahulravi7/tsp-solver
'''
Node class 
and sample Priority Queue 
'''
from Queue import PriorityQueue

pq = PriorityQueue()


class Node(object):
    def __init__(self, level=None, path=None, bound=None):
        self.level = level
        self.path = path
        self.bound = bound

    def __cmp__(self, other):
        return cmp(self.bound, other.bound)

    def __str__(self):
        return str(tuple([self.level, self.path, self.bound]))


if __name__ == '__main__':
    pq = PriorityQueue()
    pq.put(Node(2, [1, 2, 3], 6))
    pq.put(Node(4, [1, 3, 2], 1))
    pq.put(Node(1, [1, 2], 7))
    while not pq.empty():
        print pq.get()

    pq.put(Node(3, [1, 2, 5], 12))
コード例 #39
0
ファイル: metric_handler.py プロジェクト: zsjss/liota
 def __init__(self):
     PriorityQueue.__init__(self)
     self.first_element_changed = Condition(self.mutex)
コード例 #40
0
class AsyncoreReactor(object):
    _thread = None
    _is_live = False
    logger = logging.getLogger("Reactor")

    def __init__(self):
        self._timers = PriorityQueue()
        self._map = {}

    def start(self):
        self._is_live = True
        self._thread = threading.Thread(target=self._loop,
                                        name="hazelcast-reactor")
        self._thread.daemon = True
        self._thread.start()

    def _loop(self):
        self.logger.debug("Starting Reactor Thread")
        Future._threading_locals.is_reactor_thread = True
        while self._is_live:
            try:
                asyncore.loop(count=10, timeout=0.01, map=self._map)
                self._check_timers()
            except select.error as err:
                # TODO: parse error type to catch only error "9"
                self.logger.warn("Connection closed by server.")
                pass
            except:
                self.logger.exception("Error in Reactor Thread")
                # TODO: shutdown client
                return
        self.logger.debug("Reactor Thread exited. %s" % self._timers.qsize())
        self._cleanup_all_timers()

    def _check_timers(self):
        now = time.time()
        while not self._timers.empty():
            try:
                _, timer = self._timers.queue[0]
            except IndexError:
                return

            if timer.check_timer(now):
                try:
                    self._timers.get_nowait()
                except Empty:
                    pass
            else:
                return

    def add_timer_absolute(self, timeout, callback):
        timer = Timer(timeout, callback, self._cleanup_timer)
        self._timers.put_nowait((timer.end, timer))
        return timer

    def add_timer(self, delay, callback):
        return self.add_timer_absolute(delay + time.time(), callback)

    def shutdown(self):
        if not self._is_live:
            return
        self._is_live = False
        for connection in self._map.values():
            try:
                connection.close(HazelcastError("Client is shutting down"))
            except OSError, connection:
                if connection.args[0] == socket.EBADF:
                    pass
                else:
                    raise
        self._map.clear()
        self._thread.join()
コード例 #41
0
def find_path(source, destination, mesh):
    print "I'm in the method"
    sx, sy = source
    dx, dy = destination
    sourcebox = None
    destbox = None
    visited = []
    path = []
    detail_points = {}
    for box in mesh['boxes']:
        x1, x2, y1, y2 = box
        if x1 < sx and x2 > sx and y1 < sy and y2 > sy:
            #visited.append(box)
            sourcebox = box
        if x1 < dx and x2 > dx and y1 < dy and y2 > dy:
            #visited.append(box)
            destbox = box

    if sourcebox == destbox:
        return [(source, destination)], [sourcebox]

    #Dijkstra's Algorithm/ANY SEARCH THINGY HERE
    dist = {}
    prev = {}
    dist[sourcebox] = 0
    prev[sourcebox] = None
    firstboxsource = (dist[sourcebox], sourcebox)
    priorityQ = PriorityQueue()
    priorityQ.put(firstboxsource)
    standing_point = source

    path = []
    while not priorityQ.empty():
        #print "I'm in an infinite loop!"
        current = priorityQ.get()
        visited.append(current[1])
        if current[1] == destbox:
            end = current[1]
            boxes = []
            while end is not None:
                boxes.append(end)
                #print prev[end]
                end = prev[end]

            stand = source
            print boxes
            for box in boxes:
                path.append((stand, find_point(stand, box)))
                stand = find_point(stand, box)
            path.append((stand, destination))
            print path
            return path, visited

        neighbors = mesh['adj'][current[1]]
        for n in neighbors:
            alt = dist[current[1]] + point_distance(
                standing_point, find_point(standing_point, current[1]))
            if n not in dist or alt < dist[n]:
                standing_point = find_point(standing_point, current[1])
                dist[n] = alt
                prior = alt + heuristic_basic(destbox, current[1])
                priorityQ.put((prior, n))
                prev[n] = current[1]
    '''#breadth first search
    vertex = None
    prev = {}
    counter = 0
    q = Queue()
    q.put(sourcebox)
    prev[sourcebox] = None
    visited.append(sourcebox)
    finalbox = None
    while not q.empty():
        v = q.get()
        if box_equal(v, destbox):
            finalbox = v
            break
        for n in mesh['adj'][v]:
            if n not in visited:
                q.put(n)
                visited.append(n)
                prev[n] = v'''
    '''end = finalbox
    path.append(((dx, dy), (get_midpoint(end))))
   
    while prev[end] is not None:
        path.append((get_midpoint(prev[end]), get_midpoint(end)))
        end = prev[end]
    path.append(((sx, sy), get_midpoint(end)))'''

    print "No path!"
    return [], visited
コード例 #42
0
	envObj.graph[b].adjList.append(Edge(a, wt))

#agent.budget =  int(raw_input())
#envObj.congestion = int(raw_input())

# Calculate heuristic-----------------------DIJKSTRA----
#We will assume that agent always takes bus except when dist<3
agent = Agent(5)	 #agent(budget)
calcHeuristic(envObj, agent)

#-------------------------------------------

for i in range(3):
	print("===============================================")
	destinationNode = None
	pq = PriorityQueue()
	pq.put(QueueNode(envObj.source, 0, 0, None, State(envObj.source,Mode.bus, agent.budget)))
	agent = Agent(10)  #agent(budget)
	envObj.congestion = (2 - i) * 50
	print("Congestion = ", envObj.getCongestion(),"%")
	print("Budget = ", agent.budget)
	print("Source = ", envObj.graph[envObj.source].coord.toString())
	print("Goal = ", envObj.graph[envObj.goal].coord.toString())
	print("BusSpeed = ", agent.getBusSpeed(envObj))
	print("BusFare = ", agent.busFare)
	print("CycleSpeed = ", agent.getCycleSpeed())
	while(not pq.empty()):
		curr = pq.get()
		# print(curr.state.toString())
		if(envObj.isGoal(curr.ind)):           #if agent reached the goal
			destinationNode = curr
コード例 #43
0
ファイル: regexbot.py プロジェクト: cequencer/ircbots
GLOBAL_FLOOD_COOLDOWN = timedelta(
    seconds=config.getint('regexbot', 'global_flood_cooldown'))
MAX_MESSAGES = config.getint('regexbot', 'max_messages')
MAX_MESSAGE_SIZE = config.getint('regexbot', 'max_message_size')
try:
    NICKSERV_PASS = str(config.get('regexbot', 'nickserv_pass'))
except:
    NICKSERV_PASS = None

message_buffer = {}
last_message = datetime.now()
last_message_times = {}
flooders = {}
ignore_list = []
channel_list = []
user_timeouts = PriorityQueue()
channel_timeouts = PriorityQueue()

if config.has_section('ignore'):
    for k, v in config.items('ignore'):
        try:
            ignore_list.append(regex.compile(str(v), regex.I))
        except Exception, ex:
            print "Error compiling regular expression in ignore list (%s):" % k
            print "  %s" % v
            print ex
            exit(1)

for channel in CHANNELS:
    c = channel.lower()
    message_buffer[c] = []
コード例 #44
0
l3 = ListNode(2)
l3.next = ListNode(6)

lists = [l1, l2, l3]

# foreachListNode(mergeKLists1(lists))

foreachListNode(mergeKLists2(lists))


def HeapSort(list):
    # 将 list 构建成堆
    heapq.heapify(list)
    heap = []
    while list:
        heap.append(heapq.heappop(lists))
    list[:] = heap
    return list


from Queue import PriorityQueue

q = PriorityQueue()
q.put(1)
q.put(5)
q.put(4)
q.put(3)

print q.task_done()
コード例 #45
0
 def __init__(self, _k):
     self.q = PriorityQueue()
     self.k = _k
     self.s = 0
     self.min = 0
コード例 #46
0
ファイル: planner.py プロジェクト: Misha91/ARO
    def aStar(self, start, goal):
        visited = []
        goal = [int(goal[0]), int(goal[1])]
        start = [int(start[0]), int(start[1])]
        frontier = PriorityQueue()
        path = []
        bestPathSoFar = [9999999, []]
        found = False
        mult = 75
        print(goal)
        #if (self.gridToInd(start) == False):
        #    print("Robot is out of map!")
        #    return None
        H = self.heuristic(start, goal)
        frontier.put((H, 0, start, [start]))

        print("H:", H, "*", mult, "=", H * mult)
        #if (self.gridToInd(goal) == False):
        #    print("Goal is out of map!")
        #    return None
        #goalInd = self.tmp
        dist = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [-1, 1], [1, -1],
                [-1, -1]]
        print(self.width * self.height)
        print(self.grid[start[0]][start[1]])
        print(self.grid[goal[0]][goal[1]])
        H = H * mult
        while not frontier.empty():
            #for i in range(0,10):
            current = frontier.get()
            #print("New iter #")
            #print(current)
            #print(frontier.qsize())
            #print(current[2], goal)
            if current[2] == goal:
                path = current[3]
                break
            if (current[2] in visited): continue
            else: visited.append(current[2])

            if (len(visited) > H): break

            for k in range(len(dist)):
                try:
                    #print("Cons: ", current[2][0] + dist[k][0], current[2][1] + dist[k][1])
                    if ([
                            current[2][0] + dist[k][0],
                            current[2][1] + dist[k][1]
                    ] == goal):
                        print("GOAL IS FOUND!")
                        found = True
                        cost = current[1] + 1
                        if (k >= 4): cost += 0.41
                        #ind_cp =
                        path_cp = current[3][:]
                        path_cp.append(goal)
                        frontier = PriorityQueue()
                        frontier.put((priority, cost, goal, path_cp))
                    else:
                        #print(current[2][0] + dist[k][0], current[2][1] + dist[k][1], dist[k], self.grid[current[2][0] + dist[k][0]][current[2][1] + dist[k][1]])
                        if (not found and
                            ((self.grid[current[2][0] +
                                        dist[k][0]][current[2][1] + dist[k][1]]
                              < 26 and self.grid[current[2][0] + dist[k][0]][
                                  current[2][1] + dist[k][1]] >= 0)
                             or len(current[3]) < self.threshould)):
                            ind = [
                                current[2][0] + dist[k][0],
                                current[2][1] + dist[k][1]
                            ]

                            if (ind not in visited):

                                cost = current[1] + 1
                                if (k >= 4): cost += 0.41
                                #print("HERE")
                                heur = self.heuristic(ind, goal)
                                priority = cost + heur
                                path_cp = current[3][:]

                                path_cp.append(ind)
                                frontier.put((priority, cost, ind, path_cp))
                                #print("ADDED: ", priority, cost, ind, path_cp)
                                if bestPathSoFar[0] > heur:
                                    bestPathSoFar = [heur, path_cp]
                                #visited.append(ind)
                                if (self.toPlot): self.plotPlan(ind)
                except:
                    pass

        print(frontier.qsize(), len(visited))
        path_fin = []

        #    else:
        #        print("WTF IS GOING ON???")
        if (len(path) == 0):
            print("CANNOT FIND A PATH!")
            print(bestPathSoFar)
            #return bestPathSoFar[1]
            if (bestPathSoFar[0] < 35
                    and len(bestPathSoFar[1]) > 12):  #self.threshould+5
                path = bestPathSoFar[1]
        if (len(path) <= 10 or len(path) > 75): path = []
        for ind2, n in enumerate(path):
            #    if (self.indToGrid(n)):
            #print(self.grid[n[0]][n[1]])
            print(ind2, n, self.grid[n[0]][n[1]])
            #path_fin.append(np.array((n[0],n[1]), dtype=float))
            if (ind2 < (self.threshould) or
                (self.grid[n[0]][n[1]] <= 26 and self.grid[n[0]][n[1]] >= 0)):
                path_fin.append(np.array((int(n[0]), int(n[1])), dtype=int))

        return path_fin
コード例 #47
0
class ConnectionPool(object):
    """
    Container holding the :class:`~elasticsearch.Connection` instances,
    managing the selection process (via a
    :class:`~elasticsearch.ConnectionSelector`) and dead connections.

    It's only interactions are with the :class:`~elasticsearch.Transport` class
    that drives all the actions within `ConnectionPool`.

    Initially connections are stored on the class as a list and, along with the
    connection options, get passed to the `ConnectionSelector` instance for
    future reference.

    Upon each request the `Transport` will ask for a `Connection` via the
    `get_connection` method. If the connection fails (it's `perform_request`
    raises a `ConnectionError`) it will be marked as dead (via `mark_dead`) and
    put on a timeout (if it fails N times in a row the timeout is exponentially
    longer - the formula is `default_timeout * 2 ** (fail_count - 1)`). When
    the timeout is over the connection will be resurrected and returned to the
    live pool. A connection that has been peviously marked as dead and
    succeedes will be marked as live (it's fail count will be deleted).
    """
    def __init__(self,
                 connections,
                 dead_timeout=60,
                 timeout_cutoff=5,
                 selector_class=RoundRobinSelector,
                 randomize_hosts=True,
                 **kwargs):
        """
        :arg connections: list of tuples containing the
            :class:`~elasticsearch.Connection` instance and it's options
        :arg dead_timeout: number of seconds a connection should be retired for
            after a failure, increases on consecutive failures
        :arg timeout_cutoff: number of consecutive failures after which the
            timeout doesn't increase
        :arg selector_class: :class:`~elasticsearch.ConnectionSelector`
            subclass to use if more than one connection is live
        :arg randomize_hosts: shuffle the list of connections upon arrival to
            avoid dog piling effect across processes
        """
        if not connections:
            raise ImproperlyConfigured("No defined connections, you need to "
                                       "specify at least one host.")
        self.connection_opts = connections
        self.connections = [c for (c, opts) in connections]
        # remember original connection list for resurrect(force=True)
        self.orig_connections = tuple(self.connections)
        # PriorityQueue for thread safety and ease of timeout management
        self.dead = PriorityQueue(len(self.connections))
        self.dead_count = {}

        if randomize_hosts:
            # randomize the connection list to avoid all clients hitting same node
            # after startup/restart
            random.shuffle(self.connections)

        # default timeout after which to try resurrecting a connection
        self.dead_timeout = dead_timeout
        self.timeout_cutoff = timeout_cutoff

        self.selector = selector_class(dict(connections))

    def mark_dead(self, connection, now=None):
        """
        Mark the connection as dead (failed). Remove it from the live pool and
        put it on a timeout.

        :arg connection: the failed instance
        """
        # allow inject for testing purposes
        now = now if now else time.time()
        try:
            self.connections.remove(connection)
        except ValueError:
            # connection not alive or another thread marked it already, ignore
            return
        else:
            dead_count = self.dead_count.get(connection, 0) + 1
            self.dead_count[connection] = dead_count
            timeout = self.dead_timeout * 2**min(dead_count - 1,
                                                 self.timeout_cutoff)
            self.dead.put((now + timeout, connection))
            logger.warning(
                'Connection %r has failed for %i times in a row, putting on %i second timeout.',
                connection, dead_count, timeout)

    def mark_live(self, connection):
        """
        Mark connection as healthy after a resurrection. Resets the fail
        counter for the connection.

        :arg connection: the connection to redeem
        """
        try:
            del self.dead_count[connection]
        except KeyError:
            # race condition, safe to ignore
            pass

    def resurrect(self, force=False):
        """
        Attempt to resurrect a connection from the dead pool. It will try to
        locate one (not all) eligible (it's timeout is over) connection to
        return to the live pool. Any resurrected connection is also returned.

        :arg force: resurrect a connection even if there is none eligible (used
            when we have no live connections). If force is specified resurrect
            always returns a connection.

        """
        # no dead connections
        if self.dead.empty():
            # we are forced to return a connection, take one from the original
            # list. This is to avoid a race condition where get_connection can
            # see no live connections but when it calls resurrect self.dead is
            # also empty. We assume that other threat has resurrected all
            # available connections so we can safely return one at random.
            if force:
                return random.choice(self.orig_connections)
            return

        try:
            # retrieve a connection to check
            timeout, connection = self.dead.get(block=False)
        except Empty:
            # other thread has been faster and the queue is now empty. If we
            # are forced, return a connection at random again.
            if force:
                return random.choice(self.orig_connections)
            return

        if not force and timeout > time.time():
            # return it back if not eligible and not forced
            self.dead.put((timeout, connection))
            return

        # either we were forced or the connection is elligible to be retried
        self.connections.append(connection)
        logger.info('Resurrecting connection %r (force=%s).', connection,
                    force)
        return connection

    def get_connection(self):
        """
        Return a connection from the pool using the `ConnectionSelector`
        instance.

        It tries to resurrect eligible connections, forces a resurrection when
        no connections are availible and passes the list of live connections to
        the selector instance to choose from.

        Returns a connection instance and it's current fail count.
        """
        self.resurrect()
        connections = self.connections[:]

        # no live nodes, resurrect one by force and return it
        if not connections:
            return self.resurrect(True)

        # only call selector if we have a selection
        if len(connections) > 1:
            return self.selector.select(self.connections)

        # only one connection, no need for a selector
        return connections[0]
コード例 #48
0
ファイル: day23.py プロジェクト: rrti/aoc
    def calc_max_coverage_coor(xmin, ymin, zmin, xmax, ymax, zmax, rmax):
        def bot_clips_bbox(bot, bbox_xmin, bbox_ymin, bbox_zmin, bbox_xmax,
                           bbox_ymax, bbox_zmax):
            ## test if this bot's octahedral sensor-volume
            ## intersects the bbox {x,y,z}min - {x,y,z}max
            assert (bbox_xmin <= bbox_xmax)
            assert (bbox_ymin <= bbox_ymax)
            assert (bbox_zmin <= bbox_zmax)

            dist = 0

            dist += (bbox_xmin - bot[0]) * (bot[0] < bbox_xmin)
            dist += (bot[0] - bbox_xmax) * (bot[0] > bbox_xmax)

            dist += (bbox_ymin - bot[1]) * (bot[1] < bbox_ymin)
            dist += (bot[1] - bbox_ymax) * (bot[1] > bbox_ymax)

            dist += (bbox_zmin - bot[2]) * (bot[2] < bbox_zmin)
            dist += (bot[2] - bbox_zmax) * (bot[2] > bbox_zmax)

            return (dist <= bot[3])

        def pq_add(pq, bbox_xmin, bbox_ymin, bbox_zmin, cur_bbox_size):
            bbox_xmax = bbox_xmin + cur_bbox_size - 1
            bbox_ymax = bbox_ymin + cur_bbox_size - 1
            bbox_zmax = bbox_zmin + cur_bbox_size - 1

            num_bots_in_bbox = 0

            for bot in bots:
                num_bots_in_bbox += bot_clips_bbox(bot, bbox_xmin, bbox_ymin,
                                                   bbox_zmin, bbox_xmax,
                                                   bbox_ymax, bbox_zmax)

            if (num_bots_in_bbox == 0):
                return False

            ## find Manhattan distance from origin to the closest corner of the bounding-box
            ## box with largest number of bots clipping it should be processed first, negate
            ## the count since PQ is constructed as a min-heap
            ## in case of ties, PQ will yield the box with smallest origin distance and size
            ## (ordering by size first and distance second also works and is slightly faster
            ## on the given input)
            ## this processing order ensures the problem is solved when reaching a 1x1x1 box
            ## since 1) no larger box can intersect more bot sensor-volumes, and 2) no other
            ## 1x1x1 box intersecting as many sensor-volumes can be closer to the origin
            ##
            min_orig_dist_x = min(abs(bbox_xmin), abs(bbox_xmax))
            min_orig_dist_y = min(abs(bbox_ymin), abs(bbox_ymax))
            min_orig_dist_z = min(abs(bbox_zmin), abs(bbox_zmax))
            min_orig_dist = min_orig_dist_x + min_orig_dist_y + min_orig_dist_z

            ##!! pq.put((-num_bots_in_bbox, min_orig_dist, cur_bbox_size, bbox_xmin, bbox_ymin, bbox_zmin))
            pq.put((-num_bots_in_bbox, cur_bbox_size, min_orig_dist, bbox_xmin,
                    bbox_ymin, bbox_zmin))
            return True

        ## determine size of the power-of-two bounding cube
        max_size = max(xmax - xmin, ymax - ymin, zmax - zmin, rmax)
        bbox_size = next_power_of_two(max_size)

        bbox_queue = PriorityQueue()

        pq_add(bbox_queue, xmin, ymin, zmin, bbox_size)

        while (bbox_queue.qsize() > 0):
            ##!! (num_bots, orig_dist, bb_size, x, y, z) = bbox_queue.get()
            (num_bots, bb_size, orig_dist, x, y, z) = bbox_queue.get()

            if (bb_size == 1):
                return ((x, y, z), orig_dist, -num_bots)

            bb_size >>= 1

            ## insert top-left corner coordinates of each child bounding-box
            pq_add(bbox_queue, x, y, z, bb_size)
            pq_add(bbox_queue, x, y, z + bb_size, bb_size)
            pq_add(bbox_queue, x, y + bb_size, z, bb_size)
            pq_add(bbox_queue, x, y + bb_size, z + bb_size, bb_size)
            pq_add(bbox_queue, x + bb_size, y, z, bb_size)
            pq_add(bbox_queue, x + bb_size, y, z + bb_size, bb_size)
            pq_add(bbox_queue, x + bb_size, y + bb_size, z, bb_size)
            pq_add(bbox_queue, x + bb_size, y + bb_size, z + bb_size, bb_size)

        return ((0, 0, 0), 0, 0)
コード例 #49
0
from Queue import Queue
from Queue import PriorityQueue
a1 = 'a1'
a2 = 'a2'
a3 = 'a3'
a4 = 'a4'
a5 = 'a5'

b1 = 'b1'
b2 = 'b2'
b3 = 'b3'
b4 = 'b4'
b5 = 'b5'

q = Queue()
pq = PriorityQueue()
for i in xrange(5):
    p = 5 - i
    q.put("a" + str(p))
    q.put("b" + str(p))

    pq.put((p, "a" + str(p)))
    pq.put((p, "b" + str(p)))

for i in xrange(5):
    p = 5 - i
    q.put("a" + str(p) + str(i + 5))
    q.put("b" + str(p) + str(i + 5))

    pq.put((p, "a" + str(p) + str(i + 5)))
    pq.put((p, "b" + str(p) + str(i + 5)))
コード例 #50
0
from pritunl import logger
from pritunl import mongo
from pritunl import listener

from Queue import PriorityQueue
import pymongo
import random
import bson
import datetime
import threading
import time
import bson
import collections

running_queues = {}
runner_queues = [PriorityQueue() for _ in xrange(3)]
thread_limits = [
    threading.Semaphore(x) for x in (
        settings.app.queue_low_thread_limit,
        settings.app.queue_med_thread_limit,
        settings.app.queue_high_thread_limit,
    )
]


def add_queue_item(queue_item):
    if queue_item.id in running_queues:
        return
    running_queues[queue_item.id] = queue_item

    logger.debug(
コード例 #51
0
class SchedulingController(object):
    """ The SchedulingController keeps track of the actions that are scheduled and calls a callback
    when the actions has to be executed. The SchedulingController handles actions as String, it is
    the callers responsibility to create a string representation of the action. """
    def __init__(self, db_filename, callback, action_timeout=60):
        new_database = not os.path.exists(db_filename)
        self.__connection = sqlite3.connect(
            db_filename,
            detect_types=sqlite3.PARSE_DECLTYPES,
            check_same_thread=False,
            isolation_level=None)
        self.__cursor = self.__connection.cursor()
        if new_database:
            self.__create_tables()

        self.__callback = callback
        self.__action_timeout = action_timeout
        self.__stop = False

        self.__input_queue = Queue()
        self.__action_queue = PriorityQueue()

        for action in self.__read_actions():
            self.__action_queue.put(action)

        self.__thread = Thread(target=self.__run,
                               name="SchedulingController thread")
        self.__thread.daemon = True

    def __create_tables(self):
        """ Create the Scheduled actions table. """
        self.__cursor.execute(
            "CREATE TABLE actions (id INTEGER PRIMARY KEY, description TEXT, "
            "action TEXT, timestamp INTEGER);")

    def __read_actions(self):
        """ Read the actions from the table. """
        ret = []
        for row in self.__cursor.execute(
                "SELECT timestamp, id, description, action FROM actions;"):
            ret.append((row[0], row[1], row[2], row[3]))
        return ret

    def __create_action(self, timestamp, description, action):
        """ Create an action with a given timestamp in the database.
        Returns the id of the action in the datbase.
        """
        self.__cursor.execute(
            "INSERT INTO actions (timestamp, description, action) VALUES (?,?,?)",
            (timestamp, description, action))
        return (timestamp, self.__cursor.lastrowid, description, action)

    def __remove_action_from_db(self, id):
        """ Remove an action from the database. """
        self.__cursor.execute("DELETE FROM actions WHERE id = ?;", (id, ))

    def __execute_action(self, id, description, action):
        """ Execute a scheduled action, delete the action from the database and execute it. """
        LOGGER.info("Executing scheduled action '%s'", description)
        self.__remove_action_from_db(id)

        def run_callback():
            """ Run the callback. """
            try:
                self.__callback(action)
            except:
                LOGGER.exception(
                    "Exception while executing scheduled action '%s'",
                    description)

        callback_thread = Thread(target=run_callback)
        callback_thread.daemon = True
        callback_thread.start()

        callback_thread.join(self.__action_timeout)
        if callback_thread.isAlive():
            LOGGER.error(
                "Scheduled action '%s' is still executing after %d sec",
                description, self.__action_timeout)

    def start(self):
        """ Start the background thread. """
        self.__thread.start()

    def stop(self):
        """ Stop the SchedulingController. """
        self.__stop = True
        self.__input_queue.put(None)
        self.__thread.join()

    def __run(self):
        """ Code for the background thread. """
        while not self.__stop:
            # Wait on the input_queue, take actions from the action_queue when scheduled.
            timeout = None

            while self.__action_queue.qsize() > 0:
                element = self.__action_queue.get()
                (timestamp, id, description, action) = element

                if timestamp <= time():
                    self.__execute_action(id, description, action)
                else:
                    timeout = timestamp - time()
                    self.__action_queue.put(element)
                    break

            try:
                value = self.__input_queue.get(True, timeout)
                if value == None:  # Stop signal !
                    continue
                elif value == REFRESH:  # Refresh the action queue
                    self.__action_queue = PriorityQueue()
                    for action in self.__read_actions():
                        self.__action_queue.put(action)
                else:  # Got a new action
                    (timestamp, description, action) = value
                    self.__action_queue.put(
                        self.__create_action(timestamp, description, action))
            except Empty:
                pass  # Timeout - do the loop

    def schedule_action(self, timestamp, description, action):
        """ Schedule a new action, that should be executed at a given timestamp. """
        self.__input_queue.put((timestamp, description, action))

    def list_scheduled_actions(self):
        """ Get a list of all scheduled actions.
        :returns: a list of dictionaries with keys (id, timestamp, description, action)
        """
        actions = self.__read_actions()
        ret = []
        for action in actions:
            ret.append({
                'timestamp': action[0],
                'id': action[1],
                'description': action[2],
                'action': action[3]
            })
        return ret

    def remove_scheduled_action(self, id):
        """ Remove a scheduled action, when the id of the scheduled action is provided. """
        self.__remove_action_from_db(id)
        self.__input_queue.put(REFRESH)

    def close(self):
        """ Commit the changes and close the database connection. """
        self.__connection.commit()
        self.__connection.close()
コード例 #52
0
ファイル: __init__.py プロジェクト: scottanderson/SiCKRAGE
class srQueue(threading.Thread):
    def __init__(self, name="QUEUE"):
        super(srQueue, self).__init__(name=name)
        self._queue = PriorityQueue()
        self.currentItem = None
        self.min_priority = 0
        self.amActive = False
        self.lock = threading.Lock()
        self.stop = threading.Event()
        self.daemon = True

    def run(self):
        """
        Process items in this queue
        """

        while not self.stop.is_set():
            with self.lock:
                self.amActive = True

                if self.currentItem is None or not self.currentItem.is_alive():
                    if self.currentItem:
                        self.currentItem = None

                    self.currentItem = self.get()
                    if self.currentItem.priority < self.min_priority:
                        self.put(self.currentItem)
                        self.currentItem = None
                    else:
                        self.currentItem.start()

                self.amActive = False

    @property
    def queue(self):
        return self._queue.queue

    def get(self, *args, **kwargs):
        _, item = self._queue.get(*args, **kwargs)
        return item

    def put(self, item, *args, **kwargs):
        """
        Adds an item to this queue

        :param item: Queue object to add
        :return: item
        """
        item.added = datetime.now()
        item.name = "{}-{}".format(self.name, item.name)
        self._queue.put((item.priority, item), *args, **kwargs)
        return item

    def pause(self):
        """Pauses this queue"""
        sickrage.srCore.srLogger.info("Pausing queue")
        self.min_priority = 999999999999

    def unpause(self):
        """Unpauses this queue"""
        sickrage.srCore.srLogger.info("Unpausing queue")
        self.min_priority = 0

    def shutdown(self):
        self.stop.set()
        try:
            self.join(10)
        except:
            pass
コード例 #53
0
import os
import sys
from Queue import PriorityQueue

pq = PriorityQueue()
visited_states = set()


class Puzzle(object):
    """
    A* search with MANHATTAN HEURISTIC
    """
    def __init__(self, init_state, goal_state):
        # you may add more attributes if you think is useful
        self.cost = 0
        self.init_state = init_state
        self.goal_state = goal_state
        self.actions = list()
        self.explored = [0]
        self.explored_tot = 0
        self.num_nodes = [0]
        self.total_prev_h_cost = 0

    def __cmp__(self, other):
        return cmp(self.cost, other.cost)

    def to_int_rep(self):
        return tuple(map(tuple, self.init_state))

    def is_solvable(self):
        """Checks if state is solvable."""
コード例 #54
0
class MulticastChat:

    multicast_group = ('224.3.29.71', 10000)
    multicast_ip = '224.3.29.71'
    hostIp = '0.0.0.0'
    pid = os.getpid()
    queueMsg = PriorityQueue()
    numberAckList = {}
    pid_list = [pid]
    time = 0
    server_address = ((hostIp, 10000))
    conversationList = []
    sendDelay = 0

    def __init__(self, numberOfProcesses):
        self.numberOfProcesses = numberOfProcesses

        self.sock = self.initConnection()
        self.synchronize(self.numberOfProcesses)

        self.pid_list.sort()
        self.pid = self.pid_list.index(self.pid)

        thread.start_new_thread(self.recvListener, ())
        print "Ready - " + self.presentationMsg()
        print "Send your message:"
        while True:
            msg = raw_input()
            if msg[:5] == "DELAY":
                stopPid = int(msg[5:].split("-s")[0])
                seconds = int(msg[5:].split("-s")[1])
                self.sendDelayMsg(stopPid, seconds)

            elif msg[:4] == "LIST":
                self.printConversationLog()
            elif msg[:3] == "PID":
                print self.presentationMsg()
            elif msg[:3] == "STP":
                pidStop = int(msg[4:].split("-s")[0])
                seconds = int(msg[4:].split("-s")[1])
                self.sendStopMsg(pidStop, seconds)
            else:
                self.sendMsg(msg)
        print "end"

    def presentationMsg(self):
        return "I'm the process no: " + str(self.pid)

    def printConversationLog(self):
        for i in self.conversationList:
            print i

    def sendDelayMsg(self, pidDelay, seconds):
        msg = "DELAY " + str(pidDelay) + " -s" + str(seconds)
        self.sock.sendto(msg, self.multicast_group)

    def initConnection(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        membership = socket.inet_aton(self.multicast_ip) + socket.inet_aton(
            self.hostIp)

        sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
                        membership)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

        sock.bind(self.server_address)

        return sock

    def synchronize(self, numberOfProcesses):
        print '\nSynchronizing..'

        #print 'Recv %s'%self.pid
        synchroMsg = 'SYN ' + str(self.pid)

        while len(self.pid_list) < self.numberOfProcesses:
            self.sock.sendto(synchroMsg, self.multicast_group)
            data, address = self.sock.recvfrom(1024)
            pidRecv = int(data[4:])

            if not (pidRecv in self.pid_list):
                self.pid_list.append(pidRecv)
                #print 'Recv %s'%data

            self.sock.sendto(synchroMsg, self.multicast_group)

    def sendStopMsg(self, pidNo, seconds):
        msg = "STP " + str(pidNo) + " -s " + str(seconds)
        self.sock.sendto(msg, self.multicast_group)

    def sendMsg(self, msg):
        self.time += 1
        msgTosend = "MSG ID:" + str(self.time) + "-" + str(
            self.pid) + "\r\nContent:" + msg
        #print "sended: "+msgTosend

        if self.sendDelay > 0:
            print "delaying on send for %d seconds" % self.sendDelay
            time.sleep(self.sendDelay)
            self.sendDelay = 0

        self.sock.sendto(msgTosend, self.multicast_group)

    def recvMsg(self, msg, msgId, timestamp, senderId):
        msgGuard = str(senderId) + " - Time " + str(
            timestamp) + ": " + msg.split(":")[1]
        self.queueMsg.put((msgId, msgGuard))

        self.time = 1 + max(self.time, timestamp)
        self.ackMsg(msgId)

    def ackMsg(self, msgId):

        self.time += 1
        ack = "ACK " + str(msgId) + ":" + str(self.time)

        if self.sendDelay > 0:
            print "delaying on ACK for %d seconds" % self.sendDelay
            time.sleep(self.sendDelay)
            print "sending"
            self.sendDelay = 0

        self.sock.sendto(ack, self.multicast_group)

    def recvAck(self, msgId, timestamp):
        self.time = 1 + max(self.time, timestamp)

        if msgId in self.numberAckList:
            self.numberAckList[msgId] += 1
        else:
            self.numberAckList[msgId] = 1

    def deliverMsg(self, msgId):

        if self.numberAckList[msgId] < self.numberOfProcesses:
            return

        time, msg = self.queueMsg.get(False)

        if time != msgId:
            self.queueMsg.put((time, msg))
            return

        self.conversationList.append(msg)
        print msg
        self.time += 1

        del self.numberAckList[msgId]

        while not (self.queueMsg.empty()):
            time, msg = self.queueMsg.get(False)
            if (self.numberAckList[time] < self.numberOfProcesses):
                self.queueMsg.put((time, msg))
                break

            self.conversationList.append(msg)
            print msg
            self.time += 1
            del self.numberAckList[time]

    def recvListener(self):
        while True:

            data, address = self.sock.recvfrom(1024)
            cmd = data[:3]

            if data[:5] == "DELAY":
                stopPid = int(data[5:].split("-s")[0])
                seconds = int(data[5:].split("-s")[1])

                if (stopPid == self.pid):
                    self.sendDelay = seconds
                    print "Delayed on send"

            elif cmd == "ACK":
                msgID = int(data.split(":")[0][4:])
                timestamp = int(data.split(":")[1])
                self.recvAck(msgID, timestamp)
                self.deliverMsg(msgID)

            elif cmd == "MSG":
                msgId, contentMsg = data.split("\r\n")
                timeRecv = int(msgId[7:].split("-")[0])
                senderId = int(msgId[7:].split("-")[1])
                msgId = int("".join(msgId[7:].split("-")))
                content = str(msgId) + ": " + contentMsg[8:]

                self.recvMsg(content, msgId, timeRecv, senderId)

            elif cmd == "STP":
                stopPid = int(data[4:].split("-s")[0])
                seconds = int(data[4:].split("-s")[1])
                if stopPid != self.pid:
                    continue
                print "Stopped to listen for %d seconds" % seconds
                time.sleep(seconds)
                print "Woke up"
コード例 #55
0
ファイル: queue.py プロジェクト: newtratip/rje
 def __init__(self):
     self._queue = PriorityQueue()
コード例 #56
0
 def get(self):
     tup = PriorityQueue.get(self)
     newtup = tup[0] * -1, tup[1], tup[2]
     return newtup
コード例 #57
0
ファイル: a_star.py プロジェクト: lwei0402/ee183da
	def __init__(self, start, goal):
		self.path = []
		self.visitedQueue = []
		self.priorityQueue = PriorityQueue()
		self.start = start
		self.goal = goal
コード例 #58
0
 def __init__(self, maxlength=0, length_tol=1000):
     self.pq = PriorityQueue()
     self.decrease_length = multiprocessing.Lock()
     self.maxlength = maxlength
     self.length_tol = length_tol
コード例 #59
0
    def compute(self, start, goal):
        frontier = PriorityQueue()
        frontier.put((0, start))

        frontier_nodes = [start]
        already_explored = [start]

        came_from = {start: None}

        cost_so_far = {start: 0}

        while not frontier.empty():
            queue_entry = frontier.get()
            current = queue_entry[1]

            if self.draw and (current in frontier_nodes):
                frontier_nodes.remove(current)

            for next_node in current.neighbors:
                # Get the current node from the graph
                next_node = self._graph[next_node]
                new_cost = cost_so_far[current] + 1
                #self._costmap.data[current.y * self._costmap.info.height + current.x]

                if (next_node not in cost_so_far) or (new_cost <
                                                      cost_so_far[next_node]):
                    cost_so_far[next_node] = new_cost
                    priority = new_cost + self.heuristic(next_node, goal)
                    frontier.put((priority, next_node))
                    came_from[next_node] = current

                    if self.draw and (next_node not in frontier_nodes):
                        frontier_nodes.append(next_node)

                if current not in already_explored:
                    already_explored.append(current)

            # Update visualization
            if self.draw:
                self.drawNodes(frontier_nodes, "frontier")
                self.drawNodes(already_explored, "explored")

            if current == goal:
                break

        # Work our way back through the came_from chain from the goal to the start
        path = [goal]
        previous_node = came_from[goal]
        while previous_node is not None:
            path.insert(0, previous_node)
            previous_node = came_from[previous_node]

        self.drawNodes(path, "path")

        # Compute ros Path
        waypoints = []
        index = 0
        current_dir = None
        for node in path:
            try:
                next_node = path[index + 1]

                # If we've changed directions, and add a waypoint if so
                this_dir = math.atan2((next_node.y - node.y),
                                      (next_node.x - node.x))
                if current_dir == None or this_dir != current_dir:
                    current_dir = this_dir

                    # copied some stuff from
                    # https://www.programcreek.com/python/example/70252/geometry_msgs.msg.PoseStamped
                    pose = PoseStamped()
                    pose.header.seq = len(waypoints)
                    pose.header.frame_id = "map"
                    pose.header.stamp = rospy.Time.now()

                    (xc, yc) = self.mapCoordsToWorld(node.x, node.y)

                    pose.pose.position.x = xc
                    pose.pose.position.y = yc
                    pose.pose.position.z = 0

                    q = quaternion_from_euler(0.0, 0.0, current_dir)
                    pose.pose.orientation = Quaternion(*q)

                    waypoints.append(pose)

                index += 1
            except IndexError:
                # We've reached the end of the loop, add last waypoint
                pose = PoseStamped()
                pose.header.seq = len(waypoints)
                pose.header.frame_id = "map"
                pose.header.stamp = rospy.Time.now()

                (xc, yc) = self.mapCoordsToWorld(node.x, node.y)

                pose.pose.position.x = xc
                pose.pose.position.y = yc
                pose.pose.position.z = 0

                q = quaternion_from_euler(0.0, 0.0, current_dir)
                pose.pose.orientation = Quaternion(*q)

                waypoints.append(pose)

        path_msg = Path()
        path_msg.header.frame_id = "map"
        path_msg.header.stamp = rospy.Time.now()
        path_msg.poses = waypoints

        return path_msg
コード例 #60
0
    def __init__(self, limit = 0):
        PriorityQueue.__init__(self, limit)

	self.max_prio = 0
	self.none_send = False