Skip to content

fcoggins/travelling-saleswoman

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Travelling Saleswoman


Heroku Deployment: http://tspapp.herokuapp.com/

The Developer: https://www.linkedin.com/in/fcoggins

The Travelling Saleswoman provides a tool to visualize the classic computer science travelling salesman optimization problem. This visualization tool provides the ability to select a subset of US cities. Choices of "Nearest Neighbor", "Hillclimb" and "Simulated Annealing" algorithms allow comparisons of various solution methods. Solution modes include "As-the-crow-flies", "Drive", or "Fly" with the latter two modes utilizing Directions from Google Directions API and Flight data from Google's QPX Express API.

Technology Stack

  • Python
  • Flask
  • Google Maps API
  • Google Directions API
  • QPX Express API
  • PostgrSQL/SQLAlchemy
  • Javascript/JQuery/JQuery UI
  • HTML/CSS/Bootstrap

File Guide

  • model.py
  • tsp.py: calculate distance matrix, solution algorithms
  • tspapp.py: controls the Flask app
  • static/js/tsp.js: user interactions

Solution Modes

  • As the Crow Flies

    Distance between cities is calculated using the spherical law of cosines to approximate the curvature of the earth.

  • Drive

    The Google Directions API was used to find the best route between two cities. The JSON request sent latitude and longitude for the city. The returned data included total road distance between the endpoints and encoded polyline data to use for drawing the route and is stored in a PostgreSQL database. Threading is used to load data in the background at runtime.

  • Results show total tour miles and tour cost assuming $.56 per mile to drive and $30/hour for our saleswomn's time.

  • Fly Commercial

    Google's QPX Express API was queried for December 15, 2014 and the 10 least expensive routes were returned. The cost and time for each of the 10 options was stored in the PostgreSQL database. At run time, the best option is selected based on the optimum combination of fare and time. This weight was applied to the edges for optimization. The distance matrix in this mode is not symetric since the cost from city A to B does not necessarily equal the cost from city B to A.

    Total cost was calculated by adding the fares for the tour and the travel cost of $30/hour.

Algorithms

Nearest Neighbor

Nearest neighbor simply takes the starting city and simplistically travels to the next available city. The process is repeated until the possibilities are exhausted and the loop is closed. This solution more often then not is far from optimum.

Hillclimb

Hillclimb explores neighboring solution states by employing a move operator. An initial random solution is found and then a neighboring state is evaluated. If the neighbor state results in a better solution then it is accepted and the process is repeated until no better solutions can be found.

The choice of move operator determines the neighbor state:

  • Swap cities - the order of two successive cities is reversed.
  • Swap edges - two edges are disconnected and reconnected to the other's endpoints.

The problem with the simple Hillclimb is that there may exist many local maxima that are not the global maxima. The Hillclimb method can get stuck at one of these. The method can be run repeatedly up to a specified number of tries a more sophisticated method can be used such as...

Simulated Annealing

In order to avoid the problem of local maxima, SA will temporarily accept less than optimal solutions. A probability function that decreases over time determines whether less than optimal solutions are chosen.

In this implementation a better solution is always selected and a probability function determines whether a sub-optimal value will be accepted. The probability function starts out liberal and becomes more conservative over time, thus narrowing in on a solution.

The steps on the journey...

  • Static as-the-crow-flies solution with random points
  • Static as-the-crow-flies solution with cities on the map
  • Visualize the solution steps by introducing animation
  • Work with Google's direction API to get road miles and polylines
  • Animate the solution for road miles
  • Optimize to improve runtime
  • Add ability to select a subset of cities from a select list or by clicking directly on the map
  • Add the Air route data

Additional Screenshots

About

One step along the road - visualizing the classic TSP.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published