def build_graph(adj, features, labels): edges = np.array(adj.nonzero()).T y_values = np.array(labels.nonzero()).T domain_labels = [] for i in range(labels.shape[1]): domain_labels.append("c" + str(i)) # create graph graph = UndirectedGraph() id_obj_map = [] for i in range(adj.shape[0]): n = Node(i, features[i, :], domain_labels[y_values[i, 1]]) graph.add_node(n) id_obj_map.append(n) for e in edges: graph.add_edge(Edge(id_obj_map[e[1]], id_obj_map[e[0]])) return graph, domain_labels
def testIndependentSet(self): n = 100 e = 5 * n g = UndirectedGraph() for i in range(e): u = int(random() * n) v = int(random() * n) if u == v: continue if not g.contains(u): g.add_node(u) if not g.contains(v): g.add_node(v) g.connect(u, v) ind_set = g.independent_set(8) for i in ind_set: neighbors = g.e[i] self.assertEqual(neighbors.intersection(ind_set), set([]))
def create_map(self, file_name): walls = {} print 'Reading File' f = open(file_name, 'r') # Reading walls: [row, col, up, left, down, right] print '\t> Parsing walls' [MAX_ROW, MAX_COL] = f.readline().split(' ') MAX_ROW = int(MAX_ROW) MAX_COL = int(MAX_COL) for i in range(0, MAX_ROW*MAX_COL): data = f.readline().split(' ') print 'data: ', data data = map(int, data) walls[(data[0],data[1])] = (data[2],data[3],data[4], data[5]) f.close() # Generating graph print 'Generating graph' graph = UndirectedGraph() for i in range(0, MAX_ROW): for j in range(0, MAX_COL): graph.add_node((i,j,Orientation.up)) graph.add_node((i,j,Orientation.left)) graph.add_node((i,j,Orientation.down)) graph.add_node((i,j,Orientation.right)) graph.add_edge((i,j,Orientation.up), (i,j,Orientation.left)) graph.add_edge((i,j,Orientation.left), (i,j,Orientation.down)) graph.add_edge((i,j,Orientation.down), (i,j,Orientation.right)) graph.add_edge((i,j,Orientation.right), (i,j,Orientation.up)) for node, ws in walls.items(): if ws[0] == 0: graph.add_edge((node[0],node[1],Orientation.up), (node[0]+1,node[1],Orientation.up)) if ws[1] == 0: graph.add_edge((node[0],node[1],Orientation.left), (node[0],node[1]-1,Orientation.left)) if ws[2] == 0: graph.add_edge((node[0],node[1],Orientation.down), (node[0]-1,node[1],Orientation.down)) if ws[3] == 0: graph.add_edge((node[0],node[1],Orientation.right), (node[0],node[1]+1,Orientation.right)) return graph
def remove_independent_set(regions): """ Processes a set of regions, detecting and removing an independent set of vertices from the regions' graph representation, and re-triangulating the resulting holes. Arguments: regions -- a set of non-overlapping polygons that tile some part of the plane Returns: a new set of regions covering the same subset of the plane, with fewer vertices """ # Take note of which points are in which regions points_to_regions = {} for idx, region in enumerate(regions): for point in region.points: if point in points_to_regions: points_to_regions[point].add(idx) continue points_to_regions[point] = set([idx]) # Connect graph g = UndirectedGraph() for region in regions: for idx in range(region.n): u = region.points[idx % region.n] v = region.points[(idx + 1) % region.n] if not g.contains(u): g.add_node(u) if not g.contains(v): g.add_node(v) g.connect(u, v) # Avoid adding points from outer triangle removal = g.independent_set(8, avoid=bounding_triangle.points) # Track unaffected regions unaffected_regions = set([i for i in range(len(regions))]) new_regions = [] for p in removal: # Take note of affected regions affected_regions = points_to_regions[p] unaffected_regions.difference_update(points_to_regions[p]) def calculate_bounding_polygon(p, affected_regions): edges = [] point_locations = {} for j, i in enumerate(affected_regions): edge = set(regions[i].points) edge.remove(p) edges.append(edge) for v in edge: if v in point_locations: point_locations[v].add(j) else: point_locations[v] = set([j]) boundary = [] edge = edges.pop() for v in edge: point_locations[v].remove(len(edges)) boundary.append(v) for k in range(len(affected_regions) - 2): v = boundary[-1] i = point_locations[v].pop() edge = edges[i] edge.remove(v) u = edge.pop() point_locations[u].remove(i) boundary.append(u) return shapes.Polygon(boundary) # triangulate hole poly = calculate_bounding_polygon(p, affected_regions) triangles = spatial.triangulatePolygon(poly) for triangle in triangles: self.dag.add_node(triangle) for j in affected_regions: region = regions[j] self.dag.connect(triangle, region) new_regions.append(triangle) for i in unaffected_regions: new_regions.append(regions[i]) return new_regions
class FileLoader(object): def __init__(self): self.walls = {} self.starts = [] self.goals = [] self.keys = [] self.max_cols = None self.max_rows = None self.undirected_graph = UndirectedGraph() self.directed_graph = DirectedGraph() self.distance_node = {} self.node_distance = {} def read_map(self, file_name): print 'Reading File' f = open(file_name, 'r') # Reading walls: [row, col, up, left, down, right] print '\t> Parsing walls' [self.max_rows, self.max_cols] = f.readline().split(' ') self.max_rows = int(self.max_rows) self.max_cols = int(self.max_cols) for i in range(0, self.max_rows*self.max_cols): data = f.readline().split(' ') print 'data: ', data data = map(int, data) self.walls[(data[0],data[1])] = (data[2],data[3],data[4], data[5]) # Reading starts print '\t> Parsing ', f.readline() MAX_START = int(f.readline()) for i in range(0, MAX_START): data = f.readline().split(' ') if data[2][0] == 'u': orientation = 0 elif data[2][0] == 'l': orientation = 1 elif data[2][0] == 'd': orientation = 2 else: orientation = 3 self.starts.append((int(data[0]),int(data[1]),orientation)) # Reading Goals print '\t> Parsing ', f.readline() MAX_GOALS = int(f.readline()) for i in range(0, MAX_GOALS): data = f.readline().split(' ') row = int(data[0]) col = int(data[1]) self.goals.append((row,col)) # Reading Keys print '\t> Parsing ', f.readline() MAX_KEYS = int(f.readline()) for i in range(0, MAX_KEYS): data = f.readline().split(' ') row = int(data[0]) col = int(data[1]) self.keys.append((row,col)) f.close() def generate_undirected_graph(self): orientations = [Orientation.up, Orientation.left, Orientation.down, Orientation.right] for w in self.walls.keys(): row = w[0] col = w[1] for o in orientations: self.undirected_graph.add_node((row,col,o)) self.undirected_graph.add_edge((row,col,Orientation.up), (row,col,Orientation.left)) self.undirected_graph.add_edge((row,col,Orientation.left), (row,col,Orientation.down)) self.undirected_graph.add_edge((row,col,Orientation.down), (row,col,Orientation.right)) self.undirected_graph.add_edge((row,col,Orientation.right), (row,col,Orientation.up)) for node, ws in self.walls.items(): if ws[0] == 0: self.undirected_graph.add_edge((node[0],node[1],Orientation.up), (node[0]+1,node[1],Orientation.up)) if ws[1] == 0: self.undirected_graph.add_edge((node[0],node[1],Orientation.left), (node[0],node[1]-1,Orientation.left)) if ws[2] == 0: self.undirected_graph.add_edge((node[0],node[1],Orientation.down), (node[0]-1,node[1],Orientation.down)) if ws[3] == 0: self.undirected_graph.add_edge((node[0],node[1],Orientation.right), (node[0],node[1]+1,Orientation.right)) def generate_directed_graph(self): orientations = [Orientation.up, Orientation.left, Orientation.down, Orientation.right] for w in self.walls.keys(): row = w[0] col = w[1] for o in orientations: self.directed_graph.add_node((row,col,o)) self.directed_graph.add_edge((row,col,Orientation.up), (row,col,Orientation.left)) self.directed_graph.add_edge((row,col,Orientation.left), (row,col,Orientation.up)) self.directed_graph.add_edge((row,col,Orientation.left), (row,col,Orientation.down)) self.directed_graph.add_edge((row,col,Orientation.down), (row,col,Orientation.left)) self.directed_graph.add_edge((row,col,Orientation.down), (row,col,Orientation.right)) self.directed_graph.add_edge((row,col,Orientation.right), (row,col,Orientation.down)) self.directed_graph.add_edge((row,col,Orientation.right), (row,col,Orientation.up)) self.directed_graph.add_edge((row,col,Orientation.up), (row,col,Orientation.right)) for node, ws in self.walls.items(): if ws[0] == 0: self.directed_graph.add_edge((node[0],node[1],Orientation.up), (node[0]+1,node[1],Orientation.up)) if ws[1] == 0: self.directed_graph.add_edge((node[0],node[1],Orientation.left), (node[0],node[1]-1,Orientation.left)) if ws[2] == 0: self.directed_graph.add_edge((node[0],node[1],Orientation.down), (node[0]-1,node[1],Orientation.down)) if ws[3] == 0: self.directed_graph.add_edge((node[0],node[1],Orientation.right), (node[0],node[1]+1,Orientation.right)) def estimate_distances(self): #print '> Exploring distances' nodes = self.undirected_graph.nodes for node in nodes: #print '\t>>Localization::estimate_distances Node ', node distance = 0 orientation = node[2] aux_node = node test_node = node while True: if orientation == Orientation.up: test_node = (aux_node[0] + 1, aux_node[1], aux_node[2]) elif orientation == Orientation.left: test_node = (aux_node[0], aux_node[1] - 1, aux_node[2]) elif orientation == Orientation.down: test_node = (aux_node[0] - 1, aux_node[1], aux_node[2]) elif orientation == Orientation.right: test_node = (aux_node[0], aux_node[1] + 1, aux_node[2]) if not test_node in self.undirected_graph.edges[aux_node]: #BUG break aux_node = test_node distance = distance + 1 self.distance_node.setdefault(distance, []) self.distance_node[distance].append(node) self.node_distance[node] = distance