def please(n, airport_thing): if n < 0 or n > len(airport_thing["list_of_airports"]): return 0 # if you're trying to access a bad index just return if Route.calculate_distance(airport_thing["list_of_airports"][n - 1], airport_thing["list_of_airports"] [n - 2]) > airport_thing["plane"].getRange(): return 0 # likewise, if you're trying to go somewhere you can't just return airport_thing["signature"] += list_of_airports[n].getName( ) + ":" + list_of_airports[n + 1].getName() # but otherwise, update the airport thing signature for name in airport_names: if name not in signature or list_of_airports[n].getName() != home: continue else: return airport_thing original_airport_thing = dict(airport_thing) # we need to keep a copy of the airport thing because in one of the calls we're going to update it, and in another we're not going to airport_thing["score"] += Route.calculate_score( airport_thing[list_of_airports[n]], airport_thing[list_of_airports[n + 1]]) return min(please(n - 1, original_airport_thing), please(n - 1, airport_thing))
def build_dict_as_dictionaries(list_of_airports, possibilities_lookup, plane): for i in range(len(list_of_airports)): possibilities_lookup[list_of_airports[i].getName()] = {} for j in range(len(list_of_airports)): if i != j and Route.calculate_distance(list_of_airports[i], list_of_airports[j]) <= plane.getRange(): possibilities_lookup[list_of_airports[i].getName()][list_of_airports[j].getName()] = list_of_airports[j] return possibilities_lookup
def calculate_distance(self): distance = 0 previous_idx = 0 for detail_idx in sorted(self.details + [len(self.points)]): temp_route = Route(self.routes[previous_idx:detail_idx]) distance += temp_route.calculate_distance() previous_idx = detail_idx return distance
def build_dict(list_of_airports, possibilities_lookup, plane): for i in range(len(list_of_airports)): possibilities_lookup[list_of_airports[i].getName()] = [] for j in range(len(list_of_airports)): if i != j and Route.calculate_distance( list_of_airports[i], list_of_airports[j]) <= plane.getRange(): possibilities_lookup[list_of_airports[i].getName()].append( list_of_airports[j]) return possibilities_lookup
def buildRouteCosts(list_of_airports, home): costs = {} for airport in list_of_airports: costs[airport.getName()] = {} get_costs_from = possibilities[airport.getName()] for key in get_costs_from: costs_key = airport.getName() + ":" + key costs[airport.getName()][costs_key] = (Route.calculate_score( airport, possibilities[airport.getName()][key]), Route.calculate_distance( airport, home)) return costs
def two_opt_algorithm(route: Route, stop_value=1000): no_changes = 0 nodes = len(route) while no_changes <= stop_value: best_distance = route.calculate_distance() for i in range(nodes - 1): for k in range(i + 1, nodes): new_route = two_opt_swap(route, i, k) new_distance = new_route.calculate_distance() if new_distance < best_distance: route = new_route best_distance = new_distance no_changes = 0 else: no_changes += 1 return route
madrid = Airport("Madrid", 3.7038, 40.4168, 0.84) london = Airport("London", 0.1278, 51.5074, 1.17) moscow = Airport("Moscow", 37.618423, 55.7558, 0.5) shanghai = Airport("Shanghai", 121.4737, 31.2304, 0.4) paris = Airport("Paris", 2.3522, 48.864716, 1) hk = Airport("Hong Kong", 114.149139, 22.286394, 0.3) athens = Airport("Athens", 23.727539, 37.983810, 1) la = Airport("Los Angeles", -118.243683, 34.052235, 1.5) hawaii = Airport("Honolulu", -157.917480, 21.289373, 1.5) NYC = Airport("New York", -73.935242, 40.730610, 1.5) dublin = Airport("Dublin", -6.266155, 53.350140, 1) # dublin = Airport("Dublin", ) print("plane range is ", plane.getRange()) print(Route.calculate_distance(hk, la)) # stops = { # "Madrid": [london, dublin, madrid], # "Dublin": [london, madrid, dublin], # "London": [madrid, dublin, london] # } ''' Step 1: Produce a dictionary with possibilities. ''' #
def add_edge(self, u, v, plane): edge_key = u.name + ":" + v.name if edge_key not in self.edges: if Route.calculate_distance(u.airport, v.airport) <= plane.getRange(): self.edges[edge_key] = Route.calculate_score(u.airport, v.airport)