{"id":180,"date":"2021-01-20T23:55:34","date_gmt":"2021-01-20T23:55:34","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/data-science\/?p=180"},"modified":"2021-01-21T11:44:26","modified_gmt":"2021-01-21T11:44:26","slug":"solving-an-optimization-problem-with-python-step-by-step","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/data-science\/solving-an-optimization-problem-with-python-step-by-step\/","title":{"rendered":"Solving an Optimization Problem with Python &#8211; Step by Step"},"content":{"rendered":"<p>In this example, we are going to solve a typical Constraint Optimization problem. In the previous tutorial, we tool a <a href=\"https:\/\/www.kindsonthegenius.com\/data-science\/2021\/01\/20\/solving-constraints-optimization-problem-with-python\/\">simple example <\/a>of just one constraint.<\/p>\n<p><strong>Example 1: Maximize the function 2x + 2y + 3z with respect to the following constraints:<\/strong><\/p>\n<table style=\"width: 60%; font-family: 'Times New Roman';\" border=\"0\">\n<tbody>\n<tr>\n<td><em>x<\/em>\u00a0+\u00a0<sup>7<\/sup>\u2044<sub>2<\/sub>\u00a0<em>y<\/em>\u00a0+\u00a0<sup>3<\/sup>\u2044<sub>2<\/sub>\u00a0<em>z<\/em><\/td>\n<td>\u2264<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td>3<em>x<\/em>\u00a0&#8211; 5<em>y<\/em>\u00a0+ 7<em>z<\/em><\/td>\n<td>\u2264<\/td>\n<td>45<\/td>\n<\/tr>\n<tr>\n<td>5<em>x<\/em>\u00a0+ 2<em>y<\/em>\u00a0&#8211; 6<em>z<\/em><\/td>\n<td>\u2264<\/td>\n<td>37<\/td>\n<\/tr>\n<tr>\n<td><em>x<\/em>,\u00a0<em>y<\/em>,\u00a0<em>z<\/em><\/td>\n<td>\u2265<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td colspan=\"3\"><em>x<\/em>,\u00a0<em>y<\/em>,\u00a0<em>z<\/em>\u00a0integers<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Note:<\/strong> Since we are performing integer optimization, we must make sure the coefficients of the first constraint are all integer. We can achieve this by multiplying through by 2.<\/p>\n<p>So let&#8217;s now take the steps<\/p>\n<p><strong>Step 1:<\/strong> Declare the model<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> <span style=\"color: #888888;\"># Declare the Model<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">ortools.sat.python<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> cp_model\r\nmodel <span style=\"color: #333333;\">=<\/span> cp_model<span style=\"color: #333333;\">.<\/span>CpModel()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Define the variables<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Define the variables<\/span>\r\nnum_vars <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>\r\nx <span style=\"color: #333333;\">=<\/span> model<span style=\"color: #333333;\">.<\/span>NewIntVar(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">50<\/span>, <span style=\"background-color: #fff0f0;\">'x'<\/span>)\r\ny <span style=\"color: #333333;\">=<\/span> model<span style=\"color: #333333;\">.<\/span>NewIntVar(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">50<\/span>, <span style=\"background-color: #fff0f0;\">'y'<\/span>)\r\nz <span style=\"color: #333333;\">=<\/span> model<span style=\"color: #333333;\">.<\/span>NewIntVar(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">50<\/span>, <span style=\"background-color: #fff0f0;\">'z'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3:<\/strong> Create the constraints<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Set up the constraints<\/span>\r\nmodel<span style=\"color: #333333;\">.<\/span>Add(<span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">*<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">*<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">*<\/span>z <span style=\"color: #333333;\">&lt;=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">50<\/span>)\r\nmodel<span style=\"color: #333333;\">.<\/span>Add(<span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">*<\/span>x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">*<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">*<\/span>z <span style=\"color: #333333;\">&lt;=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">45<\/span>)\r\nmodel<span style=\"color: #333333;\">.<\/span>Add(<span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">*<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">*<\/span>y <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">*<\/span>z <span style=\"color: #333333;\">&lt;=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">37<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4:<\/strong> Define the cost function<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Define the objective function 2x + 2y + 3z<\/span>\r\nmodel<span style=\"color: #333333;\">.<\/span>Maximize(<span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">*<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">*<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">*<\/span>z)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 5:<\/strong> Invoke the solver<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Invoke the solver<\/span>\r\nsolver <span style=\"color: #333333;\">=<\/span> cp_model<span style=\"color: #333333;\">.<\/span>CpSolver()\r\nstatus <span style=\"color: #333333;\">=<\/span> solver<span style=\"color: #333333;\">.<\/span>Solve(model)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 6:<\/strong> Display the results<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Display the results<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">if<\/span> status <span style=\"color: #333333;\">==<\/span> cp_model<span style=\"color: #333333;\">.<\/span>OPTIMAL:\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'Value of objective function: %i'<\/span> <span style=\"color: #333333;\">%<\/span> solver<span style=\"color: #333333;\">.<\/span>ObjectiveValue())\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'x = %i'<\/span> <span style=\"color: #333333;\">%<\/span>solver<span style=\"color: #333333;\">.<\/span>Value(x))\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'y = %i'<\/span> <span style=\"color: #333333;\">%<\/span>solver<span style=\"color: #333333;\">.<\/span>Value(y))\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'z = %i'<\/span> <span style=\"color: #333333;\">%<\/span>solver<span style=\"color: #333333;\">.<\/span>Value(z))\r\n<\/pre>\n<p>The output would be as shown below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Value of objective function: 35\r\nx = 7\r\ny = 3\r\nz = 5\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Complete program is given below:<\/p>\n<figure id=\"attachment_187\" aria-describedby=\"caption-attachment-187\" style=\"width: 895px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-21-at-12.43.08.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-187 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-21-at-12.43.08.png\" alt=\"Constraint Optimization Problem in Python\" width=\"895\" height=\"848\" srcset=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-21-at-12.43.08.png 895w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-21-at-12.43.08-300x284.png 300w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-21-at-12.43.08-768x728.png 768w\" sizes=\"auto, (max-width: 895px) 100vw, 895px\" \/><\/a><figcaption id=\"caption-attachment-187\" class=\"wp-caption-text\">Constraint Optimization 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>In this example, we are going to solve a typical Constraint Optimization problem. In the previous tutorial, we tool a simple example of just one &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":[29],"tags":[],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-constraint-optimization"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/180","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=180"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/180\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}