def aStar(self): ''' START FUNCTION POINTERS ''' hashed = self.model.hashedarray get_score = self.get_cost set_score = self.set_cost heuri = self.heuristic get_total = self.get_F network = self.find_greedy_network is_goal = self.is_goal best_score = lambda x, p: -self.model.bic_score( x, self.find_best_parents(x, p)) write = self.out.write ''' END FUNCTION POINTERS ''' init_time = clock() self.init_astar() visited = {} ancestor = {} q = pqueue() start = [] set_score(start, 0) ancestor[hashed(start)] = (None, None) q.put_nowait((get_total(start), start)) expanded = 0 generated = 0 while not q.empty(): F, U = q.get_nowait() #print U , F if is_goal(U): cpu_time = clock() - init_time U = self.get_path(ancestor) write("ORDER = %s\n" % ','.join(U)) write("SCORE = %s\n" % get_score(U)) write("TIME = %s\n" % cpu_time) write("EXPANDED = %s\n" % expanded) write("GENERATED = %s\n" % generated) return network(U) if compare(get_total(U), F) < 0: continue expanded += 1 visited[hashed(U)] = True for X in self.data.fields: if X in U: continue new_U = copy(U) new_U.append(X) hu = hashed(new_U) g = get_score(U) + best_score(X, U) if hu in visited: continue if hu not in self.g or compare(g, get_score(new_U)) < 0: generated += 1 q.put_nowait((g + heuri(new_U), new_U)) set_score(new_U, g) ancestor[hu] = (hashed(U), X) return None
def aStar( self ) : ''' START FUNCTION POINTERS ''' hashed = self.model.hashedarray get_score = self.get_cost set_score = self.set_cost heuri = self.heuristic get_total = self.get_F network = self.find_greedy_network is_goal = self.is_goal best_score = lambda x , p : -self.model.bic_score( x , self.find_best_parents( x , p ) ) write = self.out.write ''' END FUNCTION POINTERS ''' init_time = clock() self.init_astar() visited = {} ancestor = {} q = pqueue() start = [] set_score( start , 0 ) ancestor[ hashed( start ) ] = ( None , None ) q.put_nowait( ( get_total( start ) , start ) ) expanded = 0 generated = 0 while not q.empty() : F , U = q.get_nowait() #print U , F if is_goal( U ) : cpu_time = clock() - init_time U = self.get_path( ancestor ) write( "ORDER = %s\n" % ','.join( U ) ) write( "SCORE = %s\n" % get_score( U ) ) write( "TIME = %s\n" % cpu_time ) write( "EXPANDED = %s\n" % expanded ) write( "GENERATED = %s\n" % generated ) return network( U ) if compare( get_total( U ) , F ) < 0 : continue expanded += 1 visited[ hashed( U ) ] = True for X in self.data.fields : if X in U : continue new_U = copy( U ) new_U.append( X ) hu = hashed( new_U ) g = get_score( U ) + best_score( X , U ) if hu in visited : continue if hu not in self.g or compare( g , get_score( new_U ) ) < 0 : generated += 1 q.put_nowait( ( g + heuri( new_U ) , new_U ) ) set_score( new_U , g ) ancestor[ hu ] = ( hashed( U ) , X ) return None
def astar(m, start, end, debug): filter_mat = sum_filter(ROBO_DIAMETER, ROBO_DIAMETER) #valid_map = scipy.ndimage.filters.gaussian_filter(valid_pixels(m, VALID_THRESHOLD), 5.0) valid_map = scipy.signal.convolve2d(valid_pixels(m, VALID_THRESHOLD), filter_mat, 'same') if debug: imgplot = plt.imshow(valid_map) plt.show() q = pqueue() #put into the queue a weight of the distance plus the heuristics #and a tuple of the current distance, and the next node to expand if not is_valid(start, valid_map): print "Finding new start" start = nearest_good_cell(valid_map, start) assert(start != None) print "Found new start" q.put((0, (0, start))) height = len(m) width = len(m[0]) closed = set() closed.add(start) parents = {} path_found = False while not q.empty(): (weight, (dist, cell)) = q.get() if cell == end: path_found = True break for neighbor in valid_neighbors(cell, width, height): if is_valid(neighbor, valid_map) and (not neighbor in closed): weight = dist + 1 + heuristic(neighbor, end) new_dist = dist + 1 q.put((weight, (new_dist, neighbor))) closed.add(neighbor) parents[neighbor] = cell if path_found: cur_cell = end path = [end] while cur_cell != start: cur_cell = parents[cur_cell] path.append(cur_cell) path.reverse() return path else: print >> stderr, "NO PATH" return []
def UCS(sentence, model): frontier = pqueue() for root in sentence[0]: node = [root] cost = model.score(node) frontier.put((cost, node)) while (True): if frontier.empty(): return None else: node = list(frontier.get()[1]) if len(node) == len(sentence): return node, cost else: index = len(node) for nextWord in sentence[index]: newNode = node + [nextWord] cost = model.score(newNode) frontier.put((cost, newNode))
def UCS(sentence, model): frontier = pqueue() for root in sentence[0]: node = [root] cost = model.score(node) frontier.put((cost, node)) while(True): if frontier.empty(): return None else: node = list(frontier.get()[1]) if len(node) == len(sentence): return node, cost else: index = len(node) for nextWord in sentence[index]: newNode = node + [nextWord] cost = model.score(newNode) frontier.put((cost, newNode))
def __init__( self ): super( PriorityQueue, self ).__init__() self.messages = pqueue()