class Node(Primitive): p_type = "node" name = "default" id = 0 position = None rf = None def __init__(self, id=0, x=0, y=0, z=0): """ Node constructor :param id: unique node identifier :param x: position x (meters) :param y: position y (meters) :param z: altitude (meters) :return: """ self.apply_config(self.get_config(self.p_type, self.name)) self.id = id self.rf = RfModule() self.rf.set_core(self) if "rf" in self.config: self.rf.apply_config(self.config["rf"]) self.position = Position() self.set_position(x, y, z) def get_name(self): return "%s_%d" % (self.name, self.id) def set_position(self, x, y, z=0): self.position.set_position(x, y, z) def get_position(self, return_obj=False): if return_obj: return self.position return self.position.get_position() def distance_to(self, node_b): return self.position.distance_to(node_b.position) def accept(self, message): if message.type_of(Message.TIME): self.log("get time")
def __init__(self, id=0, x=0, y=0, z=0): """ Node constructor :param id: unique node identifier :param x: position x (meters) :param y: position y (meters) :param z: altitude (meters) :return: """ self.apply_config(self.get_config(self.p_type, self.name)) self.id = id self.rf = RfModule() self.rf.set_core(self) if "rf" in self.config: self.rf.apply_config(self.config["rf"]) self.position = Position() self.set_position(x, y, z)
def move_in_range(self, a, b, c, r): min_d = a.planar_distance_to(b) #print min_d max_d = a.planar_distance_to(c) + c.planar_distance_to(b) #print max_d best_position = c a_direction = c.azimuth(a) b_direction = c.azimuth(b) if b_direction < a_direction: b_direction, a_direction = a_direction, b_direction optimal_line_direction = 1.0 *(a_direction + b_direction) / 2 candidates = [] d_x = r * math.sin(optimal_line_direction) d_y = -r * math.cos(optimal_line_direction) #print optimal_line_direction * 180/ math.pi #print d_x, d_y d = c.clone().move(d_x, d_y) e = c.clone().move(-d_x, -d_y) candidates.append(d) candidates.append(e) # point on AB intersection = geometry.line_intersection(a, b, c, d) #print intersection if intersection: p = Position(*intersection) if c.planar_distance_to(p) < r: candidates.append(p) for candidate in candidates: dist = a.planar_distance_to(candidate) + candidate.planar_distance_to(b) #print dist, candidate.get_position() if dist < max_d: max_d = dist best_position = candidate #print a.get_position(), b.get_position(), c.get_position(), d.get_position() #print c.azimuth(b) #print optimal_line_direction, c.get_position() #print '='*20 return best_position
def path(): report = [] traces = [] uav_altitude = 200 uav_start = position.Position(0, 0, uav_altitude) init_paths = [] opt_paths = [] repeats = 1000 report.append({'label': "Repeats", 'value': repeats}) for i in range(repeats): nodes = generate_nodes_data(10, 2000, 2000) to_visit = [] for node_id in nodes: to_visit.append(Position(*nodes[node_id])) planner = NNPathPlanner() uav_path = planner.compute_path(uav_start, to_visit) init_paths.append(planner.path_length(uav_path)) uav_path_opt = planner.reduce_path(R, 5) opt_paths.append(planner.path_length(uav_path_opt)) avg_init = sum(init_paths) / len(init_paths) avg_opt = sum(opt_paths) / len(opt_paths) reduction = (avg_init - avg_opt) / avg_init report.append({'label': "Init", 'value': avg_init}) report.append({'label': "Reduced", 'value': avg_opt}) report.append({'label': "Reduction", 'value': reduction}) params = { "title": "main", "nodes": nodes, "report": report, } return render_template("path.html", **params)
if c.planar_distance_to(p) < r: candidates.append(p) for candidate in candidates: dist = a.planar_distance_to(candidate) + candidate.planar_distance_to(b) #print dist, candidate.get_position() if dist < max_d: max_d = dist best_position = candidate #print a.get_position(), b.get_position(), c.get_position(), d.get_position() #print c.azimuth(b) #print optimal_line_direction, c.get_position() #print '='*20 return best_position if __name__ == "__main__": a = Position(0,0) b = Position(200, 0) c = Position(150, 150) red = BisectPathReducer() red.move_in_range(a, b, c, 50)