def rank_origins(origins, destinations): p('debug', 'Ranking origins: %s' % origins) midpoint_origins = midpoint(origins) midpoint_destinations = midpoint(destinations) hub_route = Segment(midpoint_origins, midpoint_destinations) for origin in origins: AC = Segment(midpoint_origins, origin) orthogonal_heading = hub_route.get_initial_bearing() + 90 (a, b) = project_segment( abs(orthogonal_heading - AC.get_initial_bearing()), AC.get_length() ) projection = midpoint_origins.get_position( orthogonal_heading, a ) midpoint_to_projection = Segment( midpoint_origins, projection ) angle = abs( hub_route.get_initial_bearing() - midpoint_to_projection.get_initial_bearing() ) if abs(angle - 90) < 0.1: distance = -1 * midpoint_to_projection.get_length() else: distance = midpoint_to_projection.get_length() origin.distance_to_midpoint = distance return sorted(origins, key = lambda point: point.distance_to_midpoint)
def rank_origins(origins, destinations): p('debug', 'Ranking origins: %s' % origins) midpoint_origins = midpoint(origins) midpoint_destinations = midpoint(destinations) hub_route = Segment(midpoint_origins, midpoint_destinations) for origin in origins: AC = Segment(midpoint_origins, origin) orthogonal_heading = hub_route.get_initial_bearing() + 90 (a, b) = project_segment( abs(orthogonal_heading - AC.get_initial_bearing()), AC.get_length()) projection = midpoint_origins.get_position(orthogonal_heading, a) midpoint_to_projection = Segment(midpoint_origins, projection) angle = abs(hub_route.get_initial_bearing() - midpoint_to_projection.get_initial_bearing()) if abs(angle - 90) < 0.1: distance = -1 * midpoint_to_projection.get_length() else: distance = midpoint_to_projection.get_length() origin.distance_to_midpoint = distance return sorted(origins, key=lambda point: point.distance_to_midpoint)
def get_hub(flights): o = [] d = [] for flight in flights: o.append(flight['route'].waypoints[0]) d.append(flight['route'].waypoints[-1]) mid_o = midpoint(o) mid_d = midpoint(d) trunk_route = Segment(mid_o, mid_d) return mid_o.get_position(trunk_route.get_initial_bearing(), trunk_route.get_length() * config.Z)
def get_hub(flights): o = [] d = [] for flight in flights: o.append(flight['route'].waypoints[0]) d.append(flight['route'].waypoints[-1]) mid_o = midpoint(o) mid_d = midpoint(d) trunk_route = Segment(mid_o, mid_d) return mid_o.get_position( trunk_route.get_initial_bearing(), trunk_route.get_length() * config.Z )
def execute(): # Construct flight list planes = generators.get_via_stdin() hubs = [] origins = [] destinations = [] for plane in planes: origins.append(plane.origin) destinations.append(plane.destination) print midpoint(origins) print midpoint(destinations)
def construct_hub(origins, destinations, Z): midpoint_origins = midpoint(origins) midpoint_destinations = midpoint(destinations) hub_route = Segment(midpoint_origins, midpoint_destinations) hub = hub_route.start.get_position(hub_route.get_initial_bearing(), hub_route.get_length() * Z) hub.origins = origins hub.destinations = destinations p('debug', 'Constructed hub at %s' % (hub)) return hub
def is_valid_formation(aircraft_list): # Get the segments segments = map(lambda aircraft: aircraft.route.segments[0], aircraft_list) # Find the reference point collection of segments midpoints = map(lambda x: x.midpoint, segments) reference = util.midpoint (midpoints) deviation_sum = 0 for segment in segments: crosstrack = Segment(segment.start, reference) deviation = util.cross_track_distance ( crosstrack.get_length(), crosstrack.get_initial_bearing(), segment.get_initial_bearing() ) deviation_sum = deviation_sum + abs(deviation) if abs(deviation) > config.max_deviation: return False #print '%s (ref = %s) crosstrack distance: %.1f' %\ #(segment, reference, d_x) return deviation_sum
def construct_hub(origins, destinations, Z): midpoint_origins = midpoint(origins) midpoint_destinations = midpoint(destinations) hub_route = Segment(midpoint_origins, midpoint_destinations) hub = hub_route.start.get_position( hub_route.get_initial_bearing(), hub_route.get_length() * Z ) hub.origins = origins hub.destinations = destinations p('debug', 'Constructed hub at %s' % (hub)) return hub
def calibrate(self): """Determines the trunk route and hookoff points""" # Determine formation trunk route destinations = [] for aircraft in self: destinations.append(aircraft.destination) arrival_midpoint = midpoint(destinations) p('destinations: %s' % destinations) p('midpoint = %s' % arrival_midpoint) hub_to_midpoint = Segment(aircraft.hub, arrival_midpoint) # Determine hookoff point for each aircraft, except the last for aircraft in self: hub_to_destination = Segment(aircraft.hub, aircraft.destination) p('flight %s hub %s to destination: %s' % ( aircraft, '%s{%d, %d}' % ( aircraft.hub, aircraft.hub.lat, aircraft.hub.lon ), aircraft.destination )) p('flight %s hub %s to midpoint: %s' % ( aircraft, '%s{%d, %d}' % ( aircraft.hub, aircraft.hub.lat, aircraft.hub.lon ), arrival_midpoint )) aircraft.hookoff_point = get_hookoff( hub_to_midpoint, aircraft.destination, config.alpha ) hub_to_hookoff = Segment(aircraft.hub, aircraft.hookoff_point) aircraft.Q = hub_to_hookoff.get_length() /\ hub_to_midpoint.get_length() p('flight %s, hub %s to hook-off point: %s' % ( aircraft, '%s{%d, %d}' % ( aircraft.hub, aircraft.hub.lat, aircraft.hub.lon ), aircraft.hookoff_point )) aircraft.hookoff_point.name = 'hookoff-%s' % aircraft.hookoff_point # Place aircraft in order, ascending with Q, to fulfill LIFO condition. formation = sorted(self, key = lambda item: item.Q) # All aircraft at the front of the formation having the same destination # should hook off where the previous buddy (having a different # destination) hooked off. # Example: formation AMS-SFO, BRU-SFO, LHR-ATL. # AMS-SFO and BRU-SFO should hook off where LHR-ATL hooked off. # @todo Let AMS-SFO and BRU-SFO continue together along a new average # formation trajectory (in this case directly to the destination) # First find the leading set of aircraft having the same destination formation.reverse() leading_destination = formation[0].destination leaders = [] for aircraft in formation: # Start with always incurring benefits aircraft.incurs_benefits = True if not aircraft.destination.coincides(leading_destination): aircraft.is_leader = False continue aircraft.is_leader = True # Only the first leader incurs no benefits at all if len(leaders) == 0: aircraft.incurs_benefits = False leaders.append(aircraft) p('Leaders of formation %s are %s' % ( formation, leaders )) # Then find the buddy just before the set of leading aircraft, if # it exists. try: # The leaders: same hookoff point as last buddy. last_buddy = formation[len(leaders)] for aircraft in leaders: aircraft.Q = last_buddy.Q #aircraft.P = last_buddy.P aircraft.hookoff_point = last_buddy.hookoff_point except IndexError: pass # Change reversed formation back to normal formation.reverse() for aircraft in formation: p('Adjusting waypoints of %s. Initial waypoints: %s' % ( aircraft, aircraft.route.waypoints )) aircraft.route.waypoints = [ #aircraft.hub, aircraft.hookoff_point, aircraft.destination] aircraft.route.init_segments() p('Adjusted waypoints of %s. New waypoints: %s' % ( aircraft, aircraft.route.waypoints )) p('Need to calibrate aircraft %s (%s) in formation %s' % ( aircraft, aircraft.route, formation )) aircraft.controller.calibrate()
def get_trunk_route(hub, formation): destinations = [] for aircraft in formation: destinations.append(aircraft['route'].waypoints[-1]) mid_d = midpoint(destinations) return Segment(hub, mid_d)
def calibrate(self): """Determines the trunk route and hookoff points""" # Determine formation trunk route destinations = [] for aircraft in self: destinations.append(aircraft.destination) arrival_midpoint = midpoint(destinations) p('destinations: %s' % destinations) p('midpoint = %s' % arrival_midpoint) hub_to_midpoint = Segment(aircraft.hub, arrival_midpoint) # Determine hookoff point for each aircraft, except the last for aircraft in self: hub_to_destination = Segment(aircraft.hub, aircraft.destination) p('flight %s hub %s to destination: %s' % (aircraft, '%s{%d, %d}' % (aircraft.hub, aircraft.hub.lat, aircraft.hub.lon), aircraft.destination)) p('flight %s hub %s to midpoint: %s' % (aircraft, '%s{%d, %d}' % (aircraft.hub, aircraft.hub.lat, aircraft.hub.lon), arrival_midpoint)) aircraft.hookoff_point = get_hookoff(hub_to_midpoint, aircraft.destination, config.alpha) hub_to_hookoff = Segment(aircraft.hub, aircraft.hookoff_point) aircraft.Q = hub_to_hookoff.get_length() /\ hub_to_midpoint.get_length() p('flight %s, hub %s to hook-off point: %s' % (aircraft, '%s{%d, %d}' % (aircraft.hub, aircraft.hub.lat, aircraft.hub.lon), aircraft.hookoff_point)) aircraft.hookoff_point.name = 'hookoff-%s' % aircraft.hookoff_point # Place aircraft in order, ascending with Q, to fulfill LIFO condition. formation = sorted(self, key=lambda item: item.Q) # All aircraft at the front of the formation having the same destination # should hook off where the previous buddy (having a different # destination) hooked off. # Example: formation AMS-SFO, BRU-SFO, LHR-ATL. # AMS-SFO and BRU-SFO should hook off where LHR-ATL hooked off. # @todo Let AMS-SFO and BRU-SFO continue together along a new average # formation trajectory (in this case directly to the destination) # First find the leading set of aircraft having the same destination formation.reverse() leading_destination = formation[0].destination leaders = [] for aircraft in formation: # Start with always incurring benefits aircraft.incurs_benefits = True if not aircraft.destination.coincides(leading_destination): aircraft.is_leader = False continue aircraft.is_leader = True # Only the first leader incurs no benefits at all if len(leaders) == 0: aircraft.incurs_benefits = False leaders.append(aircraft) p('Leaders of formation %s are %s' % (formation, leaders)) # Then find the buddy just before the set of leading aircraft, if # it exists. try: # The leaders: same hookoff point as last buddy. last_buddy = formation[len(leaders)] for aircraft in leaders: aircraft.Q = last_buddy.Q #aircraft.P = last_buddy.P aircraft.hookoff_point = last_buddy.hookoff_point except IndexError: pass # Change reversed formation back to normal formation.reverse() for aircraft in formation: p('Adjusting waypoints of %s. Initial waypoints: %s' % (aircraft, aircraft.route.waypoints)) aircraft.route.waypoints = [ #aircraft.hub, aircraft.hookoff_point, aircraft.destination ] aircraft.route.init_segments() p('Adjusted waypoints of %s. New waypoints: %s' % (aircraft, aircraft.route.waypoints)) p('Need to calibrate aircraft %s (%s) in formation %s' % (aircraft, aircraft.route, formation)) aircraft.controller.calibrate()