Computing with a distance matrix

import numpy as np def relative_distance(city_pairs_of_interest): route_relationships = ['shorter', 'longer'] from1, to1 = city_pairs_of_interest[:2] from2, to2 = city_pairs_of_interest[2:] from1_idx, to1_idx = cities.index(from1), cities.index(to1) from2_idx, to2_idx = cities.index(from2), cities.index(to2) route1 = '-'.join([from1, to1]) route2 = '-'.join([from2, to2]) route_km_difference = distance_matrix[from1_idx, to1_idx] - distance_matrix[from2_idx, to2_idx] return 'The route %s is %.2fkm %s than the route %s.' % ( route1, abs(route_km_difference), route_relationships[0 if route_km_difference < 0 else 1], route2 ) def distance_between(city1, city2): return distance_matrix[cities.index(city1), cities.index(city2)] def fuel_cost(cities, car_model, car_miles_per_gallon_combined, fuel_dollars_per_gallon): mile_to_km = 1.609344 total_distance = 0 for i in range(1, len(cities)): from_city, to_city = cities[i-1:i+1] total_distance += distance_between(from_city, to_city) total_distance_miles = total_distance / mile_to_km fuel_gallons_needed = total_distance_miles / car_miles_per_gallon_combined total_fuel_price = fuel_gallons_needed * fuel_dollars_per_gallon return total_distance, total_fuel_price cities = ['Austin', 'Atlanta', 'Milwaukee', 'Los Angeles', 'Chicago', 'San Antonio', 'Cincinnatti', 'Portland', 'Denver', 'Santa Clara', 'San Francisco', 'Dallas', 'Orlando', 'Cleveland', 'Boston', 'Pittsburgh', 'Seattle'] cities_len = len(cities) distances_km = [ ('Austin', 'Atlanta', 1539.17), ('Austin', 'Milwaukee', 1314.5), ('Austin', 'Los Angeles', 3500.63), ('Austin', 'Chicago', 1869.24), ('Austin', 'San Antonio', 128.44), ('Austin', 'Cincinnatti', 1814.47), ('Austin', 'Portland', 3304.33), ('Austin', 'Denver', 1480.44), ('Austin', 'Santa Clara', 2771.19), ('Austin', 'San Francisco', 2830.46), ('Austin', 'Dallas', 314.2), ('Austin', 'Orlando', 1813.77), ('Austin', 'Cleveland', 2215.04), ('Austin', 'Boston', 3159.75), ('Austin', 'Pittsburgh', 2277.8), ('Austin', 'Seattle', 3423.63), ('Atlanta', 'Milwaukee', 1314.5), ('Atlanta', 'Los Angeles', 3500.63), ('Atlanta', 'Chicago', 1156.92), ('Atlanta', 'San Antonio', 1590.23), ('Atlanta', 'Cincinnatti', 741.31), ('Atlanta', 'Portland', 4175.86), ('Atlanta', 'Denver', 2259.72), ('Atlanta', 'Santa Clara', 3922.02), ('Atlanta', 'San Francisco', 3981.29), ('Atlanta', 'Dallas', 1257.67), ('Atlanta', 'Orlando', 706.33), ('Atlanta', 'Cleveland', 1141.87), ('Atlanta', 'Boston', 1732.7), ('Atlanta', 'Pittsburgh', 1100.57), ('Atlanta', 'Seattle', 4242.77), ('Milwaukee', 'Los Angeles', 3308.13), ('Milwaukee', 'Chicago', 148.26), ('Milwaukee', 'San Antonio', 2052.23), ('Milwaukee', 'Cincinnatti', 631.64), ('Milwaukee', 'Portland', 3322.73), ('Milwaukee', 'Denver', 1680.83), ('Milwaukee', 'Santa Clara', 3539.24), ('Milwaukee', 'San Francisco', 3489.45), ('Milwaukee', 'Dallas', 1612.39), ('Milwaukee', 'Orlando', 4887.98), ('Milwaukee', 'Cleveland', 701.07), ('Milwaukee', 'Boston', 1728.7), ('Milwaukee', 'Pittsburgh', 888.02), ('Milwaukee', 'Seattle', 3205.35), ('Los Angeles', 'Chicago', 3243.7), ('Los Angeles', 'San Antonio', 2176.76), ('Los Angeles', 'Cincinnatti', 3498.18), ('Los Angeles', 'Portland', 1549.36), ('Los Angeles', 'Denver', 1634.94), ('Los Angeles', 'Santa Clara', 555.27), ('Los Angeles', 'San Francisco', 618.36), ('Los Angeles', 'Dallas', 2311.7), ('Los Angeles', 'Orlando', 4038.96), ('Los Angeles', 'Cleveland', 3773.41), ('Los Angeles', 'Boston', 4801.03), ('Los Angeles', 'Pittsburgh', 3904.22), ('Los Angeles', 'Seattle', 1826.83), ('Chicago', 'San Antonio', 1997.61), ('Chicago', 'Cincinnatti', 478.36), ('Chicago', 'Portland', 3410.33), ('Chicago', 'Denver', 1615.12), ('Chicago', 'Santa Clara', 3473.53), ('Chicago', 'San Francisco', 3423.74), ('Chicago', 'Dallas', 1556.66), ('Chicago', 'Orlando', 1864.15), ('Chicago', 'Cleveland', 553.73), ('Chicago', 'Boston', 1581.35), ('Chicago', 'Pittsburgh', 740.67), ('Chicago', 'Seattle', 3321.01), ('San Antonio', 'Cincinnatti', 1940.72), ('San Antonio', 'Portland', 3333.64), ('San Antonio', 'Denver', 1501.06), ('San Antonio', 'Santa Clara', 2732.0), ('San Antonio', 'San Francisco', 2791.28), ('San Antonio', 'Dallas', 440.45), ('San Antonio', 'Orlando', 1865.41), ('San Antonio', 'Cleveland', 2341.28), ('San Antonio', 'Boston', 3290.1), ('San Antonio', 'Pittsburgh', 2404.05), ('San Antonio', 'Seattle', 3452.93), ('Cincinnatti', 'Portland', 3818.05), ('Cincinnatti', 'Denver', 1919.75), ('Cincinnatti', 'Santa Clara', 3881.25), ('Cincinnatti', 'San Francisco', 3831.46), ('Cincinnatti', 'Dallas', 1503.45), ('Cincinnatti', 'Orlando', 1447.12), ('Cincinnatti', 'Cleveland', 401.07), ('Cincinnatti', 'Boston', 1420.44), ('Cincinnatti', 'Pittsburgh', 463.84), ('Cincinnatti', 'Seattle', 3745.89), ('Portland', 'Denver', 1999.53), ('Portland', 'Santa Clara', 1071.79), ('Portland', 'San Francisco', 1022.0), ('Portland', 'Dallas', 3278.53), ('Portland', 'Orlando', 4878.81), ('Portland', 'Cleveland', 3942.55), ('Portland', 'Boston', 4879.18), ('Portland', 'Pittsburgh', 4129.5), ('Portland', 'Seattle', 279.33), ('Denver', 'Santa Clara', 2056.89), ('Denver', 'San Francisco', 2013.15), ('Denver', 'Dallas', 1284.04), ('Denver', 'Orlando', 2961.7), ('Denver', 'Cleveland', 2145.67), ('Denver', 'Boston', 3173.29), ('Denver', 'Pittsburgh', 2323.43), ('Denver', 'Seattle', 2119.03), ('Santa Clara', 'San Francisco', 72.83), ('Santa Clara', 'Dallas', 2731.31), ('Santa Clara', 'Orlando', 4594.2), ('Santa Clara', 'Cleveland', 4011.87), ('Santa Clara', 'Boston', 5039.44), ('Santa Clara', 'Pittsburgh', 4198.81), ('Santa Clara', 'Seattle', 1350.48), ('San Francisco', 'Dallas', 2789.26), ('San Francisco', 'Orlando', 4652.15), ('San Francisco', 'Cleveland', 3961.34), ('San Francisco', 'Boston', 4988.96), ('San Francisco', 'Pittsburgh', 4148.28), ('San Francisco', 'Seattle', 1299.95), ('Dallas', 'Orlando', 1804.37), ('Dallas', 'Cleveland', 1901.11), ('Dallas', 'Boston', 2845.83), ('Dallas', 'Pittsburgh', 1963.88), ('Dallas', 'Seattle', 3394.3), ('Orlando', 'Cleveland', 1670.36), ('Orlando', 'Boston', 2069.94), ('Orlando', 'Pittsburgh', 1561.26), ('Orlando', 'Seattle', 4946.93), ('Cleveland', 'Boston', 1029.64), ('Cleveland', 'Pittsburgh', 214.41), ('Cleveland', 'Seattle', 3872.14), ('Boston', 'Pittsburgh', 920.51), ('Boston', 'Seattle', 4900.8), ('Pittsburgh', 'Seattle', 4072.31), ] distance_matrix = np.zeros((cities_len, cities_len)) for from_city, to_city, dist in distances_km: from_idx, to_idx = cities.index(from_city), cities.index(to_city) distance_matrix[from_idx, to_idx] = distance_matrix[to_idx, from_idx] = dist print(distance_matrix) Distance matrix for selected US cities # Which distance is shorter: Atlanta-Milwaukee or Austin-Denver? city_pairs_of_interest = ['Atlanta', 'Milwaukee', 'Austin', 'Denver'] print(relative_distance(city_pairs_of_interest)) # The route Atlanta-Milwaukee is 165.94km shorter than the route Austin-Denver. # Which distance is shorter: Cleveland-Austin or Los Angeles-Seattle? city_pairs_of_interest = ['Cleveland', 'Austin', 'Los Angeles', 'Seattle'] print(relative_distance(city_pairs_of_interest)) # The route Cleveland-Austin is 388.21km longer than the route Los Angeles-Seattle. # How long is the distance between Dallas and Denver? print("%.2fkm" % (distance_between('Dallas', 'Denver'))) # 1284.04km # How much would it cost to travel Chicago->Denver->Atlanta->Dallas->Austin with 'Toyota Corolla 2.0L (2019), assuming we traveled on straight line, the combined vehicle's MPG is 36 and the gasoline cost is 2.67$/gallon? car = '2019 Toyota Corolla Hatchback 2.0 L, 4 cyl, Automatic (AV-S10), Regular Gasoline' car_miles_per_gallon, car_fuel_price_per_gallon = 36, 2.67 cities_of_interest = ['Chicago', 'Denver', 'Atlanta', 'Dallas', 'Austin'] total_dist, total_fuel_price = fuel_cost( cities_of_interest, car, car_miles_per_gallon, car_fuel_price_per_gallon ) print('Traveling the %.2fkm on the route %s with the car %s having an MPG of %d at fuel price of %.2f$/gallon would cost %.2f$.' % (total_dist, '->'.join(cities_of_interest), car, car_miles_per_gallon, car_fuel_price_per_gallon, total_fuel_price)) # Traveling the 5446.71km on the route Chicago->Denver->Atlanta->Dallas->Austin with the car 2019 Toyota Corolla Hatchback 2.0 L, 4 cyl, Automatic (AV-S10), Regular Gasoline having an MPG of 36 at fuel price of 2.67$/gallon would cost 251.01$.