Skip to content

Instantly share code, notes, and snippets.

@aialenti
Last active December 13, 2019 17:08
Show Gist options
  • Save aialenti/3391a70fd5ce3ec73bdbbf540137d33f to your computer and use it in GitHub Desktop.
Save aialenti/3391a70fd5ce3ec73bdbbf540137d33f to your computer and use it in GitHub Desktop.
# Import Pandas
import pandas as pd
# Import PuLP modeler functions
from pulp import *
# Math functions for distance calculation
import math
# Networkx to get connected components and subtours
import networkx as nx
# Matplotlib for debugging
import matplotlib.pyplot as plt
# Visually see loops progression
from tqdm import tqdm
# To measure the optimizataion time
import time
class TSP():
cities = None
santa = None
variables_dict = None
x = None
path = None
sec_constraints = 0
execution_time = 0
def __init__(self, population_lower_bound=10e5 * 3):
cities = pd.read_csv("worldcities.csv")
self.cities = cities.loc[cities["population"] >= population_lower_bound][
["lat", "city", "lng", "population"]].reset_index()
# Add Santa's house
santa_df = pd.DataFrame([["Santa's House", 66.550331132, 25.886996452, 10000]],
columns=["city", "lat", "lng", "population"])
# Add other cities with smaller population to improve the World coverage
reykjavik = cities.loc[(cities["city_ascii"] == "Reykjavik") & (cities["country"] == "Iceland")]
algiers = cities.loc[(cities["city_ascii"] == "Algiers") & (cities["country"] == "Algeria")]
brazzaville = cities.loc[(cities["city_ascii"] == "Brazzaville")]
dublin = cities.loc[(cities["city"] == "Dublin") & (cities["country"] == "Ireland")]
guatemala_city = cities.loc[(cities["city_ascii"] == "Guatemala City")]
ulaanbaatar = cities.loc[(cities["city_ascii"] == "Ulaanbaatar")]
self.cities = pd.concat(
[self.cities, santa_df, dublin, reykjavik, algiers,
brazzaville, guatemala_city, ulaanbaatar]).reset_index()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment