{"id":18584093,"url":"https://github.com/e-candeloro/modified-tsp-optimization","last_synced_at":"2026-04-05T21:02:50.728Z","repository":{"id":114095302,"uuid":"486617304","full_name":"e-candeloro/Modified-TSP-Optimization","owner":"e-candeloro","description":"A modified Traveling Salesman Problem (TSP) optimization where a directed graph tour starting and ending at the first node is chosen so to maximize a custom objective function (net profit)","archived":false,"fork":false,"pushed_at":"2022-04-28T15:55:24.000Z","size":1747,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-16T05:37:00.410Z","etag":null,"topics":["data-visualization","graph-algorithms","gurobi","gurobipy","maximization","operational-research","optimization-algorithms","pandas","python","traveling-salesman-problem"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","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/e-candeloro.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-28T14:00:08.000Z","updated_at":"2025-03-31T04:02:54.000Z","dependencies_parsed_at":"2023-06-12T17:42:58.079Z","dependency_job_id":null,"html_url":"https://github.com/e-candeloro/Modified-TSP-Optimization","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/e-candeloro/Modified-TSP-Optimization","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-candeloro%2FModified-TSP-Optimization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-candeloro%2FModified-TSP-Optimization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-candeloro%2FModified-TSP-Optimization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-candeloro%2FModified-TSP-Optimization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/e-candeloro","download_url":"https://codeload.github.com/e-candeloro/Modified-TSP-Optimization/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-candeloro%2FModified-TSP-Optimization/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268333976,"owners_count":24233782,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["data-visualization","graph-algorithms","gurobi","gurobipy","maximization","operational-research","optimization-algorithms","pandas","python","traveling-salesman-problem"],"created_at":"2024-11-07T00:26:09.034Z","updated_at":"2025-12-30T21:52:05.840Z","avatar_url":"https://github.com/e-candeloro.png","language":"Jupyter Notebook","readme":"# Modified-TSP-Optimization\nThis is the first homework done for the course of Automated Decision Making (2021-2022) at the University of Modena and Reggio Emilia.\nIt consists of a Python script (and also a notebook used for debugging purposes) that executes modified TSP optimization algorithm, using Gurobi and other common scientific libraries such as Pandas.\n\n### Setup\nConsider a directed asymmetric graph **G = (V, A)** with vertex set **V = {0, 1, . . . , n − 1}**.\n- Vertex 0 is a depot\n- Let **c : A → Z** denote the cost of the arcs\n- Let **p : V → Z** denote the profit of the vertices\n### Example of a directed asymmetric graph\n![output](https://user-images.githubusercontent.com/67196406/165792843-c756c402-be05-426d-bc05-72b41c240ec2.png)\n\nNB: Vertex profits are represented by the blue dot dimension while the arches costs were not printed due to the high number of them. The arrows show the direction of each arch.\n\n### Objective of the homework\n**The problem asks to find a tour that starts and terminates in\nthe depot, visist a (sub)set S ⊆ V of the vertices and\nmaximizes the difference between the prizes of the vertices\nvisited and the costs of the arcs used.**\n## ALGORITHM DESCRIPTION\n**NOTE**: the main script has comments explaining thealgorithm, also in this document all the\nsupport functions used in the script for loading data, visualizing it, etc... are not described.\n### 1)Data Loading and Visualization\nInside the main() function of the script, the data for a directed graph is generated in three\npossible ways: loading the data from two CSV files, loading the data from one CSV file\n(comparison data) or generating a random directed graph.\nAfter the data generation, the graph is visualized using Matplotlib (the script will continue\nclosing the graph).\nThe three main data are: the points dict, the points profits dict and the arches dict with the\ncost associated.\n\n### 2)Model Creation\nThe Gurobi model is created and then the model variable, constraint and objective function\nare initialized.\nVariables:\n- Y_vertices = vector for selecting some or all of  the given n vertices\n- X_archs =  matrix for selecting the arches for a vertex i to a vertex j\n### 3)Constraints:\n- Depot constraint = always selects the vertex 0\n- Degree-2 constraint = every selected vertex (with the Y_vertices vector) needs to have\none arch entering it and one arch leaving it, so a degree of two\n### 4)Objective function:\nThe problem requires maximizing the total net profit: that is the difference between the total\ngains minus the total costs, where:\n- total gains = sum of all the selected  vertex profits\n- total costs = sum of all the selected arches costs\n### 5)Model Optimization using lazy constraints\nUsing the lazy constraints functionalities of Gurobi, the model is optimized applying a\nsub-tour elimination for each step. The sub-tour elimination is considered only on the\nselected vertices of that step, and it is necessary to avoid sub-tours that don’t consider the\nvertex 0 (because we can find solutions where the vertex 0 is selected, but it is inside\nanother cycle!). The toy dataset shown in the default script, shows that the sub-tour\nelimination discard multiple independent cycles as a solution.\nAfter the optimization, the results are shown printing the optimal path (as a list of selected\nvertices), the total maximum net profit obtained, the computation time and finally the graph\ntour is shown.\n\n## RESULTS\n### Test Graph\n\n![Toy graph](https://user-images.githubusercontent.com/67196406/165771780-7266bbf8-f654-4f2b-b9be-c17862bb1c90.png)\n\n### Test Graph Solution\n\n![Toy graph Solution](https://user-images.githubusercontent.com/67196406/165771951-75858949-1ea1-483d-aa36-15ae257fd60d.png)\n\n**The solution of this graph is the selection of nodes 0,1,3 with a total gain of 50 + 50 + 1 = 101 minus the arch cost 1 + 1 +1 = 3\nThe total profit is 101 - 3 = 98**\n\n### SCRIPT SOLUTION:\nOptimal tour: **(0, 1, 3)**\n\nOptimal cost: **98**\n\n## ADDITIONAL INFO\nAll the additional info can be found in the repository pdf files, as well as the notebook (.ipynb) and Python (.py) script plus the csv files for some of the graph data used.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe-candeloro%2Fmodified-tsp-optimization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fe-candeloro%2Fmodified-tsp-optimization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe-candeloro%2Fmodified-tsp-optimization/lists"}