def schedule(tasks, num_processors): _cpy_list = [t for t in tasks] total_slice = 0 for i in tasks: total_slice += i _cpy = total_slice pq = PriorityQueue() temp_out_list = [0]*len(tasks) for i in range(len(tasks)): pq.put((1.0 / tasks[i], i)) iterations = 0 misses = 0 while pq.empty() == False: temp_out_c = 0 for i in range(num_processors): if pq.empty() == True: break else: temp_out_list[temp_out_c] = pq.get()[1] temp_out_c += 1 for i in range(temp_out_c): tasks[temp_out_list[i]] -= 1 if tasks[temp_out_list[i]] != 0: pq.put(((_cpy_list[temp_out_list[i]] - tasks[temp_out_list[i]]) * _cpy / _cpy_list[temp_out_list[i]], temp_out_list[i])) iterations += 1 #print iterations for i in range(len(tasks)): lag = 1.0 * _cpy_list[i] * iterations / _cpy - (_cpy_list[i] - tasks[i]) if lag > 0: misses += math.floor(lag) return misses
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.") pq = PriorityQueue() while not best_matches.empty(): pq.put(best_matches.get()) while not good_matches.empty(): pq.put(good_matches.get()) while not (ok_matches.empty()): pq.put(ok_matches.get()) print("Results: ") matches = [] while not pq.empty(): matches.append(pq.get()[1]) return matches
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))
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
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(v, r) matches_found += 1 print(v) if matches_found == num_matches: break else: continue if r<.9: print("Good Match Found") good_matches.put(v, r) matches_found += 1 print(v) if matches_found == num_matches: break else: continue if r<=1: print("Great Match Found") best_matches.put(v, r) 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())
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'
def req_proxy(self, url): from urlparse import urlparse netloc = urlparse(url).netloc busy_queue = PriorityQueue() lazy_queue = PriorityQueue() index = 0 now = datetime.utcnow() while index < self.proxy_in_queue_count(): index += 1 proxy_url = self.get_proxy_from_queue() if not proxy_url: break if proxy_url in self.proxy_meta_map: proxy_meta = self.proxy_meta_map[proxy_url] if proxy_meta.last_used_time and (now - proxy_meta.last_used_time).total_seconds() < self.settings["interval_second"]: busy_queue.put_nowait((proxy_meta.last_used_time, proxy_url)) continue if netloc in proxy_meta.latency and proxy_meta.latency[netloc][0] >= self.settings["max_unavailable_count"]: import random if random.randint(1, 10) > 1: lazy_queue.put_nowait((proxy_meta.latency[netloc], proxy_url)) continue proxy_meta.last_used_time = now proxy_meta.master = netloc return proxy_meta.proxy while not lazy_queue.empty(): _, proxy_url = lazy_queue.get_nowait() if proxy_url in self.proxy_meta_map: proxy_meta = self.proxy_meta_map[proxy_url] proxy_meta.last_used_time = now proxy_meta.master = netloc return proxy_meta.proxy return None
def a_star(self): # like BFS, but puts coords with lowest heuristic (path length + manhattan dist to goal) up front pq = PriorityQueue(maxsize=0) pq.put_nowait((self.manhattan_distance(self.currPos, self.goalPos), (self.currPos, []))) visited = set() bestPath = None bestHeur = None numNodes = 0 while not pq.empty(): priority, curr = pq.get_nowait() coord, path = curr visited.add(coord) if bestPath is not None and priority >= bestHeur: pass elif self.getChar(coord) == '%': # wall pass else: # recursive case if self.getChar(coord) == '.': # goal print "Found a path:", path if bestPath is None or len(path) < len(bestPath): print "Is best path" bestPath = path[:] bestHeur = priority for adj, direction in self.adjacent(coord): if adj not in visited and self.getChar(adj) != '%': numNodes += 1 heur = len(path + direction) + self.manhattan_distance(adj, self.goalPos) if bestPath is None or heur < bestHeur: # preselect based on heuristic pq.put_nowait((heur, (adj, path + direction))) print "Num Nodes:", numNodes print self.debug(bestPath) # debug return bestPath
def dijkstra(self, adjacencies, start_point, end_point): seen_so_far = defaultdict(float) for k in adjacencies: seen_so_far[k] = float('inf') q = PriorityQueue() start = Vertex(start_point, [], 0.0) q.put(start) seen_so_far[start_point] = 0 while not q.empty(): v = q.get() if v.coords == end_point: new_path = v.path new_path.append(end_point) # write to file with open("output", "w") as out_file: for point in new_path: out_file.write('{} {}\n'.format(point[0], point[1])) self.best_path = [(new_path[i], new_path[i+1]) for i in range(len(new_path)-1)] return for point in adjacencies[v.coords]: # import pdb; pdb.set_trace() new_cost = v.cost+distance(v.coords, point) if seen_so_far[point]<new_cost: continue seen_so_far[point]=new_cost new_path = deepcopy(v.path) new_path.append(v.coords) q.put(Vertex(point, new_path, new_cost))
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
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)
def segment(img) : g = numpy.array(img.getdata()) g = g.reshape(img.size) gx = g[:-1,1:] - g[:-1,:-1] gy = g[1:,:-1] - g[:-1,:-1] gms = gx * gx + gy * gy seg = Image.new('L', gms.shape) taken = numpy.zeros(seg.size) #print 'original local mininum' for i in range(seg.size[0]) : for j in range(seg.size[1]) : if localmin(gms, i, j) : taken[i][j] = 1 seg.putpixel((j, i), img.getpixel((j, i))) #print (j, i), img.getpixel((j, i)) q = PriorityQueue(-1) #print 'expand' for i in range(seg.size[0]) : for j in range(seg.size[1]) : if taken[i][j] > 0 : addNeighbor(seg, gms, taken, i, j, q) #print q.qsize() while not q.empty() : (i, j) = q.get()[1] addNeighbor(seg, gms, taken, i, j, q) #print q.qsize() return seg
def main(): global CREATURE_COUNT if process_command_line(): c0 = [] c1 = PriorityQueue() print "[+]\tLoading DB..." # load in creatures from DB lc = load_DB() # f = open('database', 'a') # f.close() # f = open('database') # lc = f.read() # f.close() loaded_creatures = lc.split("\n") # finish loading print "[+]\tSuccess" print "[+]\tCreating initial batch of creatures..." cl = create_creatures(CREATURE_COUNT, GENOME_LENGTH, tools) generation = 0 for i in cl: c1.put((100, i)) for i in loaded_creatures: c1.put((50, Creature(0, i))) for ii in range(0, GENE_POOL_INFLUENCE - 1): c1.put((50, Creature(0, mutate(i)))) print "[+]\tSuccess" # print '[+]\tPre-breeeding in loaded creatures with the population' +\ # ' for great success' # while not c1.empty(): # c = c1.get()[1] # c0.append(c) # c1 = breed_it(c0) # c1 = c0 print "[+]\tSuccess" exploit_found = 0 while exploit_found == 0: generation += 1 CREATURE_COUNT = c1.qsize() print "[>]\tRunning with creature_count %d,\tgeneration %d" % (CREATURE_COUNT, generation) c2 = PriorityQueue(0) cached_c = 0 total_c = 0 while not c1.empty(): c = c1.get()[1] total_c += 1 if c.modified == 0: cached_c += 1 if fitnessfunction(c) == 1: exploit_found = 1 break c2.put((c.score, c)) # print '[i]\tEfficiency %s, cached[%d], total[%d]' %\ # (str((total_c-cached_c) * 1.0 / total_c),cached_c,total_c) c3 = cull_it(c2) c4 = [] while not c3.empty(): c = c3.get()[1] c4.append(c) c1 = breed_it(c4) print "[i]\tExploit found in %d seconds with %d requests" % (abs(int(start_time - time.time())), REQ_TOTAL)
def a_star_penalize(self, forwardPenalty, turnPenalty): # part 1.2 # using euclidean heuristic (not manhattan) pq = PriorityQueue(maxsize=0) pq.put_nowait((self.manhattan_distance(self.currPos, self.goalPos), (self.currPos, []))) visited = set() bestPath = None bestHeur = None numNodes = 0 while not pq.empty(): priority, curr = pq.get_nowait() coord, path = curr visited.add(coord) if bestPath is not None and priority >= bestHeur: pass elif self.getChar(coord) == '%': # wall pass else: # recursive case if self.getChar(coord) == '.': # goal print "Found a path:", path if bestPath is None or len(path) < len(bestPath): bestPath = path[:] for adj, direction in self.adjacent(coord): if adj not in visited and self.getChar(adj) != '%': numNodes += 1 heur = self.calculate_penalty(path + direction, forwardPenalty, turnPenalty) + self.manhattan_distance(adj, self.goalPos) * forwardPenalty if bestPath is None or heur < bestHeur: # preselect based on heuristic pq.put_nowait((heur, (adj, path + direction))) print "Num Nodes:", numNodes print self.debug(bestPath) # debug return bestPath
def Astar(start, goal, cost_matrix, strategy='minimize'): mindist = min([min(dists.values()) for c, dists in cost_matrix.items()]) maxdist = max([max(dists.values()) for c, dists in cost_matrix.items()]) def cost_fn(path): if strategy == 'minimize': return path_cost(path, cost_matrix) + (len(cost_matrix.keys()) - len(path)) * 2 * mindist else: return -(path_cost(path, cost_matrix) + (len(cost_matrix.keys()) - len(path)) * 2 * maxdist) abort_cost = 1 << 32 q = PriorityQueue() q.put((cost_fn([start]), [start])) best_path = [] while not q.empty(): total_cost, partial_path = q.get() if total_cost > abort_cost: break if partial_path[-1] == goal and len(partial_path) == len(cost_matrix.keys()): if total_cost < abort_cost: abort_cost = total_cost best_path = partial_path else: for c in cost_matrix[partial_path[-1]]: if c not in partial_path: new_path = partial_path + [c] total_cost = cost_fn(new_path) if total_cost < abort_cost: q.put((total_cost, new_path)) return best_path
def calculatePathToPoint(self, start, goal): # http://en.wikipedia.org/wiki/A*_search_algorithm#Pseudocode closedSet = set() queue = PriorityQueue(0) queue.put((self._heuristic(start, goal), start)) cameFrom = {} score = {} score[start] = 0 while not queue.empty(): _, current = queue.get() if current.x == goal.x and current.y == goal.y: return self._reconstructPath(cameFrom, goal) if current.flatten(800) in closedSet: continue closedSet.add(current.flatten(800)) for neighbor in self._getNeighborNodes(current): if neighbor.flatten(800) in closedSet: continue turnPenalty = 0.5 if current.flatten(800) in cameFrom and \ current.x - neighbor.x == cameFrom[current.flatten(800)].x - current.x and \ current.y - neighbor.y == cameFrom[current.flatten(800)].y - current.y: turnPenalty = 0 neighborScore = score[current] + 1 + turnPenalty cameFrom[neighbor.flatten(800)] = current score[neighbor] = neighborScore queue.put((neighborScore + self._heuristic(neighbor, goal), neighbor)) return []
def trapRainWater(self, board): """ :type board: List[List[int]] :rtype: int """ m = len(board) if m == 0: return 0 n = len(board[0]) queue = PriorityQueue() visited = [[False] * n for _ in range(m)] for i in range(m): visited[i][0] = True queue.put((board[i][0], i, 0)) visited[i][n-1] = True queue.put((board[i][n-1], i, n-1)) for j in range(n): visited[0][j] = True queue.put((board[0][j], 0, j)) visited[m-1][j] = True queue.put((board[m-1][j], m-1, j)) dirs = [(-1,0),(1,0),(0,-1),(0,1)] res = 0 while not queue.empty(): h, i, j = queue.get() for di, dj in dirs: ni, nj = i+di, j+dj if ni >= 0 and ni < m and nj >= 0 and nj < n and not visited[ni][nj]: visited[ni][nj] = True res += max(0, h - board[ni][nj]) queue.put((max(h, board[ni][nj]), ni, nj)) return res
def astar(source, target, graph, heuristic=null_heuristic): """ A* algorithm """ queue = PriorityQueue() 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 fn = cost + heuristic(v_out, graph) # only diference in retion to UCS if v_out not in visited: queue.put((fn, v_out)) parent[v_out] = v_in visited[v_in] = cost return None
def run(graph,start,goal): frontier = PriorityQueue() frontier.put((0,start)) came_from = {} came_from[start] = None cost_so_far = {} cost_so_far[start] = 0 while not frontier.empty(): current = frontier.get()[1] if current == goal: break for nextNode in graph.neighbors(current): new_cost = cost_so_far[current] + graph.cost(current,nextNode) if nextNode not in cost_so_far or new_cost < cost_so_far[nextNode]: frontier.put((new_cost,nextNode)) cost_so_far[nextNode] = new_cost; came_from[nextNode] = current #for x in came_from: # print (x,came_from[x],), print len(came_from) current = goal path = [current] while current != start: current = came_from[current] path.append(current) print path
def a_star(self, start, goal): """""" # Init priority queue. q = PriorityQueue() q.put(start, 0) came_from = {start: None} # The known cost needed to travel from start to a point. visited = {start: 0} while not q.empty(): # pop the point with lowest priority(cost). # The idea is always that points tends to be closer to goal first. p = q.get() if p == goal: # Reach the goal. self.reconstruct_path(came_from, goal) return for neighbor in self.neighbors(p): tentative = visited[p] + self.heuristic(p, neighbor) if neighbor not in visited or tentative < visited[neighbor]: came_from[neighbor] = p visited[neighbor] = tentative # Priority is the estimated cost that it took to travel from # neighbor to goal. In this case, it's just the Manhattan # distance assuming that there are no obstacles between them. priority = tentative + self.heuristic(neighbor, goal) q.put(neighbor, priority)
def aStar(initialState, heuristic): #stores a list of visited coords, the direction and whether the tile was affected by demolish visited = [] fringe = PriorityQueue() #add the start node to the fringe fringe.put((-(initialState.score - heuristic(initialState)), initialState)) while True: if fringe.empty(): print "No Solution" return None nextState = fringe.get()[1] #check if the tile has been affected by demolish if len(nextState.actionList) > 0 and nextState.actionList[-1] is nextState.act_demolish: if (nextState.posX, nextState.posY, "s") in nextState.demolishedTiles: tileDemolished = "S" else: if (nextState.posX, nextState.posY) in nextState.demolishedTiles: tileDemolished = "D" else: tileDemolished = "N" if nextState.isGoalState(): #we have reached the goal. Return the relevant stats return (nextState.actionList, nextState.score, len(visited)) elif (nextState.posX, nextState.posY, nextState.direction, tileDemolished) not in visited: visited.append((nextState.posX, nextState.posY, nextState.direction, tileDemolished)) for successorState in nextState.getSuccessors(): fringe.put((-(successorState.score - heuristic(successorState)), successorState))
class MinimumWeightMatchingWithEdges: """Find a minimum weight matching using a greedy method. Attributes ---------- graph : input undirected graph mate : dict with nodes (values are edges or None) cardinality : number """ # Bedzie potrzebne do problemu chinskiego listonosza. def __init__(self, graph): """The algorithm initialization.""" if graph.is_directed(): raise ValueError("the graph is directed") self.graph = graph self.mate = dict((node, None) for node in self.graph.iternodes()) self.cardinality = 0 self._pq = PriorityQueue() def run(self): """Executable pseudocode.""" for edge in self.graph.iteredges(): self._pq.put((edge.weight, edge)) while not self._pq.empty(): _, edge = self._pq.get() if (self.mate[edge.source] is None and self.mate[edge.target] is None): self.mate[edge.source] = edge self.mate[edge.target] = ~edge self.cardinality += 1
def find_path(startkey, goalkey, world): frontier = PriorityQueue() frontier.put({"key": startkey, "cost": 0}, 0) came_from = {startkey: None} cost_so_far = {startkey: 0} step = 0 while not frontier.empty() and step < 100000: step += 1 # just safety current = frontier.get() if current["key"] == goalkey: break for neighbor in get_neighbors(current["key"], world): new_cost = cost_so_far[current["key"]] + neighbor["cost"] if new_cost < cost_so_far.get(neighbor["key"], 10000000): cost_so_far[neighbor["key"]] = new_cost neighbor["cost"] += heuristic(neighbor["key"], goalkey) frontier.put(neighbor) came_from[neighbor["key"]] = current["key"] if current["key"] != goalkey: return None key = goalkey path = [] step = 0 while key and step < 1000: key = came_from[key] path.insert(0, key) return path
def run(graph,start,goal): frontier = PriorityQueue() frontier.put((0,start)) came_from = {} came_from[start] = None while not frontier.empty(): current = frontier.get()[1] if current == goal: break for next in graph.neighbors(current): new_cost = graph.gcost(next,goal) if next not in came_from: frontier.put((new_cost,next)) came_from[next] = current #for x in came_from: # print x,came_from[x] print len(came_from) current = goal path = [current] while current != start: current = came_from[current] path.append(current) print path
class EventManager(object): """ Keeps track of and triggers events in our network simulation Instance Properties: Q - A priority queue of events ordered by time """ def __init__(self): self._Q = PriorityQueue() self.t = 0 def add_event(self, t, event, args): """ Add an event onto the event queue :param t: The time of the event :param event: The event to add :param args: A list of args for the event """ self._Q.put((t, event, args)) def pop_event(self): """ Getting the first event off of the event queue. :return: Returns a tuple consisting of the time, event, and the args for the event """ return self._Q.get() def has_events(self): """ Whether or not the manager has any more events on its queue :return: True or False based on whether its empty """ return not self._Q.empty()
def greedy(self): # like DFS, but puts coords closest to goal up front pq = PriorityQueue(maxsize=0) pq.put_nowait((self.manhattan_distance(self.currPos, self.goalPos), (self.currPos, []))) visited = set() bestPath = None numNodes = 0 while not pq.empty(): priority, curr = pq.get_nowait() coord, path = curr visited.add(coord) if bestPath is not None and len(path) >= len(bestPath): pass elif self.getChar(coord) == '%': # wall pass else: # recursive case if self.getChar(coord) == '.': # goal print "Num Nodes:", numNodes print self.debug(path) return path # return on first path found for adj, direction in self.adjacent(coord): if adj not in visited and self.getChar(adj) != '%': numNodes += 1 heur = self.manhattan_distance(adj, self.goalPos) if bestPath is None: # preselect based on heuristic pq.put_nowait((heur, (adj, path + direction))) return [] # impossible
def travel(self): visitedNodes = 0 optimalPath = [] priorityQueue = PriorityQueue() optimalPathLen = -1 priorityQueue.put(self.addNode(0, [])) startTime = time.time() while not priorityQueue.empty(): currentPath = priorityQueue.get()[1] if optimalPath: pathLen = self.graph.getPathLength(optimalPath) upperBound = self.graph.upperBound(self.graph.getPathLength(currentPath), currentPath) optimalPathLen = self.graph.getPathLength(optimalPath) # Jesli nie mamy sciezki lub jest ona gorsza od UB if not optimalPath or upperBound < pathLen: visitedNodes += 1 # Jesli odwiedzono wszystkie miasta if len(currentPath) >= self.graph.size: # Jesli mozna wrocic do miasta poczatkowego z ostatniego punktu sciezki if self.graph.distance(currentPath[len(currentPath) - 1], 0) > 0: currentPath = self.addNode(0, currentPath)[1] # Jesli nie mamy sciezki lub zmodyfikowan jest lepsza niz dotychczasowe if not optimalPath or optimalPathLen > self.graph.getPathLength(currentPath): optimalPath = currentPath else: for i in range(self.graph.size): # Jesli rozwazane miasto nie bylo odwiedzone i istnieje prowadzaca do niego droga if i not in currentPath and self.graph.distance(currentPath[len(currentPath) - 1], i) > 0: newPath = copy.deepcopy(currentPath) bound, newPath = self.addNode(i, newPath) if not optimalPath or bound < optimalPathLen: priorityQueue.put((bound, newPath)) return [optimalPath, self.graph.getPathLength(optimalPath), visitedNodes, time.time() - startTime]
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]
def main(): seed = (0, str(467959)) unique = set() unique.add(seed) nextOnes = PriorityQueue() nextOnes.put(seed) _prev = list(open('./visited.txt', 'r').read()) for item in _prev: unique.add(item) global beforeNext while (not nextOnes.empty()): summID = nextOnes.get() summID = summID[1] url = makeURL(summID) json = performReq(url) matches = getMatches(json) matches.sort(reverse=True) if matches == list(): nextOnes.put(summID) continue printMatches(matches) scrapeSummIDs = getSummonerIds(matches) for summId in scrapeSummIDs: if summId[1] not in unique: nextOnes.put(summId) unique.add(summId[1]) beforeNext = 0
def IDAsearch(state, goalBoard, N): Q = PriorityQueue() initial_cost = Heuristic(state[1], N) Q.put((state, initial_cost), initial_cost) current_threshold = initial_cost next_threshold = initial_cost visited = [] while(Q.empty() != True): current_threshold = next_threshold next_threshold = maxint visit = Q.get() print visit con = 0 if(check(visit[0][1], goalBoard, N) == 0): #print "f**k yeah, found it" return visit[0][2] elif(visit[1] <= current_threshold): if visit[0][1] not in visited: con = 0 else: con = 1 if(con == 0): children = getNextState(visit[0], N) CQ = PriorityQueue() for child in children: b = Heuristic(child[1], N) h = b + 1 if( h < next_threshold): next_threshold = h if child[1] not in visited: Q.put((child, h), h) visited.append(visit[0][1])
def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ if not lists: return None dummy = res = ListNode(0) tmp = PriorityQueue() for l in lists: if l: tmp.put((l.val,l)) while not tmp.empty(): val, node = tmp.get() res.next = ListNode(val) res = res.next node = node.next if node: tmp.put((node.val, node)) return dummy.next
def getLeastNumbers(self, arr, k): """ :type arr: List[int] :type k: int :rtype: List[int] """ if k <= 0 or (not arr) or len(arr) == 0: return [] Q = PriorityQueue() for x in arr: Q.put(-x) while Q.qsize() > k: Q.get() ans = [] while not Q.empty(): ans.append(-Q.get()) return ans
def minMeetingRooms(self, intervals): """ :type intervals: List[Interval] :rtype: int """ from Queue import PriorityQueue # O(nlgn) Q = PriorityQueue() intervals.sort(key=lambda itm: itm.start) for i, interval in enumerate(intervals): if Q.empty(): Q.put(interval.end) else: head = Q.get() if head <= interval.start: Q.put(interval.end) else: Q.put(interval.end) Q.put(head) return Q.qsize()
def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ head = current = ListNode(0) q = PriorityQueue() for l in lists: if l: q.put((l.val, l)) while not q.empty(): val, node = q.get() current.next = ListNode(val) current = current.next node = node.next if node: q.put((node.val, node)) return head.next
def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ m_queue = PriorityQueue() for node in lists: if node: m_queue.put((node.val, node)) tail = ListNode(0) head = tail while not m_queue.empty(): now_val, now_node = m_queue.get() p = ListNode(now_val) tail.next = p tail = p now_node = now_node.next if (now_node): m_queue.put((now_node.val, now_node)) return head.next
def aStar(state): frontier = PriorityQueue() for move in state.getTransitions(): frontier.put((0, move)) while not frontier.empty(): dist, curr = tuple(frontier.get()) position = curr[0] targets = list(curr[1]) if len(targets) == 0: break state.move(position, targets) cost = len(state.currentPath) state.costs[position][tuple(sorted(targets))] = cost for neighbor, targets in state.getTransitions(): if len(targets) == 0: return if state.costs[neighbor][tuple(sorted(targets))] > cost + 1: state.costs[neighbor][tuple(sorted(targets))] = cost + 1 priority = cost + heuristic(position, targets) frontier.put((priority, (neighbor, targets)))
def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ sentinel = current = ListNode(0) queue = PriorityQueue() for node in lists: if node: queue.put((node.val, node)) while not queue.empty(): _, node = queue.get() current.next = node current = node if node.next: queue.put((node.next.val, node.next)) return sentinel.next
def branch_and_bound(graph, start, goal): if start == goal: return list(start) paths = PriorityQueue() for node in graph.get_connected_nodes(start): paths.put_nowait((path_length(graph, [start, node]), [start, node])) while not paths.empty(): path = paths.get_nowait() if path[1][-1] == goal: return path[1] else: cnodes = graph.get_connected_nodes(path[1][-1]) for node in cnodes: if path[1].count(node) == 0: epath = list(path[1]) epath.append(node) paths.put_nowait((path_length(graph, epath), epath)) return []
def mergeKLists(self, lists): """ Two solutions provided, one runs in O(N log k) where N is the total number of nodes and k is the number of lists, the best runtime is 180ms, this should be the fastest way of solving this problem, however, my second solution, which is putting everything into a list and then sort it, then create a ListNode for each value, this one should runs in O(N logN) time, but when I tried on LeetCode, the best runtime is 84ms which is much faster than the first solution, I am guessing it because I used Python list.sort() method which is already optimized to run in shorter time whenever possible. """ pqueue = PriorityQueue() for l in lists: if l: pqueue.put((l.val, l)) head = ListNode(-1) cur_node = head while not pqueue.empty(): val, node = pqueue.get() cur_node.next = node cur_node = node node = node.next if node: pqueue.put((node.val, node)) return head.next """
def solve(initial_board): fringe = PriorityQueue() closed = {} priority_1 = calculate_heuristic_1(initial_board, 0) fringe.put((priority_1, (initial_board, ""))) while not fringe.empty(): (priority, (state, route_so_far)) = fringe.get() closed[tuple(state)] = priority #closed.append((priority,(state))) if is_goal(state): return (route_so_far) for (succ, move) in successors(state): route = str(route_so_far + " " + move) cost_so_far = len(route_so_far.split()) priority_s = calculate_heuristic_1(succ, cost_so_far) if tuple(succ) not in closed: fringe.put((priority_s, (succ, route))) elif tuple(succ) in closed and closed[tuple(succ)] < closed[tuple( state)]: fringe.put((priority_s, (succ, route)))
def ans(self, matrix, k): if len(matrix) == 0 or len(matrix[0]) == 0 or k <= 0: return None from Queue import PriorityQueue pq = PriorityQueue() for i in range(len(matrix)): for j in range(len(matrix[0])): pq.put((matrix[i][j], matrix[i][j])) i = 0 res = None while i < k: if not pq.empty(): res = pq.get()[1] i += 1 return res
class queue: def __init__(self): self.list_queue = PriorityQueue() def push(self, node): self.list_queue.put(node) def distance(self, pt1, pt2): return np.sqrt((pt2[0] - pt1[0]) * (pt2[0] - pt1[0]) + (pt2[1] - pt1[1]) * (pt2[1] - pt1[1])) def pop(self): node = self.list_queue.get() return node def leng(self): return self.list_queue.qsize() def isfull(self): return np.invert(self.list_queue.empty())
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]
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
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)
class Dijkstra: def __init__(self, graph): self.graph = graph # Shortest path tree as a dictionary. self.parent = dict((node, None) for node in self.graph.iternodes()) self.distance = dict( (node, float("inf")) for node in self.graph.iternodes()) self.source = None self._in_queue = dict((node, True) for node in self.graph.iternodes()) self._pq = PriorityQueue() def run(self, source): self.source = source self.distance[source] = 0 for node in self.graph.iternodes(): self._pq.put((self.distance[node], node)) while not self._pq.empty(): _, node = self._pq.get() if self._in_queue[node]: self._in_queue[node] = False else: continue for edge in self.graph.iteroutedges(node): if self._in_queue[edge.target] and self._relax(edge): self._pq.put((self.distance[edge.target], edge.target)) def _relax(self, edge): alt = self.distance[edge.source] + edge.cost if self.distance[edge.target] > alt: self.distance[edge.target] = alt self.parent[edge.target] = edge.source return True return False def path(self, target): if self.source == target: return [self.source] elif self.parent[target] is None: raise ValueError("no path to target") else: return self.path(self.parent[target]) + [target]
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 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
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
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
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
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
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
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))
def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ from Queue import PriorityQueue if not lists: return None pq = PriorityQueue() for lst in lists: if lst: pq.put((lst.val, lst)) dummy = cur = ListNode(None) while not pq.empty(): v, nd = pq.pop() cur.next = nd cur = cur.next if nd.next: pq.put((nd.next.val, nd.next)) return dummy.next
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'
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
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
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()
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