# Optimization Using R

Optimization is a technique for finding out the best possible solution for a given problem for all the possible solutions. Optimization uses a rigorous mathematical model to find out the most efficient solution to the given problem.

### Example 2

Below is good example taken from lpsolve’s sourceforge website -

“Suppose a farmer has 75 acres on which to plant two crops: wheat and barley. To produce these crops, it costs the farmer (for seed, fertilizer, etc.) $120 per acre for the wheat and $210 per acre for the barley. The farmer has $15000 available for expenses. But after the harvest, the farmer must store the crops while awaiting avourable market conditions. The farmer has storage space for 4000 bushels. Each acre yields an average of 110 bushels of wheat or 30 bushels of barley. If the net profit per bushel of wheat (after all expenses have been subtracted) is $1.30 and for barley is $2.00, how should the farmer plant the 75 acres to maximize profit?”

As we did in the previous example, let's define the optimization function and the constraints.

*maximize*

* g = (110)(1.30)x + (30)(2.00)y = 143x + 60y*

*subject to*

* 120x + 210y <= 15000*

* 110x + 30y <= 4000*

* x + y <= 75*

* x >= 0*

* y >= 0*

We will be solving this problem in both excel and R.

### Excel Solver

There is a way to solve LP problems using Excel. Firstly we have to define the number of acres to plant for both these crops.

Initially they are zero, the optimum values can be obtained by using the solver plugin in excel. Next we need to define the constraints, the equations for the constraints have already been defined.

Subject to,

The initial values for the number of used resources is set at zero initially. The main workhorse behind these LP problems is to use the sumproduct equation.

The sumproduct equation is basically a product of cost multiplied by the number of acres.

Now we define the maximization equation. In this case the profit needs to be maximized. The total profit cell that is seen in the below image.

The same sumproduct formula that was previously used is repeated again.

Now try changing the values for the wheat and Barley cells. Just put some random number for both the cells. Observe the total profit cell and the used cell for all the constraints.

As seen we get a total profit figure, however have a look at the used resources. Clearly the values in the cell are more than the values in the available cells. We can try to decrease the number of acres and see what the resulting values will be.

The values are low indeed, but it is not the optimum solution. To get the optimum values for total profit. We use the solver plugin to solve this. Navigate to the data and see if there is the solver option under the analysis tab.

If it is not present, follow these steps to install it -

- Navigate for the file tab and then click on options
- Click on the Add-ins button, and then go next to the manage

Once this is done, a popup should appear.

Select the Solver add-in and press OK.

There you go now the solver option should show up. Before we proceed further, let’s set the number of acres to zero.

Now, set the objective to total profit

Set the -

- Solving method to simplex LP.
- The objective to max

Next add the three constraints,

Once done press solve, the following popup should show up along with the optimized values for the profit cell.

Press OK, now we have the number of acres to plant for Wheat and Barley. Along with the profit value.

### Implementation in R using LpsolveAPI

We previously used the Lpsolve package, in this example we illustrate how to use LpsolveAPI package. We start off by calling the lpsolveapi package.

> require(lpSolveAPI) Loading required package: lpSolveAPI > lprec <- make.lp(0, 2) > lprec Model name: C1 C2 Minimize 0 0 Kind Std Std Type Real Real Upper Inf Inf Lower 0 0

Next we set the objective function to maximization.

> lp.control(lprec, sense="max") $anti.degen [1] "none" $basis.crash [1] "none" $bb.depthlimit [1] -50 $bb.floorfirst [1] "automatic" $bb.rule [1] "pseudononint" "greedy" "dynamic" "rcostfixing" $break.at.first [1] FALSE $break.at.value [1] 1e+30 $epsilon epsb epsd epsel epsint epsperturb epspivot 1e-10 1e-09 1e-12 1e-07 1e-05 2e-07 $improve [1] "dualfeas" "thetagap" $infinite [1] 1e+30 $maxpivot [1] 250 $mip.gap absolute relative 1e-11 1e-11 $negrange [1] -1e+06 $obj.in.basis [1] TRUE $pivoting [1] "devex" "adaptive" $presolve [1] "none" $scalelimit [1] 5 $scaling [1] "geometric" "equilibrate" "integers" $sense [1] "maximize" $simplextype [1] "dual" "primal" $timeout [1] 0 $verbose [1] "neutral"

We define the objective function and the constraints

> set.objfn(lprec, c(143, 60)) > add.constraint(lprec, c(120, 210), "<=", 15000) > add.constraint(lprec, c(110, 30), "<=", 4000) > add.constraint(lprec, c(1, 1), "<=", 75)

Let’s have a look at the LP problem that we have defined.

> lprec Model name: C1 C2 Maximize 143 60 R1 120 210 <= 15000 R2 110 30 <= 4000 R3 1 1 <= 75 Kind Std Std Type Real Real Upper Inf Inf Lower 0 0

Solving

> solve(lprec) [1] 0

Getting the optimized profit value

> get.objective(lprec) [1] 6315.625

Let’s finally get the values for how many acres of Wheat and Barley to be planted.

> get.variables(lprec) [1] 21.875 53.125

Thus, to achieve the maximum profit ($6315.625), the farmer should plant 21.875 acres of wheat and 53.125 acres of barley.

### Conclusion

Now that we have figured out how to solve LP problems using excel solver as well as the packages in R, namely Lpsolve and LpsolveAPI. Go ahead and start solving your own Linear Programming problems.

LP Problem sources -

This article was contributed by **Perceptive Analytics**. Rohit, Chaitanya Sagar, Jyothirmayee Thondamallu and Saneesh Veetil contributed to this article.

Perceptive Analytics provides data analytics, data visualization, business intelligence and reporting services to e-commerce, retail, healthcare and pharmaceutical industries. Our client roster includes Fortune 500 and NYSE listed companies in the USA and India.

**Related:**

- A Primer on Web Scraping in R
- Learn Generalized Linear Models (GLM) using R
- 10 Tools to Help You Learn R