{"id":159,"date":"2021-01-20T11:11:33","date_gmt":"2021-01-20T11:11:33","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/data-science\/?p=159"},"modified":"2021-01-20T11:12:03","modified_gmt":"2021-01-20T11:12:03","slug":"integer-optimization-mixed-integer-programmip-with-python","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/data-science\/integer-optimization-mixed-integer-programmip-with-python\/","title":{"rendered":"Integer Optimization &#8211; Mixed Integer Program(MIP) with Python"},"content":{"rendered":"<p>A Mixed Integer Program(MIP) is a linear optimization problem that requires that some of the variables be integers.\u00a0 These variables could either be integer variables or boolean variables.<\/p>\n<p>Let&#8217;s take an example, then we solve it using Python.<\/p>\n<ul>\n<li><a href=\"#t1\">An MIP Example<\/a><\/li>\n<li><a href=\"#s1\">Step 1 &#8211; Create the Solver<\/a><\/li>\n<li><a href=\"#s2\">Step 2 &#8211; Declare the Variables<\/a><\/li>\n<li><a href=\"#s3\">Step 3 &#8211; Create your Constraints<\/a><\/li>\n<li><a href=\"#s4\">Step 4 &#8211; Define the Cost Function<\/a><\/li>\n<li><a href=\"#s5\">Step 5 &#8211; Invoke the Solve() and Print Results<\/a><\/li>\n<li><a href=\"#t2\">Using MIP Approach<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">An MIP Example<\/strong><\/h4>\n<p>Maximize the function <em>f(x, y) = x + 10<\/em> subject to the following constraints:<\/p>\n<table style=\"height: 191px; width: 40%; font-family: 'Times New Roman';\" border=\"0\">\n<tbody>\n<tr>\n<td>x + 7 y \u2264 17.5<\/td>\n<\/tr>\n<tr>\n<td>x \u2264 3.5<\/td>\n<\/tr>\n<tr>\n<td>x \u2265 0<\/td>\n<\/tr>\n<tr>\n<td>y \u2265 0<\/td>\n<\/tr>\n<tr>\n<td colspan=\"3\">x,\u00a0y\u00a0integers<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>Solution<\/strong><\/h5>\n<p>For clarity, we would solve this problem using the simple 5-step linear programming approach. Then we would modify the program to use an MIP approach.<\/p>\n<p><strong id=\"s1\">Step 1 &#8211; Create your solver<\/strong><\/p>\n<p>You&#8217;ll need to import the ortools.linear_solver from pywrap and create a solver variable. the code below does that<\/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;\">'GLOP'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"s2\">Step 2 &#8211; Declare the variables<\/strong><\/p>\n<p>We do this using the solver&#8217;s <em>NumVar<\/em> method and specify: the upper bound, the lower bound and the variable name.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># DECLARE THE VARIABLES<\/span>\r\nx <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>NumVar(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">3.5<\/span>, <span style=\"background-color: #fff0f0;\">'x'<\/span>)\r\ny <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>NumVar(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, solver<span style=\"color: #333333;\">.<\/span>infinity(), <span style=\"background-color: #fff0f0;\">'y'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"s3\">Step 3 &#8211; Create the Constraints<\/strong><\/p>\n<p>Constraints are of the form <em>0 &lt;= f(x,y) &lt;= 17.5<\/em>. We would create only this one constraints because the other constraints have been implicitly created with the variable declaration.<\/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\nct <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>Constraint(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">17.5<\/span>) <span style=\"color: #888888;\"># specifies upper and lower bound<\/span>\r\nct<span style=\"color: #333333;\">.<\/span>SetCoefficient(x, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>) <span style=\"color: #888888;\"># specify the x coefficient<\/span>\r\nct<span style=\"color: #333333;\">.<\/span>SetCoefficient(y, <span style=\"color: #0000dd; font-weight: bold;\">7<\/span>) <span style=\"color: #888888;\"># specify the y coefficient<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"s4\">Step 4 &#8211; Define the Cost function<\/strong><\/p>\n<p>We simply declare the objective variable and then set the coefficient of x and y in the function.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># DEFINE THE OBJECTIVE FUNCTION x + 10y<\/span>\r\nobj <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>Objective()\r\nobj<span style=\"color: #333333;\">.<\/span>SetCoefficient(x, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>)\r\nobj<span style=\"color: #333333;\">.<\/span>SetCoefficient(y, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>)\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 id=\"s5\">Step 5 &#8211; Call the Solver and display the result<\/strong><\/p>\n<p>You must say solver.Solve(). The value of the objective function is in the Value() method of the objective variable. Similarly, the value of each variable if in the solution_value() method of each of the variables. The code below displays the results.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># CALL THE SOLVER AND SHOW THE RESULT<\/span>\r\nsolver<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<span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'x = '<\/span>, x<span style=\"color: #333333;\">.<\/span>solution_value())\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'y = '<\/span>, y<span style=\"color: #333333;\">.<\/span>solution_value())\r\n<\/pre>\n<p>The result is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Objective value =  25.0\r\nx =  0.0\r\ny =  2.5\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">Solving Using MIP Approach<\/strong><\/h4>\n<p>To solve using MIP you need to make just a few changes.<\/p>\n<ul>\n<li>in the variable declaration, instead of using NumVar(), you use IntVar()<\/li>\n<li>in creating the solver, you need to use &#8216;SCIP&#8217; instead of &#8216;GLOP&#8221;<\/li>\n<\/ul>\n<p>If you make this changes, then you will have the output:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Objective value =  23.0\r\nx =  3.0\r\ny =  2.0\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Find the complete program below:<\/p>\n<figure id=\"attachment_161\" aria-describedby=\"caption-attachment-161\" style=\"width: 839px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-12.04.29.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-161 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-12.04.29.png\" alt=\"Solution to MIP Problem in Python\" width=\"839\" height=\"667\" srcset=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-12.04.29.png 839w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-12.04.29-300x238.png 300w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-12.04.29-768x611.png 768w\" sizes=\"auto, (max-width: 839px) 100vw, 839px\" \/><\/a><figcaption id=\"caption-attachment-161\" class=\"wp-caption-text\">Solution to MIP Problem in Python<\/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>A Mixed Integer Program(MIP) is a linear optimization problem that requires that some of the variables be integers.\u00a0 These variables could either be integer variables &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":[25,26,28,27],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-linear-programming","tag-linear-optimization","tag-mip","tag-mixed-integer-program","tag-solver"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/159","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=159"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/159\/revisions\/163"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}