def solve_a_new_query(self, query): user_u = query.obj_id num_edges = 0 list_edges = [] for user_w in self.user_mbr.iterkeys(): if user_w == user_u: continue if not self.user_mbr.has_key(user_u): #user_u is NEW if not self.graph_edges.has_key((user_u, user_w)) and \ MBR.is_mmb_cover(self.user_mbr[user_w], Point(query), (query.timestamp - self.last_query[user_w]) * option.MAX_SPEED): num_edges += 1 list_edges.append((user_u, user_w)) else: #user_u EXISTED if not self.graph_edges.has_key((user_u, user_w)) and \ MBR.is_mmb_cover(self.user_mbr[user_w], Point(self.last_query[user_u]), (query.timestamp - self.last_query[user_w]) * option.MAX_SPEED) and \ MBR.is_mmb_cover(self.user_mbr[user_u], Point(self.last_query[user_w]), (query.timestamp - self.last_query[user_u]) * option.MAX_SPEED): num_edges += 1 list_edges.append((user_u, user_w)) print "num_edges=", num_edges print "list_edges - elapsed : ", (time.clock() - start_time) start_time = time.clock() graph.add_to_mc_set(list_edges) print "add_to_mc_set - elapsed : ", (time.clock() - start_time) print "mc_set =", self.mc_set
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 solve_new_queries(self, timestamp): start_time = time.clock() # mmb_list = [] #list of lists # mmb_list_index = [] #list of sets query_list = self.query_log.frames[timestamp] #1. compute SC, SD, SN, (SF) set_F = set([]) new_user_set = set([]) for query in query_list: new_user_set = new_user_set | set([query.obj_id]) set_C = self.user_set & new_user_set set_D = self.user_set - new_user_set set_N = new_user_set - self.user_set print "set_D", set_D print "set_N", set_N #2. process set_D (disappear) self.remove_from_mc_set(set_D) #3. compute_mesh_expanding (MAX_SPEED) for clique in self.mc_set: if len(clique) < option.K_ANONYMITY: continue node = list(clique)[0] #print "debug", clique, node seg_list = self.map_data.compute_mesh_expanding( self.user_mesh[node], option.MAX_SPEED) self.mmb_mesh[node] = seg_list for node_2 in clique: if node_2 != node and self.user_mc_set[ node_2] == self.user_mc_set[node]: self.mmb_mesh[node_2] = seg_list #3.1 num_edges = 0 list_edges = [] for node_1 in set_C: for node_2 in set_C: if node_1 != node_2 and self.mmb_mesh.has_key( node_1) and self.mmb_mesh.has_key(node_2): point_1 = Point(self.query_log.trajs[node_1][timestamp]) point_2 = Point(self.query_log.trajs[node_2][timestamp]) if EdgeSegmentSet.is_set_cover(point_1, self.mmb_mesh[node_2]) and \ EdgeSegmentSet.is_set_cover(point_2, self.mmb_mesh[node_1]): num_edges += 1 list_edges.append((node_1, node_2)) print "num_edges (set_C) = ", num_edges graph.add_to_mc_set(list_edges) #3.2 num_edges = 0 list_edges = [] for node_1 in set_C: for node_2 in set_N: point_2 = Point(self.query_log.trajs[node_2][timestamp]) if self.mmb_mesh.has_key( node_1) and EdgeSegmentSet.is_set_cover( point_2, self.mmb_mesh[node_1]): num_edges += 1 list_edges.append((node_1, node_2)) print "num_edges (set_N) = ", num_edges graph.add_to_mc_set(list_edges) #4. update self.user_mc_set, self.user_mesh, self.user_set #4.1 compute user_mc_set, max clique for each obj_id self.user_mc_set = {} for clique in self.mc_set: for obj_id in clique: if not self.user_mc_set.has_key(obj_id): self.user_mc_set[obj_id] = clique elif len(self.user_mc_set[obj_id]) < len(clique): self.user_mc_set[obj_id] = clique print "Compute user_mc_set: DONE" #4.2 compute MMBs (CLOAKING MESHES) and self.user_set max_mesh_len = 0 min_mesh_len = 1000000 self.user_mesh = {} for clique in self.mc_set: if len(clique) >= option.K_ANONYMITY: locations = [] for node in clique: locations.append(self.query_log.trajs[node][timestamp]) mesh = self.map_data.compute_mesh_mbr( locations) #TEMPORARILY !! # temp_len = EdgeSegmentSet.length(mesh) if max_mesh_len < temp_len: max_mesh_len = temp_len if min_mesh_len > temp_len: min_mesh_len = temp_len # assign mesh to all obj_id in 'clique' for obj_id in clique: self.user_mesh[obj_id] = mesh # for obj_id in clique: self.user_set = self.user_set | set([obj_id]) print "Compute CLOAKING MESH: DONE" print "max_mesh_len =", max_mesh_len print "min_mesh_len =", min_mesh_len print "elapsed : ", (time.clock() - start_time)
def process_existing_clique(self, clique, timestamp): free_nodes = [] satisfied = True #new clique satisfied or not #1. get new_loc expanding_list = {} #dict of lists query_list = [] for node in clique: query = self.query_log.trajs[node][timestamp] query_list.append(query) for query in query_list: seg_list = self.map_data.compute_fixed_expanding( query.x, query.y, query.cur_edge_id, option.INIT_DISTANCE) seg_list = EdgeSegmentSet.clean_fixed_expanding(seg_list) expanding_list[query.obj_id] = seg_list #2. build graph degree = {} adj = {} for query in query_list: degree[query.obj_id] = 0 for i in range(len(query_list)): for j in range(i + 1, len(query_list)): if EdgeSegmentSet.is_set_cover(Point(query_list[i]), expanding_list[query_list[j].obj_id]) and \ EdgeSegmentSet.is_set_cover(Point(query_list[j]), expanding_list[query_list[i].obj_id]): adj[(query_list[i].obj_id, query_list[j].obj_id)] = 1 adj[(query_list[j].obj_id, query_list[i].obj_id)] = 1 for pair in adj.iterkeys(): degree[pair[0]] += 1 degree[pair[1]] += 1 for node in clique: if degree[node] == 0: free_nodes.append(node) del expanding_list[node] satisfied = False #3. compute union (mesh) and ... mesh = [] for seg_list in expanding_list.itervalues(): mesh = EdgeSegmentSet.union(mesh, seg_list) #3.2 check total_len if EdgeSegmentSet.length(mesh) > option.MAX_MESH_LENGTH: satisfied = False # remove (heuristically) nodes which have low degrees while True: node = min(degree, degree.get) free_nodes.append(node) del degree[node] for node_2 in clique: if adj.has_key((node, node_2)): del adj[(node, node_2)] del adj[(node_2, node)] degree[node_2] = degree[node_2] - 1 #re-compute union (mesh) and check total_len mesh = [] for seg_list in expanding_list.itervalues(): mesh = EdgeSegmentSet.union(mesh, seg_list) if EdgeSegmentSet.length(mesh) <= option.MAX_MESH_LENGTH: break #4. return return (satisfied, free_nodes, mesh)
def init_mc_set(self): start_time = time.clock() expanding_list = {} #[[]] * option.MAX_USER #dict of lists query_list = self.query_log.frames[0] #init timestamp min_obj_id = option.MAX_USER max_obj_id = 0 #1. compute expanding_list for query in query_list: if max_obj_id < query.obj_id: max_obj_id = query.obj_id if min_obj_id > query.obj_id: min_obj_id = query.obj_id # self.num_user = max(self.num_user, query.obj_id) seg_list = self.map_data.compute_fixed_expanding( query.x, query.y, query.cur_edge_id, option.INIT_DISTANCE) seg_list = EdgeSegmentSet.clean_fixed_expanding(seg_list) expanding_list[query.obj_id] = seg_list #1.2 check connectivity of each seg_list --> OK # print "Connectivity check: STARTED" # for query in query_list: # if not Map.check_connected_expanding(expanding_list[query.obj_id]): # print "error found at obj_id=", query.obj_id # print "Connectivity check: DONE" #2. init self.mc_set self.reset() for query in query_list: self.mc_set.append(set([query.obj_id])) #3. compute mc_set num_edges = 0 list_edges = [] for i in range(len(query_list)): for j in range(i + 1, len(query_list)): if get_distance(query_list[i].x, query_list[i].y, query_list[j].x, query_list[j].y) > \ option.INIT_DISTANCE: continue if EdgeSegmentSet.is_set_cover(Point(query_list[i]), expanding_list[query_list[j].obj_id]) and \ EdgeSegmentSet.is_set_cover(Point(query_list[j]), expanding_list[query_list[i].obj_id]): num_edges += 1 list_edges.append( (query_list[i].obj_id, query_list[j].obj_id)) print "num_edges=", num_edges print "list_edges - elapsed : ", (time.clock() - start_time) start_time = time.clock() graph.add_to_mc_set(list_edges) print "add_to_mc_set - elapsed : ", (time.clock() - start_time) print "mc_set =", self.mc_set #4. compute user_mc_set, max clique for each obj_id self.user_mc_set = {} for clique in self.mc_set: if len(clique) >= option.K_ANONYMITY: for obj_id in clique: if not self.user_mc_set.has_key(obj_id): self.user_mc_set[obj_id] = clique elif len(self.user_mc_set[obj_id]) < len(clique): self.user_mc_set[obj_id] = clique print "Compute user_mc_set: DONE" print "user_mc_set = ", self.user_mc_set #5. compute MMBs (CLOAKING MESHES) and self.user_set max_mesh_len = 0 min_mesh_len = 1000000 for clique in self.mc_set: if len(clique) >= option.K_ANONYMITY: mesh = [] for obj_id in clique: mesh = EdgeSegmentSet.union(mesh, expanding_list[obj_id]) # temp_len = EdgeSegmentSet.length(mesh) if max_mesh_len < temp_len: max_mesh_len = temp_len if min_mesh_len > temp_len: min_mesh_len = temp_len # assign mesh to all obj_id in 'clique' for obj_id in clique: self.user_mesh[obj_id] = mesh # for obj_id in clique: self.user_set = self.user_set | set([obj_id]) print "self.user_set = ", self.user_set print "Compute CLOAKING MESH: DONE" print "max_mesh_len =", max_mesh_len print "min_mesh_len =", min_mesh_len #5.2 check connectivity of each user_mesh --> OK # start_time = time.clock() # print "MMB Connectivity check: STARTED" # for clique in self.mc_set: # for obj_id in clique: # if not Map.check_connected_expanding(self.user_mesh[obj_id]): # print "error found at clique=", clique # continue # print "MMB Connectivity check: DONE" # print "elapsed : ", (time.clock() - start_time) #DEBUG print "len(graph.mc_set) = ", len(graph.mc_set) print graph.mc_set
def solve_new_queries(self, timestamp): start_time = time.clock() expanding_list = {} #dict of lists query_list = self.query_log.frames[timestamp] # timestamp min_obj_id = option.MAX_USER max_obj_id = 0 #1. compute expanding_list for query in query_list: if max_obj_id < query.obj_id: max_obj_id = query.obj_id if min_obj_id > query.obj_id: min_obj_id = query.obj_id # seg_list = self.map_data.compute_fixed_expanding( query.x, query.y, query.cur_edge_id, option.INIT_DISTANCE) seg_list = EdgeSegmentSet.clean_fixed_expanding(seg_list) expanding_list[query.obj_id] = seg_list #2. init self.mc_set self.reset() for query in query_list: self.mc_set.append(set([query.obj_id])) #3. compute mc_set start_time = time.clock() num_edges = 0 list_edges = [] for i in range(len(query_list)): for j in range(i + 1, len(query_list)): if get_distance(query_list[i].x, query_list[i].y, query_list[j].x, query_list[j].y) > \ option.INIT_DISTANCE: continue if EdgeSegmentSet.is_set_cover(Point(query_list[i]), expanding_list[query_list[j].obj_id]) and \ EdgeSegmentSet.is_set_cover(Point(query_list[j]), expanding_list[query_list[i].obj_id]): num_edges += 1 list_edges.append( (query_list[i].obj_id, query_list[j].obj_id)) print "num_edges=", num_edges print "list_edges - elapsed : ", (time.clock() - start_time) start_time = time.clock() graph.add_to_mc_set(list_edges) print "add_to_mc_set - elapsed : ", (time.clock() - start_time) print "mc_set =", self.mc_set #4. compute user_mc_set, max clique for each obj_id start_time = time.clock() self.user_mc_set = {} for clique in self.mc_set: if len(clique) >= option.K_ANONYMITY: for obj_id in clique: if not self.user_mc_set.has_key(obj_id): self.user_mc_set[obj_id] = clique elif len(self.user_mc_set[obj_id]) < len(clique): self.user_mc_set[obj_id] = clique print "Compute user_mc_set - elapsed : ", (time.clock() - start_time) print "user_mc_set = ", self.user_mc_set #5. compute MMBs (CLOAKING MESHES) and self.user_set start_time = time.clock() max_mesh_len = 0 min_mesh_len = 1000000 for clique in self.mc_set: if len(clique) >= option.K_ANONYMITY: mesh = [] for obj_id in clique: mesh = EdgeSegmentSet.union(mesh, expanding_list[obj_id]) # temp_len = EdgeSegmentSet.length(mesh) if max_mesh_len < temp_len: max_mesh_len = temp_len if min_mesh_len > temp_len: min_mesh_len = temp_len # assign mesh to all obj_id in 'clique' for obj_id in clique: self.user_mesh[obj_id] = mesh # for obj_id in clique: self.user_set = self.user_set | set([obj_id]) #print "self.user_set = ", self.user_set print "Compute CLOAKING MESH - elapsed : ", (time.clock() - start_time) print "max_mesh_len =", max_mesh_len print "min_mesh_len =", min_mesh_len