{"id":176,"date":"2021-01-20T16:44:21","date_gmt":"2021-01-20T16:44:21","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/data-science\/?p=176"},"modified":"2021-01-20T16:44:21","modified_gmt":"2021-01-20T16:44:21","slug":"solving-constraints-optimization-problem-with-python","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/data-science\/solving-constraints-optimization-problem-with-python\/","title":{"rendered":"Solving Constraints Optimization  Problem with Python"},"content":{"rendered":"<p>Constraint Optimization is the class of problems that requires identifying feasible solution among a very large set of possibilities. The problem can be modelled in terms of contraints<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Constrained_optimization\" target=\"_blank\" rel=\"noopener\">Constraint Optmization(also called Constraint Programming &#8211; CP)<\/a> is based on finding a feasible solution(feasibility) rather than finding an optimal solution(optimization). Unlike normal linear, programming, the focus here is on the constraints rather than on the cost function.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Employee Scheduling Example<\/strong><\/p>\n<p>A typical example of where CP is applied is in the employee scheduling. For instance, a company that runs 3 8-hour daily shift for its employee. And there are four employees. Three of the 4 employees need to be assigned to different shift each day while the 4th employee has a day off. Here you can see that the number of possible schedules is much &#8211; 4! = 4 * 3 * 2 * 1 = 24. For a week we have 247. That is very much! Therefore, to narrow down the number, we need to apply some constraints.For example, each employee must work certain minimum number of days in a week. CP allows us to keep track of solutions that remains feasible as constraints are added.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>A Simple Example with Python<\/strong><\/p>\n<p>There are three variables x, y and z that could assume on of three values: 0, 1, 2.<\/p>\n<p>There is once constraint that says: x \u2260 y<\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>We follow 5 steps to solve this problem in Python<\/p>\n<p><strong>Step 1<\/strong>: Declare your model<\/p>\n<p>You will first import the cp_model from <em>ortools.sat.python<\/em><\/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: x, y and z<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Define your 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>, num_vars <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/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>, num_vars <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/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>, num_vars <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"background-color: #fff0f0;\">'z'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3:<\/strong> Add the constraint<\/p>\n<p>In this case, we have only one constraint: x \u2260 y<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Add the constraint<\/span>\r\nmodel<span style=\"color: #333333;\">.<\/span>Add(x <span style=\"color: #333333;\">!=<\/span> y)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4:<\/strong> Invoke the solver<\/p>\n<p>You must call\u00a0 the <em>CpSolve()<\/em> method of 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 5:<\/strong> Display your results<\/p>\n<p>In this case we would display the optimal solution<\/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: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'x = <\/span><span style=\"background-color: #eeeeee;\">%i<\/span><span style=\"background-color: #fff0f0;\">'<\/span> <span style=\"color: #333333;\">%<\/span>solver<span style=\"color: #333333;\">.<\/span>Value(x))\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'y = <\/span><span style=\"background-color: #eeeeee;\">%i<\/span><span style=\"background-color: #fff0f0;\">'<\/span> <span style=\"color: #333333;\">%<\/span>solver<span style=\"color: #333333;\">.<\/span>Value(y))\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">'z = <\/span><span style=\"background-color: #eeeeee;\">%i<\/span><span style=\"background-color: #fff0f0;\">'<\/span> <span style=\"color: #333333;\">%<\/span>solver<span style=\"color: #333333;\">.<\/span>Value(z))\r\n<\/pre>\n<p>The output is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">x = 1\r\ny = 0\r\nz = 0\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The complete program shown below displays all the the results, not just the optimal one.<\/p>\n<figure id=\"attachment_178\" aria-describedby=\"caption-attachment-178\" style=\"width: 957px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-17.40.06.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-178 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-17.40.06.png\" alt=\"Contraints Programming in Python\" width=\"957\" height=\"932\" srcset=\"https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-17.40.06.png 957w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-17.40.06-300x292.png 300w, https:\/\/www.kindsonthegenius.com\/data-science\/wp-content\/uploads\/sites\/12\/2021\/01\/Screenshot-2021-01-20-at-17.40.06-768x748.png 768w\" sizes=\"auto, (max-width: 957px) 100vw, 957px\" \/><\/a><figcaption id=\"caption-attachment-178\" class=\"wp-caption-text\">Contraints Programming 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>Constraint Optimization is the class of problems that requires identifying feasible solution among a very large set of possibilities. The problem can be modelled in &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":[30],"class_list":["post-176","post","type-post","status-publish","format-standard","hentry","category-constraint-optimization","tag-constraint-optimization"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/176","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=176"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/176\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}