Пример #1
0
class LazyQueue:

    def __init__(self):
        self._queue = PriorityQueue()

    def enq(self, item):
        self._queue.put(item)

    def deq(self):
        if self._queue.empty():
            return None
        else:
            return self._queue.get()

    def __len__(self):
        return self._queue.qsize()

    def mget(self, n):
        if n > self._queue.qsize():
            n = self._queue.qsize()
        lst = []
        for _ in xrange(n):
            lst.append(self._queue.get())

        return lst

    def extend(self, iterable):
        for ele in iterable:
            self.enq(ele)

    def append(self, item):
        self.enq(item)
Пример #2
0
class BHCAgenda(object):
    """
        Maintain a priority queue for possible merges
    """
    def __init__(self, sufficient_condition=None):
        if not sufficient_condition:
            self.sufficient_condition = lambda score: True
        else:
            self.sufficient_condition = sufficient_condition
        self.priority_queue = PriorityQueue()
        self.graveyard = set()

    def add_many(self, iterable):
        for item in iterable:
            pass

    def pop(self):
        score, (left_index, right_index) = self.priority_queue.get()
        # print "popped with score %2.5f" % score
        # either left or right index is in graveyard, then draw  another
        # or score is not good enough
        # but no matter what, the queue size must be larger than 0
        while ((left_index in self.graveyard or
               right_index in self.graveyard or
               not self.sufficient_condition(score)) and
               self.priority_queue.qsize() > 0):
            # print 'trying a second time. '
            score, (left_index, right_index) = self.priority_queue.get()
            # print 'score this time: %2.5f' % score
        # avoiding queue errors
        if (   (left_index in self.graveyard or
               right_index in self.graveyard or
               not self.sufficient_condition(score)) and
               self.priority_queue.qsize() == 0):
            return  0,0,0

        # bookkeep seen things
        self.graveyard.add(left_index)
        self.graveyard.add(right_index)

        return score, left_index, right_index

    def clear(self):
        self.priority_queue = PriorityQueue()
        self._put = self.strategy(self.priority_queue)
        self.graveyard = set()
        self.manifest = set()

    def push(self, score, left, right):
        """
            The queue needs to have the score as the first item in the tuple
            This is how it orders the queue
        """
        self.priority_queue.put((score, (left, right)))

    def __len__(self):
        return self.priority_queue.qsize()
Пример #3
0
	def apply_idastar(self,start,goal,bound,debug=False):
		closedList = Set([])
		openList = PriorityQueue()
		self.cameFrom  = {}
		self.gScore = {}
		self.fScore = {}
		xstart = self.encode(start)
		xgoal = self.encode(goal)
		self.gScore[xstart] = 0
		self.fScore[xstart] = self.heuristic_cost_estimate(start,goal)

		count = 1
		openList.put((self.fScore[xstart],count,xstart))

		while openList.qsize() > 0:
			fcurrent,other,xcurrent = openList.get()
			current = self.decode(xcurrent)
			if xcurrent == xgoal:
				return True,self.reconstruct_path(goal),openList.qsize()+len(closedList)
			
			closedList.add(xcurrent)
			neighbours = self.get_neighbours(current)
			next_bound = sys.maxsize
			for n in neighbours:
				xn = self.encode(n)
				if xn in closedList:
					continue
				tentative_gScore = self.get_gScore(xn) + 1
				tentative_fScore = tentative_gScore + self.heuristic_cost_estimate(n,goal)

				if tentative_fScore > bound:
					if next_bound > tentative_fScore:
						next_bound = tentative_fScore
					continue

				neighbourInOpnedList = False
				for value,cnt,xitem in openList.queue:
					if xitem == xn:
						neighbourInOpnedList = True
						break
				if neighbourInOpnedList == False:
					openList.put((tempScore,count,xneighbour))

				#If this is not a better path
				elif tentative_gScore >= self.get_gScore(xn):				
					continue
				
				self.cameFrom[xn] = current
				self.gScore[xn] = tentative_gScore
				self.fScore[xn] = tentative_fScore
		return False,next_bound,len(closedList)+openList.qsize()
def plot_method_pairs_and_matrix(case_studies,fileappend=''):
    case_cov=np.cov(case_studies.transpose())
    case_corr=np.corrcoef(case_studies.transpose())
    cmatrix= case_corr
    fig = plt.figure(figsize=(twocol,twocol),dpi=figdpi,tight_layout=True)
    ax = fig.add_subplot(111)
    ppl.pcolormesh(fig,ax,cmatrix[inds][:,inds],#-np.diag([.99]*len(meths)),
                   yticklabels=np.array(mindex)[inds].tolist(),
                   xticklabels=np.array(mindex)[inds].tolist(),
                   cmap=ppl.mpl.cm.RdBu,vmax=0.4,vmin= -0.4)
    ax.tick_params(axis='both', which='major', labelsize=8)
    plt.setp(ax.get_xticklabels(), rotation='vertical')
    cm=dark2
    [l.set_color(cm[m_codes[mindex.index(l.get_text())]]) for i,l in enumerate(ax.get_yticklabels())]
    [l.set_color(cm[m_codes[mindex.index(l.get_text())]]) for i,l in enumerate(ax.get_xticklabels())]
    fig.show()
    fig.savefig(figure_path+('method_matrix%s.pdf'%fileappend))
    
    #Show the highly correlated methods
    pq=PQ()
    pq_cross=PQ()
    for i in range(len(meths)):
        for j in range(i+1,len(meths)):
            m1text='(%s) %s'%(meths[i,1],meths[i,0])
            m2text='(%s) %s'%(meths[j,1],meths[j,0])
            pq.put((-cmatrix[i,j],(m1text,m2text)))
            if meths[i,1]!= meths[j,1]:
                pq_cross.put((-cmatrix[i,j],(m1text,m2text)))
    
    # Output the method correlations
    # Sets how many highly correlated methods should be displayed
    print_cap = 20
    moutfile = open(results_path+('method_corrs%s.csv'%fileappend),'w')
    print 'All methods:'
    for i in range(pq.qsize()):
        v,(m1,m2)=pq.get()
        if i < print_cap:
            print '%.2f & %s & %s\\\\'%(-v,m1,m2)
        moutfile.write('%.9f | %s | %s\n'%(-v,m1,m2))
    moutfile.close()
    
    moutfile = open(results_path+('method_corrs_cross%s.csv'%fileappend),'w')
    print 'Just cross methods:'
    for i in range(pq_cross.qsize()):
        v,(m1,m2)=pq_cross.get()
        if i < print_cap:
            print '%.2f & %s & %s\\\\'%(-v,m1,m2)
        moutfile.write('%.9f | %s | %s\n'%(-v,m1,m2))
    moutfile.close()
Пример #5
0
def dijkstra(graph, source, target):
    """Dijkstra's shortest path algorithm"""
    queue = PriorityQueue()
    dist = {source: 0}
    prev = {}

    for vertex in graph:
        if vertex != source:
            dist[vertex] = float("inf")
        queue.put((dist[vertex], vertex))

    while queue.qsize() != 0:
        u_dist, u = queue.get()
        u_node = graph[u]

        if u == target:
            break

        for v in u_node['linkTo']:
            alt = dist[u] + euclidean_dist(u_node['x'], u_node['y'],
                                           graph[v]['x'], graph[v]['y'])
            if alt < dist[v]:
                dist[v] = alt
                prev[v] = u
                queue.put((alt, v))

    path = []
    curr = target
    while curr in prev:
        path.append(curr)
        curr = prev[curr]
    path.append(source)
    return path[::-1]
Пример #6
0
class SendTaskQueue:
    def __init__(self):
        self.queue = PriorityQueue()

    def put_task(self, task):
        self.queue.put(task)

    @sms_exception
    def fetch_sms(self, count=100):
        msg_list = []
        qsize = self.queue.qsize()
        for _ in range(qsize):
            task = self.queue.get()  # task is instance of SimpleSendTask or CommonSendTask
            try:
                ms = task.fetch_sms(count)
                if len(ms) > 0:
                    msg_list += ms
                    count -= len(ms)
                if task.get_size() > 0:
                    self.queue.put(task)
            except:
                log("SMS_SEND_TASK_FETCH_SMS_ERROR", content=task.__dict__, logger='sms', level='error')
                if not SendTaskClear.all_done(task.task):
                    self.queue.put(task)
            if count <= 0:
                break
        return msg_list
Пример #7
0
class QueueBackend(QueueInterface):
    def __init__(self, unique=False, **kwargs):
        super(QueueInterface, self).__init__(**kwargs)
        self.queue_object = PriorityQueue()
        self.unique = unique
        self.unique_dict = {}

    def put(self, task, priority):
        if self.unique:
            key = unique_key(task)
            if key in self.unique_dict:
                return
            self.unique_dict[key] = True
        self.queue_object.put((priority, task))

    def get(self, timeout):
        priority, task = self.queue_object.get(True, timeout)
        if self.unique:
            key = unique_key(task)
            del self.unique_dict[key]
        return task

    def size(self):
        return self.queue_object.qsize()

    def clear(self):
        try:
            while True:
                self.queue_object.get(False)
        except Empty:
            pass
class Astar:
    def __init__(self, start, goal, n):
        self.path = []
        self. visited = []
        self.priority_queue = PriorityQueue()
        self.start = start
        self.goal = goal
        self.n = n

    def solve(self):
        start_state = State(value=self.start, start=self.start, goal=self.goal, n=self.n)

        self.priority_queue.put((0, start_state))
        while(not self.path and self.priority_queue.qsize()):
            closest_child = self.priority_queue.get()[1]
            closest_child.create_children()
            self.visited.append(closest_child.value)
            for child in closest_child.children:
                if child.value not in self.visited:
                    # If child's value is equal to goal, solution is found
                    if child.value == self.goal:
                        self.path = child.path
                        break
                    self.priority_queue.put((child.cost, child))
        if not self.path:
            print "Goal is unreachable. Terminating program."
        return self.path
def from_dict_to_prioqueue_urlset(dict,starting_url) :
    prioq = PriorityQueue()
    urlset = set()
    #first key is going to be the start url -> initiate with 0 distance (key)
    is_start_url = True
    for key in dict:
        urlset.add(key.lower())
        if key.lower() == starting_url.lower() and is_start_url:
            key_tnode = TreeNode(0, key.lower())
            prioq.put_nowait(key_tnode)
            prioq.queue.sort(key=attrgetter("key"))
            is_start_url = False
        else:
            key_tnode = TreeNode(float("inf"), key.lower())
            prioq.put_nowait(key_tnode)
        # for each of the urls in its val, check if tnode already exists in circnode ring
        for val_url in dict[key]:
            urlset.add(val_url.lower())
            # if it doesn't exist, add the tnode
            val_tnode = TreeNode(float("inf"), val_url.lower())
            if not pq_member(prioq, val_url.lower()) :
                prioq.put_nowait(val_tnode)
                prioq.queue.sort(key=attrgetter("key"))
            key_tnode.neighbors.append(val_tnode)
    print "\tmin key: %g, url: %s" % (
        prioq.queue[0].key, prioq.queue[0].self_url)
    print "\tsize of prioqueue: %d" % prioq.qsize()
    print "\tsize of dict: %d" % len(dict)
    return prioq, urlset
Пример #10
0
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])
Пример #11
0
class AStarSolver(object):
    """docstring for AStarSolver"""
    def __init__(self, start, goal):
        self.path = []
        self.visited_queue = []
        self.priority_queue = PriorityQueue()
        self.start = start
        self.goal = goal

    def solve(self):
        start_state = StateString(
                        self.start,
                        0,
                        self.start,
                        self.goal
                        )
        self.priority_queue.put((0,start_state))
        while not self.path and self.priority_queue.qsize():
            closest_child = self.priority_queue.get()[1]
            closest_child.create_children()
            self.visited_queue.append(closest_child.value)
            for child in closest_child.children:
                if child.value not in self.visited_queue:
                    if not child.dist:
                        self.path = child.path
                        break
                    self.priority_queue.put((child.dist, child))
        if not self.path:
            print "Could not reach the goal: " + self.goal
        return self.path
Пример #12
0
def main():
    global CREATURE_COUNT
    if process_command_line():
        c0 = []
        c1 = PriorityQueue()
        print "[+]\tLoading DB..."
        # load in creatures from DB
        lc = load_DB()
        # f = open('database', 'a')
        # f.close()
        # f = open('database')
        # lc = f.read()
        # f.close()
        loaded_creatures = lc.split("\n")
        # finish loading
        print "[+]\tSuccess"
        print "[+]\tCreating initial batch of creatures..."
        cl = create_creatures(CREATURE_COUNT, GENOME_LENGTH, tools)
        generation = 0
        for i in cl:
            c1.put((100, i))
        for i in loaded_creatures:
            c1.put((50, Creature(0, i)))
            for ii in range(0, GENE_POOL_INFLUENCE - 1):
                c1.put((50, Creature(0, mutate(i))))
        print "[+]\tSuccess"
        # print '[+]\tPre-breeeding in loaded creatures with the population' +\
        #    ' for great success'
        # while not c1.empty():
        #    c = c1.get()[1]
        #    c0.append(c)
        # c1 = breed_it(c0)
        # c1 = c0
        print "[+]\tSuccess"
        exploit_found = 0
        while exploit_found == 0:
            generation += 1
            CREATURE_COUNT = c1.qsize()
            print "[>]\tRunning with creature_count %d,\tgeneration %d" % (CREATURE_COUNT, generation)
            c2 = PriorityQueue(0)
            cached_c = 0
            total_c = 0
            while not c1.empty():
                c = c1.get()[1]
                total_c += 1
                if c.modified == 0:
                    cached_c += 1
                if fitnessfunction(c) == 1:
                    exploit_found = 1
                    break
                c2.put((c.score, c))
            # print '[i]\tEfficiency %s, cached[%d], total[%d]' %\
            # (str((total_c-cached_c) * 1.0 / total_c),cached_c,total_c)
            c3 = cull_it(c2)
            c4 = []
            while not c3.empty():
                c = c3.get()[1]
                c4.append(c)
            c1 = breed_it(c4)
        print "[i]\tExploit found in %d seconds with %d requests" % (abs(int(start_time - time.time())), REQ_TOTAL)
Пример #13
0
	def generate_policy(self,end,reduced_map=None):
		if not reduced_map:
			reduced_map = self.default_graph
		h,w = len(reduced_map),len(reduced_map[0])
		DIRS = [-1,0],[0,1],[1,0],[0,-1]
		distances = [[sys.maxint] * w for _ in range(h)]
		directions = [[-1] * w for _ in range(h)]
		distances = PriorityQueue()
		distances.put((0,end))
		visited = set()
		todo = {}
		while(not distances.empty()):
			if not distances.qsize() % (h*w/5):
				print "{0} nodes visited".format(len(visited))
			min_dist,min_node = distances.get()
			if min_node not in visited:
				for i,direction in enumerate(DIRS):
					candidate = (min_node[0] + direction[0],min_node[1] + direction[1])
					if candidate[0] >= 0 and candidate[0] < h and candidate[1] >=0 and candidate[1] < w and candidate not in visited and not reduced_map[candidate[0]][candidate[1]]:
						if candidate not in todo or min_dist + 1 < todo[candidate]: 
							distances.put((min_dist + 1,candidate))	
							todo[candidate] = min_dist + 1
							directions[candidate[0]][candidate[1]] = DIRS[(i+2)%4]
				visited.add(min_node)
		return directions
class AStar_Solver:
	def __init__(self, start, goal):
		''' Store the start and goal of program, and set up vars '''
		self.path = []
		self.visitedQueue = []
		self.priorityQueue = PriorityQueue()
		self.start = start
		self.goal = goal

	def Solve(self):
		''' Create start state, then organize children 
		based on value and check children of highest value child '''
		startState = State_String(self.start, 
									0, 
									self.start, 
									self.goal)
		count = 0
		self.priorityQueue.put((0, count, startState))
		while(not self.path and self.priorityQueue.qsize()):
			closestChild = self.priorityQueue.get()[2]
			closestChild.CreateChildren()
			self.visitedQueue.append(closestChild.value)
			for child in closestChild.children:
				if child.value not in self.visitedQueue:
					count += 1
					if not child.dist:
						self.path = child.path
						break
					self.priorityQueue.put((child.dist, count, child))
		if not self.path:
			print ("Goal of " + self.goal + " is not possible!")
		return self.path
Пример #15
0
class BreakpointSolver:
    """Solver that finds the smallest amount of generations needed to get from
    one fruit fly to one with a different genome"""
    def __init__(self, start, goal):
        self.path = []
        self.unique_mutations_checked = []
        self.priority_queue = PriorityQueue()
        self.start = start
        self.goal = goal
        self.moved_genes = 0

    def solve(self):
        fruit_fly = FruitFlyMutation(self.start, 0, self.start, self.goal)
        count = 0
        self.priority_queue.put((0, count, fruit_fly))
        while (not self.path and self.priority_queue.qsize()):
            closest_child = self.priority_queue.get()[2]
            closest_child.create_children()
            self.unique_mutations_checked.append(closest_child.genome)
            for child in closest_child.children:
                if child.genome not in self.unique_mutations_checked:
                    count +=1
                    if child.genome == self.goal:
                        self.path = child.path
                        self.moved_genes = child.moved_genes
                        break
                    self.priority_queue.put((child.generation, count,child))

        if not self.path:
            print ("Goal of ", self.goal,
                  " is not possible for this starting genome!")
        return self
Пример #16
0
def create_huffman_tree(word_counts):
    """Make a huffman tree from a dictionary containing word counts.

    This method creates a binary huffman tree, that is required for
    :class:`BinaryHierarchicalSoftmax`.
    For example, ``{0: 8, 1: 5, 2: 6, 3: 4}`` is converted to
    ``((3, 1), (2, 0))``.

    Args:
        word_counts (``dict`` of ``int`` key and ``int`` or ``float`` values.): 
            Dictionary representing counts of words.

    Returns:
        Binary huffman tree with tuples and keys of ``word_coutns``.
    """
    if len(word_counts) == 0:
        raise ValueError('Empty vocabulary')

    q = PriorityQueue()
    for w, c in word_counts.iteritems():
        q.put((c, w))

    while q.qsize() >= 2:
        (count1, word1) = q.get()
        (count2, word2) = q.get()
        count = count1 + count2
        tree = (word1, word2)
        q.put((count, tree))

    return q.get()[1]
Пример #17
0
	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
 def crossOver(self):
     """
     Fill in the rest of the population by 'mating' pairs of chromosomes
     """
     # calculate fitness scores
     self.calculateFitness()
     # create an empty priority queue for the new generation
     newGeneration = PriorityQueue()
     # keep going until we've got a full population
     while self.generation.qsize() + newGeneration.qsize() < self.populationSize:
         # get a chromosome
         chromosome1 = self.generation.get()
         # get a second chromosome
         if not self.generation.empty():
             chromosome2 = self.generation.get()
         else:
             break
         # copy the chromosome
         copy1 = copy.deepcopy(chromosome1)
         copy2 = copy.deepcopy(chromosome2)
         # mutate the copies
         copy1.crossOver(copy2)
         # put the chromosomes in the new generation
         newGeneration.put(chromosome1)
         newGeneration.put(chromosome2)
         newGeneration.put(copy1)
         newGeneration.put(copy2)
     # keep the new generation
     while not newGeneration.empty():
         chromosome = newGeneration.get()
         self.generation.put(chromosome)
class AStarSolver:
    def __init__(self,start, goal):
        self.path = []
        self.visited_queue = []
        self.priority_queue = PriorityQueue()
        self.start = start
        self.goal = goal

    def solve(self):
        start_state = StateString(self.start, 0, self.start, self.goal)
        count = 0
        self.priority_queue.put(0, count, start_state)
        while not self.path and self.priority_queue.qsize():
            closest_child = self.priority_queue.get()[2]
            closest_child.create_children()
            self.visited_queue.append(closest_child.value)
            for child in closest_child.children:
                if child.value not in self.visited_queue:
                    count += 1
                    if not child.dist:
                        self.path = child.path
                        break
                    self.priority_queue.put(child.dist, count)
        if not self.path:
            print "Goal of {0} is not possible!".format(self.goal)
        return self.path
Пример #20
0
def ucs(wam, start, goal):
    pQueue = PriorityQueue()
    vertices = set()

    # Start node has a priority of 0
    pQueue.put((0, start))

    while pQueue.qsize() > 0:
        path = pQueue.get()
        node = path[-1][-1]
        vertices.add(node)

        if node == goal:
            path = str(path)
            path = [item for sublist in path for item in sublist if ord(item) >=\
                    65 and ord(item) <= 90]
            return path

        # Get connected nodes.
        for localNode in wam[node]:
            if not localNode in vertices:
                new_path = list(path)
                new_path.append(localNode)

                # Append node(s) with priority of final node.
                priority = wam[node][localNode]

                # Flatten the path.
                pQueue.put((priority, new_path))

    return "Search failed."
Пример #21
0
    def dikstra(self, start):
        """Return shortest path according to Dikstra's algorithm"""
        # Find the node with the start value:
        start_node = self._get_node(start)
        start_node.distance = 0
        # Create priority queue
        dpq = PriorityQueue()
        dpq.put((start_node.distance, start_node))

        while dpq.qsize() > 0:
            curr = dpq.get()[1]
            curr.visited = True
            for neighb in self._neighbors(curr.value):
                if not neighb.visited:
                    alt = (curr.distance +
                           self.weight_edge(curr.value, neighb.value))
                    if alt < neighb.distance:
                        neighb.distance = alt
                        neighb.previous = curr
                        dpq.put((neighb.distance, neighb))
        # Create list of shortest path from start to final
        shortest = [start]
        while curr.value != start:
            shortest.insert(1, curr.value)
            curr = curr.previous
        return shortest
Пример #22
0
class AStar_Solver:
    def __init__(self,start,goal):
        self.path = []
        self.visitedQueue = []
        self.priorityQueue = PriorityQueue()
        self.start = start
        self.goal = goal

    def Solve(self):
        startState = State_String(self.start,0,self.start,self.goal)
        count = 0
        self.priorityQueue.put((0,count,startState))
        while(not self.path and self.priorityQueue.qsize()):
            closestChild = self.priorityQueue.get()[2]
            closestChild.CreateChildren()
            self.visitedQueue.append(closestChild.value)
            for child in closestChild.children:
                if child.value not in self.visitedQueue:
                    count +=1
                    if child.value == self.goal:
                        self.path = child.path
                        break
                    self.priorityQueue.put((child.dist,count,child))

        if not self.path:
            print "Goal of ", self.goal, " is not possible for this starting genome!"
        return self.path
Пример #23
0
def getLocalOutlierFactors(vects, k):
	
	#TMP HACK
	return [0]*len(vects)
	pairDist = getPairwiseDist(vects)
	

	#The list of neighbor ids for each point
	neighbors = [None for x in range(len(vects))]
	#Populate each of these lists with the k nearest neighbors
	for i in range(len(vects)):
		pq = PriorityQueue()
		for j in range(len(vects)):
			if(i != j):
				pq.put((-pairDist[i][j], j))
				if(pq.qsize() > k):
					pq.get()
		neighbors[i] = [neighborId for (negDist, neighborId) in pq.queue]
		
	#print("neighbors " + str(neighbors))
	
	#For each point, the distance to its kth nearest neighbor
	kDist = [0] * len(vects)
	for i in range(len(vects)):
		for j in neighbors[i]:
			kDist[i] = max(kDist[i], pairDist[i][j])
	
	#print("kdist " + str(kDist))
			
			
	
	#local reachability density - the inverse of the average reachability distance to all of my neighbors
	lrd = [0] * len(vects)
	
	for i in range(len(vects)):
		#first compute the sum
		for j in neighbors[i]:
			lrd[i] += max(kDist[j], pairDist[i][j])
		#normalize and invert in one step
		lrd[i] = k / lrd[i]
	
	#print("lrd " + str(lrd))
	
	#local outlier factor - average ratio  of neighbor densities to my density
	lof = [0] * len(vects)
	for i in range(len(vects)):
		#first compute the sum
		for j in neighbors[i]:
			lof[i] += lrd[j]
		#normalize and divide by my density
		lof[i] = lof[i] / (k*lrd[i])
	
	#print("lof " + str(lof))
	
	return lof
	
	
	
	
Пример #24
0
class ChartAgenda(object):
    CKY_STRATEGY = "CKY"

    def __init__(self, strategy):
        """ Put strategies into here.
            Add their name as a constant
            Add their function as class function """
        strategy_options = {self.CKY_STRATEGY:ChartAgenda._shortest_str_strategy}
        self.strategy = strategy_options[strategy]
        self.settings = config.ChartSettings.default()
        self.priority_queue = PriorityQueue()
        self._put = self.strategy(self.priority_queue)
        self.graveyard = set()
        self.manifest = set()

    @staticmethod
    def _shortest_str_strategy(priority_queue):
        def put(item):
            priority_queue.put((len(item), item))
        return put

    def _existence_check(self, item):
        gravecheck = item in self.graveyard
        manifestcheck = item in self.manifest
        return manifestcheck or gravecheck

    def _length_check(self, item):
        if self.settings.aggressively_prune:
            if item.boundaries[1] > self.settings.utter_len:
                return False
        return True

    def add_many(self, iterable):
        for item in iterable:
            if not self._existence_check(item):
                self._put(item)
                self.manifest.add(item)

    def add(self, item):
        if not self._existence_check(item) and self._length_check(item):
            self._put(item)
            self.manifest.add(item)

    def pop(self):
        edge_len, edge = self.priority_queue.get()
        self.manifest.remove(edge)
        self.graveyard.add(edge)
        return edge

    def clear(self):
        self.priority_queue = PriorityQueue()
        self._put = self.strategy(self.priority_queue)
        self.graveyard = set()
        self.manifest = set()

    def __len__(self):
        return self.priority_queue.qsize()
Пример #25
0
def find_nearest_neighbors(origin_node, num_neighbors, on_forward_graph=True):
    # maintain set of boundary  nodes that have been visited by this search

    
    visited_nodes_cost = {}
    origin_node.cost = 0
    

    # Initialize Dijkstra queue with the origin node
    nodes_to_search = PriorityQueue()
    nodes_to_search.put((0, origin_node))

    expanded_count = 0
    max_pq_size = 0

    while(not nodes_to_search.empty()):
        # Get the nearest node from the priority queue
        max_pq_size = max(nodes_to_search.qsize(), max_pq_size)
        (cost, node) = nodes_to_search.get()
        expanded_count += 1

        visited_nodes_cost[node] = cost
            
        if(len(visited_nodes_cost) >= num_neighbors):
            break
        
        connecting_links = None
        if on_forward_graph:
            connecting_links = node.backward_links
        else:
            connecting_links = node.forward_links
        # Propagate to neighbors on the forward graph using the backward links
        for connecting_link in connecting_links:
            neighbor = None
            if on_forward_graph:
                neighbor = connecting_link.origin_node
            else:
                neighbor = connecting_link.connecting_node

            
            proposed_cost = node.cost + connecting_link.time
            if(proposed_cost < neighbor.cost):
                neighbor.cost = proposed_cost
                nodes_to_search.put((proposed_cost, neighbor))


    #cleanup
    for node in visited_nodes_cost:
        node.cost = float('inf')
    
    for (cost,node) in nodes_to_search.queue:
        node.cost = float('inf')


    # Now, all origin nodes (and some other nodes) all know their distance from
    # the given origin_node
    return visited_nodes_cost
Пример #26
0
def compute_pr_cluster_indices(ordering, boundary, n_clusters,
                               compute_thick_part, min_thick=0.0001):
    def find_split(C, boundary):
        best_pr = sys.float_info.max
        index = -1
        for i in range(1, len(C) - 2):
            if (boundary[C[i - 1]] >= boundary[C[i]] and 
                boundary[C[i]] <= boundary[C[i + 1]]):
                thick_width = compute_thick_part(boundary, C, i)
                pr = boundary[C[i]] / max(thick_width, min_thick)
                if pr < best_pr:
                    best_pr = pr
                    index = i
        return best_pr, index + 1

    pinch_ratios = []
    queue = PriorityQueue()
    C = range(len(ordering))
    pr, idx = find_split(C, boundary)
    queue.put((pr, idx, C))
    pinch_ratios.append(pr)
    while queue.qsize() < n_clusters:
        q, t, C_i = queue.get()
        if t < 0:
            # no split loc
            break
        C_j, C_k = C_i[:t], C_i[t:]
        pr1, idx1 = find_split(C_j, boundary)
        pr2, idx2 = find_split(C_k, boundary)
        pinch_ratios.append(pr1)
        pinch_ratios.append(pr2)
        queue.put((pr1, idx1, C_j))
        queue.put((pr2, idx2, C_k))

    cluster_indices, n_actual_clusters = [], 0
    for i in range(min(queue.qsize(), n_clusters)):
        q, t, C_i = queue.get()
        if C_i:
            cluster_indices.append(C_i)
            n_actual_clusters += 1

    pinch_ratios = sorted(pinch_ratios)[:n_actual_clusters - 1]
    return pinch_ratios, cluster_indices
 def mergeKLists(self, lists):
     dummy = ListNode(None)
     curr = dummy
     q = PriorityQueue()
     for node in lists:
         if node: q.put((node.val,node))
     while q.qsize()>0:
         curr.next = q.get()[1]
         curr=curr.next
         if curr.next: q.put((curr.next.val, curr.next))
     return dummy.next
def solve(ropes):
    if len(ropes) <= 1: return 0
    min_heap = PriorityQueue()
    for i in ropes:
        min_heap.put(i)
    total = 0
    while min_heap.qsize() > 1:
        cost = min_heap.get() + min_heap.get()
        total += cost
        min_heap.put(cost)
    return total
Пример #29
0
def buildHuffmanTree(strlen, charFreqList):
    pq = PriorityQueue()
    
    for key, value in charFreqList.iteritems():
        pq.put((float(value) / float(strlen), HuffmanNode(key, float(value) / float(strlen))))
    
    while pq.qsize() > 2:
        x,y,z = pq.get(), pq.get(), pq.get()
        pq.put((x[0] + y[0] + z[0], HuffmanNode(None, x[0] + y[0] +z [0], x[1], y[1], z[1])))
    
    return pq.get()[1]
Пример #30
0
def median_maintenance(num_stream):
    q_left = PriorityQueue()
    q_right = PriorityQueue()
    first = num_stream[0]
    second = num_stream[1]
    if first <= second:
        q_left.put(-first)
        q_right.put(second)
    else:
        q_left.put(-second)
        q_right.put(first)
    for num in num_stream[2:]:
        if num <= -q_left.queue[0]: 
            q_left.put(-num)
        else: 
            q_right.put(num)
        if abs(q_right.qsize() - q_left.qsize()) > 1:
            if q_left.qsize() > q_right.qsize(): 
                q_right.put(-q_left.get())
            else: 
                q_left.put(-q_right.get())
    return -q_left.queue[0], q_right.queue[0]
Пример #31
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        n = len(lists)
        q = PriorityQueue(maxsize=n)
        dummy = ListNode(None)
        cur = dummy
        for node in lists:
            if node: q.put((node.val, node))
        while q.qsize() > 0:
            temp = q.get()
            cur.next = temp[1]
            cur = cur.next
            if cur.next: q.put((cur.next.val, cur.next))

        return dummy.next
Пример #32
0
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     from Queue import PriorityQueue
     vhead = ListNode(-1)
     curr = vhead
     q = PriorityQueue()
     for l in lists:
         if l:
             q.put((l.val, l))
     while q.qsize() > 0:
         curr.next = q.get()[1]
         curr = curr.next
         if curr.next:
             q.put((curr.next.val, curr.next))
     return vhead.next
Пример #33
0
 def kthSmallest1(self, matrix, k):
     '''
     优先队列
     '''
     q = PriorityQueue()
     n = len(matrix)
     limit = n * n - k + 1
     for i in matrix:
         for k in i:
             if q.qsize() == limit:
                 tmp = q.get()
                 if tmp < k:
                     q.put(k)
                 else:
                     q.put(tmp)
             else:
                 q.put(k)
     return q.get()
Пример #34
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        from Queue import PriorityQueue
        dummy = ListNode(None)
        curr = dummy
        q = PriorityQueue()

        for node in lists:
            if node:
                q.put((node.val, node))
        while q.qsize() > 0:
            curr.next = q.get()[1]  # always get the min node
            curr = curr.next
            if curr.next: q.put((curr.next.val, curr.next))
        return dummy.next
    def _build_trie(self, frequency):
        """
        Build a binary trie and return its root node
        """
        nodes = PriorityQueue()
        for char, freq in frequency.iteritems():
            if freq > 0:
                nodes.put(self.Node(char, freq))

        # merge two smallest tries
        while nodes.qsize() > 1:
            left = nodes.get()
            right = nodes.get()
            parent = self.Node(0, left.frequency + right.frequency, left, right)
            nodes.put(parent)

        # return the root node
        return nodes.get()
def createHuffmanTree(weightDictionary):
    uniqueID = 0
    Q = PriorityQueue()

    for key, weight in weightDictionary.items():
        node = Node(weight, key, None, None)
        Q.put((weight, uniqueID, node))
        uniqueID += 1

    while Q.qsize() > 1:
        tup1 = Q.get()
        tup2 = Q.get()
        weight = tup1[0] + tup2[0]
        node = Node(weight, '_', tup1[2], tup2[2])
        Q.put((weight, uniqueID, node))
        uniqueID += 1

    return Q.get()[2]
Пример #37
0
class CrawlerPQueue(PriorityQueue):
    def __init__(self):
        self.pq = PriorityQueue(-1)

    def qsize(self):
        return self.pq.qsize()

    def empty(self):
        return self.pq.empty()

    def full(self):
        return self.pq.full()

    def put(self, item):
        self.pq.put(item)

    def get(self):
        return self.pq.get()
Пример #38
0
class QueueBackend(QueueInterface):
    def __init__(self, spider_name, **kwargs):
        super(QueueBackend, self).__init__(spider_name, **kwargs)
        self.queue_object = PriorityQueue()
        self.schedule_list = []

    def put(self, task, priority, schedule_time=None):
        if schedule_time is None:
            self.queue_object.put((priority, task))
        else:
            self.schedule_list.append((schedule_time, task))

    def get(self):
        now = datetime.utcnow()

        removed_indexes = []
        index = 0
        for schedule_time, task in self.schedule_list:
            if schedule_time <= now:
                self.put(task, 1)
                removed_indexes.append(index)
            index += 1

        self.schedule_list = [
            x for idx, x in enumerate(self.schedule_list)
            if idx not in removed_indexes
        ]

        _, task = self.queue_object.get(block=False)
        return task

    def size(self):
        return self.queue_object.qsize() + len(self.schedule_list)

    def clear(self):
        try:
            while True:
                self.queue_object.get(False)
        except Empty:
            pass
        self.schedule_list = []

    def close(self):
        pass
Пример #39
0
class PressureControlQueue(object):
    __shared_state = {}

    def __init__(self, frequency_time):
        # self.__dict__ = self.__shared_state
        self._lock = threading.Lock()
        self._qsize_lock = threading.Lock()
        self._queue_lock = threading.Lock()
        self.tick_time = time.time()
        self.frequency_time = frequency_time
        self.link_priority_queue = PriorityQueue()
        self.qsize = 0

    def get(self):
        with self._lock:
            while True:
                current = time.time()
                if self._has_pending_site_until(current):
                    link_job = self.pop_link()
                    return link_job
                time.sleep(0.2)

    def statics(self):
        with self._queue_lock:
            return "links queue size : %d" % self.link_priority_queue.qsize()

    def pop_link(self):
        link_job = self.link_priority_queue.get(True, 3)
        with self._qsize_lock:
            self.qsize -= 1
        return link_job

    def put_link(self, link_job):
        with self._queue_lock:
            self.tick_time += self.frequency_time
            link_job.tick_time = self.tick_time
            self.link_priority_queue.put(link_job)
            with self._qsize_lock:
                self.qsize += 1

    def _has_pending_site_until(self, current):
        if self.link_priority_queue.empty():
            return False
        return current >= self.link_priority_queue.queue[0].tick_time
Пример #40
0
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     dummy = ListNode(0)
     cur = dummy
     q = PriorityQueue()
     
     for node in lists:
         if node:
             q.put((node.val, node))
     while q.qsize() > 0:
         node = q.get()[1]
         cur.next = node
         cur = cur.next
         if cur.next:
             q.put((cur.next.val, cur.next))
     return dummy.next
Пример #41
0
def dij(m):
    L = len(m)
    C = len(m[0])
    pq = PriorityQueue()
    pq.put((1, (0, 0)))

    visit = set()
    while pq.qsize() > 0:
        (dist, (l, c)) = pq.get()
        if (l, c) == (L - 1, C - 1):
            return dist
        if (l, c) in visit:
            continue
        visit.add((l, c))
        for (ml, mc) in moves:
            nextPos = (l + ml, c + mc)
            if availablePos(nextPos, m):
                pq.put((dist + 1, nextPos))
    return 500
Пример #42
0
def bfs_search(state, dfs_route):
    stateQueue = PriorityQueue()
    if fit_regular(state) == False: return None
    new_state = copy.deepcopy(state)
    new_state.sol_matrix = np.where(new_state.sol_matrix > 0, True, False)
    if checkSolution(new_state):
        return new_state
    stateQueue.put((len((np.where(state.isfilled_matrix == False))[0]), state))
    while not stateQueue.empty():
        notfilled_num, state = stateQueue.get()

        row_branch = None
        for row_positons in dfs_route:
            if False in state.isfilled_matrix[row_positons[1]]:
                state.isfilled_matrix[row_positons[1]] = True
                row_branch = row_positons
                break

        if row_branch == None:
            continue

        for row in row_branch[2]:
            new_state = copy.deepcopy(state)
            new_state.sol_matrix[row_branch[1]] = row
            if fit_regular(new_state) == True:
                notfilled_num = len(
                    (np.where(new_state.isfilled_matrix == False))[0])
                if settings.DEBUGG:
                    print notfilled_num, " size: ", stateQueue.qsize()
                if notfilled_num == 0:
                    new_state.sol_matrix = np.where(new_state.sol_matrix > 0,
                                                    True, False)
                    if checkSolution(new_state):
                        del stateQueue
                        return new_state
                    else:
                        # print new_state.sol_matrix
                        if settings.DEBUGG:
                            print "error"
                        del new_state
                        continue
                stateQueue.put((notfilled_num, new_state))
    return None
Пример #43
0
 def topKFrequent(self, nums, k):
     frequent = dict()
     for i in nums:
         frequent.setdefault(i, 0)
         frequent[i] += 1
     q = PriorityQueue()
     for num, cnt in frequent.items():
         if q.qsize() == k:
             val, key = q.get()
             if cnt > val:
                 q.put((cnt, num))
             else:
                 q.put((val, key))
         else:
             q.put((cnt, num))
     res = list()
     for i in q.queue:
         res.append(i[1])
     return res
Пример #44
0
 def topKFrequent(self, nums, k):
     freqs = dict()
     for i in nums:
         freqs.setdefault(i, 0)
         freqs[i] += 1
     q = PriorityQueue()
     for k1, v in freqs.items():
         if q.qsize() == k:
             val, key = q.get()
             if v > val:
                 q.put((v, k1))
             else:
                 q.put((val, key))
         else:
             q.put((v, k1))
     res = list()
     for i in q.queue:
         res.append(i[1])
     return res
Пример #45
0
def PQ(food, snake, key):
    queue = PriorityQueue()
    queue.put((heuristic(snake, food), [snake, [key]]))
    visited = set()
    visited.add(tuple([l for k in snake for l in k]))
    f.write("queue: " + str(queue) + "\n")
    while (queue.empty() == False):
        state = queue.get()
        if (state[1][0][0] == food):
            return state[1][1][1:]
        moves = getMoves(food, state[1][0])
        f.write(str(queue.qsize()) + "\n")
        for m in moves:
            temp = tuple([l for k in m[0] for l in k])
            if temp not in visited:
                m[1] = state[1][1] + m[1]
                queue.put((heuristic(m[0], food), m))
                visited.add(temp)
    return [27]
Пример #46
0
    def solve(self):
        ID = 0
        startTime = time()
        currNode = Node(self.init_state)
        #self.printP()
        if self.checkSolvable(currNode.puzzle) == False:
            return ["UNSOLVABLE"]
        # 2 Data Structures to keep track of...
        openList = PriorityQueue()
        openList.put((currNode.getH(), currNode)) # STABLEST
        costMap = {currNode.key:0}
        steps = 0 #Nodes popped off frontier
        while True:
            steps += 1
##            if steps % 100000 == 0:
##                print 'step:', steps, 'size', openList.qsize()
##                sys.stdout.flush()
            if openList.empty(): #Empty frontier
                print 'Empty Queue!'
                break
            currNode = openList.get()[1] # STABLEST
            if self.isGoalState(currNode.puzzle):
                ans = self.reconstruct(currNode)
                self.timeTaken = time() - startTime
                self.nodesPopped = steps
                self.nodesInside = openList.qsize()
                self.finalMoves = len(ans)
##                print 'TIME:', self.timeTaken
##                print 'nodesPopped', self.nodesPopped
                return ans
            for child in currNode.getChildren(currNode):
                ID += 1
                child.g = currNode.g + 1
                #Child is now a Node          
                if child.key not in costMap or child.g < costMap[child.key]:
                    costMap[child.key] = child.g
                    child.h = child.getH() #ONLINE
                    child.tick = ID
                    openList.put((child.g + child.h, child)) # STABLEST
        timeTaken = time() - startTime
        print 'Time taken:', str(timeTaken)
        return ["UNSOLVABLE"]
Пример #47
0
class MaxQueue(object):
    ''' 
    A priority queue sorted in descending order instead of ascending order
    
    If maxlength > 0, queue keeps only the maxlength entries with highest values
    not memory efficient in python since small memory is not reused
    
        # reduce length to maxlength if queue is too long
        if self.maxlength > 0 and len(self) > self.maxlength + self.length_tol:
            with self.decrease_length:
                print 'Reducing length of MaxQueue by removing values less than',       
                print 'Used memory before = ', self.memory_mon.usage()
                new_pq = PriorityQueue()
                for i in range(self.maxlength - 1):
                    new_pq.put(self.pq.get())
                print -self.pq.get()[0], 'from consideration'
                self.pq = new_pq
                print 'Used memory after = ', self.memory_mon.usage()
    '''
    def __init__(self, maxlength=0, length_tol=1000):
        self.pq = PriorityQueue()
        self.decrease_length = multiprocessing.Lock()
        self.maxlength = maxlength
        self.length_tol = length_tol

    def put(self, tup):
        # add new item to queue
        self.pq.put((-tup[0], tup[1]))

    def get(self, block=True):
        tup = self.pq.get(block)
        return (-tup[0], tup[1])

    def get_nowait(self):
        tup = self.pq.get_nowait()
        return (-tup[0], tup[1])

    def __len__(self):
        return self.pq.qsize()

    def empty(self):
        return self.pq.empty()
Пример #48
0
def minMeetingRooms(intervals):
    from Queue import PriorityQueue
    if not intervals:
        return 0
    else:
        q = PriorityQueue()
        for i in range(len(intervals)):
            q.put([intervals[i][0], i, 'start'])
        maxRoom = 0
        currentRoom = 0
        while q.qsize() > 0:

            time, idx, status = q.get()
            if status == 'start':
                currentRoom += 1
                maxRoom = max(maxRoom, currentRoom)
                q.put([intervals[idx][1], idx, 'end'])
            else:
                currentRoom -= 1
        return maxRoom
Пример #49
0
 def getRequest(self):
     with self.condition:
         LOG.info("getRequest condition acquired")
         requestPQ = None
         while ((requestPQ == None) and (not self.isDestroying)):
             if PriorityQueue.qsize(self) > 0:
                 _, requestPQ = PriorityQueue.get(self)
             else:
                 self.condition.wait()
         LOG.info("isDestroying=%s" % self.isDestroying)
         requestDB = None
         if (not self.isDestroying):
             LOG.info("idRecord %s" % (requestPQ["idRecord"]))
             idRecord = requestPQ["idRecord"]
             requestDB = self.__getDB(idRecord)
             requestDB.update(requestPQ)
         self.condition.notifyAll()
     request = requestDB
     LOG.info("getPQ condition released")
     return request
Пример #50
0
 def minMeetingRooms(self, intervals):
     """
     :type intervals: List[Interval]
     :rtype: int
     """
     from Queue import PriorityQueue
     # O(nlgn)
     Q = PriorityQueue()
     intervals.sort(key=lambda itm: itm.start)
     for i, interval in enumerate(intervals):
         if Q.empty():
             Q.put(interval.end)
         else:
             head = Q.get()
             if head <= interval.start:
                 Q.put(interval.end)
             else:
                 Q.put(interval.end)
                 Q.put(head)
     return Q.qsize()
Пример #51
0
def preProcess(edgeweights,sdist,qvalues):
    N = traci.junction.getIDCount() #no. of junctions
    print("no. of nodes: ",N)
    '''write djikstra'''
    pq = PriorityQueue()
    for i in range(N):
        pq.put((0,i))
        sdist[i][i] = 0
        while pq.qsize() > 0:
            top = pq.get()
            #print("top: ",top)
            u = top[1]
            for j in range(N):
                if  edgeweights[u][j] > 0 and sdist[i][u]+edgeweights[u][j] < sdist[i][j]:
                    sdist[i][j] = sdist[i][u] + edgeweights[u][j]
                    pq.put((sdist[i][j],j))

        sdist[i][i] = Parameters.inf
    print(pd.DataFrame(sdist))
    return
    def getLeastNumbers(self, arr, k):
        """
        :type arr: List[int]
        :type k: int
        :rtype: List[int]
        """

        if k <= 0 or (not arr) or len(arr) == 0:
            return []

        Q = PriorityQueue()
        for x in arr:
            Q.put(-x)
            while Q.qsize() > k:
                Q.get()

        ans = []
        while not Q.empty():
            ans.append(-Q.get())
        return ans
Пример #53
0
def merge_k_sorted_linkedlists(lists):
    from Queue import PriorityQueue
    newhead = ListNode(0)
    current = newhead
    q = PriorityQueue()

    # putting the headnodes of each list
    # to the q is the same as putting
    # the whole list there, because we are
    # dealing with LINKED list here.
    for headnode in lists:
        if headnode: q.put((headnode.val, headnode))
    while q.qsize() > 0:
        current.next = q.get()[1]  # index [1] to get the actual node
        current = current.next  # move the new position in the new list
        # while we are at it,
        # we put the current's next node to q
        if current.next:
            q.put((current.next.val, current.next))
    return newhead.next
Пример #54
0
class queue:
    def __init__(self):
        self.list_queue = PriorityQueue()

    def push(self, node):
        self.list_queue.put(node)

    def distance(self, pt1, pt2):
        return np.sqrt((pt2[0] - pt1[0]) * (pt2[0] - pt1[0]) +
                       (pt2[1] - pt1[1]) * (pt2[1] - pt1[1]))

    def pop(self):
        node = self.list_queue.get()
        return node

    def leng(self):
        return self.list_queue.qsize()

    def isfull(self):
        return np.invert(self.list_queue.empty())
Пример #55
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        dump = ListNode(0)
        queue = PriorityQueue()
        for item in lists:
            while item is not None:
                queue.put(item.val)
                item = item.next

        node = dump
        while queue.qsize() > 0:
            data = queue.get()
            while node.next:
                node = node.next
            node.next = ListNode(data)

        return dump.next
Пример #56
0
def rec_for_a_cus(cus_id, num_rec=3):
	from Queue import PriorityQueue
	from rspy.db import db_rs

	q = PriorityQueue()
	rates = db_rs.read_rate(CID = cus_id)
	goods_dict = db_rs.get_goods_dict(reverse=True)
	for record in rates:
		if record['REAL'] == 0:
			q.put(Movie(record['GID'], goods_dict[record['GID']], record['RATE']))

	temp = ""
	if not q.empty():
		s = q.qsize()
		for i in xrange(num_rec if num_rec < s else s):
			temp += "\n"+q.get().__str__()
		print temp[1:len(temp)]
	else:
		print ''
		print 'There is no such customer!'
Пример #57
0
def chargingChaos(f):
  soln = ''
  cases = int(f.readline())
  for case in xrange(cases):
    row = f.readline().split(' ')
    N = int(row[0])
    L = int(row[1])
      # Tracer()()
    current = sorted(f.readline().strip().split(' '))
    required = sorted(f.readline().strip().split(' '))
    # Tracer()()
    states = PriorityQueue()
    states.put((0, current, 0))
    visited = set({})
    visited.add(' '.join(current))
    found = False
    sol = 0
    while not states.qsize() == 0:
      topstr = states.get()
      top = topstr[1]
      steps = topstr[2]
      # Tracer()()
      if len([1 for x,y in zip(required, top) if not x==y]) == 0:
        found = True
        sol = steps
        break;
      for i in xrange(L):
        if i >= L:
          Tracer()()
        nextstate = flipper(top, i)
        # Tracer()()
        if not ' '.join(nextstate) in visited:
          visited.add(' '.join(nextstate))
          states.put((steps+1, nextstate, steps+1))
      # print states.qsize()
      # Tracer()()
    if not found:
      steps = "NOT POSSIBLE"
    soln += 'Case #{0}: {1}\n'.format(case+1, steps)
    # print soln
  return soln
Пример #58
0
 def topKFrequent(self, words, k):
     """
     :type words: List[str]
     :type k: int
     :rtype: List[str]
     """
     mapping = {}
     for word in words:
         if word not in mapping:
             mapping[word] = Word(word)
         else:
             mapping[word].freq += 1
     pq = PriorityQueue()
     for word in mapping:
         pq.put(mapping[word])
         if pq.qsize() > k:
             pq.get()
     res = [None for _ in range(k)]
     for i in range(k - 1, -1, -1):
         res[i] = pq.get().word
     return res
Пример #59
0
def create_tree(freq):
	p = PriorityQueue()
	for f in freq:
		p.put(f)
	while p.qsize() > 1:
		l, r = p.get(), p.get()
		node = None
		dat = ""
		if isinstance(l[1], Node):
			dat += l[1].data
		else:
			dat += l[1]
		if isinstance(r[1], Node):
			dat += r[1].data
		else:
			dat += r[1]

		node = Node(l, r, dat)
		p.put((l[0] + r[0], node))

	return p.get()
Пример #60
0
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     if lists is None:
         return None
     if len(lists) == 0:
         return []
     cur = ListNode()
     newHead = cur
     q = PriorityQueue()
     for node in lists:
         if node:
             q.put((node.val, node))
     while q.qsize() > 0:
         cur.next = q.get()[1]
         cur = cur.next
         if cur.next:
             q.put((cur.next.val, cur.next))
     return newHead.next