{"id":212,"date":"2021-01-23T22:24:31","date_gmt":"2021-01-23T22:24:31","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/data-science\/?p=212"},"modified":"2021-01-23T22:25:17","modified_gmt":"2021-01-23T22:25:17","slug":"solving-the-travelling-salesman-problem-tsp-with-python","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/data-science\/solving-the-travelling-salesman-problem-tsp-with-python\/","title":{"rendered":"Solving the Travelling Salesman Problem (TSP) with Python"},"content":{"rendered":"<p>In this tutorial, we would see how to solve the TSP problem in Python. The following sections are covered.<\/p>\n<ol>\n<li><a href=\"#t1\">Approach to Solving the TSP Problem<\/a><\/li>\n<li><a href=\"#t2\">The Routing Model and Index Manager<\/a><\/li>\n<li><a href=\"#t3\">The Distance Callback<\/a><\/li>\n<li><a href=\"#t4\">Travel Cost and Search Parameters<\/a><\/li>\n<li><a href=\"#t5\">Function to the Print the Solution<\/a><\/li>\n<li><a href=\"#t6\">Putting it all Together<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Approach to Solving the TSP Problem<\/strong><\/h4>\n<p>To be able to solve a TSP problem in Python, we need the following items:<\/p>\n<ul>\n<li>List of cities<\/li>\n<li>List of distances between the cities<\/li>\n<li>Number of vehicles<\/li>\n<li>Starting location of the vehicles<\/li>\n<\/ul>\n<p><strong>List of Cities<\/strong><\/p>\n<p>In this problem we have a list of 12 cities. They are listed below. The indices of the cities are provided against the name.<\/p>\n<p><em>0. New York &#8211; 1. Los Angeles &#8211; 2. Chicago &#8211; 3. Minneapolis &#8211; 4. Denver &#8211; 5. Dallas &#8211; 6. Seattle &#8211; 7. Boston &#8211; 8. San Francisco &#8211; 9. St. Louis &#8211; 10. Houston &#8211; 11. Phoenix &#8211; 12. Salt Lake City<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>List of Distances<\/strong><\/p>\n<p>It is normally better to represent the list of distances as a square matrix with intersecting cells holding distance between the row and column headers. If we use indexes i and j for this matrix, then data[i][j] is the distance between city i and city j.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">data[<span style=\"background-color: #fff0f0;\">'distance_matrix'<\/span>] <span style=\"color: #333333;\">=<\/span> [\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2451<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">713<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1018<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1631<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1374<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2408<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">213<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2571<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">875<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1420<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2145<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1972<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">2451<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1745<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1524<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">831<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1240<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">959<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2596<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">403<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1589<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1374<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">357<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">579<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">713<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1745<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">355<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">920<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">803<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1737<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">851<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1858<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">262<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">940<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1453<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1260<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">1018<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1524<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">355<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">700<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">862<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1395<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1123<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1584<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">466<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1056<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1280<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">987<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">1631<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">831<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">920<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">700<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">663<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1021<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1769<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">949<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">796<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">879<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">586<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">371<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">1374<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1240<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">803<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">862<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">663<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1681<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1551<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1765<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">547<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">225<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">887<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">999<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">2408<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">959<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1737<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1395<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1021<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1681<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2493<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">678<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1724<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1891<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1114<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">701<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">213<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2596<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">851<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1123<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1769<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1551<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2493<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2699<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1038<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1605<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2300<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2099<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">2571<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">403<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1858<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1584<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">949<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1765<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">678<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2699<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1744<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1645<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">653<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">600<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">875<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1589<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">262<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">466<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">796<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">547<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1724<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1038<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1744<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">679<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1272<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1162<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">1420<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1374<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">940<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1056<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">879<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">225<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1891<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1605<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1645<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">679<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1017<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1200<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">2145<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">357<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1453<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1280<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">586<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">887<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1114<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2300<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">653<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1272<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1017<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">504<\/span>],\r\n    [<span style=\"color: #0000dd; font-weight: bold;\">1972<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">579<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1260<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">987<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">371<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">999<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">701<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2099<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">600<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1162<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1200<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">504<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>],\r\n] \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Number of Vehicles<\/strong><\/p>\n<p>In this case, since it a TSP, the number of vehicles is 1. The Python code is<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">data[<span style=\"background-color: #fff0f0;\">'no_of_vehicles'<\/span>] <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Starting Point<\/strong><\/p>\n<p>In this example, the starting point or &#8216;depot&#8217; is location 0, that is New York.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">data[<span style=\"background-color: #fff0f0;\">'depot'<\/span>] <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. The Routing Model and Index Manager<\/strong><\/h4>\n<p>To solve the TSP in Python, you need to create the RoutingIndexManager and the RoutingModel. The RoutingIndexManager manages conversion between the internal solver variables and NodeIndexes. In this way, we can simply use the NodeIndex in our programs.<\/p>\n<p>The RoutingIndexManager takes three parameters:<\/p>\n<ul>\n<li>number of location, including the depot (number of rows of the distance matrix<\/li>\n<li>number of vehicles<\/li>\n<li>the starting node (depot node)<\/li>\n<\/ul>\n<p>The code below creates the\u00a0 routing model and index manager<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">data <span style=\"color: #333333;\">=<\/span> create_data_model() <span style=\"color: #888888;\"># call the create_data_model() function<\/span>\r\n\r\nmanager <span style=\"color: #333333;\">=<\/span> pywrapcp<span style=\"color: #333333;\">.<\/span>RoutingIndexManager(\r\n    <span style=\"color: #007020;\">len<\/span>(data[<span style=\"background-color: #fff0f0;\">'distance_matrix'<\/span>]), \r\n    data[<span style=\"background-color: #fff0f0;\">'no_of_vehicles'<\/span>],\r\n    data[<span style=\"background-color: #fff0f0;\">'depot'<\/span>])\r\n\r\nrouting <span style=\"color: #333333;\">=<\/span> pywrapcp<span style=\"color: #333333;\">.<\/span>RoutingModel(manager)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. The Distance Callback<\/strong><\/h4>\n<p>To be able to use the solver,\u00a0 we need to create a distance callback. What is a distance callback? This is a function that takes two locations and returns the distance between them. Since these distances are available in the distance matrix, then we can use the distance matrix. The two functions passed to the distance callback are routing variable indexes, so we need to convert them to distance matrix NodeIndex using IndexToNode() method from the RoutingIndexManager.<\/p>\n<p>The code below does that<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Gives the distance between two nodes<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">distance_callback<\/span>(from_index, to_index):\r\n    \r\n    <span style=\"color: #888888;\"># Convert routing variable Index to distance matrix NodeIndex.<\/span>\r\n    from_node <span style=\"color: #333333;\">=<\/span> manager<span style=\"color: #333333;\">.<\/span>IndexToNode(from_index)\r\n    to_node <span style=\"color: #333333;\">=<\/span> manager<span style=\"color: #333333;\">.<\/span>IndexToNode(to_index)\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> data[<span style=\"background-color: #fff0f0;\">'distance_matrix'<\/span>][from_node][to_node]\r\n\r\ntransit_callback_index <span style=\"color: #333333;\">=<\/span> routing<span style=\"color: #333333;\">.<\/span>RegisterTransitCallback(distance_callback)\r\n<\/pre>\n<p>&nbsp;<br \/>\n<strong><\/p>\n<h4 id=\"t4\">4. The Travel Cost and Search Parameters<\/h4>\n<p><\/strong><br \/>\nThe cost of travel is the cost to travel the distance between two nodes. In the case of the solver, you need to set an arc cost evaluator function that does this calculation. This function takes as parameter the transit_callback_index returned by the distance_callback. In our case, the travel cost is simply the distance between the locations.<\/p>\n<p>However, in a real world scenario, there may be other factors to be considered.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">routing<span style=\"color: #333333;\">.<\/span>SetArcCostEvaluatorOfAllVehicles(transit_callback_index)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>We also need to set the search parameters as well as the heuristic for finding the first solution. In this case, the strategy is PATH_CHEAPEST_ARCH. This heuristic repeatedly adds edge with the least weight that don&#8217;t lead to a previously visited node..<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">search_parameters <span style=\"color: #333333;\">=<\/span> pywrapcp<span style=\"color: #333333;\">.<\/span>DefaultRoutingSearchParameters()\r\nsearch_parameters<span style=\"color: #333333;\">.<\/span>first_solution_strategy <span style=\"color: #333333;\">=<\/span> (\r\n    routing_enums_pb2<span style=\"color: #333333;\">.<\/span>FirstSolutionStrategy<span style=\"color: #333333;\">.<\/span>PATH_CHEAPEST_ARC)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Function to Print the Solution<\/strong><\/h4>\n<p>The function below prints out the solution to the screen by extracting the route from the solution<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Displays the solution to the output<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">print_solution<\/span>(manager, routing, solution):\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'Objective: {} miles'<\/span><span style=\"color: #333333;\">.<\/span>format(solution<span style=\"color: #333333;\">.<\/span>ObjectiveValue()))\r\n    index <span style=\"color: #333333;\">=<\/span> routing<span style=\"color: #333333;\">.<\/span>Start(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n    \r\n    plan_output <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'Best route found for vehicle 0:<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n<\/span><span style=\"background-color: #fff0f0;\">'<\/span>\r\n    route_distance <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\n    \r\n    <span style=\"color: #008800; font-weight: bold;\">while<\/span> <span style=\"color: #000000; font-weight: bold;\">not<\/span> routing<span style=\"color: #333333;\">.<\/span>IsEnd(index):\r\n        plan_output <span style=\"color: #333333;\">+=<\/span> <span style=\"background-color: #fff0f0;\">' {} -&gt;'<\/span><span style=\"color: #333333;\">.<\/span>format(manager<span style=\"color: #333333;\">.<\/span>IndexToNode(index))\r\n        previous_index <span style=\"color: #333333;\">=<\/span> index\r\n        index <span style=\"color: #333333;\">=<\/span> solution<span style=\"color: #333333;\">.<\/span>Value(routing<span style=\"color: #333333;\">.<\/span>NextVar(index))\r\n        route_distance <span style=\"color: #333333;\">+=<\/span> routing<span style=\"color: #333333;\">.<\/span>GetArcCostForVehicle(previous_index, index, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n    plan_output <span style=\"color: #333333;\">+=<\/span> <span style=\"background-color: #fff0f0;\">' {}<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n<\/span><span style=\"background-color: #fff0f0;\">'<\/span><span style=\"color: #333333;\">.<\/span>format(manager<span style=\"color: #333333;\">.<\/span>IndexToNode(index))\r\n    \r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(plan_output)\r\n    plan_output <span style=\"color: #333333;\">+=<\/span> <span style=\"background-color: #fff0f0;\">'Total distance for route: {}miles<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n<\/span><span style=\"background-color: #fff0f0;\">'<\/span><span style=\"color: #333333;\">.<\/span>format(route_distance)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Putting it all together<\/strong><\/h4>\n<p>Finally, call the <em>SolveWithParameters()<\/em> method of the routing model and then call the print_solution function.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Call the SolveWithParameters() and print the solution<\/span>\r\nsolution <span style=\"color: #333333;\">=<\/span> routing<span style=\"color: #333333;\">.<\/span>SolveWithParameters(search_parameters)\r\n<span style=\"color: #008800; font-weight: bold;\">if<\/span> solution:\r\n    print_solution(manager, routing, solution)\r\n<\/pre>\n<p>The final output of the program is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Objective: 6465 miles\r\nBest route found for vehicle 0:\r\n 0 -&gt; 7 -&gt; 2 -&gt; 3 -&gt; 9 -&gt; 10 -&gt; 5 -&gt; 4 -&gt; 12 -&gt; 11 -&gt; 1 -&gt; 8 -&gt; 6 -&gt; 0\r\n<\/pre>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we would see how to solve the TSP problem in Python. The following sections are covered. Approach to Solving the TSP Problem &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-212","post","type-post","status-publish","format-standard","hentry","category-vehicle-routing"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/212\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}