Ejemplo n.º 1
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]
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    def MRVval(self):

        q = PriorityQueue()
        for blank in self.blanks:
            possible = self.getAllVal(blank, False)
            q.put((len(possible), blank))

        blanks = []
        blanks.append(q.get())
        minVal = blanks[0][0]

        while not q.empty(): 
            next = q.get()
            if next[0] == minVal:
                blanks.append(next)
            else:
                break

        dmax = len(self.getAssocBlks(blanks[0][1]))
        dmaxBlank = blanks[0]

        for blank in blanks:
            degree = len(self.getAssocBlks(blank[1]))
            if degree > dmax:
                dmaxBlank = blank
                dmax = degree
        return dmaxBlank[1]
    def getMRV(self):

        # Build the MRV priority queue
        q = PriorityQueue()
        for blank in self.AllEmptyPositions:
            possible = self.getPossibleValues(blank, True)
            q.put((len(possible), blank))

        # Get the first one among (possibly equal)
        blanks = []
        blanks.append(q.get())
        minVal = blanks[0][0]

        # Build max Degree list
        while not q.empty():  # Get all equally-prioritized blanks
            next = q.get()
            if next[0] == minVal:
                blanks.append(next)
            else:
                break

        maxDeg = len(self.getNeighborBlanks(blanks[0][1]))
        maxDegBlank = blanks[0]

        for blank in blanks:
            degree = len(self.getNeighborBlanks(blank[1]))
            if degree > maxDeg:
                maxDegBlank = blank
                maxDeg = degree
        return maxDegBlank[1]
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def run(boardS, populationS, maxKills, v):
#finds one optimal placement for queens
	generation = PriorityQueue()
	#intital population
	for g in range(populationS): 
		n=getRandomBoard(boardS)
		generation.put((fitness(n),n))

	best=generation.get()
	count=1
	bestL=best
	vari=0
	while best[0]>maxKills:
		if v: print str(best)
		if best==bestL: 
			vari+=1
			if vari>5: 
				change=mutate(best[1], 1)
				#print change
				best=(fitness(change),change)
		else: 
			vari=1
			bestL=best
		generation.put(best)
		generation=newGeneration(generation)
		best=generation.get()
		count+=1
	if v: 
		print "Solution: "+str(best[1]) +" in "+str(count)+" generations"
		print 
	return count
Ejemplo n.º 7
0
	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()
Ejemplo n.º 8
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])
Ejemplo n.º 9
0
    def getMRV(self):
        
        q = PriorityQueue()
        for blank in self.blanks:
            possible = self.getPossibleValues(blank, True)
	    q.put((len(possible), blank))
        blanks = []
        blanks.append(q.get())
        minVal = blanks[0][0]

        while not q.empty(): #Get all equally-prioritized blanks
            next = q.get()
            if next[0] == minVal:
                blanks.append(next)
            else:
                break
            
        maxDeg = len(self.getNeighborBlanks(blanks[0][1]))
        maxDegBlank = blanks[0]

        for blank in blanks:
            degree = len(self.getNeighborBlanks(blank[1]))
            if degree > maxDeg:
                maxDegBlank = blank
                maxDeg = degree
        return maxDegBlank[1]
Ejemplo n.º 10
0
def getMRV():
	q = PriorityQueue()
        for blank in blanks:
		possible = getPossibleValues(blank)
		q.put((len(possible), blank))

        #Get the first one among (possibly equal)
        min_blanks = []
        min_blanks.append(q.get())
        minVal = min_blanks[0][0]

        #Build max Degree list
        while not q.empty(): #Get all equally-prioritized blanks
		next = q.get()
		if next[0] == minVal:
	                min_blanks.append(next)
		else:
			break

        maxDeg = len(getNeighborBlanks(min_blanks[0][1]))
        maxDegBlank = min_blanks[0]

        for blank1 in min_blanks:
		degree = len(getNeighborBlanks(blank1[1]))
		if degree > maxDeg:
			maxDegBlank = blank1
			maxDeg = degree
        return maxDegBlank[1]
Ejemplo n.º 11
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
	
	
	
	
Ejemplo n.º 12
0
 def beamSearch(self,a,sv):
     # get 1 hot vector
     a = self._get1hot(a,self.da)
     sv= self._get1hot(sv,self.dsv)
     # end nodes
     endnodes = []
     # initial layers
     h0,c0 = np.zeros(self.dh),np.zeros(self.dh)
     # starting node
     node = BeamSearchNode(h0,c0,None,1,0,1)
     node.sv = sv
     node.a  = a
     # queue for beam search
     nodes= PriorityQueue()
     nodes.put((-node.eval(),node))
     qsize = 1
     # start beam search
     while True:
         # give up when decoding takes too long 
         if qsize>10000: break
         # fetch the best node
         score, n = nodes.get()
         # if end of sentence token 
         if n.wordid==1 and n.prevNode!=None:
             # update score with sem cost
             n.logp -= np.sum(abs(n.sv))
             score = -n.eval()
             endnodes.append((score,n))
             # if reach maximum # of sentences required
             if len(endnodes)>=self.overgen: break
             else:                           continue
         # decode for one step using decoder 
         words, probs, sv, c, h = self._gen(n)
         # put them into a queue
         for i in range(len(words)):
             node = BeamSearchNode(h,c,n,words[i],
                     n.logp+np.log10(probs[i])-
                     np.sum(0.0001*(100.0**abs(sv-n.sv)))
                     ,n.leng+1)
             node.sv = sv
             node.a  = a
             nodes.put( (-node.eval(),node) )
         # increase qsize
         qsize += len(words)-1
     # if no finished nodes, choose the top scored paths
     if len(endnodes)==0:
         endnodes = [nodes.get() for n in range(self.overgen)]
     # choose nbest paths, back trace them
     utts = []
     for score,n in sorted(endnodes,key=operator.itemgetter(0)):
         utt = [n.wordid]
         while n.prevNode!=None:
             # back trace
             n = n.prevNode
             utt.append(n.wordid)
         utt = utt[::-1]
         utts.append((score,utt))
     return utts
Ejemplo n.º 13
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()
def search_schedule(plant):
    # todo AI scheduling alg can base on this: heat priorities in q, caster priorities
    # todo put index to the heats, delete heat+1, group+1
    # todo AI: find a fastest schedule, then cal the adjustment room in time, then shift the EAF tasks
    # todo AI: exchange the groups orders, see what happens with the final ECost
    # todo aim for a ChemE journal paper
    # currently, heat/group starting at 1, time starting at 0
    """Find a feasible schedule by matching heats and equipment units.

    Main Idea:
        Match available heats (intermediate products) to available equipment units.
        Maintain availability by priority queues (priority index: available time).
    """
    schedule = []
    q_t_h = PriorityQueue()   # ready time (priority), heat
    for heat in range(1, plant.num_heats+1):
        q_t_h.put((0, heat))
    for stage in [1, 2, 3]:
        q_t_u = PriorityQueue()     # ready time (priority), unit
        for unit, num in plant.stage2units[str(stage)].items():
            for u_id in range(num):
                q_t_u.put((0, unit, u_id))
        q_t_h_next = PriorityQueue()    # ready time of heat for next stage
        while not q_t_h.empty():
            t_heat, heat = q_t_h.get()
            t_unit, unit, u_id = q_t_u.get()
            t_start = max(t_heat, t_unit)
            task = plant.tasks[unit][heat-1]
            t_end_process = t_start + plant.task_duration[task]
            q_t_u.put((t_end_process, unit, u_id))
            # todo, RTN2
            transport = plant.tasks['TR_S%d' % stage][heat-1]
            t_end_trans = t_end_process + plant.task_duration[transport]
            q_t_h_next.put((t_end_trans, heat))
            schedule.append((stage, heat, unit, u_id, task, t_start, t_end_process, t_end_process))
            schedule.append((stage, heat, 'TR_S%d' % stage, 0, transport, t_end_process, t_end_trans, t_end_trans))
        q_t_h = q_t_h_next
    heat2time = dict()
    while not q_t_h.empty():
        t, heat = q_t_h.get()
        heat2time[heat] = t
    q_t_caster = PriorityQueue()     # ready time (priority), unit
    for unit, num in plant.stage2units['4'].items():
        for u_id in range(num):
            q_t_caster.put((0, unit, u_id))
    for group in range(1, plant.num_groups+1):
        t_caster, caster, caster_id = q_t_caster.get()
        t_group = max([heat2time[heat] - plant.heat_consume_time_in_group[caster][heat]
                       for heat in plant.group2heats[str(group)]])
        t_start = max(t_group, t_caster)
        task = plant.tasks[caster][group-1]
        t_end_process = t_start + plant.task_duration[task]
        t_cleanup = t_start + plant.task_cleanup_duration[task]
        schedule.append((4, group, caster, caster_id, task, t_start, t_end_process, t_cleanup))
        q_t_caster.put((t_cleanup, caster, caster_id))
    return schedule
Ejemplo n.º 15
0
def get_matches(vector, num_matches):
  best_matches = PriorityQueue(num_matches)
  good_matches = PriorityQueue(num_matches)
  ok_matches = PriorityQueue(num_matches)


  matches_found = 0
  vectors = get_vectors()
  for v in vectors:
    r = sim_coeff(vector, v)
    print(r)
    if r<.5:
      print("not a match")
      continue
    if r<.7:
      print("Ok Match Found")
      ok_matches.put((r, v))
      matches_found += 1
      print(v)
      if matches_found == num_matches:
        break
      else:
        continue
    if r<.9:
      print("Good Match Found")
      good_matches.put((r, v))
      matches_found += 1
      print(v)
      if matches_found == num_matches:
        break
      else:
        continue
    if r<=1:
      print("Great Match Found")
      best_matches.put((r, v))
      if not ok_matches.empty():
        ok_matches.get()
      else:
        matches_found += 1
      print(v)
      if matches_found == num_matches:
        break
      else:
        continue
    if r>1:
      print("Esthena is bad at math.")
      
  print("Ok: ")
  while not ok_matches.empty():
    print(ok_matches.get())
  print("Good: ")
  while not good_matches.empty():
    print(good_matches.get())
  print("Best: ")
  while not best_matches.empty():
    print(best_matches.get())
Ejemplo n.º 16
0
class WorkersList(object):
	"""docstring for WorkersList"""
	def __init__(self):
		super(WorkersList, self).__init__()
		self.queue = PriorityQueue()
		self.workers = []

	def empty_queue(self):
		while not self.queue.empty():
			next_level = self.queue.get()
			print 'Processing level:', next_level.schedule

	def get_from_queue(self):
		return self.queue.get()

	def queue_size(self):
		return self.queue.size()

	def _collect_workers(self):
		while not self.queue.empty():
			next_job = self.queue.get()
			worker = Worker(next_job, next_job.description)
			self.workers.append(worker)
			worker.start()

	def start(self):
		self._collect_workers()
		for worker in self.workers:
			worker.join()

	def put_in_queue(self, job):
		self.queue.put(job)

	def stop_single_worker(self, value):
		for worker in self.workers:
			if worker.name == value:
				worker.triggerStop = False
				break
		else:
			raise WorkerException("Worker with name/id {}".format(value))

	def stop_all_workers(self):
		for worker in self.workers:
			worker.triggerStop = False

	def terminate_all_workers(self):
		for worker in workers:
			worker.terminate()

	def terminate_single_worker(self, value):
		for worker in self.workers:
			if worker.name == value:
				worker.terminate()
				break
		else:
			raise WorkerException("Worker with name/id {}".format(value))
Ejemplo n.º 17
0
class EventsCollection(object):

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

    def add_event(self, event):
        self.events.put(event)

    def pop(self):
        self.events.get()
Ejemplo n.º 18
0
def search_image(filepath, codebook, tfidf, control="with_tfidf"):
    print_detail = True
    k, _ = codebook.shape
    if print_detail:
        print "-------------------------------------------------------------------------------"
    bow = get_bow(filepath, codebook)
    if print_detail:
        print "-------------------------------------------------------------------------------"
        print "tfidf matrix shape:"
        print tfidf.shape
        print "-------------------------------------------------------------------------------"
        print "Bag of Word of: ", filepath
        print bow
        print "-------------------------------------------------------------------------------"
    _, l = tfidf.shape
    control = "no_tfidf"
    if control == "with_tfidf":
        idi = np.zeros((1, l))
        for i in range(k):
            if bow[i] != 0:
                idi = np.add(idi, tfidf[i])
        rank = [(i, j) for (i, j) in zip([i for i in range(l)], idi.tolist()[0])]
    else:
        bow = [float(i) / sum(bow) for i in bow.tolist()]
        rank = np.dot(np.asarray(bow), tfidf)
        rank = [(i, j) for (i, j) in zip([i for i in range(l)], rank.tolist())]

    q = PriorityQueue(50)
    for (x, y) in rank:
        if not q.full():
            q.put(Image(y, x))
        else:
            if y > q.queue[0].similarity():
                q.get()
                q.put(Image(y, x))

    result = []
    while not q.empty():
        result.append(q.get())

    # images_data_path = "/Users/minhuigu/FoodAdvisor/app/outputs/images_data.txt"
    # images_folder = "/Users/minhuigu/Desktop/"
    # img_list = []
    # json_content = open(images_data_path).read()
    # for each in json.loads(json_content):
    #     img_list.append(images_folder + each['relpath'])
    #
    # for a in [i.id() for i in result][::]:
    #     print img_list[a]
    if print_detail:
        print "Best rank images: "
        print [i.id() for i in result][::]
        print "-------------------------------------------------------------------------------"
    # decreasing according to similarity
    return [i.id() for i in result][::]
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
Ejemplo n.º 20
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]
Ejemplo n.º 21
0
def construct_huffman_tree(freqs):
    pq = PriorityQueue()
    for i in freqs:
        pq.put(TreeNode(i))
    for i in xrange(len(freqs)-1):
        left = pq.get()
        right = pq.get()
        # (left.val,right.val).p()
        node = TreeNode(left.val+right.val)
        node.left, node.right = left, right
        pq.put(node)
    return pq.get()
Ejemplo n.º 22
0
def kMostFreqHeapThreePass(nums, k):
    freq = {}
    heap = PriorityQueue(k)
    # count
    for i in nums: freq[i] = freq.get(i, 0)+1
    for key, val in freq.iteritems():
        if not heap.full():
            heap.put((val, key))
        else:
            if heap.queue[0][0] < val:
                heap.get()
                heap.put((val, key))

    return [itm[1] for itm in reversed(heap.queue)]
Ejemplo n.º 23
0
def compute_k_closest_stars(stars, k):
    if len(stars) == 0:
        return
    max_heap = PriorityQueue()
    for i in xrange(k):
        max_heap.put(stars[i])
    for i in xrange(k+1, len(stars)):
        top = max_heap.get()
        max_heap.put(max(top, stars[i]))
    res = []
    while not max_heap.empty():
        res.append(max_heap.get().name)
    res.reverse()
    return res
Ejemplo n.º 24
0
def enumerate_min(n):
    pl = primes.primes_list

    # graph
    startnode = (2, ((2, 1),))
    """(2,((2,1),(3,1),(5,1),
			(7,1),(11,1),(13,1),
			(17,1),(19,1),(23,1),
			(29,1),(31,1),(37,1)))"""
    nodes = set(startnode)
    nextnodes = PriorityQueue()
    nextnodes.put(startnode)
    cur_node = nextnodes.get()
    cur_comb = combinations_distinct(cur_node[1])

    max_found = 0
    # print max,raw_input()
    while cur_comb < n:
        value, curprimes = cur_node
        # nextnodes are added
        # we increment an existing primes, and we add an existing
        # add 1 to each existing prime
        prev = curprimes[0][1] + 1
        for i, p in enumerate(curprimes):
            prime, count = p
            if count + 1 > prev:
                break
            prev = count + 1
            # check if this way can even theoretically get
            # us to where we want
            newprimes = curprimes[:i] + ((prime, count + 1),) + curprimes[i + 1 :]
            newnode = degen_product(newprimes), newprimes
            if newnode not in nodes:
                nodes.add(newnode)
                nextnodes.put(newnode)
                # add new prime
        newprimes = curprimes + ((int(pl[(len(curprimes))]), 1),)
        newvalue = degen_product(newprimes)
        newnode = newvalue, newprimes
        if newnode not in nodes:
            nodes.add(newnode)
            nextnodes.put(newnode)
        cur_node = nextnodes.get()
        cur_comb = combinations_distinct(cur_node[1])
        value, curprimes = cur_node
        if cur_comb > max_found:
            print (value, cur_comb, curprimes)
            max_found = cur_comb
    print cur_node, combinations_distinct(cur_node[1])
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()
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
Ejemplo n.º 27
0
def ucs(source, target, graph):
    """ Uniform-cost graph search """
    queue = PriorityQueue() # fringe
    queue.put((0, source))

    parent = {source:None}
    visited = {}

    while not queue.empty():
        (d, v_in) = queue.get()

        if v_in not in visited or d < visited[v_in]:

            if v_in == target:
                return (d, build_path(parent, target))

            for v_out in graph.adj(v_in):
                cost = graph.distance(v_in, v_out) + d
                if v_out not in visited:
                    queue.put((cost, v_out))
                    parent[v_out] = v_in

            visited[v_in] = cost

    return None
Ejemplo n.º 28
0
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
Ejemplo n.º 29
0
def solve(N, L, start, goal):
    """search for minimum bit switches to make start == goal
    NB: treat goal and start as sets of currents -- that is, order doesn't
    matter in comparing start to goal and all currents in start/goal
    are mutually unique"""
    # BFS w/ priority queue; queue orders by min bits flipped;
    # we don't use an 'explored' set because we may need to revisit the same
    # configuration of currents several times as index i increments;
    # instead, to constrain the search space, we use the is_consistent filter
    # before placing a state in the queue in order to check that the
    # configuration of currents in that state is consistent with goal at least
    # up to bit i-1
    start_state = (0, start, 0)  # ( n_bits_flipped, state_of_currents, index )
    queue = PriorityQueue()
    queue.put( start_state )

    while not queue.empty():
        nchanges, state, index = queue.get()

        if is_goal(state, goal):
            return nchanges

        for nch,s,i in successors(nchanges, state, index):
            if is_consistent(s, goal, i):
                # when i = len(goal)+1, s will be added to queue
                # only if s == goal
                queue.put([nch, s, i])

    return 'NOT POSSIBLE'
Ejemplo n.º 30
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()
Ejemplo n.º 31
0
def astar_multi(env,
                starts,
                goals,
                constraint_fn=lambda node, lastnode, t: True):
    starts = tuple(starts)
    goals = tuple(goals)
    pq = PriorityQueue()
    cost = 0.0
    t = 0.0
    heur = sum(
        [env.estimate(start, goal, t) for start, goal in zip(starts, goals)])
    costmap = {starts: cost}
    prevmap = {starts: None}
    pq.put((heur + cost, starts, t))
    while not pq.empty():
        totcost, curr, t = pq.get()
        at_goal = all([node == goal for node, goal in zip(curr, goals)])
        if at_goal:
            return construct_path_multi(prevmap, curr)

        transitions = [env.next(node, t) for node in curr]
        children = [[t[0] for t in node] for node in transitions]
        step_costs = [[t[1] for t in node] for node in transitions]
        children_combined = combine_actions(children)
        step_costs_combined = combine_actions(step_costs)
        for child, step_cost in zip(children_combined, step_costs_combined):
            if len(set(child)) < len(child):  # skip if there is a collision
                continue
            child_t = t + 1
            if not constraint_fn(child, curr, child_t):
                continue
            child_cost = costmap.get(curr, float('inf')) + sum(step_cost)
            if child_cost < costmap.get(child, float('inf')):
                prevmap[child] = curr
                costmap[child] = child_cost
                child_totcost = sum([
                    env.estimate(child_i, goal_i, child_t)
                    for child_i, goal_i in zip(child, goals)
                ]) + child_cost
                pq.put((child_totcost, child, child_t))
    return None
Ejemplo n.º 32
0
    def AStar(self, heuristic):
        startTime = time()
        expanded = 0
        PQueue = PriorityQueue()
        visited = set()
        queueGrids = set()  #Grids In Queue But Not Visited Yet
        PQueue.put((heuristic(self.initialState), self.initialState))

        while (not PQueue.empty()):
            front = (PQueue.get())[1]
            queueGrids.discard(front.grid)

            if front.grid in visited:  #Visited Before
                continue

            if front.grid in queueGrids:  #Found in Queue
                for node in PQueue.queue:
                    grid = node[1].grid

                    if grid == front.grid:
                        if node[0] > front.cost + heuristic(node[1]):
                            node[0] = front.cost + heuristic(node[1])

            #Processing
            if front.isGoal():
                path = front.backTrack()
                #self.drawGrids(path)
                self.processResult(path, front.cost, expanded,
                                   time() - startTime,
                                   "AStar-" + heuristic.__name__[:9])
                return path

            visited.add(front.grid)
            expanded += 1
            queueGrids.add(front.grid)

            children = front.generateChildren()  #Expanding
            for child in children:
                PQueue.put((heuristic(child) + child.cost, child))

        return False
Ejemplo n.º 33
0
def search(targest_artist_profile, ogartist, connection):
    underrated = PriorityQueue()
    lokey = set()
    fringe = q.Queue()
    fringe.put(ogartist)
    getRelatedArtists_bfs(ogartist, lokey, {ogartist[1]}, fringe, [])

    quadrant_size = len(lokey) / 4
    lokey_list = list(lokey)
    processes = []
    qu = mp.Queue()
    for i in range(0, 4):
        process = Process(
            target=generateProfilePriority,
            args=(lokey_list[0 + (quadrant_size * i):(quadrant_size * i) +
                             quadrant_size], targest_artist_profile, qu))
        process.start()
        processes += [process]

    for process in processes:
        process.join()

    print '-------------results--------------'
    while not qu.empty():
        item = qu.get()
        print item
        underrated.put(item)

    results = []
    while not underrated.empty():
        item = underrated.get()
        results.append(item)

    a = (json.dumps({"a": results})).encode()
    json_file_size = str(len(a))

    print(json_file_size)
    connection.send(json_file_size)
    connection.send("\n")
    connection.send(a)
    print(a)
 def get_inferred_norms(self, topNorms=1):
     call(["java", self.javaAppClass])
     pq = PriorityQueue()
     normProbabilities = {}
     with open(self.oniOutputFileName, 'r') as fOutOni:
         for line in fOutOni:
             parts = line.split(
             )  # norm as a string of action chars, and then probability
             if len(parts) > 0:
                 norm = ('eventually', parts[0][0]) if len(
                     parts[0]) == 1 else (parts[0][0], 'next', parts[0][1])
                 pq.put((float(parts[1]), norm))
                 normProbabilities[norm] = float(parts[1])
     with open(self.pniOutputFileName, 'r') as fOutPni:
         for line in fOutPni:
             parts = line.split(
             )  # norm as a string of action chars, and then probability
             if len(parts) > 0:
                 norm = ('never', parts[0][0]) if len(
                     parts[0]) == 1 else (parts[0][0], 'not next',
                                          parts[0][1])
                 pq.put((float(parts[1]), norm))
                 normProbabilities[norm] = float(parts[1])
     sorted_norms = []
     while not pq.empty():
         sorted_norms += [pq.get()[1]]
     norms = [x for x in reversed(sorted_norms)]
     # Check that we select either the topNorms, or the first ones with the same odds
     for (i, n) in enumerate(norms):
         if normProbabilities[n] == 0:
             topNorms = i
             break
         if (i + 1 == topNorms):
             tied = len(norms) > i + 1 and (
                 normProbabilities[n] == normProbabilities[norms[i + 1]])
             if (tied):
                 topNorms += 1
             else:
                 break
     #endfor
     return norms[0:topNorms]
Ejemplo n.º 35
0
def find_path(start, goal):
    global xx
    im = Image.open("output.png")  #Can be many different formats.
    output = Image.new('RGBA', im.size)
    pix = im.load()
    dimensions = im.size  #Get the width and hight of the image for iterating over
    width = dimensions[0]
    height = dimensions[1]
    # initial parameters
    frontier = PriorityQueue()
    frontier.put(start, 0)
    closed_set = []
    costs_so_far = {}
    came_from = {}
    open_set = [start]
    costs_so_far[start] = 0
    came_from[start] = None
    max_y = height
    max_x = width
    while not frontier.empty():
        xx = xx + 1
        current = frontier.get()
        if current[0] == goal[0] and current[1] == goal[1]:
            break
        x = current[0]
        y = current[1]
        for i in range(-1, 2):
            for j in range(-1, 2):
                if x + i < max_x and y + j < max_y and x + i >= 0 and y + j >= 0 and x + i != x and y + j != y:
                    new_cost = costs_so_far[current] + pix[x + i, y + j][0]
                    if (x + i, y +
                            j) not in costs_so_far or new_cost < costs_so_far[
                                (x + i, y + j)]:
                        costs_so_far[(x + i, y + j)] = new_cost
                        priority = new_cost + heuristic(goal, (x + i, y + j))
                        frontier.put((x + i, y + j), priority)
                        came_from[(x + i, y + j)] = current
        if xx > 1000:
            break
            #print current
    return (came_from, costs_so_far)
Ejemplo n.º 36
0
    def a_star_tsp(self, num_cities, g, s_city_name):
        s_time = time.time()

        num_nodes = 0
    
        s = g[s_city_name]

        q = PriorityQueue()

        q.put((0, [[s_city_name], 0]))
        num_nodes += 1
    
        while not q.empty():
            temp  = q.get()[1]
            path = temp[0]
            prev_acc_cost = temp[1]
        
            if time.time() - s_time > 300:
                return path, prev_acc_cost, num_nodes, False

            # Travelled all cities and then back to start
            if len(path) == num_cities + 1:
                return path, prev_acc_cost, num_nodes, True

            # Travelled all cities 
            if len(path) == num_cities:
                heur, acc_cost = self.calc_heur(g, path, s_city_name, s, prev_acc_cost)
                new_path = path[:]
                new_path.append(s_city_name)
                q.put((heur, [new_path, acc_cost]))
                num_nodes += 1
            # Haven't travelled all cities
            else:
                not_visited = set(path).symmetric_difference(set(g.keys()))

                for city in not_visited:
                    heur, acc_cost = self.calc_heur(g, path, city, s, prev_acc_cost)
                    new_path = path[:]
                    new_path.append(city)
                    q.put((heur, [new_path, acc_cost]))
                    num_nodes += 1
Ejemplo n.º 37
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        if lists == None or len(lists) == 0:
            return []
        pq = PriorityQueue()
        res = []
        for node in lists:
            if node:
                pq.put((node.val, node))

        while not pq.empty():
            node = pq.get()[1]
            res.append(node.val)
            node = node.next
            if node:
                pq.put((node.val, node))

        return res
    def mergeKLists(self, lists):
        priorityQueue = PriorityQueue()

        for l in lists:
            if l:
                priorityQueue.put((l.val, l))

        head = ListNode(-1)
        pointer = head

        while not priorityQueue.empty():
            nodeVal, node = priorityQueue.get()
            pointer.next = ListNode(nodeVal)

            pointer = pointer.next
            node = node.next

            if node:
                priorityQueue.put((node.val, node))

        return head.next
Ejemplo n.º 39
0
def merge(lists):
    # error checking
    if not lists:
        return []
    lists = filter(lambda x: x != [], lists)

    # initialize priority queue
    pqueue = PriorityQueue()
    for lst in lists:
        if lst:
            pqueue.put((lst[0], lst[1:]))

    # merge the arrays
    result = []
    while not pqueue.empty():
        elt = pqueue.get()
        num, rest = elt[0], elt[1]
        result.append(num)
        if rest:
            pqueue.put((rest[0], rest[1:]))
    return result
Ejemplo n.º 40
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        from Queue import PriorityQueue  # in py3, use queue

        head = p = ListNode(0)
        q = PriorityQueue()
        for l in lists:
            if l:
                # in py3, 当第一个值一样的时候,比较第二个值,所以若第二值不支持比较,就会报错。
                q.put((l.val, l))
        while not q.empty():
            val, node = q.get()
            p.next = node
            p = p.next
            if node.next:
                q.put((node.next.val, node.next))

        return head.next
Ejemplo n.º 41
0
def calcHeuristic( envObj, agent):
	exploredState = set()   #set to store explored states
	#We will assume that agent always takes bus
	pq = PriorityQueue()
	pq.put(QueueNode(envObj.goal, 0, 0, None, State(envObj.goal, Mode.bus)))

	while (not pq.empty()):
		curr = pq.get()
		if (curr.ind in exploredState):
			continue
		envObj.graph[curr.ind].heuristic = curr.g     #save heuristic for curr.ind

		exploredState.add(curr.ind)
		for edge in envObj.graph[curr.ind].adjList:
			if edge.destination in exploredState:
				continue
			g = curr.g + edge.wt
			pq.put(QueueNode(edge.destination, g, g, curr, State(curr.ind, Mode.bus)))
	
	for node in envObj.graph:                 
		node.heuristic = node.heuristic / agent.busMaxSpeed
Ejemplo n.º 42
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """

        head = point = ListNode(0)
        q = PriorityQueue()
        for node in lists:
            if node:
                q.put((node.val, node))

        while not q.empty():

            val, node = q.get()
            point.next = ListNode(val)
            point = point.next
            if node.next:
                q.put((node.next.val, node.next))

        return head.next
Ejemplo n.º 43
0
def getP3(puzzle, pattern):
    output = ''
    tempMap = {}
    pq = PriorityQueue()
    for key in pattern:
        tempMap[key] = 0
        pq.put((key, key))
        #print 'keyA', key
    for i in range(len(puzzle)):
        for j in range(len(puzzle)):
            if puzzle[i][j] in pattern:
                temp = ''
                temp += str(i + 1)
                temp += str(j + 1)
                tempMap[puzzle[i][j]] = temp
                #print 'num',puzzle[i][j],i,j
    while not pq.empty():
        key = pq.get()[1]
        output += str(tempMap[key])
        #print 'keyB', key, 'value', tempMap[key]
    return output
Ejemplo n.º 44
0
def aStarSearch(graph, h, start, goal):
    frontier = PriorityQueue()
    frontier.put((0, start))

    cost = {}
    cost[start] = 0

    parents = {}
    while not frontier.empty():
        _, current = frontier.get()

        if current == goal:
            return backtrace(parents, start, goal)

        successors = graph[current]

        for successor in successors:
            cost[successor] = cost[current] + graph[current][successor]

            parents[successor] = current
            frontier.put((h[successor] + cost[successor], successor))
Ejemplo n.º 45
0
    def getStrongest(self, arr, k):
        """
        :type arr: List[int]
        :type k: int
        :rtype: List[int]
        """
        res = []
        pq = PQ()
        length = len(arr)
        arr.sort()

        median = arr[int((length - 1) / 2)]

        for i in range(length):
            pq.put((-abs(arr[i] - median), -arr[i], i))

        for i in range(k):
            tmp = pq.get()
            res.append(arr[tmp[2]])

        return res
Ejemplo n.º 46
0
    def lazy_prim_simplified(self):
        """
        wrap a method visit() to make it more compact
        :return:
        """
        processed = {}
        pq = PriorityQueue()
        # start from 0
        pq = self.visit(0, pq, processed)

        while not pq.empty():
            cur_node_w, cur_node_from, cur_node_to = pq.get()
            # if processed, do nothing
            if processed.get(cur_node_to) is True:
                continue
            # if not processed yet, it is one edge in MST
            self.mst.append((cur_node_from, cur_node_to, cur_node_w))
            # visit the other node in current minimum weight edge
            pq = self.visit(cur_node_to, pq, processed)

        self.mst_weight = self._calc_weight()
Ejemplo n.º 47
0
    def mergeKLists(self, lists):
        """
		:type lists: List[ListNode]
		:rtype: ListNode
		"""
        # THIS WAS WHAT I WAS LOOKING FOR
        # NOT HHAVING TO MANUALLY PUSH A NEW NODE TO THEH TAIL, O(N)
        head = pt = ListNode()

        q = PriorityQueue()
        for ll in lists:
            cur = ll
            while cur is not None:
                q.put(cur.val)
                cur = cur.next

        while not q.empty():
            i = q.get()
            pt.next = ListNode(i)
            pt = pt.next
        return head.next
Ejemplo n.º 48
0
    def fix_graph(self, roots):
        graph = self.buildGraph
        fixed_graph = nx.Graph()
        pq = PQ()

        for node in roots:
            self.add_node_to_queue_z(node, graph, pq)
            fixed_graph.add_node(node)

        #Now the priority queue has been initialized
        while not pq.empty():  #While there are still edges
            print fixed_graph.number_of_nodes()
            (node, edge) = pq.get()
            print 'in the loop'
            if not (edge[1]
                    in fixed_graph):  #if sink of edge hasn't been added yet
                fixed_graph.add_node(edge[1])
                fixed_graph.add_edge(node, edge[1], points=edge[2])
                self.add_node_to_queue_z(edge[1], graph, pq)

        return fixed_graph
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     dummy = cur = ListNode(0)
     queue = PriorityQueue()
     # O(k)
     for l in lists:
         if l:  # there may be empty linked list
             queue.put((l.val, l))
     # O(nlogk),因为一共有n个元素
     while not queue.empty():
         # O(logk)
         val, node = queue.get()
         cur.next = node
         cur = cur.next
         node = node.next
         if node:
             queue.put((node.val, node))
     return dummy.next
Ejemplo n.º 50
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
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        head = point = ListNode(0)  # dummy node to point to new linked list
        q = PriorityQueue()

        # initial iteration through lists, if linked list not empty, put its head as a (value, node) pair in PQ
        for l in lists:
            if l: q.put((l.val, l))

        # get the (value, node) pair at the front of the PQ and add a new node with the value to the linked list
        # use node.next to get the next lowest element of the linked list to add to the PQ
        while not q.empty():
            val, node = q.get()
            point.next = ListNode(val)
            point = point.next
            node = node.next
            if node: q.put((node.val, node))
        return head.next
Ejemplo n.º 52
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        if len(lists) == 0:
            return None
        dummy = ListNode(None)
        current = dummy
        pq = PriorityQueue()
        for list in lists:
            if list:
                pq.put((list.val, list))

        while pq.qsize() > 0:
            current.next = pq.get()[1]
            current = current.next
            if current.next:
                pq.put((current.next.val, current.next))

        return dummy.next
Ejemplo n.º 53
0
    def findPlan(self):

        frontier = PriorityQueue()
        frontier.put((0, Node(self.start, None, None, 0)))
        count = 0

        while (not frontier.empty()):
            node = frontier.get(False)[1]
            count += 1
            if count % 100 == 0:
                print(count)

            if (node.state.contains(self.goal)):
                print('Found goal', count)
                return self.get_action_sequance(node)

            actions = self.domain.getApplicableActions(node.state)
            for action in actions:
                newState = action.apply(node.state)
                newNode = Node(newState, action, node, node.cost + 1)
                frontier.put((newNode.cost, newNode))
Ejemplo n.º 54
0
def a_star_search(graph, start, goal):
    frontier = PriorityQueue()
    frontier.put(start, 0)
    came_from = {start: None}
    cost_so_far = {start: 0}

    while not frontier.empty():
        current = frontier.get()

        if current == goal:
            break

        for next in graph.neighbors(current):
            new_cost = cost_so_far[current] + graph.cost(current, next)
            if next not in cost_so_far or new_cost < cost_so_far[next]:
                cost_so_far[next] = new_cost
                priority = new_cost + heuristic(goal, next)
                frontier.put(next, priority)
                came_from[next] = current

    return came_from, cost_so_far
Ejemplo n.º 55
0
def AStar():
    queue = PriorityQueue()
    cur_node = Node(0, 0)
    queue.put((get_FCost(cur_node), cur_node))
    visited = [get_index(cur_node)]
    path = [-1 for _ in xrange(width * height)]
    while not queue.empty():
        _, cur_node = queue.get()

        for node in find_next_nodes(cur_node):
            if get_index(node) in visited:
                continue

            path[get_index(node)] = get_index(cur_node)

            if is_target(node):
                print_path(path)
                return
            queue.put((get_FCost(node), node))
            visited.append(get_index(node))
    print 'not found'
Ejemplo n.º 56
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        pq = PriorityQueue()
        head = point = ListNode(0)

        for l in lists:
            if l:
                pq.put((l.val, l))

        while not pq.empty():
            val, node = pq.get()
            point.next = ListNode(val)
            point = point.next
            node = node.next
            if node:
                pq.put((node.val, node))

        return head.next
Ejemplo n.º 57
0
    def MCV(self, assigned):
        """
        Degree Heuristic for variable selection
        :param assigned: Assigned variables
        :return: index (x, y)
        """
        pq = PriorityQueue()

        # assign row and col conflict scores, and assign values to tileDomain
        for row in range(self.n):
            for col in range(self.n):
                if not assigned[self.getIndex(row, col)]:
                    neighbours = self.neighbours(self.getIndex(row, col))
                    x = 0
                    for n in neighbours:
                        if not assigned[n]:
                            x += 1
                    pq.put((x, self.getIndex(row, col)))

        [val, idx] = pq.get()
        return idx
Ejemplo n.º 58
0
    def aStarBetweenTwoNodes(self, nodeA, nodeB):
        frontier = PriorityQueue()
        frontier.put((0, str(nodeA)))
        cost_so_far = {}
        cost_so_far[nodeA] = 0
        parentMap = {}

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

            if s == nodeB:
                self.addDotsBetweenTwoNodes(nodeA, nodeB, s, parentMap)
                break

            for i in self.vertex[s]['edge']:
                new_cost = cost_so_far[s] + 1
                if i not in cost_so_far or new_cost < cost_so_far[i]:
                    cost_so_far[i] = new_cost
                    priority = new_cost + self.manhattan(s, nodeB)
                    frontier.put((priority, i))
                    parentMap[i] = s
Ejemplo n.º 59
0
    def kClosest(self, points, K):
        """
        :type points: List[List[int]]
        :type K: int
        :rtype: List[List[int]]
        """

        pq = PriorityQueue()
        result = []

        for point in points:
            x, y = point
            distance = math.sqrt(x**2 + y**2)
            pq.put((distance, point))

        for _ in range(K):

            distance, point = pq.get()
            result.append(point)

        return result
Ejemplo n.º 60
0
def solve(n, k):
    q = PriorityQueue()
    counts = {n: 1}
    q.put(-n)
    while k > 0:
        v = -q.get()
        if v not in counts:
            continue
        count = counts[v]
        del counts[v]
        k -= count
        if k <= 0:
            return v
        if v % 2 == 1:
            q.put(-(v / 2))
            add_to(counts, v / 2, count * 2)
        else:
            q.put(-(v / 2))
            add_to(counts, v / 2, count)
            q.put(-((v / 2) - 1))
            add_to(counts, (v / 2) - 1, count)