{"id":13595976,"url":"https://github.com/afourmy/pyTSP","last_synced_at":"2025-04-09T13:34:02.542Z","repository":{"id":54629111,"uuid":"119424777","full_name":"afourmy/pyTSP","owner":"afourmy","description":"A 2D/3D visualization of the Traveling Salesman Problem main heuristics","archived":false,"fork":false,"pushed_at":"2018-10-10T19:00:51.000Z","size":10854,"stargazers_count":168,"open_issues_count":0,"forks_count":40,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-04T22:07:37.633Z","etag":null,"topics":["3d-visualization","algorithm-visualisation","flask","flask-socketio","genetic-algorithm","genetic-algorithms","gis","heuristics","leafletjs","linear-programming","openstreetmap","travelling-salesman-problem","webgl"],"latest_commit_sha":null,"homepage":"http://mintoo.pythonanywhere.com","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/afourmy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-29T18:42:49.000Z","updated_at":"2025-04-01T01:15:39.000Z","dependencies_parsed_at":"2022-08-13T22:00:57.116Z","dependency_job_id":null,"html_url":"https://github.com/afourmy/pyTSP","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afourmy%2FpyTSP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afourmy%2FpyTSP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afourmy%2FpyTSP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afourmy%2FpyTSP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/afourmy","download_url":"https://codeload.github.com/afourmy/pyTSP/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248049843,"owners_count":21039278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3d-visualization","algorithm-visualisation","flask","flask-socketio","genetic-algorithm","genetic-algorithms","gis","heuristics","leafletjs","linear-programming","openstreetmap","travelling-salesman-problem","webgl"],"created_at":"2024-08-01T16:02:03.015Z","updated_at":"2025-04-09T13:33:58.224Z","avatar_url":"https://github.com/afourmy.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Introduction\n\nThe travelling salesman problem (TSP) asks the following question: \"Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city ?\n\npyTSP uses various approaches to solve the TSP (linear programming, construction heuristics, optimization heuristics, genetic algorithm).\nIt provides a geographical step-by-step visualization of each of these algorithms.\n\n![pyTSP](readme/intro.gif)\n\n**You can find a \u003ca href=\"http://mintoo.pythonanywhere.com/\" target=\"_blank\"\u003edemo of pyTSP here !\u003c/a\u003e (U.S cities with a population larger than 900 000 inhabitants)**\n\n# Algorithms\n\nThe following algorithms are implemented in pyTSP:\n\n- Construction heuristics\n  - Nearest neighbor\n  - Nearest insertion\n  - Farthest insertion\n  - Cheapest insertion\n- Linear programming\n- Optimization heuristics\n  - Pairwise exchange (2-opt)\n  - Node insertion\n  - Edge insertion\n- Genetic algorithm\n\n# Construction heuristics\n\n## Nearest neighbor\n\n```\n- Start from a random city.\n- Travel to the nearest unvisited city.\n- Repeat until every city has been visited.\n```\n\n![Nearest neighbor](readme/nearest_neighbor.gif)\n\n## Nearest insertion\n\n\u003cpre\u003e\n- Start from a random city.\n- Find the city closest to the partial tour, i.e the city \u003cb\u003ei\u003c/b\u003e which minimizes \u003cb\u003ed(i, j)\u003c/b\u003e\nwith \u003cb\u003ej\u003c/b\u003e a city already in the tour.\n- Insert \u003cb\u003ei\u003c/b\u003e before or after \u003cb\u003ej\u003c/b\u003e, depending on which option is shorter.\n- Repeat until every city has been visited.\n\u003c/pre\u003e\n\n![Nearest insertion](readme/nearest_insertion.gif)\n\n## Cheapest insertion\n\n\u003cpre\u003e\n- Start from a random city.\n- Find the city which insertion in the tour causes the smallest increase in length,  \ni.e the city \u003cb\u003ek\u003c/b\u003e which minimizes \u003cb\u003ed(i, k)  + d(k, j) - d(i, j)\u003c/b\u003e with \u003cb\u003e(i, j)\u003c/b\u003e an edge in the partial tour.\n- Insert \u003cb\u003ek\u003c/b\u003e between \u003cb\u003ei\u003c/b\u003e and \u003cb\u003ej\u003c/b\u003e.\n- Repeat until every city has been visited.\n\u003c/pre\u003e\n\n![Cheapest insertion](readme/cheapest_insertion.gif)\n\n## Farthest insertion\n\n\u003cpre\u003e\n- Start from a random city.\n- Find the city \u003cb\u003ek\u003c/b\u003e farthest from any node in the tour (i.e the city \u003cb\u003ek\u003c/b\u003e which maximizes \u003cb\u003ed(c, k)\u003c/b\u003e with \u003cb\u003ec\u003c/b\u003e\na city in the partial tour), and insert \u003cb\u003ek\u003c/b\u003e where it causes the smallest increase in length \n(by minimizing \u003cb\u003ed(i, k)  + d(k, j) - d(i, j)\u003c/b\u003e, with \u003cb\u003e(i, j)\u003c/b\u003e an edge in the partial tour).  \n- Repeat until every city has been visited.\n\u003c/pre\u003e\n\n![Farthest insertion](readme/farthest_insertion.gif)\n\n# Linear programming\n\n![First constraints](readme/linear_programming.png)\n\n![Example of disjoint subtours](readme/subtours.png)\n\n![Subtour constraint](readme/linear_programming_subtour_constraint.png)\n\n![Final solution](readme/lp_solution.png)\n\n**Note**: there is an exponentially growing number of subtour constraints, which makes this algorithm inefficient for larger instances of the TSP. One way to improve it is to use lazy constraints, i.e ignore the subtour constraints and eliminate them one by one when looking for a feasible solution.\n\n# Optimization heuristics\n\n## Pairwise exchange (2-opt)\n\n![Pairwise exchange](readme/pairwise_exchange.png)\n\n```\n- Given a pair of edges, there is only one way of deleting and reconnecting the edges to obtain\na valid tour. If this new tour is shorter, make the change.\n- Repeat for any pair of edges until no further improvement can be made.\n```\n\n![Pairwise exchange](readme/pairwise_exchange.gif)\n\n## Node insertion\n\n![Node insertion](readme/node_insertion.png)\n\n```\n- Given a node, remove it from the tour and insert it at the best possible position.\n- Repeat for any node until no further improvement can be made.\n```\n\n![Node insertion](readme/node_insertion.gif)\n\n## Edge insertion\n\n![Edge insertion](readme/edge_insertion.png)\n\n```\n- Given an edge, remove it from the tour and insert it at the best possible position.\n- Repeat for any edge until no further improvement can be made.\n```\n\n![Edge insertion](readme/edge_insertion.gif)\n\n# Genetic algorithm\n\npyTSP implements a genetic algorithm with the following properties:\n- 3 mutation methods: random swap, insertion or displacement.\n- 3 crossover methods: order, maximally preservative, or partially mapped.\n- Selection: at each generation, 30 individuals are chosen randomly, and the 10 best are kept for the next generation.\n- Mutation and crossover rates default to 50%. They can be modified with sliders.\n\n![Genetic algorithm](readme/genetic_algorithm.gif)\n\n**Note**: the genetic algorithm is processed by the server, and websockets (or long-polling if the server does not support websockets) are used to update the display client-side every new generation.\n\n# Getting started\n\nThe following modules are used in pyTSP:\n```\nflask\nflask_socketio (sockets)\nflask_sqlalchemy (database)\nnumpy (linear programming)\ncvxopt (linear programming)\nxlrd (graph import)\n```\n\nIn order to use pyTSP, you need to:\n    \n- (optional) set up a [virtual environment](https://docs.python.org/3/library/venv.html) .\n\n- clone pyTSP (or download as a zip archive from github)\n```\ngit clone https://github.com/afourmy/pyTSP.git\n```\n    \n- install the requirements\n```\ncd pyTSP\npip install -r requirements.txt\n```\n\n- run **/flask_app.py**.\n```\npython flask_app.py\n```\n\n- go the http://127.0.0.1:5000/.\n\n# Credits \n\n[Bootstrap](https://getbootstrap.com/ \"Bootstrap\"): Front-end HTML/CSS framework.\n\n[Bootstrap slider](http://seiyria.com/bootstrap-slider/): A slider component for Bootstrap.\n\n[CVXOPT](https://github.com/cvxopt/cvxopt): A library for convex optimization.\n\n[Flask](http://flask.pocoo.org/ \"Flask\"): A microframework based on the Werkzeug toolkit and Jinja2 template engine.\n\n[Flask SQLAlchemy](http://flask-sqlalchemy.pocoo.org/ \"Flask SQLAlchemy\"): Adds support for SQLAlchemy to Flask.\n\n[Jquery](https://jquery.com/ \"Jquery\"): JavaScript library designed to simplify the client-side scripting of HTML.\n\n[Leaflet](http://leafletjs.com/ \"Leaflet\"): JavaScript library for mobile-friendly interactive maps.\n\n[OpenStreetMap](https://www.openstreetmap.org/ \"OpenStreetMap\"): Collaborative project to create a free editable map of the world.\n\n[WebGL Earth](http://www.webglearth.org \"WebGL Earth\"): 3D digital globe for web and mobile devices.\n\n[xlrd](https://github.com/python-excel/xlrd): Library to extract data from Microsoft Excel (tm) spreadsheet files.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafourmy%2FpyTSP","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fafourmy%2FpyTSP","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafourmy%2FpyTSP/lists"}