コード例 #1
0
def traverse(start_node, subset, flight_record):

    # next time through, start_node is next in subset if not in visited

    flight_record["previously_visited"].append(start_node.getName())

    if flight_record["previous_airport"] == None:
        flight_record["previous_airport"] = start_node
    else:
        flight_record["previous_airport"] = flight_record["current_airport"]
    flight_record["current_airport"] = start_node

    if len(subset) == 0:
        start = flight_record["current_airport"]
        previous = flight_record["previous_airport"]
        score = Route.calculate_score(flight_record["previous_airport"],
                                      flight_record["current_airport"])
        flight_record[
            "accumulated_score"] = flight_record["accumulated_score"] + score
        return flight_record

    for airport in subset:
        start = flight_record["current_airport"]
        previous = flight_record["previous_airport"]
        flight_record = traverse(airport, [
            x for x in stops[airport.getName()]
            if x.getName() not in flight_record["previously_visited"]
        ], flight_record)
        score = Route.calculate_score(previous, start)
        flight_record[
            "accumulated_score"] = flight_record["accumulated_score"] + score

        return flight_record
コード例 #2
0
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))
コード例 #3
0
def buildRouteCosts(list_of_airports):
    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])

    return costs
コード例 #4
0
berlin = Airport("Berlin", 13.4050, 13.4050, 1.99)

stops = {
    "Madrid": [london, dublin, madrid],
    "Dublin": [london, madrid, dublin],
    "London": [madrid, dublin, london]
}

flight_record = {
    "previously_visited": [],
    "current_airport": None,
    "previous_airport": None,
    "accumulated_score": 0
}

what_we_want = Route.calculate_score(madrid, london) + Route.calculate_score(
    london, dublin)


def traverse(start_node, subset, flight_record):

    # next time through, start_node is next in subset if not in visited

    flight_record["previously_visited"].append(start_node.getName())

    if flight_record["previous_airport"] == None:
        flight_record["previous_airport"] = start_node
    else:
        flight_record["previous_airport"] = flight_record["current_airport"]
    flight_record["current_airport"] = start_node
コード例 #5
0
    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)