{"id":164,"date":"2021-01-20T14:25:57","date_gmt":"2021-01-20T14:25:57","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/data-science\/?p=164"},"modified":"2021-01-20T14:25:57","modified_gmt":"2021-01-20T14:25:57","slug":"an-mip-problem-with-python-with-constraints-define-with-arrays","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/data-science\/an-mip-problem-with-python-with-constraints-define-with-arrays\/","title":{"rendered":"An MIP Problem with Python with Constraints Define with Arrays"},"content":{"rendered":"<p>In the previous tutorial, we learnt how to solve a Mixed Integer Program(MIP) problem in Python. In that example there were very few constraints and so we manually specified them.<\/p>\n<p>We would now look at situation where there are many constraints such that they have to be represented as an array. Let&#8217;s take an example:<\/p>\n<p><strong>Example 1 &#8211; Maximize the function<em> 7x<sub>1<\/sub>\u00a0+ 8x<sub>2<\/sub>\u00a0+ 2x<sub>3<\/sub>\u00a0+ 9x<sub>4<\/sub>\u00a0+ 6x<sub>5<\/sub><\/em> with respect to the following constraints:<\/strong><\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td>5<em>x<sub>1<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>7<em>x<sub>2<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>9<em>x<sub>3<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>2<em>x<sub>4<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>1<em>x<sub>5<\/sub><\/em><\/td>\n<td>\u2264<\/td>\n<td>250<\/td>\n<\/tr>\n<tr>\n<td>18<em>x<sub>1<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>4<em>x<sub>2<\/sub><\/em><\/td>\n<td>&#8211;<\/td>\n<td>9<em>x<sub>3<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>10<em>x<sub>4<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>12<em>x<sub>5<\/sub><\/em><\/td>\n<td>\u2264<\/td>\n<td>285<\/td>\n<\/tr>\n<tr>\n<td>4<em>x<sub>1<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>7<em>x<sub>2<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>3<em>x<sub>3<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>8<em>x<sub>4<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>5<em>x<sub>5<\/sub><\/em><\/td>\n<td>\u2264<\/td>\n<td>211<\/td>\n<\/tr>\n<tr>\n<td>5<em>x<sub>1<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>13<em>x<sub>2<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>16<em>x<sub>3<\/sub><\/em><\/td>\n<td>+<\/td>\n<td>3<em>x<sub>4<\/sub><\/em><\/td>\n<td>&#8211;<\/td>\n<td>7<em>x<sub>5<\/sub><\/em><\/td>\n<td>\u2264<\/td>\n<td>315<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Also <em>x<sub>1<\/sub><\/em>, <em>x<sub>2<\/sub><\/em>, <em>x<sub>3<\/sub><\/em>, <em>x<sub>4<\/sub><\/em>, <em>x<sub>5 <\/sub><\/em>are integers<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>We would follow the same 5 step procedure we followed previously<\/p>\n<p>First let&#8217;s represent the given problem using the following:<\/p>\n<ul>\n<li>a 2-d array of coefficients &#8211; coefs<\/li>\n<li>array of the maximums(right-hand-side) &#8211; maxs<\/li>\n<li>array of unknowns (x1, x2,&#8230;, x5) &#8211; unknowns<\/li>\n<li>array of cost function coefficients &#8211; cost_fn<\/li>\n<\/ul>\n<p>All these are clearly shown below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">coefs <span style=\"color: #333333;\">=<\/span> [[<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">7<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">9<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>],\r\n        [<span style=\"color: #0000dd; font-weight: bold;\">18<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">9<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">12<\/span>],\r\n        [<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">7<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">8<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>],\r\n        [<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">13<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">15<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span>]]\r\n\r\nmaxs <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">250<\/span>,  <span style=\"color: #0000dd; font-weight: bold;\">285<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">211<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">315<\/span>] \r\n\r\nunknowns <span style=\"color: #333333;\">=<\/span> [<span style=\"background-color: #fff0f0;\">'x1'<\/span>,<span style=\"background-color: #fff0f0;\">'x2'<\/span>,<span style=\"background-color: #fff0f0;\">'x3'<\/span>,<span style=\"background-color: #fff0f0;\">'x4'<\/span>,<span style=\"background-color: #fff0f0;\">'x5'<\/span>]\r\n\r\ncost_fn <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">7<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">8<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">9<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">6<\/span>]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create the Solver<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># IMPORT THE SOLVER<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">ortools.linear_solver<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> pywraplp\r\nsolver <span style=\"color: #333333;\">=<\/span> pywraplp<span style=\"color: #333333;\">.<\/span>Solver<span style=\"color: #333333;\">.<\/span>CreateSolver(<span style=\"background-color: #fff0f0;\">'SCIP'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong> &#8211; Declare the variables<\/p>\n<p>To do this, we loop through the unknown and populate the variables array as shown below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># DECLARE THE VARIABLES<\/span>\r\nvariables <span style=\"color: #333333;\">=<\/span> [[]] <span style=\"color: #333333;\">*<\/span> <span style=\"color: #007020;\">len<\/span>(unknowns)\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #007020;\">range<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #007020;\">len<\/span>(unknowns)):\r\n    variables[i] <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>IntVar(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, solver<span style=\"color: #333333;\">.<\/span>infinity(), unknowns[i])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> &#8211; Create the contraints.<\/p>\n<p>We do this by looping\u00a0 through the constraints with an outer loop. Then, for each iteration, create a new constraint. Then we use an inner loop to iterate through the coefficients and assign the coefficients of each variable.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># CREATE THE CONSTRAINT 0 &lt;= f(x,y) &lt;= 17.5<\/span>\r\nconstraints <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>] <span style=\"color: #333333;\">*<\/span> <span style=\"color: #007020;\">len<\/span>(coefs)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #007020;\">range<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #007020;\">len<\/span>(coefs)):\r\n    constraints[i] <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>Constraint(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, maxs[i])\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> j <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #007020;\">range<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #007020;\">len<\/span>(coefs[i])):\r\n        constraints[i]<span style=\"color: #333333;\">.<\/span>SetCoefficient(variables[j], coefs[i][j])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Setup the Objective function<\/p>\n<p>To do this, we loop through the cost_fn array and assign coefficients to the corresponding variable<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># DEFINE THE OBJECTIVE FUNCTION 7x1\u00a0+ 8x2\u00a0+ 2x3\u00a0+ 9x4\u00a0+ 6x5<\/span>\r\nobj <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>Objective()\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #007020;\">range<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #007020;\">len<\/span>(cost_fn)):\r\n    obj<span style=\"color: #333333;\">.<\/span>SetCoefficient(variables[i], cost_fn[i])\r\n\r\nobj<span style=\"color: #333333;\">.<\/span>SetMaximization() <span style=\"color: #888888;\"># set the problem goal as maximization<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 5<\/strong> &#8211; Call the Solver() and print the result<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># CALL THE SOLVER AND SHOW THE RESULT<\/span>\r\nstatus <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>Solve()\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'Objective value = '<\/span>, obj<span style=\"color: #333333;\">.<\/span>Value())\r\n\r\n<span style=\"color: #888888;\"># PRINT THE RESULTS<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #007020;\">range<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #007020;\">len<\/span>(unknowns)):\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'<\/span><span style=\"background-color: #eeeeee;\">%s<\/span><span style=\"background-color: #fff0f0;\"> = <\/span><span style=\"background-color: #eeeeee;\">%f<\/span><span style=\"background-color: #fff0f0;\">'<\/span> <span style=\"color: #333333;\">%<\/span>(unknowns[i], variables[i]<span style=\"color: #333333;\">.<\/span>solution_value()))\r\n<\/pre>\n<p>The output of the program is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Objective value =  260.0000000000009\r\nx1 = 8.000000\r\nx2 = 21.000000\r\nx3 = 0.000000\r\nx4 = 2.000000\r\nx5 = 3.000000\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The complete program is given below:<\/p>\n<figure id=\"attachment_174\" aria-describedby=\"caption-attachment-174\" style=\"width: 876px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-15.24.18.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-174 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-15.24.18.png\" alt=\"MIP Linear Program with Python using Array\" width=\"876\" height=\"843\" srcset=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-15.24.18.png 876w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-15.24.18-300x289.png 300w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-15.24.18-768x739.png 768w\" sizes=\"auto, (max-width: 876px) 100vw, 876px\" \/><\/a><figcaption id=\"caption-attachment-174\" class=\"wp-caption-text\">MIP Linear Program with Python using Array<\/figcaption><\/figure>\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 the previous tutorial, we learnt how to solve a Mixed Integer Program(MIP) problem in Python. In that example there were very few constraints and &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":[2],"tags":[],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-linear-programming"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/164","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=164"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/164\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}