def quantize(self): print("计算主题") if self.h * self.w < self.maxColor: raise AttributeError( "Image({0}x{1}) too small to be quantized".format( self.w, self.h)) self.pixHisto = self.getPixHisto() orgVbox = self.createVbox(self.pixData) pOneQueue = PQueue(self.maxColor) pOneQueue.put((orgVbox.priority, orgVbox)) popcolors = int(self.maxColor * self.fraction) pOneQueue = self.iterCut(popcolors, pOneQueue) boxQueue = PQueue(self.maxColor) while not pOneQueue.empty(): vbox = pOneQueue.get()[1] vbox.priority *= int(vbox.vol) boxQueue.put((vbox.priority, vbox)) boxQueue = self.iterCut(self.maxColor - popcolors + 1, boxQueue, True) theme = [] count = 0 while not boxQueue.empty(): themep = self.boxAvgColor(boxQueue.get()[1]) #RGB值和颜色占比 count += themep[3] theme.append(themep) del themep print("计算结束") return theme
def quantize(self): if self.h * self.w < self.maxColor: raise AttributeError( "Image({0}x{1}) too small to be quantized".format( self.w, self.h)) self.pixHisto = self.getPixHisto() orgVbox = self.createVbox(self.pixData) pOneQueue = PQueue(self.maxColor) # Priority Queue 数字越小优先级越高 pOneQueue.put((orgVbox.priority, orgVbox)) popcolors = int(self.maxColor * self.fraction) pOneQueue = self.iterCut(popcolors, pOneQueue) boxQueue = PQueue(self.maxColor) while not pOneQueue.empty(): vbox = pOneQueue.get()[1] vbox.priority *= vbox.vol boxQueue.put((vbox.priority, vbox)) boxQueue = self.iterCut(self.maxColor - popcolors + 1, boxQueue, True) theme = [] ntot = [] ntotQueue = PQueue(self.maxColor) while not boxQueue.empty(): ave, nt = self.boxAvgColor(boxQueue.get()[1]) ntotQueue.put((-nt, ave)) while not ntotQueue.empty(): nt, ave = ntotQueue.get() theme.append(ave) ntot.append(-nt) return theme, ntot
def shortest_path(graph, start, goal): queue = PQueue() queue.put(start, 0) # Dictionary to track previous node prev = {start: None} # Dictionary to track cost cost = {start: 0} while queue.empty() == False: # get lowest priority node curr = queue.get() # when lowest priority is at goal node if curr == goal: get_path(prev, start, goal) for next in graph.roads[curr]: # Compute distance from current intersection to next intersection # current cost + straightline distance cost new_cost = cost[curr] + get_distance(graph.intersections[curr], graph.intersections[next]) if next not in cost or new_cost < cost[next]: cost[next] = new_cost # Priority is new_cost + straightline distance cost priority = new_cost + get_distance(graph.intersections[curr], graph.intersections[next]) # Push the next_node onto the Priority queue queue.put(next, priority) prev[next] = curr # helper function return get_path(prev, start, goal)
def loadingHeuristic(): headNode = CompareAble(0, sum(globalWeight), -1) queue = PQueue() queue.put(headNode) while queue.qsize() > 0: headNode = queue.get() print("get: ", headNode.Wt, headNode.Bd, headNode.idx) if headNode.idx + 1 >= globalNum: return headNode.Wt else: current = globalWeight[headNode.idx + 1] # 扩展左子节点 son = CompareAble(headNode.Wt + current, headNode.Bd - current, headNode.idx + 1) if son.Wt <= globalC1: queue.put(son) # print("left: ", son.Wt, son.Bd, son.idx) #扩展右子节点 son = CompareAble(headNode.Wt, headNode.Bd - current, headNode.idx + 1) queue.put(son) # print("right:", son.Wt, son.Bd, son.idx) return -1
def find_path(airports, open_queue, open_dict, close_dict, goal, pos): current_node = open_queue.get()[1] open_dict.pop(current_node.index) roads = list(nx.neighbors(airports, current_node.index)) update_node = False for i in roads: if i not in close_dict: distance = cal_dist(i, current_node.index, pos) new_g = current_node.g + distance if i in open_dict: if new_g < open_dict[i].g: open_dict[i].parent = current_node open_dict[i].g = new_g update_node = True else: new_h = cal_dist(i, goal, pos) new_node = node(i, current_node, new_g, new_h) open_dict[i] = new_node open_queue.put((new_node.h + new_node.g, new_node)) if update_node: open_queue = PQueue() for v in open_dict.values(): open_queue.put((v.h + v.g, v)) close_dict[current_node.index] = current_node
def haffmanCoding(freq): pq = PQueue() total = 0 sumFreq = 0 for item in freq: total += item.freq pq.put(item) level = 0 while pq.qsize() >= 2: item1 = pq.get() item2 = pq.get() level += 1 print(level, item1.name, item2.name) jointFreq = item1.freq + item2.freq jointName = item1.name + item2.name sumFreq += jointFreq pq.put(CompareAble(jointName, jointFreq)) else: item = pq.get() print(item.name) return sumFreq / total
def topKFrequent(self, nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] """ record = {} for ele in nums: if ele in record: record[ele] += 1 else: record[ele] = 1 pq = PQueue() for key, v in record.items(): if len(pq.queue) < k: pq.put((v, str(key))) else: tmp = pq.get() if tmp[0] < v: pq.put((v, str(key))) else: pq.put(tmp) res = [] for ele in pq.queue: res.append(int(ele[1])) return res
def _influence_maximazation(self, G, features_matrix, threshold): num_node = G.number_of_nodes() Q = PQueue() seed_list = [] for node in G.nodes(): act_num = self._simulation(G, features_matrix, node, threshold) Q.put((-1 * act_num, [node, 1])) # print("Inside PriorityQueue: ", Q.queue) total_num = 0.0 for i in range(self.num_seed): while not Q.empty(): tp = Q.get() act_num, node, k = -tp[0], tp[1][0], tp[1][1] if k == i: total_num += act_num seed_list.append(node) break else: act_num = self._simulation(G, features_matrix, node, threshold) Q.put((-1 * (act_num - total_num), [node, i])) return total_num / num_node, seed_list
def find_path(M, open_queue, open_dict, close_dict, goal): # 从open_node找出一个点使得f值最小 current_node = open_queue.get()[1] open_dict.pop(current_node.index) # 找到该点附近的点 roads = M.roads[current_node.index] update_node = False for i in roads: # 遍历过的结点不用再次遍历 if i not in close_dict: distance = cal_dist(M.intersections[i], M.intersections[current_node.index]) new_g = current_node.g + distance if i in open_dict: if new_g < open_dict[i].g: open_dict[i].parent = current_node open_dict[i].g = new_g update_node = True else: new_h = cal_dist(M.intersections[i], M.intersections[goal]) new_node = node(current_node, i, new_g, new_h) open_dict[i] = new_node open_queue.put((new_node.h + new_node.g, new_node)) # 如果修改了结点的内容需要新建队列 if update_node: open_queue = PQueue() for v in open_dict.values(): open_queue.put((v.h + v.g, v)) # 把当前点放入关闭列表,表示已经访问过 close_dict[current_node.index] = current_node
def Astar(self, From, To, leg_map, ban=[]): cameFrom = {} # 记录父节点 gScore = {} fScore = {} openSet = PQueue() closeSet = [] gScore[From] = 0 fScore[From] = gScore[From] + self.dist(From, To) openSet.put((fScore[From], From)) # 起始点入队 cameFrom[From] = From while not openSet.empty(): current = openSet.get()[1] # 取出队首元素坐标 closeSet.append(current) if (current == To): # 到达目的地 # route存移动坐标 # route = [current] # while current in cameFrom.keys() and current != From: # current = cameFrom[current] # 取出current父节点 # route.append(current) # route存移动方向 route = [] while current in cameFrom.keys() and current != From: parent = cameFrom[current] # 取出current父节点 route.append(leg_map.graph[parent][current]) current = parent # 列表倒转 route.reverse() # # print(route) return route # 遍历相邻节点 for neighbor in leg_map.graph[current].keys(): if neighbor in closeSet or neighbor in ban: continue gScore_temp = gScore[current] + 1 # 由于每一步代价相同所以都加1 fScore_temp = gScore_temp + self.dist(neighbor, To) if neighbor not in gScore.keys(): openSet.put((fScore_temp, neighbor)) elif gScore_temp >= gScore[neighbor]: continue cameFrom[neighbor] = current gScore[neighbor] = gScore_temp fScore[neighbor] = fScore_temp return []
def chooseApple(numbers): ans = 0 pq = PQueue() for n in numbers: pq.put(n) while pq.qsize() > 1: n1 = pq.get() n2 = pq.get() ans += n1 + n2 pq.put(n1 + n2) return ans
def closestKValues(self, root, target, k): """ :type root: TreeNode<float>, :type target: float :type k: int, range: [0:n] :rtype: List[int] """ if not root: return None pqueue = PQueue() res = [0] * k self.traverse(root, pqueue, target) for i in range(k): _, res[i] = pqueue.get() return res
def getMaxValue(Num, Capacity, Weight, Value): pq = PQueue() for i in range(Num): item = CompareAble(Weight[i], Value[i]) pq.put(item) cap = Capacity maxV = 0 while pq.qsize() > 0: item = pq.get() n = cap // item.weight maxV += n * item.value cap -= n * item.weight return maxV
def airplanetxt(airports, pos, dian, zhongdian, zhong, tim, pri, planeindex, jinjiqingkuang): lenths = [] paths = [] for index in range(len(planeindex)): chang = [] path = shortest_path(airports, dian[index], zhongdian[index], pos) lenth = tim[index] chang.append(lenth) for index1 in range(len(path) - 1): mmm = cal_dist(path[index1], path[index1 + 1], pos) lenth = lenth + mmm chang.append(lenth) paths.append(path) lenths.append(chang) print(planeindex[index]) print(pri[index]) print(path) print(chang) plane_dic = {} plane_queue = PQueue() for index in range(len(pri)): plane = airplane(planeindex[index], zhong[index], dian[index], zhongdian[index], tim[index], pri[index], paths[index], lenths[index]) plane_dic[planeindex[index]] = plane plane_queue.put((pri[index], plane)) with open('airplane2.pickle', 'wb') as k: pickle.dump(plane_dic, k, pickle.HIGHEST_PROTOCOL) #pickle.dump(plane_queue, k, pickle.HIGHEST_PROTOCOL) pickle.dump(jinjiqingkuang, k, pickle.HIGHEST_PROTOCOL) pickle.dump(planeindex, k, pickle.HIGHEST_PROTOCOL) pickle.dump(pri, k, pickle.HIGHEST_PROTOCOL) pickle.dump(dian, k, pickle.HIGHEST_PROTOCOL) pickle.dump(zhongdian, k, pickle.HIGHEST_PROTOCOL) pickle.dump(zhong, k, pickle.HIGHEST_PROTOCOL) pickle.dump(tim, k, pickle.HIGHEST_PROTOCOL) pickle.dump(paths, k, pickle.HIGHEST_PROTOCOL) pickle.dump(lenths, k, pickle.HIGHEST_PROTOCOL)
def get_least_numbers2(self, arr: List[int], k: int) -> List[int]: ''' 获取最小k个数 Args: arr: 数组 k: 数个数 Returns: 最小k个数 ''' pq = PQueue() new_arr = [] for num in arr: pq.put(num) if pq.qsize() > len(arr) - k: new_arr.append(pq.get()) return new_arr
def greedyEventSchedule(n, timeStart, timeFinish): pq = PQueue() for i in range(n): time = TimeCompareAble(timeStart[i], timeFinish[i]) pq.put(time) # 记录已安排活动的最后结束时间 lastFinishTime = 0 s = [] while pq.qsize() > 0: time = pq.get() if time.start >= lastFinishTime: s.append(time) lastFinishTime = time.finish print(time.start, time.finish) return s
def haffmanCoding(freq): pq = PQueue() total = 0 sumFreq = 0 for number in freq: total += number pq.put(number) while pq.qsize() > 1: jointFreq = 0 for _ in range(2): jointFreq += pq.get() sumFreq += jointFreq pq.put(jointFreq) return sumFreq / total
def prim(start): pq = PQueue() pq.put((0, start)) cost = [INF for _ in range(n + 1)] visited = [False for _ in range(n + 1)] count = 0 ## cost[1]=0 while not pq.empty(): c, node = pq.get() if visited[node]: continue visited[node] = True cost[node] = c count += 1 if count == n: ## print(cost[1:]) return max(cost[1:]) for e in edges[node]: if not visited[e[1]]: pq.put((e[0], e[1]))
def shortest_path(M, start, goal): open_queue = PQueue() open_dict = {} close_dict = {} start_h = cal_dist(M.intersections[start], M.intersections[goal]) start_node = node(None, start, 0, start_h) open_queue.put((start_node.h + start_node.g, start_node)) open_dict[start] = start_node # loop获取路径 while goal not in close_dict and not open_queue.empty(): find_path(M, open_queue, open_dict, close_dict, goal) # 找到路径 path = [] if goal in close_dict: current_node = close_dict[goal] while current_node.parent != None: path.append(current_node.index) current_node = current_node.parent path.append(start) path = [path[len(path) - i - 1] for i in range(len(path))] return path
def shortest_path(airports, start, goal, pos): open_queue = PQueue() open_dict = {} close_dict = {} start_h = cal_dist(start, goal, pos) start_node = node(start, None, 0, start_h) open_queue.put((start_node.h + start_node.g, start_node)) open_dict[start] = start_node while goal not in close_dict and not open_queue.empty(): find_path(airports, open_queue, open_dict, close_dict, goal, pos) path = [] if goal in close_dict: current_node = close_dict[goal] while current_node.parent != None: path.append(current_node.index) current_node = current_node.parent path.append(start) path = [path[len(path) - i - 1] for i in range(len(path))] return path
def shortest_path1(airports, start, goal, pos, tim, chongtudian, chongtuplane, chongtuplanetime, chongtuplanezhonglei, chongtuwaittime, chongtutime2, chongtudian2, jgtime): open_queue = PQueue() open_dict = {} close_dict = {} start_h = cal_dist(start, goal, pos) start_node = node(start, None, tim, start_h) open_queue.put((start_node.g, start_node)) open_dict[start] = start_node while goal not in close_dict and not open_queue.empty(): find_path1(airports, open_queue, open_dict, close_dict, goal, pos, tim, chongtudian, chongtuplane, chongtuplanetime, chongtuplanezhonglei, chongtuwaittime, chongtudian2, chongtutime2, jgtime) path = [] if goal in close_dict: current_node = close_dict[goal] while current_node.parent != None: path.append(current_node.index) current_node = current_node.parent path.append(start) path = [path[len(path) - i - 1] for i in range(len(path))] chang = [] for index in range(len(path)): mmm = close_dict[path[index]].g chang.append(mmm) jieguo = [] jieguo.append(path) jieguo.append(chang) return jieguo
def Run(self): self.Answer=[None]*self.NumCustom self.Search(0,self.SystemInit,self.CustomInit,True,0,PQueue())
for i in range(N): if p.visited[i] == False: min1 = MAX min2 = MAX for j in range(N): min1 = D[i][j] if min1 > D[i][j] else min1 for m in range(N): min2 = D[m][i] if min2 > D[m][i] else min2 pre_length = pre_length + min1 + min2 return pre_length / 2 start_time = time.time() up = get_upper_bound(0, 0, 0) pq = PQueue() #创建一个优先队列 visited = [False] * N visited[0] = True #初始点的lb不起作用,因此随便赋值为0 node = Node(visited=visited, start=0, end=0, k=1, length=0, lb=0, city_list=[0]) index = 0 pq.put((node.lb, index, node)) #将起点加入队列 while pq.qsize() != 0:
if not node.is_leaf(): viz_index += 1 node.left.viz_id = viz_index queue.append(node.left) g.edge(str(node.viz_id), str(node.left.viz_id), label="0") viz_index += 1 node.right.viz_id = viz_index queue.append(node.right) g.edge(str(node.viz_id), str(node.right.viz_id), label="1") g.view() if __name__ == '__main__': n_char = int(input()) nodes = [] queue = PQueue() for _ in range(n_char): inputs = list(input().split()) c = str(inputs[0]) n = int(inputs[1]) queue.put(Node(c, n)) tree = HuffmanTree() while queue.qsize() is not 0: if queue.qsize() is 1: # last tree.root = queue.get() break else: min1 = queue.get() min2 = queue.get()
def get_commend(handle): """ 得到该用户前600发提交 """ print('start') if (cache.get(handle) != None): return json.loads(cache.get(handle)) HandleUrl = "http://codeforces.com/api/user.status?handle=%s&from=1&count=600" % ( handle) text = requests.get(HandleUrl, headers={}).text if (text == None): return {"status": "error"} print('ok') SubmissionSet = json.loads(text).get('result') if (SubmissionSet == None): return {"status": "error"} AcProblemSetUp20 = set() AllACProblem = set() AllSubmission = {} f = open('ProblemSetMY.txt', 'r') text = f.read() f.close() ProblemSet = json.loads(text) MaxRatingAcProblem = PQueue(maxsize=20) for Submission in SubmissionSet: ProblemId = str(Submission.get('problem').get('contestId')) + \ str(Submission.get('problem').get('index')) if (ProblemSet.get(ProblemId) == None): continue if (Submission.get('verdict') == 'OK'): AllACProblem.add(ProblemId) if (MaxRatingAcProblem.full()): MaxRatingAcProblem.get() MaxRatingAcProblem.put(ProblemSet.get(ProblemId).get('difficulty')) if (Submission.get('verdict') == 'OK' and len(AcProblemSetUp20) < 20): AcProblemSetUp20.add(ProblemId) if (AllSubmission.get(ProblemId) == None): AllSubmission[(ProblemId)] = 1 else: AllSubmission[ProblemId] = AllSubmission[ProblemId] + 1 print('ok') if (len(AcProblemSetUp20) == 0): return {"status": "error"} else: Result = {} max_rating = 0.0 max_sub = 0 max_label = -1 real_rating = 0.0 tot = MaxRatingAcProblem.qsize() while not MaxRatingAcProblem.empty(): real_rating = real_rating + MaxRatingAcProblem.get() real_rating = real_rating / (1.0 * tot) SetOfLabels = set() for problemid in AcProblemSetUp20: Result[problemid] = AllSubmission[problemid] max_rating = max(max_rating, ProblemSet.get(problemid).get('difficulty')) SetOfLabels.add(ProblemSet.get(problemid).get('kmeans_result')) if (AllSubmission[problemid] > max_sub): max_sub = AllSubmission[problemid] max_label = ProblemSet.get(problemid).get('kmeans_result') commend_problem_highest_error_rate_set = [] commend_problem_untouch_set = [] if max_sub > 3: for problemname in ProblemSet: problem = ProblemSet[problemname] if (problemname in AllACProblem): continue if (random.randint(0, 20) >= 10): continue if (problem.get('kmeans_result') == max_label and math.fabs(real_rating - problem.get('difficulty')) < 300.0): commend_problem_highest_error_rate_set.append(problem) if len(commend_problem_highest_error_rate_set) >= 5: break for problemname in ProblemSet: problem = ProblemSet[problemname] if (problemname in AllACProblem): continue if (problem.get('kmeans_result') not in SetOfLabels and math.fabs(real_rating - problem.get('difficulty')) < 300.0 and problem.get('difficulty') > real_rating): if (problem.get('difficulty') < real_rating and random.randint(0, 20) >= 10): continue if (random.randint(0, 20) >= 10): continue commend_problem_untouch_set.append(problem) if len(commend_problem_untouch_set) >= 5: break cache.set( handle, json.dumps( { "status": "ok", "result": Result, "real_rating": real_rating, "commend_problem_highest_error_rate_set": commend_problem_highest_error_rate_set, "commend_problem_untouch_set": commend_problem_untouch_set }, cls=MyEncoder)) return { "status": "ok", "result": Result, "real_rating": real_rating, "commend_problem_highest_error_rate_set": commend_problem_highest_error_rate_set, "commend_problem_untouch_set": commend_problem_untouch_set }
def find_path1(airports, open_queue, open_dict, close_dict, goal, pos, tim, chongtudian, chongtuplane, chongtuplanetime, chongtuplanezhonglei, chongtuwaittime, chongtudian2, chongtutime2, jgtime): current_node = open_queue.get()[1] open_dict.pop(current_node.index) roads = list(nx.neighbors(airports, current_node.index)) update_node = False for i in roads: if i not in close_dict: distance1 = cal_dist(i, current_node.index, pos) m = current_node.g + distance1 if (i in chongtudian) and (chongtudian.count(i) == 1): index1 = chongtudian.index(i) if chongtuplanezhonglei[index1] == 1: t1 = chongtuplanetime[index1] if (m - t1 > 0) and (m - t1 < jgtime): ooo = jgtime - abs(m - t1) new_g = m + ooo elif (m - t1 <= 0) and (m - t1 > -jgtime): ooo = jgtime + abs(m - t1) new_g = m + ooo else: new_g = m elif chongtuplanezhonglei[index1] == 2: t1 = chongtuplanetime[index1] t2 = chongtutime2[index1] dian2 = chongtudian2[index1] distance2 = cal_dist(i, dian2, pos) n = m + distance2 if (m > t1) and (m < t1 + jgtime): ooo = jgtime - abs(m - t1) new_g = m + ooo elif (m - t1 <= 0) and (n - t2 >= 0): ooo = jgtime + abs(m - t1) new_g = m + ooo elif (n - t2 < 0) and (n - t2 > -jgtime): ooo = jgtime + abs(m - t1) new_g = m + ooo else: new_g = m elif chongtuplanezhonglei[index1] == 3: t1 = chongtuplanetime[index1] t2 = chongtutime2[index1] dian2 = chongtudian2[index1] distance2 = cal_dist(i, dian2, pos) n = m + distance2 if (m > t1) and (m < t1 + jgtime): ooo = 100000 new_g = ooo elif (m - t1 <= 0) and (n - t2 >= 0): ooo = 100000 new_g = ooo elif (n - t2 < 0) and (n - t2 > -jgtime): ooo = 100000 new_g = ooo else: new_g = 100000 elif chongtuplanezhonglei[index1] == 4: t1 = chongtuplanetime[index1] ooo = jgtime + abs(m - t1) new_g = m + ooo elif (i in chongtudian) and (chongtudian.count(i) > 1): shu = chongtudian.count(i) ctplane = [] cttime = [] ctzhonglei = [] cttime2 = [] ctdian2 = [] shunxu = PQueue() mm = m for index in range(len(chongtudian)): if chongtudian[index] == i: ctplane.append(chongtuplane[index]) cttime.append(chongtuplanetime[index]) ctzhonglei.append(chongtuplanezhonglei[index]) cttime2.append(chongtutime2[index]) ctdian2.append(chongtudian2[index]) for index1 in range(len(cttime)): shunxu.put((cttime[index1], index1)) while (not shunxu.empty()): current = shunxu.get()[1] if (ctzhonglei[current] == 1): t1 = cttime[current] if (mm - t1 > 0) and (mm - t1 < jgtime): ooo = jgtime - abs(mm - t1) mm += ooo elif (mm - t1 <= 0) and (mm - t1 > -jgtime): ooo = jgtime + abs(mm - t1) mm += ooo elif (ctzhonglei[current] == 2): t1 = cttime[current] t2 = cttime2[current] dian2 = ctdian2[current] distance2 = cal_dist(i, dian2, pos) nn = mm + distance2 if (mm > t1) and (mm < t1 + jgtime): ooo = jgtime - abs(mm - t1) mm += ooo elif (mm - t1 <= 0) and (nn - t2 >= 0): ooo = jgtime + abs(mm - t1) mm += ooo elif (nn - t2 < 0) and (nn - t2 > -jgtime): ooo = jgtime + abs(mm - t1) mm += ooo elif (ctzhonglei[current] == 3): t1 = cttime[current] t2 = cttime2[current] dian2 = ctdian2[current] distance2 = cal_dist(i, dian2, pos) nn = mm + distance2 if (mm > t1) and (mm < t1 + jgtime): ooo = 100000 mm += ooo elif (mm - t1 <= 0) and (nn - t2 >= 0): ooo = 100000 mm += ooo elif (nn - t2 < 0) and (nn - t2 > -jgtime): ooo = 100000 mm += ooo new_g = mm else: new_g = m if i in open_dict: if new_g < open_dict[i].g: open_dict[i].parent = current_node open_dict[i].g = new_g update_node = True else: new_h = cal_dist(i, goal, pos) new_node = node(i, current_node, new_g, new_h) open_dict[i] = new_node open_queue.put((new_node.g, new_node)) if update_node: open_queue = PQueue() for v in open_dict.values(): open_queue.put((v.g, v)) close_dict[current_node.index] = current_node
def build_huffman_tree(project_name, gv=global_var): """ 构建霍夫曼树 :param gv: :param project_name: :return: """ global_lock[project_name].acquire() try: if gv.load_hf_tree(project_name): return gv.hf_root word_count = {} class_names = get_all_need_class_name(project_name) ## 统计此项目的word count for root, dirs, files in os.walk(gv.projects_source_dir): if not root.__contains__(project_name): continue for file in files: full_class_name = get_full_class_name(root, file) if full_class_name is not None and full_class_name in class_names: with open(os.path.join(root, file), "r") as _file_obj: try: content = _file_obj.read() if content is None: continue logging.debug( 'building huffman tree,processing file %s ' % os.path.join(root, file)) ast_tree = jl.parse.parse(content) for _path, _node in ast_tree: name = get_node_name(_node) if name is not None: word_count[name] = word_count.get(name, 0) + 1 except jl.parser.JavaSyntaxError: logging.error('file %s syntax error!' % os.path.join(root, file)) except AttributeError: logging.error('file %s attribute error' % os.path.join(root, file)) except UnicodeDecodeError: logging.error( 'parse file %s unicode decode error' % os.path.join(root, file)) pq = PQueue() for k, v in word_count.items(): vi = VocabNode(k) vi.count = v pq.put(vi) del word_count while pq.qsize() > 1: nl = StemNode(pq.get(), pq.get()) pq.put(nl) result = pq.get() # 确定huffman树的每一个节点的路径编码 encode_path(result, []) gv.hf_root = result gv.dump_hf_tree(project_name, result) return result finally: global_lock[project_name].release()
def clear(self): self._priority_queue = PQueue() self._put_closed = False self._get_closed = False
def Search(self,TimeNow,System,AllocatedSource,CanAlloc,deep,WaitQueue): # print('Time=',TimeNow) # print('System=',System) # print('AllocatedSource=',AllocatedSource) # print('CanAlloc=',CanAlloc) # print('Deep=',deep) # print("QueueSize=",WaitQueue.qsize()) # que=[] # while WaitQueue.empty() == False: # tmp=WaitQueue.get() # print('Time=',tmp.NeedTime) # print('Custom=',tmp.Custom,'\n') # que.append(tmp) # for i in que: # WaitQueue.put(i) # print('Answer=',self.Answer,'\n\n') # if self.NumSafe >= 100: # return if CanAlloc == False: # 无法分配,释放资源,时间跳到下一个节点 if WaitQueue.qsize() == 0: # 死锁,直接返回 return else: # 出队 tmp = WaitQueue.get() TimeNow = TimeNow + tmp.NeedTime for i in range(self.NumResource): System[i] = System[i] + self.MaxSource[tmp.Custom][i] if TimeNow > self.TimeMinl: # 剪枝条件 return if deep == self.NumCustom: # 得到安全序列,输出到文件,回溯 self.NumSafe = self.NumSafe + 1 self.WriteFile(WaitQueue) return CustomNeed = [None] * self.NumCustom for key in range(self.NumCustom): # 得到当前用户需要的资源数量 CustomNeed[key] = [None]*self.NumResource for i in range(self.NumResource): CustomNeed[key][i] = self.MaxSource[key][i] - AllocatedSource[key][i] for key in range(self.NumCustom): # 遍历每个用户判断是否能进行分配 if key in self.Answer:# 判定重复 continue flag = False # flag为False为能分配 for i in range(self.NumResource): if System[i] < CustomNeed[key][i]: flag = True;break if flag == False: CanAlloc = True # 深拷贝一堆参数 TmpAllcatedSource = copy.deepcopy(AllocatedSource) TmpSystem = copy.deepcopy(System) TmpQueue = PQueue() TmpVector = [] while WaitQueue.empty() == False: Tmp = copy.deepcopy(WaitQueue.get()) TmpVector.append(Tmp) TmpQueue.put(Tmp) for it in TmpVector: WaitQueue.put(it) # print('TmpSystem=',TmpSystem) # print('TmpAllcatedSource=',TmpAllcatedSource) # print('AllocatedSource=',AllocatedSource) # print('Need=',CustomNeed) for i in range(self.NumResource): # 扣除该用户需要的资源 TmpSystem[i] = System[i] - CustomNeed[key][i] TmpAllcatedSource[key][i] = self.MaxSource[key][i] # 标记安全序列 self.Answer[deep] = key TmpQueue.put(Node(self.NeedTime[key] + TimeNow,key)) # print('Key=',key) # print('AfterTmpSystem=',TmpSystem) # print('AfterTmpAllcatedSource=',TmpAllcatedSource) self.Search(TimeNow,TmpSystem,TmpAllcatedSource,CanAlloc,deep+1,TmpQueue) # 满足分配条件,进行分配 self.Answer[deep] = None else: CanAlloc = False if CanAlloc == False: # 不满足分配条件,进入递归 self.Search(TimeNow,System,AllocatedSource,CanAlloc,deep,WaitQueue)
def h(start, end, heristicFunc=EuclideanDistance): # return heuristic cost estimation from start to end return fuc(start, end) def path(cameFrom, node): path = [node] while node in cameFrom.keys(): node = cameFrom[node] path.insert(0, node) return path def Astar(problem): start = problem.sart goal = problem.goal visited = set() toVisit = PQueue() cameFrom = {} f = defaultdict(lambda: float('inf')) g = defaultdict(lambda: float('inf')) g[start] = 0 f[start] = g[start] + h(start, goal) toVisit.put((f[start], start)) while not toVisit.empty(): f_score, node = toVisit.get() if node == goal: return path(cameFrom, node) visited.add(node) for child in graph[node]: tentative_score = g[node] + c[node][child] if tentative_score < g[child]: