<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Claudemir Woche on Medium]]></title>
        <description><![CDATA[Stories by Claudemir Woche on Medium]]></description>
        <link>https://medium.com/@titowoche30?source=rss-c2320d9bc37b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EPLvw9KCfGlL2xb_ROj42Q.jpeg</url>
            <title>Stories by Claudemir Woche on Medium</title>
            <link>https://medium.com/@titowoche30?source=rss-c2320d9bc37b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 11:33:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@titowoche30/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Model and solution of the Traveling Salesman Problem with Python and Pyomo]]></title>
            <link>https://medium.com/analytics-vidhya/model-and-solution-of-the-traveling-salesman-problem-with-python-and-pyomo-db45f2631e8c?source=rss-c2320d9bc37b------2</link>
            <guid isPermaLink="false">https://medium.com/p/db45f2631e8c</guid>
            <category><![CDATA[pyomo]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[operation-research]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[travelling-salesman]]></category>
            <dc:creator><![CDATA[Claudemir Woche]]></dc:creator>
            <pubDate>Tue, 19 Nov 2019 17:28:28 GMT</pubDate>
            <atom:updated>2019-12-31T22:21:07.423Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*71lAnN9K5Wh8d2XHiDgp8A.png" /><figcaption>Credit: <a href="https://optimization.mccormick.northwestern.edu/index.php/Traveling_salesman_problems">https://optimization.mccormick.northwestern.edu/index.php/Traveling_salesman_problems</a></figcaption></figure><p>Step-by-step modeling and solution of the Traveling Salesman Problem using Python and Pyomo.</p><p>In this post, we will go through one of the most famous Operations Research problem, the TSP(Traveling Salesman Problem). The problem asks the following question:<strong> “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?&quot;</strong>.</p><h3><strong>Pyomo</strong></h3><p>To solve this problem we will make use of Pyomo, which is a Python-based open-source optimization modeling language with a diverse set of optimization capabilities.<br>If you had experience with any programing language (especially Python), modeling and solving a problem with Pyomo will be a simple task.</p><p><a href="http://www.pyomo.org/">Pyomo</a></p><p>Pyomo allows you to choose among a variety of solvers, both open-source and commercial.</p><p>I have installed Pyomo with conda in my machine, but it can also be done with pip. Installation instructions can be read on <a href="http://www.pyomo.org/installation">http://www.pyomo.org/installation</a>.</p><p>For this post, we will make use of the CPLEX IBM Solver, to solve a Integer Linear Programming problem. Feel free to use any other solver for ILP.</p><h4>Problem description</h4><p>A traveling salesman has the task of find the shortest route visiting each city and returning to it’s starting point.</p><h4>Model formulation</h4><p>The Miller-Tucker-Zemlin (MTZ) formulation of the TSP is described bellow:</p><p>Label the cities with 1, …, <em>n</em> and define the decision variable:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*UpCyrnwkiCkAhW4Nq-wTSA.png" /><figcaption>Decision Variables</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nuxIn5QeGTPIUBI5Jg6i3A.png" /></figure><p>Then TSP can be written as the following integer linear programming problem:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*BKNnLkDZgV0xozshkdeDmw.png" /><figcaption>credit: <a href="https://en.wikipedia.org/wiki/Travelling_salesman_problem">https://en.wikipedia.org/wiki/Travelling_salesman_problem</a></figcaption></figure><p>“The first set of equalities requires that each city is arrived at from exactly one other city, and the second set of equalities requires that from each city there is a departure to exactly one other city. The last constraints enforce that there is only a single tour covering all cities, and not two or more disjointed tours that only collectively cover all cities.”</p><h4><strong>Pyomo Formulation</strong></h4><p>The first step is to enter the data, this means to provide the model the Cost Matrix. <br>For didactic and pratical purposes, i downloaded one instance with a Cost Matrix for 17 cities.<br>I’ve read the file using python in the following way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1359428d263a4c5b1a1ffa1ecbc6752/href">https://medium.com/media/e1359428d263a4c5b1a1ffa1ecbc6752/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*bwk-T2CVg1tSr4W20vKbWw.png" /><figcaption>The Cost Matrix</figcaption></figure><p>The variables <strong>cost_matrix</strong> and <strong>n </strong>contain the Cost Matrix and the number of cities. Now we will be able to call these elements when defining constraints.</p><p>To use Pyomo and solve the problem we need to make a single import.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2748295dba103caea4ea9b1ccf96f35/href">https://medium.com/media/c2748295dba103caea4ea9b1ccf96f35/href</a></iframe><p>Now we can initialize the model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a6f1a0bdb0ad8c323bf042a6ee2d24f/href">https://medium.com/media/3a6f1a0bdb0ad8c323bf042a6ee2d24f/href</a></iframe><ul><li><strong><em>ConcreteModel()</em>- </strong>Creates the model.</li><li><strong><em>RangeSet(n)</em>- </strong>Creates an index from 1 to n.</li><li><strong><em>RangeSet(2,n)</em>- </strong>Creates an index from 2 to n.</li></ul><p>Creating the decision variables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/127a5687ff112c5fa02f989c9ea65b13/href">https://medium.com/media/127a5687ff112c5fa02f989c9ea65b13/href</a></iframe><ul><li><strong>Var(model.N,model.M, within=pyEnv.Binary)</strong>Creates <strong>N </strong>binary decision variables of size <strong>M.</strong></li><li><strong>Var(model.N, within=pyEnv.NonNegativeIntegers,bounds=(0,n-1))</strong>Creates <strong>N </strong>Non negative integer decision variables that can only assume values between 0 and n-1.</li></ul><p>We must provide the model the Cost Matrix.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d61d35577142197ef4a7a0aa99409db/href">https://medium.com/media/9d61d35577142197ef4a7a0aa99409db/href</a></iframe><ul><li><strong>Param(modelo.N, model.M,initialize=lambda model, i, j: cost_matrix[i-1][j-1])</strong>Provides a <strong>N</strong>x<strong>M</strong> parameter to the model using a lambda function.</li></ul><p>After defining all the variables and provide the parameters, we are able to create the objective function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b962b1830bbdf77a7ba43ada9715b33/href">https://medium.com/media/0b962b1830bbdf77a7ba43ada9715b33/href</a></iframe><p>First we create a Python function that returns our objective function.<br>I used a a marvelous python recourse called <em>List Comprehension, </em>if you’re not familiar with it, this page gives a pretty good tutorial: <a href="https://www.pythonforbeginners.com/basics/list-comprehensions-in-python">https://www.pythonforbeginners.com/basics/list-comprehensions-in-python</a>.</p><ul><li><strong>Objective(rule=obj_func,sense=pyEnv.minimize) </strong>Creates the objective function of the model and it sense’s(maximize or minimize).</li></ul><h4><strong>CONSTRAINTS</strong></h4><p>We provide the constraints in a very similar way.</p><p>The first constraint which ensures that only 1 leaves each city can be formulated in the following way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b354f6c2466d61f0c0c2f17c9fac4742/href">https://medium.com/media/b354f6c2466d61f0c0c2f17c9fac4742/href</a></iframe><ul><li><strong>Constraint(model.M,rule=rule_const1)<em> </em></strong>Creates <strong>M </strong>constraints defined by the rule_const1.</li></ul><p>The second constraint which ensures that each city receives only 1 can be formulated by:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd2edb22fa5f32fa2857b83a7843c75d/href">https://medium.com/media/cd2edb22fa5f32fa2857b83a7843c75d/href</a></iframe><ul><li><strong>Constraint(model.N,rule=rule_const2)<em> </em></strong>Creates <strong>N</strong> constraints defined by the rule_const2.</li></ul><p>The third and last constraint it’s the one that enforce that there is only a single tour covering all cities, and not two or more disjointed tours that only collectively cover all cities.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f5647c037a726f6e685c28f0d3aa02b/href">https://medium.com/media/9f5647c037a726f6e685c28f0d3aa02b/href</a></iframe><ul><li><strong>Constraint(model.U,Model.N,rule=rule_const3)<em> </em></strong>Creates <strong>UxN</strong> constraints defined by the rule_const3.</li></ul><p>A <em>rule </em>type function must provide a <em>Pyomo Object</em>, so that’s why i had to wrote that weird else condition.</p><h4>SOLUTION</h4><p>To solve our model, we need to have a solver installed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6be18dc82d959fe9fa2d27e233f61ed/href">https://medium.com/media/f6be18dc82d959fe9fa2d27e233f61ed/href</a></iframe><ul><li><strong>pprint()</strong>Prints de entire model( it might not be a good idea if the model is too big).</li><li><strong>pprint(filename=’file.txt’)</strong>Prints the model in a specific file.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fee8f13675a7aa609cd4684c652a16a3/href">https://medium.com/media/fee8f13675a7aa609cd4684c652a16a3/href</a></iframe><ul><li><strong>SolverFactory(‘cplex’)</strong> I initialized the IBM solver called CPLEX.</li><li><strong>solve(model, tee= False)</strong> Solves the model. Using tee = False, you won’t see the solver’s log screen. If you want to see it, just go with tee= True.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*OQe1Wm09SGCUMyPdCcC2GA.png" /><figcaption>Problem solved.</figcaption></figure><p>If you wish to see which decision variables were chosen, you can do this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d47f662a2eed17ed81bf987c46b9a7bb/href">https://medium.com/media/d47f662a2eed17ed81bf987c46b9a7bb/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/134/1*QOBkJQgT2iCnE1tLnkVffA.png" /><figcaption>(city i, city j)</figcaption></figure><p>This means that the optimal tour is 1–17–8–9–4–5–15–7–16–6–13–10–11–2–14–3–12–1.</p><h4>EXTRAS</h4><p>To access more examples of Pyomo you can go to their <a href="https://github.com/Pyomo/pyomo/tree/master/examples">GitHub page</a>.</p><p>There are some models examples and a more complete manual(both in Portuguese) in my own <a href="https://github.com/titowoche30/PyomoExamples">github page</a>.</p><p>This was my first article :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db45f2631e8c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/model-and-solution-of-the-traveling-salesman-problem-with-python-and-pyomo-db45f2631e8c">Model and solution of the Traveling Salesman Problem with Python and Pyomo</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>