<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Software for Exploratory Data Analysis and Statistical Modelling &#187; Linear Programming</title>
	<atom:link href="http://www.wekaleamstudios.co.uk/topics/r-environment/linear-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wekaleamstudios.co.uk</link>
	<description>Statistical Modelling with R</description>
	<lastBuildDate>Wed, 01 Feb 2012 19:44:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Solving Linear Programming Problems with the Gnu Linear Programming Kit</title>
		<link>http://www.wekaleamstudios.co.uk/posts/solving-linear-programming-problems-with-the-gnu-linear-programming-kit/</link>
		<comments>http://www.wekaleamstudios.co.uk/posts/solving-linear-programming-problems-with-the-gnu-linear-programming-kit/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 18:40:52 +0000</pubDate>
		<dc:creator>Ralph</dc:creator>
				<category><![CDATA[Linear Programming]]></category>
		<category><![CDATA[GLPK]]></category>
		<category><![CDATA[GNU Linear Programming Kit]]></category>
		<category><![CDATA[Rglpk]]></category>
		<category><![CDATA[Rglpk_solve_LP]]></category>

		<guid isPermaLink="false">http://www.wekaleamstudios.co.uk/?p=461</guid>
		<description><![CDATA[The GNU Linear Programming Kit (GLPK) can be accessed in R and used for solving large-scale linear programming (LP), mixed integer programming (MIP) and other scenarios. The Rglpk package can be installed and provides a simple interface to using the GLPK. The main function of interest in this package is Rglpk_solve_LP and there are various [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.gnu.org/software/glpk/">GNU Linear Programming Kit</a> (<strong>GLPK</strong>) can be accessed in R and used for solving large-scale linear programming (LP), mixed integer programming (MIP) and other scenarios. The <strong>Rglpk</strong> package can be installed and provides a simple interface to using the <strong>GLPK</strong>.<span id="more-461"></span></p>
<p>The main function of interest in this package is <strong>Rglpk_solve_LP</strong> and there are various options that can be specified by the user. The specification for the function is:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Rglpk_solve_LP(obj, mat, dir, rhs, types = NULL, max = FALSE, bounds = NULL,
  verbose = FALSE)</pre></div></div>

<p>To make use of this function we need to determine the variables and how they combine to form our objective function that will either be minimised or maximised subject to a set of constraints. The <strong>obj</strong> argument is specified as a vector of the coefficients of the variables in the objective function.</p>
<p>The constraints are specified across three arguments (<strong>mat</strong>, <strong>dir</strong>, and <strong>rhs</strong>) that correspond to the coefficients of the constraints (using a matrix), the sign associated with the constraints (e.g. greater than) and the value for the constraint condition (the right hand side of the constraint equations). The <strong>types</strong> argument is optimal and will specify whether the variable is binary, integer or a continuous value. The <strong>max</strong> variable is logical (TRUE or FALSE) and determines whether the objective function should be minimised or maximised.</p>
<p>As an example consider a shop that manufactures and sells two models of the same item, a basic and deluxe version. These two models retail for £5 and £20 respectively. The manufacturing costs can be divided into materials and labour costs. For the basic model these costs are £2 and £1 respectively and for the deluxe model these costs are £6 and £4 respectively. Each week the shop can allocate 75 hours to manufacturing the items and the basic model takes 0.5 hours and the deluxe model takes 1.75 hours to manufacture. In a given week the shop owner will not expect to be able to sell more than 75 units of the basic model and 25 units of the deluxe model.</p>
<p>We need to convert these conditions and relationships into linear equations that can be used by the linear programming algorithms to identify a solution. The objective function is based on the retail price minus the materials and labour costs so for B and D units of the basic and deluxe mode we have:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">(5*B + 20*D) - (2*B + 6*D) - (1*B + 4*D) = 2*B + 10*D</pre></div></div>

<p>and we want to maximise this value. The construction constraint is based on 75 hours maximum and using the time to build from above we have:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">0.5*B + 1.75*D &lt;= 75</pre></div></div>

<p>The number of units that can be sold can be specified reasonably simply as</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">B &lt;= 100
D &lt;= 25</pre></div></div>

<p>We can specify the various arguments to the functions as objects:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">obj = c(2, 10)
mat = matrix(c(0.5, 1.75, 1, 0, 0, 1), nrow = 3, byrow = T)
dir = c(&quot;&lt;=&quot;, &quot;&lt;=&quot;, &quot;&lt;=&quot;)
types = c(&quot;I&quot;, &quot;I&quot;)
rhs = c(75, 100, 25)</pre></div></div>

<p>It should be reasonably straight forward to match these values up with the equations described above. The <strong>obj</strong> argument is a vector of two elements, which are the coefficients of the function to maximise. The <strong>mat</strong> object is a matrix of the coefficients for the three condition statements. The <strong>dir</strong> vector is character strings specifying the type of condition and the <strong>rhs</strong> vector is the values on the right hand side of the constraint equations. The <strong>types</strong> argument is specified as integers as we can&#8217;t build a non integer number of units.</p>
<p>The function call to solve this problem is:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Rglpk_solve_LP(obj, mat, dir, rhs, types, max = TRUE)</pre></div></div>

<p>and the output from this function call:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$optimum
[1] 374
&nbsp;
$solution
[1] 62 25
&nbsp;
$status
[1] 0</pre></div></div>

<p>The function has been maximised to a value of £374, which corresponds to making 62 of the basic model and 25 of the deluxe model during the week.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wekaleamstudios.co.uk/posts/solving-linear-programming-problems-with-the-gnu-linear-programming-kit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

