def compute_path(grid,start,goal,cost,heuristic): # Use the OrderedSet for your closed list closed_set = OrderedSet() # Use thePriorityQueue for the open list open_set = PriorityQueue(order=min, f=lambda v: v.f) # Keep track of the parent of each node. Since the car can take 4 distinct orientations, # for each orientation we can store a 2D array indicating the grid cells. # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up parent = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]] # The path of the car path =[['-' for row in range(len(grid[0]))] for col in range(len(grid))] x = start[0] y = start[1] theta = start[2] h = heuristic[x][y] g = 0 f = g+h open_set.put(start, Value(f=f,g=g)) # your code: implement A* # Initially you may want to ignore theta, that is, plan in 2D. # To do so, set actions=forward, cost = [1, 1, 1, 1], and action_name = ['U', 'L', 'R', 'D'] # Similarly, set parent=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))] return path, closed_set
def __init__(self, config): """ Implements full node that not only tracks other nodes, but also maintains full blockchain, accepts transactions and mines new blocks. """ super(NodeFull, self).__init__(config) self._hash_prev = None self._blockchain = set() # Producer-consumer queues for exchanging data with the mining thread self._transaction_queue = PriorityQueue(f_priority=favor_higher_fees) self._mining = config.mining # Launch mining and block and transaction discovery if self._mining: Thread(target=self.mine, name="mine").start() # Launch transaction generating and sharing if config.gen_transactions: Thread(target=self.generate_transactions).start() Thread(target=self.discover_blocks).start() Thread(target=self.share_blocks).start() Thread(target=self.discover_transactions).start() Thread(target=self.share_transactions).start()
def __init__(self, env, identifier, neighbourList, nodes, params): self.env = env self.identifier = identifier self.neighbourList = neighbourList self.params = params self.nodes = nodes self.transactionQueue = PriorityQueue() self.prevTransactions = []
def compute_path(grid,start,goal,cost,heuristic): # Use the OrderedSet for your closed list closed_set = OrderedSet() # Use thePriorityQueue for the open list open_set = PriorityQueue(order=min, f=lambda v: v.f) # Keep track of the parent of each node. Since the car can take 4 distinct orientations, # for each orientation we can store a 2D array indicating the grid cells. # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up parent = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]] # The path of the car path =[['-' for row in range(len(grid[0]))] for col in range(len(grid))] x = start[0] y = start[1] theta = start[2] h = heuristic[x][y] g = 0 f = g+h open_set.put(start, Value(f=f,g=g)) # your code: implement A* # Initially you may want to ignore theta, that is, plan in 2D. # To do so, set actions=forward, cost = [1, 1, 1, 1], and action_name = ['U', 'L', 'R', 'D'] # Similarly, set parent=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))] while open_set: node = open_set.pop() closed_set.add(node[0]) if node[0] == goal: break #finding child of node #write function for finding child node #setting the cost values for neighboring nodes neighbors = get_neighbors(node[0]) print(neighbors) for i in neighbors: x = i[0] y = i[1] theta = i[2] h = heuristic[x][y] g = node[1].g + costfunction(node[0],i) f = g+h if i not in open_set or i not in closed_set: open_set.put(i,Value(f,g)) elif i in open_set and f < open_set.get(i).f: open_set.put(i,Value(f,g)) return path, closed_set
def depth_limited_best_first_graph_search(problem, f, depth_limit): f = memoize(f, 'f') node = Node(problem.initial) total_nodes = 0 if problem.goal_test(node.state): return node, total_nodes frontier = PriorityQueue(min, f) frontier.append(node) explored = set() while frontier: node = frontier.pop() total_nodes += 1 if problem.goal_test(node.state): return node, total_nodes explored.add(node.state) if node.depth < depth_limit: for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if f(child) < f(incumbent): del frontier[incumbent] frontier.append(child) return None, total_nodes
def best_first_graph_search(problem, f): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" f = memoize(f, 'f') node = Node(problem.initial) if problem.goal_test(node.state): return node frontier = PriorityQueue(min, f) frontier.append(node) explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if f(child) < f(incumbent): del frontier[incumbent] frontier.append(child) return None
def best_first_graph_search(problem, f): f = memoize(f, 'f') node = Node(problem.initial) frontier = PriorityQueue('min', f) frontier.append(node) explored = list() while frontier: node = frontier.pop() print("Current Node:", node.state) if problem.goal_test(node.state): trace_path(node) return node explored.append(node.state) print("Explored Nodes:", explored) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) temp_front = list() for e in frontier.heap: val, node = e temp_front.append(node.state) print("Frontier Nodes:", temp_front) print("\n") return None
def a_star_search(problem, stats=False): h = memoize(problem.h_g, 'h') node = Node(problem.initial) nodes_generated = 1 explored = set() if problem.goal_test(node.state): if stats: return (node, explored, nodes_generated) return node frontier = PriorityQueue('min', h) frontier.append(node) while frontier: node = frontier.pop() if problem.goal_test(node.state): if stats: return (node, explored, nodes_generated) return node explored.add(node.state) for child in node.expand(problem): nodes_generated += 1 if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if h(child) < h(incumbent): del frontier[incumbent] frontier.append(child) return None
def best_first_graph_search(problem, f): '''MODIFICATION: a timeout check has been added''' """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" f = memoize(f, 'f') node = Node(problem.initial) frontier = PriorityQueue('min', f) frontier.append(node) explored = set() start = time.time() while frontier and (time.time() - start < TIMEOUT): node = frontier.pop() if problem.goal_test(node.state): return node explored.add(tuple(sorted(node.state.pieces))) for child in node.expand(problem): if tuple(sorted(child.state.pieces) ) not in explored and child not in frontier: frontier.append(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) return None
def best_first_graph_search(problem, f): """Пребарувај низ следбениците на даден проблем за да најдеш цел. Користи функција за евалуација за да се одлучи кој е сосед најмногу ветува и потоа да се истражи. Ако до дадена состојба стигнат два пата, употреби го најдобриот пат. :param problem: даден проблем :param f: дадена функција за евристика :return: Node or None """ f = memoize(f, 'f') node = Node(problem.initial) if problem.goal_test(node.state): return node frontier = PriorityQueue(min, f) frontier.append(node) explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: incumbent = frontier[child] if f(child) < f(incumbent): del frontier[incumbent] frontier.append(child) return None
def best_first_graph_search(problem, f, display=False): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" f = memoize(f, 'f') node = Node(problem.initial) frontier = PriorityQueue('min', f) frontier.append(node) explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): if display: print(len(explored), "paths have been expanded and", len(frontier), "paths remain in the frontier") print("Total path cost is:" + str(node.path_cost)) return node explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) return None
def greedy_best_first(board, heuristic): """ an implementation of the greedy best first search algorithm. it uses a heuristic function to find the quickest way to the destination :param board: (Board) the board you start at :param heuristic: (function) the heuristic function :return: (list) path to solution, (int) number of explored boards """ frontier = PriorityQueue() node = Node(board) frontier.add(node, heuristic(node.data)) explored = [] while frontier.has_next(): node = frontier.pop() if node.data.is_solved(): return node.path(), len(explored) + 1 for move in node.data.legal_moves(): child = Node(node.data.forecast(move), node) if (not frontier.has(child)) and (child.data not in explored): frontier.add(child, heuristic(child.data)) explored.append(node.data) return None, len(explored)
def compute_path(grid, start, goal, cost, heuristic): # Use the OrderedSet for your closed list closed_set = OrderedSet() # Use thePriorityQueue for the open list open_set = PriorityQueue(order=min, f=lambda v: v.f) parent = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))], [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]] # The path of the car path = [['-' for row in range(len(grid[0]))] for col in range(len(grid))] x = start[0] y = start[1] theta = start[2] g_value = 0 h_value = heuristic[x][y] f = g_value + h_value parent_cost = [[[1000000000 for row in range(len(grid[0]))] for col in range(len(grid))], [[1000000000 for row in range(len(grid[0]))] for col in range(len(grid))], [[1000000000 for row in range(len(grid[0]))] for col in range(len(grid))], [[1000000000 for row in range(len(grid[0]))] for col in range(len(grid))]] open_set.put(start, Value(f=f, g=g_value)) ########################### IMPLEMENTATION OF DIJKSTRA ALGORITHM ################################### #################################################################################################### #################################################################################################### def dijkstra(grid, source): length = len(grid) Q_value = [(0, source)] value0 = [inf for i in range(length)] value0[source] = 0 while len(Q) != 0: (cost, u) = hq.heappop(Q_value) for v in range(n): if value0[v] > value0[u] + grid[u][v]: value0[v] = value0[u] + grid[u][v] hq.heappush(Q, (value0[v], v)) return value0 print('The dijkstra value is:', value0) return path, closed_set
def getPrioritySucc(self, state): items = state.getSuccessors().items() if not self.use_extentions[0] or self.time_manager.time_left < 0.3: return items factor = state.getCurrentPlayer() == self.player and -1 or 1 pq = PriorityQueue(lambda x: factor * self.utility(x[1])) for item in items: pq.append(item) if self.time_manager.bTimeOver: return items return pq
def getPrioritySucc(self, state): items = state.getSuccessors().items() if not self.use_extentions[0] or self.time_manager.time_left < 0.3: return items factor = state.getCurrentPlayer() == self.player and -1 or 1 pq = PriorityQueue(lambda x : factor * self.utility(x[1])) for item in items: pq.append(item) if self.time_manager.bTimeOver: return items return pq
def _reset(self): self.env.reset() self.model = Model( self.env) # warning: this breaks if env resets again start = self.Node(self.env._state, [], 0, False) frontier = PriorityQueue(key=self.eval_node( noisy=True)) # this is really part of the Meta Policy frontier.push(start) reward_to_state = defaultdict(lambda: -np.inf) best_done = None # Warning: state is mutable (and we mutate it!) self._state = self.State(frontier, reward_to_state, best_done) return self._state
def best_first_graph_search(problem, f): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" global frontier, node, explored, counter if counter == -1: f = memoize(f, 'f') node = Node(problem.initial) display_current(node) if problem.goal_test(node.state): return node frontier = PriorityQueue('min', f) frontier.append(node) display_frontier(frontier) explored = set() add_node(node) draw_tree() if counter % 3 == 0 and counter >= 0: node = frontier.pop() display_current(node) if problem.goal_test(node.state): return node explored.add(node.state) mark_exploring(node) draw_tree() if counter % 3 == 1 and counter >= 0: for child in node.expand(problem): if child.state not in explored and child not in frontier: frontier.append(child) add_node(child) elif child in frontier: if f(child) < frontier[child]: del frontier[child] frontier.append(child) remove_node(child) display_frontier(frontier) draw_tree() if counter % 3 == 2 and counter >= 0: display_explored(node) mark_explored(node) draw_tree() return None
def all_pairs_dijkstra(self, biGraph, weight='weight'): for node in biGraph.nodes(): g = biGraph.copy() attributes = nx.get_edge_attributes(g, 'rname') dist = {} prev = {} last_attribute = {} Q = PriorityQueue() dist[node] = 0 prev[node] = [node] last_attribute[node] = None for n in g.nodes(): if n != node: dist[n] = float('Inf') prev[n] = [] Q.insert(dist[n], n) while Q.size() > 0: p, u = Q.pop() for v in g.neighbors(u): p_attribute = last_attribute[u] attribute = attributes[(u, v)] num = 100 if p_attribute == 'is-a' and attribute == 'is-a2' else 0 alt = dist[u] + g[u][v].get('weight', 1) + num if alt < dist[v]: dist[v] = alt prev[v] = prev[u] + [v] last_attribute[v] = attribute Q.insert(dist[v], v) yield (node, (dist, prev))
def breadth_first(self, xy1, xy2): """Execute a breadth first search""" tile_col1, tile_row1 = self.the_map.xy_to_cr(xy1[0], xy1[1]) tile_col2, tile_row2 = self.the_map.xy_to_cr(xy2[0], xy2[1]) successor_to_parent_map = {} start_state = (tile_col1, tile_row1) successor_to_parent_map[( start_state, None)] = None # (Successor, Action) -> (Parent, Action) open_list = PriorityQueue() open_list.update((start_state, None), 0) closed = [] while not open_list.isEmpty(): current_state, action_to_current_state = open_list.pop() if current_state == (tile_col2, tile_row2): return self.__get_action_path( (current_state, action_to_current_state), successor_to_parent_map) if current_state not in closed: for successor_state, action, step_cost in self.__get_successors( current_state): open_list.update((successor_state, action), 0) if successor_state not in closed: successor_to_parent_map[(successor_state, action)] = ( current_state, action_to_current_state) closed.append(current_state) return []
def uc_search(self, initial_state): """ Uniform-Cost Search. It returns the path as a list of directions among { Direction.left, Direction.right, Direction.up, Direction.down } """ # use a priority queue with the minimum queue. from utils import PriorityQueue open_list = PriorityQueue() open_list.push([(initial_state, None)], 0) closed_list = set([initial_state]) # keep already explored positions while not open_list.isEmpty(): # Get the path at the top of the queue current_path, cost = open_list.pop() # Get the last place of that path current_state, current_direction = current_path[-1] #print("current_state -> ", current_state._position, " direction -> ", current_state._direction, " cost -> ", cost) # Check if we have reached the goal if current_state.is_goal_state(): return (list(map(lambda x: x[1], current_path[1:]))) else: # Check were we can go from here next_steps = current_state.get_successor_states() # Add the new paths (one step longer) to the queue for state, direction, weight in next_steps: # Avoid loop! if state not in closed_list: closed_list.add(state) open_list.push((current_path + [(state, direction)]), cost + weight) return []
def travel(dist_mat, startcity=0): optimal_tour = [] u = Node() pq = PriorityQueue() opt_len = 0 v = Node(level=0, path=[0]) min_len = sys.maxsize v.bound = bound(dist_mat, v) pq.put(v) while not pq.empty(): v = pq.get() if v.bound < min_len: u.level = v.level + 1 for i in filter(lambda x: x not in v.path, range(1, num_cities)): u.path = v.path[:] u.path.append(i) if u.level == num_cities - 2: l = set(range(1, num_cities)) - set(u.path) u.path.append(list(l)[0]) u.path.append(0) _len = length(dist_mat, u) if _len < min_len: min_len = _len opt_len = _len optimal_tour = u.path[:] else: u.bound = bound(dist_mat, u) if u.bound < min_len: pq.put(u) # make a new node at each iteration! u = Node(level=u.level) return optimal_tour, opt_len
def aStarSearch(pos, goal): fringe, visited, best = PriorityQueue(), set(), {} fringe.push((pos, [], 0), manhattanDistance(pos, goal)) while not fringe.isEmpty(): current_point, actions, total_cost = fringe.pop() if current_point in visited or \ (current_point in best and best[current_point] <= total_cost): continue visited.add(current_point) best[current_point] = total_cost # current vertex is a solution if current_point == goal: return actions for (point, action) in getSuccessors(current_point): # if node not visited add it to the fringe if point not in visited: actions_copy = list(actions) actions_copy.append(action) cost = total_cost + 1 fringe.push((point, actions_copy, cost), \ cost + manhattanDistance(point, goal)) raise Exception('Problem Not Solved')
def __init__(self): self.modules = {} self.module_type_map = defaultdict(deque) self.device_state = DeviceState() self.event_queue = PriorityQueue() self.alarms = [] self.modules = {} self.event_listeners = defaultdict(deque) self.trace_reader = None self.verbose = False self.debug_mode = False self.debug_interval = 1 self.debug_interval_cnt = 0
def search(self, vobj): frontier = PriorityQueue() frontier.put(self.start, 0) came_from = {} cost_so_far = {} came_from[self.start] = None cost_so_far[self.start] = 0 while not frontier.empty(): current = frontier.get() if current == self.goal: break for next in self.graph.neighbors(current): new_cost = cost_so_far[current] + self.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 + self.heuristic(self.goal, next) frontier.put(next, priority) came_from[next] = current # Reconstruct path path = [current] while current != self.start: current = came_from[current] path.append(current) nppath = np.asarray(path[::-4]) * self.graph.gridsize return np.copy(nppath[:])
def optimum_policy2D(grid, init, goal, cost): Nr = len(grid) Nc = len(grid[0]) inf = 999 policy2D = [[' ' for j in range(Nc)] for i in range(Nr)] value2D = [[inf for j in range(Nc)] for i in range(Nr)] value3D = [[[inf for j in range(Nc)] for i in range(Nr)] for o in range(4)] policy3D = [[[' ' for j in range(Nc)] for i in range(Nr)] for o in range(4)] visited = [] frontier = PriorityQueue() cumcost = 0 frontier.push([init, ' ', cumcost], cumcost + heuristic_fun(init[0:2], goal)) while not frontier.isEmpty(): loc, move_name, cumcost = frontier.pop() if not loc in visited: visited.append(loc) value3D[loc[2]][loc[0]][loc[1]] = cumcost policy3D[loc[2]][loc[0]][loc[1]] = move_name if loc[0:2] == goal: # print 'Value:' # for row in value: # print '---' # for i in row: # print i # print 'Policy:' # for row in policy3D: # print '---' # for i in row: # print i # return policy2D policy2D[goal[0]][goal[1]] = '*' value2D[goal[0]][goal[1]] = value3D[loc[2]][goal[0]][goal[1]] while loc[0:2] != init[0:2]: loc, loc_move = reverse_move( loc, policy3D[loc[2]][loc[0]][loc[1]]) policy2D[loc[0]][loc[1]] = loc_move value2D[loc[0]][loc[1]] = value3D[loc[2]][loc[0]][loc[1]] print('Value') for i in value2D: print(i) return policy2D for nextloc, move_name, move_cost in childNode( grid, loc, forward, forward_name): if not nextloc in visited: nextcumcost = cumcost + move_cost frontier.push([nextloc, move_name, nextcumcost], nextcumcost + heuristic_fun(nextloc[0:2], goal)) return 'fail'
def get_all_actions(self, scratch_game): healthy = [] sick = [] for z in scratch_game.control_zone_1: if 'H' in scratch_game.state[z]: healthy.append(('vaccinate', z), ) if 'S' in scratch_game.state[z]: sick.append(('quarantine', z), ) to_quarantine = [] for q in range(1, police + 1): to_quarantine = to_quarantine + list( combinations(sick, min(q, len(sick)))) to_vaccinate = list(combinations(healthy, min(medics, len(healthy)))) a = list(product(to_quarantine, to_vaccinate)) + to_vaccinate actions = PriorityQueue(max, lambda x: x[-1]) for a1 in a: if len(a1) == 2: # account for cases where there are quarantines aa = a1[0] + a1[1] else: aa = a1 # only vaccinations fom = 0 for atomic_action in aa: z1 = (atomic_action[1][0] - 1, atomic_action[1][1]) in scratch_game.control_zone_1 z2 = (atomic_action[1][0] + 1, atomic_action[1][1]) in scratch_game.control_zone_1 z3 = (atomic_action[1][0], atomic_action[1][1] - 1) in scratch_game.control_zone_1 z4 = (atomic_action[1][0], atomic_action[1][1] + 1) in scratch_game.control_zone_1 x1 = scratch_game.state[(atomic_action[1][0] - 1, atomic_action[1][1])] x2 = scratch_game.state[(atomic_action[1][0] + 1, atomic_action[1][1])] x3 = scratch_game.state[(atomic_action[1][0], atomic_action[1][1] - 1)] x4 = scratch_game.state[(atomic_action[1][0], atomic_action[1][1] + 1)] if atomic_action[0] == 'vaccinate': fom = fom + (x1[0] == 'S' or x2[0] == 'S' or x3[0] == 'S' or x4[0] == 'S') else: fom = fom + (x1[0] == 'H') * (z1 * 2 - 1) + ( x2[0] == 'H') * (z2 * 2 - 1) + (x3[0] == 'H') * ( z3 * 2 - 1) + (x4[0] == 'H') * (z4 * 2 - 1) fom = fom - 4 # penalty for using police: 4 aa = aa + (fom, ) actions.append(aa) return actions
def __init__(self, qid, op_type, default_limit=ALL): self.qid = qid options, unwrapped = get_unwrapped_options(op_type) self.op_type = op_type self.unwrapped_type = unwrapped self.options = options self.unique_key = options.get('unique_key', 'unique_key') self.unique_func = get_unique_func(self.unique_key) self.priority = options.get('priority', 0) self.priority_func = get_priority_func(self.priority) self.default_limit = default_limit self.param_set = set() self.op_queue = PriorityQueue() self._dup_params = []
class OperationQueue(object): # TODO: chunking/batching should probably happen here # with the assistance of another queue for prioritized params # (i.e., don't create subops so eagerly) def __init__(self, qid, op_type, default_limit=ALL): self.qid = qid options, unwrapped = get_unwrapped_options(op_type) self.op_type = op_type self.unwrapped_type = unwrapped self.options = options self.unique_key = options.get('unique_key', 'unique_key') self.unique_func = get_unique_func(self.unique_key) self.priority = options.get('priority', 0) self.priority_func = get_priority_func(self.priority) self.default_limit = default_limit self.param_set = set() self.op_queue = PriorityQueue() self._dup_params = [] def enqueue(self, param, **kw): unique_key = self.unique_func(param) if unique_key in self.param_set: self._dup_params.append(unique_key) return priority = self.priority_func(param) kwargs = {'limit': self.default_limit} kwargs.update(kw) new_subop = self.op_type(param, **kwargs) new_subop._origin_queue = self.qid self.op_queue.add(new_subop, priority) self.param_set.add(unique_key) def enqueue_many(self, param_list, **kw): for param in param_list: self.enqueue(param, **kw) return def __len__(self): return len(self.op_queue) def peek(self, *a, **kw): return self.op_queue.peek(*a, **kw) def pop(self, *a, **kw): return self.op_queue.pop(*a, **kw)
class TransactionPool: """Transaction pool for a miner""" def __init__(self, env, identifier, neighbourList, nodes, params): self.env = env self.identifier = identifier self.neighbourList = neighbourList self.params = params self.nodes = nodes self.transactionQueue = PriorityQueue() self.prevTransactions = [] def getTransaction(self, transactionCount): """Returns transactionCount number of Transactions. Returns top transactions based on miner reward""" return self.transactionQueue.get(transactionCount) def popTransaction(self, transactionCount): """Remove transactions from transaction pool. Called when transactions are added by a received block or a block is mined.""" poppedTransactions = self.transactionQueue.pop(transactionCount) self.prevTransactions.append(poppedTransactions) def putTransaction(self, transaction, sourceLocation): """Add received transaction to the transaction pool and broadcast further""" destLocation = self.nodes[self.identifier].location delay = getTransmissionDelay(sourceLocation, destLocation) yield self.env.timeout(delay) if ( not self.transactionQueue.isPresent(transaction) and transaction not in self.prevTransactions ): self.transactionQueue.insert(transaction) broadcast( self.env, transaction, "Transaction", self.identifier, self.neighbourList, self.params, nodes=self.nodes, ) if self.params["verbose"] == "vv": print( "%7.4f : %s accepted by %s" % (self.env.now, transaction.identifier, self.identifier) )
def __init__(self): self._sim_modules = {} self._module_type_map = defaultdict(deque) self._device_state = DeviceState() self._event_queue = PriorityQueue() self._current_time = None self._warmup_period = None self._event_listeners = defaultdict(deque) self._trace_reader = None self._trace_executed = False self._verbose = False self._debug_mode = False self._debug_interval = 1 self._debug_interval_cnt = 0
def search(self, vobj): frontier = PriorityQueue() frontier.put(self.start, 0) came_from = {} cost_so_far = {} came_from[self.start] = None cost_so_far[self.start] = 0 while not frontier.empty(): current = frontier.get() if current == self.goal: break for next in self.graph.neighbors(current): new_cost = cost_so_far[current] + self.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 + self.heuristic(self.goal, next) frontier.put(next, priority) came_from[next] = current # Reconstruct path path = [current] while current != self.start: current = came_from[current] path.append(current) nppath = np.asarray(path[::-4]) * self.graph.gridsize return np.copy(nppath[:])
def best_first_search_tree(problem, f): """Search the nodes with the lowest f scores first. You specify the function f(node) that you want to minimize; for example, if f is a heuristic estimate to the goal, then we have greedy best first search; if f is node.depth then we have breadth-first search. There is a subtlety: the line "f = memoize(f, 'f')" means that the f values will be cached on the nodes as they are computed. So after doing a best first search you can examine the f values of the path returned.""" # print("he sido llamado") f = memoize(f, 'f') node = Node(problem.initial) frontier = PriorityQueue('min', f) frontier.append(node) # frontier.mostrar() # explored = set() while frontier: node = frontier.pop() if problem.goal_test(node.state): return node # explored.add(node.state) for child in node.expand(problem): frontier.append(child) '''if child.state not in explored and child not in frontier: frontier.append(child) elif child in frontier: if f(child) < frontier[child]: # mira si ya hay una forma de llegar q es mayor a la que encontre ahora? del frontier[child] frontier.append(child)''' return None
def __init__(self, link, allow_external, allow_redirects, max_limit = 10): self.root = link self.unparsed_urls = PriorityQueue() self.allow_external = allow_external self.allow_redirects = allow_redirects self.domain = None self.max_limit = max_limit self.opener = None self.create_opener()
def astarSearch(problem, heuristic = 'eqn'): print bcolors.HEADER + "\n>> Running A* Search" + bcolors.ENDC startState = problem.getStartState() goalState = problem.getGoalState() [T, r, Z] = problem.getTransform() listOfPredicates = problem.listOfPredicates goalCompliantConditions = problem.goalCompliantConditions fval = float(heuristic(startState, problem)) print bcolors.OKGREEN + "--> Initial heuristic estimate = " + bcolors.OKBLUE + str(fval) + bcolors.ENDC fringe = PriorityQueue() closed = [] numberOfStatesExpanded = 0 printloop = 0 fringe.push([startState, [], 0.0], fval) while not fringe.isEmpty(): printloop += 1 if printloop == 300: printloop = 0 print bcolors.OKGREEN + "--> Number of states expanded > " + str(numberOfStatesExpanded) + bcolors.ENDC node = fringe.pop() if problem.isGoalState(node[0]): print bcolors.OKGREEN + "--> Goal Found. Final number of states expanded = " + bcolors.OKBLUE + str(numberOfStatesExpanded) + bcolors.ENDC return node numberOfStatesExpanded += 1 successor_list = [] if node[0] not in closed: closed.append(node[0]) successor_list = problem.getSuccessors(node) while len(successor_list) > 0: put = successor_list.pop() if put[0] not in closed: hval = heuristic(put[0], problem) if hval != -1: newnode = copy.deepcopy(node) newnode[0] = put[0] newnode[1] = newnode[1] + [put[1]] newnode[2] = put[2] + hval fringe.push(newnode,newnode[2]) print bcolors.OKGREEN + "--> Search Terminated. Final number of states expanded = " + bcolors.OKBLUE + str(numberOfStatesExpanded) + bcolors.ENDC return None
def aStarSearch(self, heuristicName = 'equality'): method = getattr(self, 'heuristic_' + heuristicName) print bcolors.HEADER + "\n>> Running A* Search" + bcolors.ENDC startState = self.getStartState() goalState = self.getGoalState() [T, r, Z] = self.getTransform() listOfPredicates = self.listOfPredicates goalCompliantConditions = self.goalCompliantConditions fval = float(method(startState)) print bcolors.OKGREEN + "--> Initial heuristic estimate = " + bcolors.OKBLUE + str(fval) + bcolors.ENDC fringe = PriorityQueue() closed = [] numberOfStatesExpanded = 0 printloop = 0 fringe.push([startState, [], 0.0], fval) while not fringe.isEmpty(): if numberOfStatesExpanded%100 == 0: print bcolors.OKGREEN + "--> Number of states expanded > " + str(numberOfStatesExpanded) + bcolors.ENDC node = fringe.pop() if self.isGoalState(node[0]): print bcolors.OKGREEN + "--> Goal Found. Final number of states expanded = " + bcolors.OKBLUE + str(numberOfStatesExpanded) + bcolors.ENDC return [node[1], node[2]] numberOfStatesExpanded += 1 successor_list = [] if node[0] not in closed: closed.append(node[0]) successor_list = self.getSuccessors(node) while len(successor_list) > 0: put = successor_list.pop() if put[0] not in closed: hval = float(method(put[0])) if hval != -1: newnode = [put[0], node[1] + [put[1]], put[2] + hval] fringe.push(newnode, newnode[2]) print bcolors.OKGREEN + "--> Search Terminated. Final number of states expanded = " + bcolors.OKBLUE + str(numberOfStatesExpanded) + bcolors.ENDC return None
def uniformCostSearch(problem): "** Search the node of least total cost first. **" fringe = PriorityQueue() closed = {} # Bookeeping for visisted nodes fringe.push(Node(problem.initial)) while fringe: node = fringe.pop() # Choose a node to expand if problem.goal_test(node.state): # Check goal state return node if node.state not in closed: closed[node.state] = True # Add state node to visited tree fringe.extend(node.expand(problem)) # Expanding node return None
def iterate_chrono(self): unvisited_nodes = PriorityQueue() already_seen = set() for initial_commit in self.sentinel.out_neighbours(): unvisited_nodes.push(initial_commit, self.commit_timestamp[initial_commit]) already_seen.add(initial_commit) while True: # iterate over commits in order of commit_timestamps try: commit_node = unvisited_nodes.pop() except IndexError: raise StopIteration yield commit_node children = commit_node.out_neighbours() new_nodes = [child for child in children if child not in already_seen] for node in new_nodes: unvisited_nodes.push(node, self.commit_timestamp[node]) already_seen |= set(new_nodes)
class Balerion(object): """ Once the largest dragon in westeros, also the name of this simple python web crawler :-) . """ def __init__(self, link, allow_external, allow_redirects, max_limit = 10): self.root = link self.unparsed_urls = PriorityQueue() self.allow_external = allow_external self.allow_redirects = allow_redirects self.domain = None self.max_limit = max_limit self.opener = None self.create_opener() def pre_process(self): """ exit the function if seed url is not a valid url """ if(self.allowed_for_processing(self.root)): self.unparsed_urls.add(self.root, priority = 0) parsed_url = urlparse.urlparse(self.root) self.domain = parsed_url.netloc else: LOGGER.warning("Non followable root: %s " % self.root) exit() def process_page(self, response) : """ override this method to do any kind of processing on the page. """ pass def create_opener(self): """ creates http-link opener based on options choosen """ self.opener = urllib2.build_opener() if not self.allow_redirects: self.opener = urllib2.build_opener(BalerionRedirectHandler) @classmethod def allowed_for_processing(cls, next_url): """ placeholder """ parsed_url = urlparse.urlparse(next_url) if(parsed_url.scheme != 'http'): LOGGER.warning("Non followable URl: %s " % next_url) return False ROBOT.set_url(parsed_url.scheme + parsed_url.netloc + "/robots.txt") if not ROBOT.can_fetch('Balerion', next_url.encode('ascii', 'replace')): LOGGER.warning("Url disallowed by robots.txt: %s " % next_url) return False return True def process_page_links(self, raw_html, url): """ simply extracts html links using awesome beautifulsoup """ beautiful_html = BeautifulSoup(raw_html) links = [a.get('href') for a in beautiful_html.find_all('a')] links = [link for link in links if link is not None] for link in links: link_info = urlparse.urlparse(link) if not link_info.scheme and not link_info.netloc: link = urlparse.urljoin(url, link) link_info = urlparse.urlparse(link) if('http' not in link_info.scheme) : continue if self.domain not in link_info.netloc: if not self.allow_external : continue # throwing out external link else: priority = 2 # insert external link with low priority else: priority = 1 self.unparsed_urls.add(link, priority) def fetch_url(self, url): """ fetches url and returns an object represenation which store headers and status etc. """ page = AttrDict() try: # getting response from given URL resp = self.opener.open(unicode(url)) page = AttrDict({ 'body': resp.read(), 'url': resp.geturl(), 'headers': AttrDict(dict(resp.headers.items())), 'status': resp.getcode() }) except urllib2.HTTPError, err : if err.code == 404: page = AttrDict({'status': 404}) LOGGER.exception("page not found : %s at %s" % (err.code, url)) elif err.code == 403: page = AttrDict({'status': 403}) LOGGER.error("access denied : %s at %s " % (err.code, url)) else: page = AttrDict({'status': 500}) #choosing 500 as default bad access code LOGGER.error("something bad happened : %s at %s " % (err.code, url)) except urllib2.URLError, err: page = AttrDict({'status': 500}) LOGGER.error("server error %s at %s " % (err.reason, url))
def search(self, vobj): """The Hybrid State A* search algorithm.""" tic = time.clock() get_grid_id = self.graph.get_grid_id frontier = PriorityQueue() frontier.put(list(self.start), 0) came_from = {} cost_so_far = {} came_from[tuple(self.start)] = None cost_so_far[get_grid_id(self.start)] = 0 dubins_path = False num_nodes = 0 while not frontier.empty(): current = frontier.get() if num_nodes % self.dubins_expansion_constant == 0: dpath,_ = dubins.path_sample(current, self.goal, self.turning_radius, self.step_size) if not self.map.is_occupied_discrete(dpath): # Success. Dubins expansion possible. self.path_found = True dubins_path = True break if np.linalg.norm(current[0:2] - self.goal[0:2]) < self.eps \ and np.abs(current[2]-self.goal[2]) < np.pi/8: self.path_found = True break for next in self.graph.neighbors(current, vobj.model.est_r_max): new_cost = cost_so_far[get_grid_id(current)] + \ self.graph.cost(current, next) if get_grid_id(next) not in cost_so_far or new_cost < cost_so_far[get_grid_id(next)]: cost_so_far[get_grid_id(next)] = new_cost priority = new_cost + heuristic(self.goal, next) frontier.put(list(next), priority) came_from[tuple(next)] = current num_nodes += 1 # Reconstruct path path = [current] while tuple(current) != tuple(self.start): current = came_from[tuple(current)] path.append(current) if dubins_path: path = np.array(path[::-1] + dpath) else: path = np.array(path[::-2]) print "Hybrid A-star CPU time: %.3f. Nodes expanded: %d" % ( time.clock() - tic, num_nodes) #print self.start return np.copy(path)
def search(self, start, goal): """ Perform the search. If given a new graph, replace the current one with it, and start fresh. Also, if specified, start fresh with the current graph. This is non-recursive and can therefore run infinitely until memory is exhausted. :param start: The node to start from :param goal: The node to go to :return: """ # start with a Priority Queue to hold the nodes to visit. # A priority queue means we'll visit the most promising nodes first # because they'll get higher priority to_visit = PriorityQueue() # But for now, we need to start by only knowing where we're at. It has lowest # priority because when you're trying to catch someone, (in this case) it's # best to keep moving to_visit.put(start, 0) # Yay hash tables! This will store the path we took while visiting nodes. Sorta kinda # like a linked list, but not (almost) visited = dict() # Woo hash tables! Here we'll store hashes of the nodes (tuples) we visit # and their costs costs = dict() # We've visited where we're starting from. It's nice and all, but the scenery is # kinda bland. Oh and we didn't arrive here from anywhere, so the previous node # is nothing. visited[start] = None # It didn't cost us anything to get here, which is nice since I'm pretty broke costs[start] = 0 # While we have things in the queue to visit, check out the neighbors # and see if they're worth visiting. I mean, I'm sure they're lovely people # and all, but if they can't get us to our destination, then we don't need to # waste our time. It's the capitalist way! Also, they're weird and have electric # lawnmowers. while not to_visit.is_empty: # Travel to the next best node current = to_visit.get() # Hey! We got to where we needed to go! if current == goal: break # Here's where we figure out if we're gonna be neighborly and visit nodes # surrounding us. Reasons for not visiting our neighbors: # 1) They're a wall. It'd look weird to visit a wall # 2) It'd be more costly to visit them than others. # 3) They support Trump # 4) They use electric lawnmowers # 5) They're clingy # 6) They always ask for a cup of sugar but when you try to reciprocate, # they're always suddenly out # 7) They listen to Insane Clown Posse for next in self.board.nodes_to_visit(current): new_cost = costs[current] + self.board.cost(next) # See item two in the above list if next not in costs or new_cost < costs.get(next, Infinity): # Oh hey, it's cheaper to visit them than not. Cool! # Record the cost of visiting the neighbor costs[next] = new_cost # Figure out where they stand on your priorities list # and put them in the Queue. Hermes would be proud to_visit.put(next, new_cost + AStar.heuristic(goal, next)) # Mark down how you got to them visited[next] = current # Okay, we've figured out a path. It's... somewhere in here. Hrm... return visited, costs
class Simulator(SimulatorBase): EVENT_QUEUE_THRESHOLD = 100 def __init__(self): self._sim_modules = {} self._module_type_map = defaultdict(deque) self._device_state = DeviceState() self._event_queue = PriorityQueue() self._current_time = None self._warmup_period = None self._event_listeners = defaultdict(deque) self._trace_reader = None self._trace_executed = False self._verbose = False self._debug_mode = False self._debug_interval = 1 self._debug_interval_cnt = 0 def has_module_instance(self, name): return name in self._sim_modules def get_module_instance(self, name): return self._sim_modules[name] def get_module_for_type(self, module_type): if module_type in self._module_type_map: return self._module_type_map[module_type.value][0] else: return None def register(self, sim_module, override=False): if not isinstance(sim_module, SimModule): raise TypeError("Expected SimModule object") if sim_module.get_name() in self._sim_modules: raise Exception("Module %s already exists" % sim_module.get_name()) self._sim_modules[sim_module.get_name()] = sim_module if override: self._module_type_map[sim_module.get_type().value].appendleft(sim_module) else: self._module_type_map[sim_module.get_type().value].append(sim_module) def build(self, args): self._verbose = args.verbose self._debug_mode = args.debug # Instantiate necessary modules based on config files config = configparser.ConfigParser() config.read(args.sim_config) config['DEFAULT'] = {'modules': '', 'warmup_period': ''} if 'Simulator' not in config: raise Exception("Simulator section missing from config file") sim_settings = config['Simulator'] # Identify the set of modules to include modules_str = sim_settings['modules'] if modules_str: modules_list = modules_str.split(' ') else: modules_list = [] self._warmup_period = \ self.__parse_warmup_setting(sim_settings['warmup_period']) # Setup the trace file reader and initial simulator time self._trace_reader = get_trace_reader(args.trace) self._trace_reader.build() self._trace_executed = False self._current_time = self._trace_reader.get_start_time() for module_name in modules_list: module_settings = {} if module_name in config: module_settings = config[module_name] self.register(get_simulator_module(module_name, self, module_settings)) # Build list of modules for sim_module in self._sim_modules.values(): sim_module.build() def run(self): # Check if we need to enter debug mode immediately if self._debug_mode: self._debug_interval_cnt = 0 self.__debug() # Add alarm event for the warmup period warmup_finish_alarm = SimAlarm( timestamp=self._trace_reader.get_start_time() + self._warmup_period, handler=self.__enable_stats_collection, name='Warmup Period Alarm') self._event_queue.push(warmup_finish_alarm, (warmup_finish_alarm.timestamp, Priority.SIMULATOR)) while not self._trace_reader.end_of_trace() \ or not self._event_queue.empty(): # Populate event queue if it is below the threshold number of events if self._event_queue.size() < Simulator.EVENT_QUEUE_THRESHOLD \ and not self._trace_reader.end_of_trace(): self.__populate_event_queue_from_trace() continue # Look at next event to execute cur_event = self._event_queue.peek() # Look at next event from trace file trace_event = self._trace_reader.peek_event() # If trace event is supposed to occur before current # event, than we should populate event queue with more # events from the trace file if trace_event and cur_event.timestamp > trace_event.timestamp: self.__populate_event_queue_from_trace() continue self._event_queue.pop() # Set current time of simulator self._current_time = cur_event.timestamp if self._verbose: print(cur_event) if self._debug_mode: self._debug_interval_cnt += 1 if self._debug_interval_cnt == self._debug_interval: self.__debug() self._debug_interval_cnt = 0 self.__execute_event(cur_event) self.__finish() def subscribe(self, event_type, handler, event_filter=None): if event_type not in self._event_listeners: self._event_listeners[event_type] = [] self._event_listeners[event_type].append((event_filter, handler)) def broadcast(self, event): if event.timestamp: if event.timestamp != self._current_time: raise Exception("Broadcasting event with invalid timestamp.") else: event.timestamp = self._current_time # Get the set of listeners for the given event type listeners = self._event_listeners[event.event_type] for (event_filter, handler) in listeners: # Send event to each subscribed listener if not event_filter or event_filter(event): handler(event) def register_alarm(self, alarm): self._event_queue.push(alarm, (alarm.timestamp, Priority.ALARM)) def get_current_time(self): return self._current_time def get_device_state(self): return self._device_state def __parse_warmup_setting(self, setting_value): if setting_value: if setting_value.endswith('h'): num_hours = int(setting_value[:-1]) return datetime.timedelta(hours=num_hours) raise Exception("Invalid warmup period setting format") else: return datetime.timedelta() def __enable_stats_collection(self): for sim_module in self._sim_modules.values(): sim_module.enable_stats_collection() def __disable_stats_collection(self): for sim_module in self._sim_modules.values(): sim_module.disable_stats_collection() """ Private method that handles execution of an event object. """ def __execute_event(self, event): if event.event_type == EventType.SIM_DEBUG: self.__debug() elif event.event_type == EventType.SIM_ALARM: if not self._trace_executed: event.fire() if event.is_repeating(): self._event_queue.push(event, (event.timestamp, Priority.ALARM)) elif event.event_type == EventType.TRACE_END: self._trace_executed = True else: self.broadcast(event) def __populate_event_queue_from_trace(self): # Fill in event queue from trace events = self._trace_reader.get_events(count=Simulator.EVENT_QUEUE_THRESHOLD) for x in events: self._event_queue.push(x, (x.timestamp, Priority.TRACE)) def __finish(self): output_file = sys.stdout # Print status from all modules for sim_module in self._sim_modules.values(): header = "======== %s Stats ========\n" % sim_module.get_name() footer = "=" * (len(header) - 1) + '\n' output_file.write(header) sim_module.print_stats(output_file) output_file.write(footer) # Call finish for all modules for sim_module in self._sim_modules.values(): sim_module.finish() def __debug(self): while True: command = input("(uamp-sim debug) $ ") if command: tokens = command.split(sep=' ') cmd = tokens[0] args = tokens[1:] if cmd == 'quit' or cmd == 'exit' or cmd == 'q': # TODO(dmanatunga): Handle simulation quitting better print("Terminating Simulation") exit(1) elif cmd == 'interval': if len(args) == 1: try: self._debug_interval = int(args[0]) except ValueError: print("Command Usage Error: interval command expects one numerical value") else: print("Command Usage Error: interval command expects one numerical value") elif cmd == 'verbose': if len(args) == 0: self._verbose = True elif len(args) == 1: if args[0] == 'on': self._verbose = True elif args[0] == 'off': self._verbose = False else: print("Command Usage Error: verbose command expects 'on' or 'off' for argument") else: print("Command Usage Error: verbose command expects at most one argument") else: break