Quoting an example from Head First Data Analysis, the profit of a rubber duck is $5 and that of a rubber fish is $4. The profit, P, is therefore:

P = 5d + 4f

The production of a rubber duck uses 100 units of rubber pellet. Fish uses 125 units. Altogether, there is 50,000 units available, limiting production by the following constraint:

100d + 125f <= 50000

The objective is to find the numbers for d and f such that P is maximised. Unfortunately, the book uses Excel Solver. Here, I am showing how to solve this using the lpsolve library in R.

First, install and load the package if not already:

> install.packages("lpSolveAPI")
> library("lpSolveAPI")

Now, our model has 2 variables, d and f. We construct an lprec to represent that:

> lprec <- make.lp(0, 2)

And tell lpsolve we want to maximise the objective function, P.

> lp.control(lprec, sense="max")

We have to maintain the same ordering of the variables throughout. Here, 5d + 4f is represented in the vector c(5, 4).

> set.objfn(lprec, c(5, 4))

To represent 100d + 125f <= 50000, we use:

> add.constraint(lprec, c(100, 125), "<=", 50000)

Again, be careful to maintain the same ordering of d and f.

To see the model, do:

> lprec
Model name: 
            C1    C2           
Maximize     5     4           
R1         100   125  <=  50000
Kind       Std   Std           
Type      Real  Real           
Upper      Inf   Inf           
Lower        0     0         

To solve it:

> solve(lprec)

It is quite strange R does not give the results right away. To see the results:

> get.objective(lprec)

[1] 2500

That says our profit will be $2,500.

Our profit is achieved by:

> get.variables(lprec)

[1] 500   0

That is, we make 500 ducks and no fish at all.

The book example actually says we can’t make more than 400 ducks or 300 fish. Just add the following constraints to the model to get the book answer of $2,320.

> add.constraint(lprec, c(1,0), "<=", 400)
> add.constraint(lprec, c(0,1), "<=", 300)

They say 1d + 0f <= 400 and 0d + 1f <= 300, respectively.

> lprec
Model name: 
            C1    C2           
Maximize     5     4           
R1         100   125  <=  50000
R2           1     0  <=    400
R3           0     1  <=    300
Kind       Std   Std           
Type      Real  Real           
Upper      Inf   Inf           
Lower        0     0         

> solve(lprec)
[1] 0

> get.objective(lprec)
[1] 2320

> get.variables(lprec)
[1] 400  80

lpsolve is applicable when we can represent our optimisation in the standard form of linear programming.