def compute_mesh_mbr(self, locations): result = [] # min_x = 100000000 min_y = 100000000 max_x = -100000000 max_y = -100000000 for point in locations: if min_x > point.x: min_x = point.x if min_y > point.y: min_y = point.y if max_x < point.x: max_x = point.x if max_y < point.y: max_y = point.y # print "compute_mesh_mbr - min,max (X,Y)", min_x, min_y, max_x, max_y #Solution 1: linear scan # start = time.clock() for edge in self.edges.itervalues(): if self.is_edge_cut_rec(min_x, min_y, max_x, max_y, edge): p1_x = self.nodes[edge.start_node_id].x p1_y = self.nodes[edge.start_node_id].y p2_x = self.nodes[edge.end_node_id].x p2_y = self.nodes[edge.end_node_id].y result.append(EdgeSegment(p1_x, p1_y, p2_x, p2_y, edge.edge_id)) # print "Elapsed ", (time.clock() - start) # print "Solution 1: len=", len(result) # for edge_seg in result: # print edge_seg.cur_edge_id #Solution 2: Interval Tree # start = time.clock() # set_x = set(self.interval_tree_x.findRange([min_x, max_x])) # set_y = set(self.interval_tree_y.findRange([min_y, max_y])) # print "set_x", set_x # print "set_y", set_y # # cutting_edges = set_x & set_y # print "cutting_edges", cutting_edges # result = [] # for edge_id in cutting_edges: # if self.is_edge_cut_rec(min_x, min_y, max_x, max_y, self.edges[edge_id]): # p1_x = self.nodes[self.edges[edge_id].start_node_id].x # p1_y = self.nodes[self.edges[edge_id].start_node_id].y # p2_x = self.nodes[self.edges[edge_id].end_node_id].x # p2_y = self.nodes[self.edges[edge_id].end_node_id].y # result.append(EdgeSegment(p1_x, p1_y, p2_x, p2_y, edge_id)) # print "Elapsed ", (time.clock() - start) # print "Solution 2: len=", len(result) # for edge_seg in result: # print edge_seg.cur_edge_id return result
def find_partial_coverage(self, part_edges, query): if len(part_edges) == 0: return [] #binary search lo = 0 hi = len(part_edges) - 1 mid = (lo + hi) / 2 found = False while True: if part_edges[mid][0].cur_edge_id == query.cur_edge_id: found = True break if part_edges[mid][0].cur_edge_id > query.cur_edge_id: hi = mid - 1 if hi < lo: break else: lo = mid + 1 if lo > hi: break mid = (lo + hi) / 2 if found == False: return [] # lo = mid while lo - 1 > 0 and part_edges[lo - 1][0].cur_edge_id == query.cur_edge_id: lo = lo - 1 hi = mid while hi + 1 < len(part_edges) and part_edges[ hi + 1][0].cur_edge_id == query.cur_edge_id: hi = hi + 1 result = [] for pair in part_edges[lo:hi + 1]: if EdgeSegment.is_line_cover(Point(query), pair[0]) == True: result.append(pair[1]) return result
def find_partial_coverage(self, part_edges, query): if len(part_edges) == 0: return [] #binary search lo = 0 hi = len(part_edges) - 1 mid = (lo + hi) / 2 found = False while True: if part_edges[mid][0].cur_edge_id == query.cur_edge_id: found = True break if part_edges[mid][0].cur_edge_id > query.cur_edge_id: hi = mid - 1 if hi < lo: break else: lo = mid + 1 if lo > hi: break mid = (lo + hi) / 2 if found == False: return [] # lo = mid while lo-1 > 0 and part_edges[lo-1][0].cur_edge_id == query.cur_edge_id: lo = lo - 1 hi = mid while hi+1 < len(part_edges) and part_edges[hi+1][0].cur_edge_id == query.cur_edge_id: hi = hi + 1 result = [] for pair in part_edges[lo:hi+1]: if EdgeSegment.is_line_cover(Point(query), pair[0]) == True: result.append(pair[1]) return result
def compute_fixed_expanding(self, x, y, cur_edge_id, length): result = [] stack = Stack() # is_node = self.get_next_node_id(x, y) > -1 stack.push(SegItem(x, y, -1, -1, length, cur_edge_id, is_node)) while stack.get_size() > 0: # DEBUG # stack.print_all() # print "END OF LIST" # item = stack.get() # if item.length == 0: # result.append(item) # continue # case 1, is_node == True if item.is_node == True: node_id = self.get_next_node_id(item.x, item.y) for end_node_id in self.adj[ node_id]: #traverse adjacent edges... edge_len = get_edge_length(self.nodes[node_id], self.nodes[end_node_id]) if edge_len < item.length: remaining_len = item.length - edge_len # result.append( EdgeSegment( self.nodes[node_id].x, self.nodes[node_id].y, self.nodes[end_node_id].x, self.nodes[end_node_id].y, self.node_pair_to_edge[(node_id, end_node_id)])) # for edge_id in self.node_to_edges[ end_node_id]: #one choice for each adjacent edge stack.push( SegItem(self.nodes[end_node_id].x, self.nodes[end_node_id].y, -1, -1, remaining_len, edge_id, True)) else: end_x = item.x + item.length * ( self.nodes[end_node_id].x - item.x) / edge_len end_y = item.y + item.length * ( self.nodes[end_node_id].y - item.y) / edge_len result.append( EdgeSegment( self.nodes[node_id].x, self.nodes[node_id].y, end_x, end_y, self.node_pair_to_edge[(node_id, end_node_id)])) # case 2, is_node == False else: for end_node_id in (self.edges[item.cur_edge_id].start_node_id, self.edges[item.cur_edge_id].end_node_id): segment_len = get_segment_length(self.nodes[end_node_id], item.x, item.y) if segment_len < item.length: remaining_len = item.length - segment_len # end_node_id.xy go first to comply with convention: first point always graph node !! result.append( EdgeSegment(self.nodes[end_node_id].x, self.nodes[end_node_id].y, item.x, item.y, item.cur_edge_id)) # for edge_id in self.node_to_edges[end_node_id]: stack.push( SegItem(self.nodes[end_node_id].x, self.nodes[end_node_id].y, -1, -1, remaining_len, edge_id, True)) else: end_x = item.x + item.length * ( self.nodes[end_node_id].x - item.x) / segment_len end_y = item.y + item.length * ( self.nodes[end_node_id].y - item.y) / segment_len result.append( EdgeSegment(item.x, item.y, end_x, end_y, item.cur_edge_id)) # DEBUG # print "stack.max_size", stack.max_size # # print "length(result) = ", length(result) # for item in result: # print "%15d %8.2f %10.2f %10.2f %10.2f %10.2f" % (item.cur_edge_id, EdgeSegment.length(item), \ # item.start_x, item.start_y, item.end_x, item.end_y) return result
# timestamp = 0; expanding_list = {} #dict of lists query_list = query_log.frames[timestamp] # timestamp #1. compute expanding_list start_time = time.clock() print "#users =", len(query_list) count = 0 for query in query_list: seg_list = map_data.compute_fixed_expanding(query.x, query.y, query.cur_edge_id, query.dist) #old: option.DISTANCE_CONSTRAINT seg_list_length = 0.0 for seg in seg_list: seg_list_length += EdgeSegment.length(seg); print "seg_list.size = %d - %f" %(len(seg_list), seg_list_length) seg_list = EdgeSegmentSet.clean_fixed_expanding(seg_list) print "AFTER seg_list.size =", len(seg_list) expanding_list[query.obj_id] = seg_list count += 1 if count == 10: break; print "expanding_list - elapsed : ", (time.clock() - start_time)
def read_result(self, config_name, distance_constraint, max_speed, timestamp): file_name = config_name + "-" + str(distance_constraint) + "-" + str( max_speed) #1. read positive_mc_set positive_mc_set_file_name = "C:/" + file_name + "_positive_mc_set" + "_" + str( timestamp) + ".out" if not os.path.exists(positive_mc_set_file_name): print "File Not Found !" return f = open(positive_mc_set_file_name, "r") fstr = f.read() f.close() lines = fstr.split("\n") for line in lines: if line == "": # EOF break items = line.split(",") items.pop() #remove last empty item self.positive_mc_set.append(set([int(item) for item in items])) print "positive_mc_set.len =", len(self.positive_mc_set) #2. read user_mc_set (remember: max clique for each id) user_mc_set_file_name = "C:/" + file_name + "_user_mc_set" + "_" + str( timestamp) + ".out" if not os.path.exists(user_mc_set_file_name): print "File Not Found !" return f = open(user_mc_set_file_name, "r") fstr = f.read() f.close() lines = fstr.split("\n") for line in lines: if line == "": # EOF break items = line.split(" ") self.user_mc_set[int(items[0])] = int(items[1]) print "user_mc_set.len =", len(self.user_mc_set) #3. read user_mesh user_mesh_file_name = "C:/" + file_name + "_user_mesh" + "_" + str( timestamp) + ".out" if not os.path.exists(user_mesh_file_name): print "File Not Found !" return f = open(user_mesh_file_name, "r") fstr = f.read() f.close() lines = fstr.split("\n") for i in range(len(lines) / 2): obj_id = int(lines[i * 2]) seg_list_str = lines[i * 2 + 1] self.user_mesh[obj_id] = [] for seg_str in seg_list_str.split(":"): if len(seg_str) > 0: items = seg_str.split(",") self.user_mesh[obj_id].append( EdgeSegment(float(items[0]), float(items[1]), float(items[2]), float(items[3]), int(items[4]))) print "user_mesh.len =", len(self.user_mesh)