{"id":5631,"date":"2024-10-24T23:21:35","date_gmt":"2024-10-24T14:21:35","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=5631"},"modified":"2024-10-26T09:43:55","modified_gmt":"2024-10-26T00:43:55","slug":"%e3%80%90python%e3%80%91pulp%e3%81%a7%e6%9c%80%e9%81%a9%e5%8c%96%e5%95%8f%e9%a1%8c%e3%82%92%e8%a7%a3%e3%81%84%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=5631","title":{"rendered":"\u3010Python\u3011PuLP\u3067\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u3044\u3066\u307f\u308b"},"content":{"rendered":"\n\n\n\n\n<p>Python\u306b\u306fPuLP\u3068\u3044\u3046\u6570\u7406\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u306f\u7c21\u5358\u306a\u7dda\u5f62\u8a08\u753b\u554f\u984c\u3092\u4f8b\u306bPuLP\u3092\u4f7f\u7528\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=5631\/#%E4%BE%8B%E9%A1%8C\" >\u4f8b\u984c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=5631\/#%E5%AE%9A%E5%BC%8F%E5%8C%96\" >\u5b9a\u5f0f\u5316<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=5631\/#PuLP%E3%81%A7%E6%9B%B8%E3%81%84%E3%81%A6%E3%81%BF%E3%82%8B\" >PuLP\u3067\u66f8\u3044\u3066\u307f\u308b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=5631\/#%E3%81%BE%E3%81%A8%E3%82%81\" >\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BE%8B%E9%A1%8C\"><\/span>\u4f8b\u984c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6750\u6599A\u3068B\u304b\u3089\u5408\u6210\u3067\u304d\u308b\u88fd\u54c1P\u3068Q\u304c\u3042\u308b\u3002 P\u30921\u500b\u4f5c\u308b\u306e\u306b\u3001A\u304c3kg\u3001B\u304c1kg\u5fc5\u8981\u3067Q\u30921\u500b\u4f5c\u308b\u306e\u306b\u3001A\u304c1kg\u3001B\u304c2kg\u5fc5\u8981\u3067\u3042\u308b\u3002 \u307e\u305f\u3001P\u306e\u4e00\u500b\u3042\u305f\u308a\u306e\u5229\u76ca\u306f300\u5186\u3067Q\u306e1\u500b\u5f53\u308a\u306e\u5229\u76ca\u306f200\u5186\u3067\u3042\u308b\u3002 \u6750\u6599A\u306f20kg\u3001B\u306f14kg\u3057\u304b\u306a\u3044\u3068\u304d\u306b\u3001P\u3068Q\u306e\u5229\u76ca\u306e\u5408\u8a08\u304c\u6700\u5927\u306b\u306a\u308b\u3088\u3046\u306b\u3059\u308b\u306b\u306f\u3001 P\u3068Q\u3092\u305d\u308c\u305e\u308c\u3069\u308c\u3060\u3051\u4f5c\u6210\u3059\u308c\u3070\u3088\u3044\u304b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%9A%E5%BC%8F%E5%8C%96\"><\/span>\u5b9a\u5f0f\u5316<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4e0a\u8a18\u306e\u554f\u984c\u3092PuLP\u3067\u89e3\u304f\u524d\u306b\u307e\u305a\u306f\u5b9a\u5f0f\u5316\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305a\u3001\u305d\u308c\u305e\u308cP\u3092x\u500b\u3001Q\u3092y\u500b\u4f5c\u308b\u3068\u3057\u305f\u3068\u304d\u306b\u6700\u5927\u5316\u3055\u305b\u305f\u3044\u5229\u76ca\u3092\u76ee\u7684\u95a2\u6570\u3068\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u5f0f\u3067\u8868\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\\( \u76ee\u7684\u95a2\u6570 = 300*x +200 *y \\)<\/p>\n\n\n\n<p>\u6b21\u306b\u5236\u7d04\u6761\u4ef6\u3092\u8003\u3048\u307e\u3059\u3002\u6750\u6599A\u306f20kg\u3057\u304b\u306a\u3044\u306e\u3067\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\\( 3*x + y \\leq 20 \\)<\/p>\n\n\n\n<p>\u6750\u6599B\u306f14Kg\u3057\u304b\u306a\u3044\u306e\u3067\u4ee5\u4e0b\u306e\u5f0f\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\\( x + 2*y \\leq 14 \\)<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u3067\u5b9a\u5f0f\u5316\u306f\u7d42\u308f\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PuLP%E3%81%A7%E6%9B%B8%E3%81%84%E3%81%A6%E3%81%BF%E3%82%8B\"><\/span>PuLP\u3067\u66f8\u3044\u3066\u307f\u308b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u307e\u305a\u306fPython\u74b0\u5883\u306bPuLP\u3092pip\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-bash\" data-lang=\"Bash\"><code>pip install pulp<\/code><\/pre><\/div>\n\n\n\n<p>\u3042\u3068\u306f\u5148\u7a0b\u5b9a\u5f0f\u5316\u3057\u305f\u3082\u306e\u3092PuLP\u306e\u304a\u4f5c\u6cd5\u901a\u308a\u306b\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\"><code>from pulp import LpMaximize, LpProblem, LpVariable, value\n\nm = LpProblem(sense=LpMaximize) # \u7dda\u5f62\u8a08\u753b\u554f\u984c\u306e\u6700\u5927\u5316\u554f\u984c\u3068\u3059\u308b\nx = LpVariable(&quot;x&quot;, lowBound=0) # lowBound \u5909\u6570\u306e\u4e0b\u9650\ny = LpVariable(&quot;y&quot;, lowBound=0) # lowBound \u5909\u6570\u306e\u4e0b\u9650\nm += 300*x + 200*y # \u76ee\u7684\u95a2\u6570\u306e\u8a2d\u5b9a\nm += 3*x + y &lt;= 20 # \u5236\u7d04\u6761\u4ef6\nm += x + 2*y &lt;= 14 # \u5236\u7d04\u6761\u4ef6\nm.solve()\nprint(value(x), value(y))<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">5.2 4.4<\/pre>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u5fc5\u8981\u306a\u95a2\u6570\u3092import\u3057\u3001\u5f0f\u3092\u8a18\u8ff0\u3057\u305f\u5f8c\u306fm.solve()\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3067\u6700\u9069\u89e3\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u306a\u304a\u3001\u4eca\u56de\u306e\u4f8b\u984c\u3067\u306f\u500b\u6570\u3092\u6c42\u3081\u308b\u305f\u3081\u3001\u6700\u5f8c\u306e\u7d50\u679c\u306e5.2,4,4\u3068\u3044\u3046\u6570\u5b57\u3067\u306f\u73fe\u5b9f\u7684\u306b\u306f\u4f7f\u3048\u307e\u305b\u3093\u3002\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u306f\u6574\u6570\u8a08\u753b\u554f\u984c\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306bLpInteger\u3092\u7528\u3044\u308b\u3053\u3068\u3067\u6574\u6570\u306e\u89e3\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>from pulp import LpMaximize, LpProblem, LpVariable, LpInteger, value #LpInteger\u3092import\n\nm = LpProblem(sense=LpMaximize)\n# x = LpVariable(&quot;x&quot;, lowBound=0) \n# y = LpVariable(&quot;y&quot;, lowBound=0)\nx = LpVariable(&quot;x&quot;, lowBound=0, cat=LpInteger) # cat\u306e\u5f15\u6570\u306bLpInteger\u3092\u5165\u308c\u3066\u304a\u304f\ny = LpVariable(&quot;y&quot;, lowBound=0, cat=LpInteger) # cat\u306e\u5f15\u6570\u306bLpInteger\u3092\u5165\u308c\u3066\u304a\u304f\nm += 300*x + 200*y\nm += 3*x + y &lt;= 20\nm += x + 2*y &lt;= 14\nm.solve()\nprint(value(x), value(y))<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">5.0 4.0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%81%BE%E3%81%A8%E3%82%81\"><\/span>\u307e\u3068\u3081<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>PuLP\u306fpip\u3060\u3051\u3067\u30bd\u30eb\u30d0\u30fc\u3092\u542b\u3081\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u4f8b\u984c\u306e\u3088\u3046\u306a\u5358\u7d14\u306a\u7dda\u5f62\u8a08\u753b\u554f\u984c\u306e\u5834\u5408\u306f\u975e\u5e38\u306b\u7c21\u5358\u306b\u4f7f\u7528\u3057\u3001\u89e3\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306b\u306fPuLP\u3068\u3044\u3046\u6570\u7406\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u30e9\u30a4\u30d6&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=5631","footnotes":""},"categories":[7,116],"tags":[],"class_list":["post-5631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-116","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5631"}],"version-history":[{"count":5,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5631\/revisions"}],"predecessor-version":[{"id":5637,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5631\/revisions\/5637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}