

{"id":108,"date":"2016-10-11T17:43:16","date_gmt":"2016-10-11T15:43:16","guid":{"rendered":"https:\/\/project.inria.fr\/readinggroupoc\/?p=108"},"modified":"2017-06-11T12:17:53","modified_gmt":"2017-06-11T10:17:53","slug":"linear-programming-a-hello-world-in-sagemath","status":"publish","type":"post","link":"https:\/\/project.inria.fr\/readinggroupoc\/linear-programming-a-hello-world-in-sagemath\/","title":{"rendered":"Linear programming: a &#8220;Hello world&#8221; in SageMath"},"content":{"rendered":"<p>In this post we show how to formulate an LP in <a href=\"https:\/\/en.wikipedia.org\/wiki\/SageMath\" target=\"_blank\">SageMath<\/a>, and to solve it with <a href=\"https:\/\/en.wikipedia.org\/wiki\/GNU_Linear_Programming_Kit\" target=\"_blank\">GLPK<\/a>, a standard open-source mathematical optimization package.<\/p>\n<p>A mathematical optimization problem of the form:<\/p>\n\\(\\displaystyle \\text{minimize } c^T x \\\\ \\text{subject to } a_i^T x \\leq b_i,~~ i=1,\\ldots,m \\)\n<p>is known as a <em>linear program<\/em> (or LP for short), because both the objective and constraint functions are linear. Here the vectors\u00a0\\(\\displaystyle c, a_1, \\ldots, a_m \\in \\mathbb{R}^n \\) and scalars \\(\\displaystyle b_1, \\ldots, b_m \\in \\mathbb{R} \\) are problem parameters. Linear programs are a subclass of convex programs, since any linear function is convex.<\/p>\n<p>As a toy example, consider:<br \/>\n\\(\\displaystyle \\begin{aligned}<br \/>\n\\text{min}&amp; ~x_1 + x_2 \\\\ \\text{s.t.}&amp; ~x_1, x_2 \\in [-1,1] \\\\ &amp; ~2x_1 + x_2 \\leq 0.5<br \/>\n\\end{aligned} \\)<\/p>\n<p>Below is the SageMath script (<a href=\"http:\/\/sagecell.sagemath.org\/?z=eJx9UtFq20AQfBfoH4YEo1PiijqQl7R-TQh1qaGhL20wZ3klb5DuzN1JVvr13ZMcl7xkQeiYnZkd7ekS21fsqNJdE-bgAO1915JH1rLhlv_qwNZkBY6EUhv4A5VcvaLVw7mLI4c9wp6gdzuOiG5w0E63FMi9py7x5DpKk9Vajt95oN2jCVSTW7Eh7dbO1qJT3ja9SJe4eFitv13kaYI0uURNhpwOhF471ttGcqbJILTVujB03LzBiifT5b1uPL2pJR1Ka3xwWvo-higE2_zHFD4t8HWJ4ffiOb4XyL98SLv5mHZzNTpdn5mfi9uRO-bxFGC3L1QG7glVZ8q4ojQRDFOG66gbnYW7OXOVnKaPOjiZg-xKCj9lZ2xq2QXUeCNxdTliL4PUKIhWe3tUk9z2clPj9saFR3SwhzBBtYyUfkdeDafMp3E_zqF_xf5dNkd0SpPKOvAcPdggGhUsP4A8rVf5nThInSyGzczLmFmVYQYVNfm7_h-T_QNpG8uq&amp;lang=sage\" target=\"_blank\">see the outcome here!<\/a>):<\/p>\n<pre lang=\"python\" line=\"1\"># by default, it assumes 'minimization'. we can specify maximization with the additional parameter maximization = True\r\nLP = MixedIntegerLinearProgram(solver = \"GLPK\")\r\n\r\n# generate variables \r\nx = LP.new_variable(integer=False)\r\n\r\n# add constraints\r\nLP.add_constraint( -1 <= x[1] <= 1 );\r\nLP.add_constraint( -1 <= x[2] <= 1 );\r\nLP.add_constraint( 2*x[1] + x[2] <= 0.5 );\r\n\r\n# set objective function\r\nobj = x[1]+x[2]\r\nLP.set_objective(obj)\r\n\r\nprint '**** Solving LP (with GLPK) ****'    \r\n\r\nLP.show()\r\n\r\noval = LP.solve()\r\nxopt = LP.get_values(x);\r\n    \r\nprint 'Objective Value:', oval\r\nfor i, v in xopt.iteritems():\r\n    print 'x_%s = %f' % (i, v)\r\n    print '\\n'<\/pre>\n<p>There are dozens of LP solvers (practically any language for doing scientific computing comes with its own package for optimization purposes!); <a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_optimization_software\" target=\"_blank\">check this list<\/a>. It is worth noting that several commercial ones propose free academic licenses; among these, <a href=\"http:\/\/www.gurobi.com\/resources\/switching-to-gurobi\/open-source-solvers\" target=\"_blank\">Gurobi<\/a> is very interesting to consider.<\/p>\n<p>We have chosen SageMath because it is a full-fledged platform for scientific computing. Moreover, it is free software, built upon Python (and Cython, for performance); consequently, there is a huge number of lines of code that you can readily re-use in your projects to achieve different functionalities. <\/p>\n<hr \/>\n<p>References:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.sagemath.org\/\" target=\"_blank\"> To install SageMath in your local machine. <\/a><\/li>\n<li><a href=\"https:\/\/cloud.sagemath.com\/\" target=\"_blank\"> To start using SageMathCloud, an open-source platform for doing collaborative mathematics,<\/a> and an <a href=\"https:\/\/www.youtube.com\/watch?v=-SqzarfTqRY\" target=\"_blank\"> introductory video here. <\/a><\/li>\n<li>An <a href=\"http:\/\/www.gregorybard.com\/interacts\/feasible_region.html\" target=\"_blank\">interactive applet for a simple linear program<\/a> (by Prof. Gregory V. Bard).<\/li>\n<\/ul>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>In this post we show how to formulate an LP in SageMath, and to solve it with GLPK, a standard open-source mathematical optimization package. A mathematical optimization problem of the form: is known as a linear program (or LP for short), because both the objective and constraint functions are linear.\u2026<\/p>\n<p> <a class=\"continue-reading-link\" href=\"https:\/\/project.inria.fr\/readinggroupoc\/linear-programming-a-hello-world-in-sagemath\/\"><span>Continue reading<\/span><i class=\"crycon-right-dir\"><\/i><\/a> <\/p>\n","protected":false},"author":1077,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-information"],"_links":{"self":[{"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/users\/1077"}],"replies":[{"embeddable":true,"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":108,"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/posts\/108\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/project.inria.fr\/readinggroupoc\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}