Пример #1
0
    def _try_to_allocate_vm(self, vm):
        temp_heap = []
        while self.occupied_bins:
            # noinspection PyBroadException
            try:
                bin_ = heapq_max.heappop_max(self.occupied_bins)
                heapq_max.heappush_max(temp_heap, bin_)

                bin_.add(vm)
                heapq_max.heappush_max(self.occupied_bins, bin_)
                temp_heap.remove(bin_)

                while temp_heap:
                    temp_bin = heapq_max.heappop_max(temp_heap)
                    heapq_max.heappush_max(self.occupied_bins, temp_bin)

                self.vm_bins[vm.uuid] = bin_
                return True
            except:
                continue
        else:
            self.occupied_bins = temp_heap
            if not self.bins:
                self._reject(vm)
                return False
            else:
                bin_ = self.bins.pop()
                bin_.add(vm)
                heapq_max.heappush_max(self.occupied_bins, bin_)
                self.vm_bins[vm.uuid] = bin_
                return True
def writefile(number, trie, tag_dict):

    detail_info = {}
    name_list = {}  # eklenen kelimeleri ismini tut
    for i in tag_dict:
        name_list[i] = []
        # name_list[i].append(i)
        detail_info[i] = []
    min_list = {}  # temp min list
    for j in tag_dict:
        min_list[j] = []

    for x in trie:
        for y in tag_dict:
            result = spatial.distance.cosine(trie[x], tag_dict[y])
            heapq_max.heappush_max(min_list[y], (result, x))
            if len(min_list[y]) > number:
                heapq_max.heappop_max(min_list[y])

    for name in min_list:

        length = len(min_list[name])
        for i in range(length):
            out = heapq_max.heappop_max(min_list[name])
            if name != out[1]:
                detail_info[name].append(out)
                name_list[name].append(out[1])
        detail_info[name].append((0.0, name))
        name_list[name].append(name)
    f1 = open(
        "/home/asus/Desktop/Classification/lastversion/2-ChooseWordsFromGlove/Data/datanew"
        + str(number), "w")
    for y in name_list:
        name_list[y].reverse()
        # print(name_list[y])
        f1.write(str(name_list[y]))
        f1.write("\n")
    f2 = open(
        "/home/asus/Desktop/Classification/lastversion/2-ChooseWordsFromGlove/Data/datadnew"
        + str(number), "w")
    for y in detail_info:
        detail_info[y].reverse()
        # print(detail_info[y])
        f2.write(str(detail_info[y]))
        f2.write("\n")
    f3 = open(
        "/home/asus/Desktop/Classification/lastversion/2-ChooseWordsFromGlove/Data/datapnew"
        + str(number), "w")
    for y in name_list:
        # name_list[y].reverse()
        # print(name_list[y])
        num1 = 0
        for j in name_list[y]:
            # num1=num1+1
            # if num1>1900 and num1 <2500:
            f3.write(str(j))
            if j != name_list[y][-1]:
                f3.write("$#$")
        f3.write("\n")
Пример #3
0
 def push(self, value):
     #print "****" ,value
     if len(self.__heap) < self.__max_item_count:
         heapq_max.heappush_max(self.__heap, value)
     else:
         if self.__heap[0][0] <= value[
                 0]:  #a bigger value is trying to come inside, dont let it
             pass
         else:
             heapq_max.heappop_max(self.__heap)  #throw away
             heapq_max.heappush_max(self.__heap, value)  #add new value
Пример #4
0
    def test_push_pop(self):
        # 1) Push 256 random numbers and pop them off, verifying all's OK.
        heap = []
        data = []
        self.check_invariant(heap)
        for i in range(256):
            item = random.random()
            data.append(item)
            heappush_max(heap, item)
            self.check_invariant(heap)
        results = []
        while heap:
            item = heappop_max(heap)
            self.check_invariant(heap)
            results.append(item)
        data_sorted = data[:]
        data_sorted.sort(reverse=True)
        self.assertEqual(data_sorted, results)
        # 2) Check that the invariant holds for a sorted array
        self.check_invariant(results)

        self.assertRaises(TypeError, heappush_max, [])
        try:
            self.assertRaises(TypeError, heappush_max, None, None)
            self.assertRaises(TypeError, heappop_max, None)
        except AttributeError:
            pass
 def addNum(self, num: int) -> None:
     heapq_max.heappush_max(self.left, num)
     a = heapq_max.heappop_max(self.left)
     heapq.heappush(self.right, a)
     if not self.count % 2:
         b = heapq.heappop(self.right)
         heapq_max.heappush_max(self.left, b)
     self.count += 1
Пример #6
0
def top(iterable, key=toolz.identity):
    """Generates elements from max to min."""
    h = []
    for i, value in enumerate(iterable):
        # Use the index as a tie breaker.
        heapq_max.heappush_max(h, (key(value), i, value))
    while h:
        yield toolz.nth(2, heapq_max.heappop_max(h))
Пример #7
0
 def test_order_of_occupied_bins_correct_for_worst_fit(self):
     for alg in self.algorithm_executions:
         last_cpu_capacity = 999999
         temp_heap = []
         while alg.occupied_bins:
             bin_ = heapq_max.heappop_max(alg.occupied_bins)
             cpu = bin_.capacity.get("cpu")
             self.assertLessEqual(cpu, last_cpu_capacity)
             last_cpu_capacity = cpu
             heapq_max.heappush_max(temp_heap, bin_)
         alg.occupied_bins = temp_heap
Пример #8
0
    def search(self, t, k, Q, depth=None):
        if distancia(t, self.loc) - self.radius >= distancia(
                t, Q[0][2]) or depth == 0:
            return Q
        elif self.child1 is None and self.child2 is None:
            c_i = 0
            for p in (self.data).tolist():

                if distancia(t, p) < distancia(t, Q[0][2]):
                    point = [distancia(t, p), self.image[c_i], p]
                    heappush_max(Q, point)
                    if len(Q) > k:
                        heappop_max(Q)
                c_i = c_i + 1
        else:
            if self.child1 is not None:
                if depth is None:
                    self.child1.search(t, k, Q)
                    self.child2.search(t, k, Q)
                elif depth > 0:
                    self.child1.search(t, k, Q, depth - 1)
                    self.child2.search(t, k, Q, depth - 1)
Пример #9
0
def top(iterable: Iterable, key=identity):
    """Generates elements from max to min.

    >>> tuple(top((1, 3, 2)))
    (3, 2, 1)

    >>> tuple(top(('a', 'aa', 'aaa'), len))
    ('aaa', 'aa', 'a')
    """
    h: List = []
    for i, value in enumerate(iterable):
        # Use the index as a tie breaker.
        heapq_max.heappush_max(h, (key(value), i, value))
    while h:
        yield nth(2)(heapq_max.heappop_max(h))
Пример #10
0
def get_shortest_path(ref,
                      qry,
                      ref_coords,
                      species,
                      ce,
                      genome_size,
                      verbose=False):
    if verbose:
        print('current species: (might be a dead end)')
    shortest_path = {}
    orange = []
    heapq_max.heappush_max(orange, (1, ref, ref_coords))
    shortest_path[ref] = (1.0, '', ref_coords, (), ())

    while len(orange) > 0:
        (current_score, current_species,
         current_coords) = heapq_max.heappop_max(orange)
        if shortest_path.get(current_species, (0, ))[0] > current_score:
            continue  # the current species was already reached by a faster path, ignore this path and go to the next species
        if verbose:
            print(
                current_species
            )  # remember: this is not necessarily going to the shortest path as it might be a dead end that doesn't lead to the qry. not all printed species are part of the shortest path!
        if current_species == qry:
            break  # qry species reached, stop
        for nxt_species in species[species != current_species]:
            nxt_best_score = shortest_path.get(nxt_species, (0, ))[
                0]  # current score entry for nxt_species in shortest_path
            if current_score < nxt_best_score:
                continue  # if the score to current_species was lower than any previous path to nxt_species, nxt_species won't be reached faster through current_species. ignore and move on to the next species
            else:
                nxt_score, nxt_coords, current_anchors, nxt_anchors = project_genomic_location(
                    current_species, nxt_species, current_coords,
                    current_score, ce, genome_size)
            if nxt_score < nxt_best_score:
                continue  # only save the current path to nxt_species if it was indeed faster than any previous path to it
            else:
                shortest_path[nxt_species] = (nxt_score, current_species,
                                              nxt_coords, current_anchors,
                                              nxt_anchors)
                heapq_max.heappush_max(orange,
                                       (nxt_score, nxt_species, nxt_coords))
    shortest_path_to_qry = get_shortest_path_to_qry(qry, shortest_path)
    return shortest_path_to_qry, shortest_path, orange
Пример #11
0
heapq.heappush(Heap_high, MyList[0])
heapq_max.heappush_max(Heap_low, MyList[1])

M = 6331 + 2793

for x in MyList[2:]:
    if x >= Heap_high[0]:
        heapq.heappush(Heap_high, x)
    else:
        heapq_max.heappush_max(Heap_low, x)
    combined_len = len(Heap_low) + len(Heap_high)
    if combined_len % 2 == 0:
        if len(Heap_high) > len(Heap_low):
            popped_h = heapq.heappop(Heap_high)
            heapq_max.heappush_max(Heap_low, popped_h)
        if len(Heap_high) < len(Heap_low):
            popped_l = heapq_max.heappop_max(Heap_low)
            heapq.heappush(Heap_high, popped_l)
        m_k = Heap_low[0]
    elif len(Heap_high) > len(Heap_low):
        m_k = Heap_high[0]
    else:
        m_k = Heap_low[0]
    M = M + m_k

print(len(Heap_low))
print(len(Heap_high))
print("M", M)
M_mod = M % 10000
print("M_mod", M_mod)
Пример #12
0
 def pop(self):
     if (self.size() == 0):
         raise SizeError
     return heappop_max(self._heap)
Пример #13
0
import heapq_max
arr = [4, 3, 33, 90, 321, 23, 567]

heapq_max.heapify_max(arr)
print(arr)
print(heapq_max.heappop_max(arr))
heapq_max.heappush_max(arr, 999)
print(arr)
heapq_max.heapreplace_max(arr, 100)
print(arr)
Пример #14
0
 def pop(self):
     if self.__heap <> []:
         return heapq_max.heappop_max(self.__heap)
     else:
         return None
Пример #15
0
                          '659874532')
shops['Damario'] = ('plaza', 'italian', 'plaza number 8', 3, 14, '698547123')

shops_heap = []  #Create List so after we can convert it to heap.

for keys, condiciones in shops.items(
):  #Here we do a loop that will go through the dictionary. And will get the different intersections values.
    condicion = set(condiciones)
    priority = len(preferencia
                   & condicion)  #We save the lenght of the intersection.
    shop = (priority, keys)
    heapq_max.heappush_max(shops_heap, shop)

#Check the restaurants that bests fits your preferences
print('Restaurant info: ', shops[shops_heap[0][1]])
print(heapq_max.heappop_max(shops_heap)[1])
input('Press enter if you want to see the next restaurant')

#Check the next one
print('Restaurant info: ', shops[shops_heap[0][1]])
print(heapq_max.heappop_max(shops_heap)[1])
input('Press enter if you want to see the next restaurant')

#Check the next one
print('Restaurant info: ', shops[shops_heap[0][1]])
print(heapq_max.heappop_max(shops_heap)[1])
input('Press enter if you want to see the next restaurant')

#Check the next one
print('Restaurant info: ', shops[shops_heap[0][1]])
print(heapq_max.heappop_max(shops_heap)[1])
Пример #16
0
 def pop(self):
     '''
     Returns the item with highest priority
     It's a pop action so the element is delete from the heap
     '''
     return heapq_max.heappop_max(self.h)
 def Pop(self):
     return heappop_max(self.heap)
Пример #18
0
import heapq
import heapq_max

if __name__ == '__main__':
    f = open('Median.txt')
    #f = open('Test.txt')
    medians = []
    Hhi = []
    Hlo = []

    for x in f:
        i = int(x)
        if len(Hhi) == 0:
            heapq_max.heappush_max(Hlo, i)
        elif i > Hlo[0]:
            heapq.heappush(Hhi, i)
        else:
            heapq_max.heappush_max(Hlo, i)

        if len(Hhi) > len(Hlo) + 1:
            heapq_max.heappush_max(Hlo, heapq.heappop(Hhi))
        elif len(Hlo) > len(Hhi) + 1:
            heapq.heappush(Hhi, heapq_max.heappop_max(Hlo))

        if len(Hlo) < len(Hhi):
            medians.append(Hhi[0])
        else:
            medians.append(Hlo[0])

    print(medians)
    print(sum(medians) % 10000)
Пример #19
0
def writefile(number,trie,tag_dict):

    detail_info={}
    name_list = {}  # eklenen kelimeleri ismini tut
    for i in tag_dict:
        name_list[i] = []
        # name_list[i].append(i)
        detail_info[i]=[]
    min_list = {}  # temp min list
    for j in tag_dict:
        min_list[j] = []

    print("[INFO] -> 1st and 2nd loops end.")
    for x in trie:
        for y in tag_dict:
                result=spatial.distance.cosine(trie[x],tag_dict[y])
                heapq_max.heappush_max(min_list[y],(result,x))
                if len(min_list[y])>number:
                    heapq_max.heappop_max(min_list[y])

    '''
    for x in trie:
        for y in tag_dict.keys():
            vector = tag_dict.get(y)
            if type(vector) is list:
                try:
                    print("list")
                    result1 = spatial.distance.cosine(trie[x],vector[0])
                    result2 = spatial.distance.cosine(trie[x],vector[1])
                    minResult = min(result1, result2)
                    heapq_max.heappush_max(min_list[y],(minResult,x))
                    if len(min_list[y]) > number:
                        heapq_max.heappop_max(min_list[y])
                
                except KeyError:
                    print("[ERROR] -> Key error occured in 'if' ")
                except ValueError:
                    print("[ERROR] -> Value error occured in 'if' ")

            else:
                print("not list")
                print(vector)
                try:
                    result=spatial.distance.cosine(trie[x],vector)
                    heapq_max.heappush_max(min_list[y],(result,x))
                    if len(min_list[y])>number:
                        heapq_max.heappop_max(min_list[y])
                except KeyError:
                    print("[ERROR] -> Key error occured in 'else' ")
                except ValueError:
                    print("[ERROR] -> Value error occured in 'else' ")
    '''

    print("[INFO] ->  3rd loop end.")
    for name in min_list:
        
        length=len(min_list[name])
        for i in range(length):
            out=heapq_max.heappop_max(min_list[name])
            if name!=out[1]:
                detail_info[name].append(out)
                name_list[name].append(out[1])
        detail_info[name].append((0.0,name))
        name_list[name].append(name)
    
    print("[INFO] -> 4th loop end.")
    f1=open("./AllCatWords/words"+str(number),"w")  
    print("[INFO] -> Writing 1st file.")          
    for y in name_list:
        name_list[y].reverse()
        # print(name_list[y])
        f1.write(str(name_list[y]))
        f1.write("\n")
    f2=open("./AllCatWords/wordsD"+str(number),"w")
    print("[INFO] -> Writing 2nd file.")
    for y in detail_info:
        detail_info[y].reverse()
        # print(detail_info[y])
        f2.write(str(detail_info[y]))
        f2.write("\n")
    f3=open("./AllCatWords/wordsP"+str(number),"w")
    print("[INFO] -> Writing 3rd file.")
    for y in name_list:
        # name_list[y].reverse()
        # print(name_list[y])
        num1=0
        for j in name_list[y]:
            # num1=num1+1
            # if num1>1900 and num1 <2500:
            f3.write(str(j))
            if j!= name_list[y][-1]:
                f3.write("$#$")
        f3.write("\n")
Пример #20
0
    for x in trie:
        count += 1
        if found_name == "":
            for i in tag_dict:
                if x not in name_list[i]:
                    # try:
                    result = spatial.distance.cosine(trie[x],
                                                     mid_points_list[i])

                    # except  :
                    # print(closest_points_list[i])
                    heapq_max.heappush_max(min_list[i],
                                           (result, trie[x], x, i))
                    # pushes a new item on the heap
                    if len(min_list[i]) > 1:
                        heapq_max.heappop_max(min_list[i])
                    heapq_max.heappush_max(heap_list[i], (result, x))

                    if len(heap_list[i]) > 40:
                        heapq_max.heappop_max(heap_list[i])
                    # if result < min_list[i]:
                    #     min_list[i]=result
        else:
            if x not in name_list[found_name]:
                # try:
                result = spatial.distance.cosine(trie[x],
                                                 mid_points_list[found_name])

                # except  :
                # print(closest_points_list[i])
                heapq_max.heappush_max(min_list[found_name],
Пример #21
0
import heapq_max

if __name__ == '__main__':
    Jobs = []
    with open("jobs.txt") as f:
        for x in f:
            weight, length = map(int, x.split())
            #Jobs.append((weight-length, weight, length))
            Jobs.append((weight / length, weight, length))

    heapq_max.heapify_max(Jobs)

    currentTime = 0
    sum = 0
    while Jobs:
        job = heapq_max.heappop_max(Jobs)
        currentTime += job[2]
        sum += (job[1] * currentTime)

    print(sum)