{"id":16323594,"url":"https://github.com/im-rises/travelling_salesman_problem_lp","last_synced_at":"2026-04-30T10:02:33.619Z","repository":{"id":37452922,"uuid":"494450306","full_name":"Im-Rises/travelling_salesman_problem_lp","owner":"Im-Rises","description":"Travelling Salesman Problem Solver made in Python with Google OR-Tools simplex linear programming.","archived":false,"fork":false,"pushed_at":"2023-01-07T21:59:56.000Z","size":215,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-17T04:24:40.380Z","etag":null,"topics":["or-tools","python","tsp","tsp-problem","tsp-solver"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Im-Rises.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-20T12:11:51.000Z","updated_at":"2023-05-10T17:05:00.000Z","dependencies_parsed_at":"2022-08-19T12:40:59.014Z","dependency_job_id":null,"html_url":"https://github.com/Im-Rises/travelling_salesman_problem_lp","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/Im-Rises%2Ftravelling_salesman_problem_lp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Im-Rises%2Ftravelling_salesman_problem_lp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Im-Rises%2Ftravelling_salesman_problem_lp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Im-Rises%2Ftravelling_salesman_problem_lp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Im-Rises","download_url":"https://codeload.github.com/Im-Rises/travelling_salesman_problem_lp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254160636,"owners_count":22024574,"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":["or-tools","python","tsp","tsp-problem","tsp-solver"],"created_at":"2024-10-10T22:55:12.548Z","updated_at":"2026-04-30T10:02:28.599Z","avatar_url":"https://github.com/Im-Rises.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# travelling_salesman_problem_lp\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Python-3776AB?style=for-the-badge\u0026logo=python\u0026logoColor=white\" alt=\"pythonLogo\" style=\"height:50px\"/\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/59691442/174799409-9bd85fe6-b58e-4ba3-a4a4-cdfaaa5b91f0.jpg\" alt=\"googleOrtoolsLogo\" style=\"height:50px\"/\u003e\n\u003c/p\u003e\n\n## Description\n\nTravelling salesman problem solver using linear programming with Google Or-Tools.\n\nThe Travelling Salesman Problem also known as TSP is an NP-hard problem in combinatorial optimization.  \nImagine a set of city disposed on a map, you have a set of salesman (population) and they must all go to every city in\nthe least amount of time/distance.  \nThe optimization solution is the one where a salesman goes through all the cities with the least distance or/and time.\n\nIn the image below you can see a representation of the tsp problem with cities named A, B, C. Going from a city to\nanother take more or less time than other depending on the distance.\n\n\u003c!-- \u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/59691442/165635831-5bfc72b5-0dd3-4a9f-afb0-b5ffd402ee88.png\" alt=\"tspExampleImage\" style=\"height:400px\"/\u003e\n\u003c/p\u003e --\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/59691442/175610459-6ff46e53-08f3-45a1-b2c6-3d17761158c7.png\" alt=\"tspExampleImage\" style=\"height:300px\"/\u003e\n\u003c/p\u003e\n\n\u003e **Note**  \n\u003e We also implemented a version using genetic algorithm:  \n\u003e https://github.com/Im-Rises/travelling-salesman-problem-ga\n\n## Problem modeling\n\nBelow is the problem modeling in Linear Programming.\n\n- Parameters:\n    1. $i,j$: indices on set V of customers\n    2. $d_{i,j}$: distance between customers i and j\n\n- Variables: for each $i \\ne j \\in V, x_{i,j} = 1$ if the salesman travels directly from i to j and 0 otherwise.\n\n- Objective function:\n\n$$min {\\sum_{i \\ne j \\in V}}{d_{ij} x{ij}}$$\n\n- Constraints\n\n    1. (one successor) $$\\forall i \\in V \\sum x_{i,j} = 1$$\n    2. (one predecessor) $$\\forall j \\in V \\sum x_{i \\in V,i \\ne j} = 1$$\n    3. (sub-tour elimination) $$\\forall S \\nsubseteq V {\\sum_{i \\in S,j \\in V \\backslash S}}{x_{ij}} \\geq 1$$\n\n\u003c!--\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/59691442/169556846-231900f0-2195-478d-be14-0990f52ea1b4.png\" alt=\"tspExampleImage\" style=\"height:400px\"/\u003e\n\u003c/p\u003e\n--\u003e\n\n## Linear Programming\n\nLinear programming implementation is completely set in the solve function.\n\nOutput :\n\n```\nObjective value = 11669 miles\nRoute:\nSydney -\u003e S.C.G. -\u003e Carrara -\u003e Gabba -\u003e Riverway Stadium -\u003e Cazaly's Stadium -\u003e Marrara Oval -\u003e Traeger Park -\u003e Perth Stadium -\u003e Adelaide Oval -\u003e Eureka Stadium -\u003e Kardinia Park -\u003e Docklands -\u003e M.C.G. -\u003e York Park -\u003e Bellerive Oval -\u003e Manuka Oval -\u003e Sydney\n```\n\n## Routing implementation\n\nTo verify the app output result from the linear programming. We created a file named `example_with_routings.py` that will\nsolve our tsp problem but\nby using routing.\n\nOutput :\n\n```\nObjective value = 11669 miles\nRoute:\nSydney -\u003e S.C.G. -\u003e Carrara -\u003e Gabba -\u003e Riverway Stadium -\u003e Cazaly's Stadium -\u003e Marrara Oval -\u003e Traeger Park -\u003e Perth Stadium -\u003e Adelaide Oval -\u003e Eureka Stadium -\u003e Kardinia Park -\u003e Docklands -\u003e M.C.G. -\u003e York Park -\u003e Bellerive Oval -\u003e Manuka Oval -\u003e Sydney\n```\n\n## Quick start\n\n### Use the linear_prog_res.py script\n\nYou need python3 to start the app. you also need some packages that are listed in the `requirements.txt`.  \nTo install them all type the following command :\n\n```bash\npip install -r requirements.txt\n```\n\nYou can then start the program by starting the `linear_prog_res.py` file like below:\n\n```bash\npy linear_prog_res.py \u003ccity\u003e \u003cpath/to/excel\u003e \u003csheet_name\u003e\n```\n\nExample :\n\n```bash\npy linear_prog_res.py \"Sydney\" data.xlsx sheet1 \n```\n\n### Use the routings_res.py script\n\nThis script use routing system to solve the problem.\n\nTo start it, use it exactly the same way as the `linear_prog_res.py` script.\n\n```bash\npy routings_res.py \u003ccity\u003e \u003cpath/to/excel\u003e \u003csheet_name\u003e\n```\n\nExample :\n\n```bash\npy routings_res.py \"Sydney\" data.xlsx sheet1\n```\n\n## Unit test\n\nTo start the test file just run the following command :\n\n```bash\npy unit_test.py\n```\n\nAn assertion will be returned if any difference of output happen between the linear programming script and the routing\nscript.\n\nA GitHub actions is set with the project explained below.\n\n## GitHub actions\n\n[![Python application](https://github.com/Im-Rises/travelling_salesman_problem_lp/actions/workflows/python-app.yml/badge.svg?branch=main)](https://github.com/Im-Rises/travelling_salesman_problem_lp/actions/workflows/python-app.yml)\n\nThe app state is verified with a test script detailed in the sections `Start Unit Test Python script`.\n\nA GitHub actions workflow is set to verify the good behaviour of the script while creating a pull request to the main\nbranch.\n\n### Use another Excel file\n\nOur project contains an Excel file that is load by our app.\nThe Excel contains the distances of all cities relatively from each other.\n\n**CSV Example :**\n\nThe Excel file you load send as a parameter at the script needs to be formed like below:\n\n|               | M.C.G. | Docklands | Adelaide Oval |\n|---------------|--------|-----------|---------------|\n| M.C.G.        | 0      | 3         | 657           |\n| Docklands     | 3      | 0         | 654           |\n| Adelaide Oval | 657    | 654       | 0             |\n\nThe Excel is loaded in our scripts, it will search the city by the name you set as starting city. The script will\ninternally move the row and column of the target city at index [0,0] of the internal array to start the travel from this city.\n\nFor example, if you want to begin your travel from Adelaide Oval, the data will look like below :\n\n**CSV internal script modification example :**\n\n|               | M.C.G. | Docklands | Adelaide Oval |\n|---------------|--------|-----------|---------------|\n| M.C.G.        | 0      | 3         | 657           |\n| Docklands     | 3      | 0         | 654           |\n| Adelaide Oval | 657    | 654       | 0             |\n\nwill become:\n\n|               | Adelaide Oval | M.C.G. | Docklands |\n|---------------|---------------|--------|-----------|\n| Adelaide Oval | 0             | 657    | 654       |\n| M.C.G.        | 657           | 0      | 3         |\n| Docklands     | 654           | 3      | 0         |\n\n## Documentations and API\n\nGoogle Or-Tools :  \n\u003chttps://developers.google.com/optimization/\u003e\n\nTSP solvers Or-Tools :  \n\u003chttps://developers.google.com/optimization/routing/tsp\u003e\n\nTSP Linear Programming solver :  \n\u003chttps://hal.archives-ouvertes.fr/hal-02947086/document\u003e\n\n## Contributors\n\nQuentin Morel :\n\n- @Im-Rises\n- \u003chttps://github.com/Im-Rises\u003e\n\nClément Reiffers :\n\n- @clementreiffers\n- \u003chttps://github.com/clementreiffers\u003e\n\n[![GitHub contributors](https://contrib.rocks/image?repo=im-rises/travelling_salesman_problem_lp)](https://github.com/im-rises/travelling_salesman_problem_lp/graphs/contributors)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fim-rises%2Ftravelling_salesman_problem_lp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fim-rises%2Ftravelling_salesman_problem_lp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fim-rises%2Ftravelling_salesman_problem_lp/lists"}