def find_approx_k_center(self, entry_points, geometry_solver=None): point_util = None if geometry_solver: point_util = geometry_solver.point_util if len(entry_points) <= self.k: centers = [ entry_points[i % len(entry_points)] for i in range(self.k) ] radius = 0 return radius, centers centers = [entry_points[0]] for i in range(1, self.k): next_center = None distance = 0 for ep in entry_points: ep_dis = PointUtil.get_min_distance_from_points( ep, centers, point_util) if distance < ep_dis: distance = ep_dis next_center = ep centers.append(next_center) radius = 0 for ep in entry_points: if ep in centers: continue radius = max( radius, PointUtil.get_min_distance_from_points(ep, centers, point_util)) return radius, list(centers) if centers else None
def init_points(file_name): input_points = [] with open(file_name, 'r') as file: simulator_configure = file.readline().strip().split(' ') eps = float(simulator_configure[0]) max_radius = float(simulator_configure[1]) window_size = int(simulator_configure[2]) max_axis_length = max_radius * math.sqrt(2) / 2 for line in file.readlines(): args = line.strip().split(' ') cmd = args[0] number_of_points = int(args[1]) if cmd == 'insert_random': input_points += [PointUtil.generate_random_point(max_axis_length)\ for i in range(number_of_points)] elif cmd == 'insert_exact': x = float(args[2]) y = float(args[2]) if not(-max_axis_length/2 < x < max_axis_length/2 and \ -max_axis_length/2 < y < max_axis_length/2): print("Problem with input") input_points += [Point(x, y) for i in range(number_of_points)] elif cmd == 'insert_in_range': x_0, y_0, x_1, y_1 = [float(arg) for arg in args[2:6]] input_points += [PointUtil.generate_customized_random_point \ (x_0, y_0, x_1, y_1) for i in range(number_of_points)] if len(input_points) == 0: input_points = None return input_points, eps, max_radius, window_size
def find_k_center(self, entry_points, geometry_solver=None): point_util = None if geometry_solver: point_util = geometry_solver.point_util if len(entry_points) < self.k: centers = [ entry_points[i % len(entry_points)] for i in range(self.k) ] radius = 0 return radius, centers radius = INF centers = None for possible_centers in combinations(entry_points, self.k): min_radius = 0 for ep in entry_points: if ep in possible_centers: continue dis = PointUtil.get_min_distance_from_points( ep, possible_centers, point_util) min_radius = max(min_radius, dis) if min_radius < radius: radius, centers = min_radius, possible_centers return radius, list(centers) if centers else None
def does_new_point_fit(self, new_entry_point): centers = self.extra_data if centers and all([c.is_alive() for c in centers]): cell_dis = PointUtil.get_min_distance_from_points( new_entry_point, centers, self.point_util) if cell_dis <= self.max_valid_distance(): self.result = max(self.result, cell_dis) return True return False
def clear_extra_entry_points(self, new_entry_point): selected_points = [new_entry_point] most_recent_farthest_ep = None for ep in reversed(self.entry_points): if PointUtil.get_min_distance_from_points( ep, selected_points, self.point_util) > 2 * self.max_valid_distance(): if len(selected_points) == self.k: most_recent_farthest_ep = ep break selected_points.append(ep) if most_recent_farthest_ep: for ep in self.entry_points: cell_dis = PointUtil.get_min_distance_from_points( ep, selected_points, self.point_util) if ep != most_recent_farthest_ep and cell_dis > 2 * self.max_valid_distance( ): self.entry_points.remove(ep) return True return False
def does_new_point_fit(self, new_entry_point): if self.extra_data: corner_old, corner_new = self.extra_data if corner_old.is_alive() and corner_new.is_alive(): return PointUtil.is_in_circle(new_entry_point, corner_old, corner_new, self.point_util) # cell_dis = get_distance_from_point(new_entry_point, self.centers, self.point_util) # if cell_dis <= self.max_valid_distance(): # self.radius = max(self.radius, cell_dis) # return True return False
def clear_extra_entry_points(self, new_entry_point): most_recent_farthest_ep_index = -1 for i, ep_i in enumerate(self.entry_points): for j, ep_j in enumerate(self.entry_points[i + 1:]): if PointUtil.get_distance_from_point( ep_i, ep_j, self.point_util) > self.max_valid_distance(): most_recent_farthest_ep_index = i if most_recent_farthest_ep_index != -1: self.entry_points = self.entry_points[ most_recent_farthest_ep_index:] return True return False
def find_diameter(entry_points, geometry_solver=None): point_util = None if geometry_solver: point_util = geometry_solver.point_util if len(entry_points) < 2: corner_old, corner_new = entry_points[0], entry_points[0] diameter = 0 return diameter, (corner_old, corner_new) diameter = 0 corner_old, corner_new = None, None for possible_corners in combinations(entry_points, 2): tmp_diameter = PointUtil.get_distance_from_point( possible_corners[0], possible_corners[1], point_util) if tmp_diameter >= diameter: diameter, corner_old, corner_new = tmp_diameter, possible_corners[ 0], possible_corners[1] if corner_old is not None: return diameter, (corner_old, corner_new) return diameter, None