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.
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.
g = (110)(1.30)x + (30)(2.00)y = 143x + 60y
120x + 210y <= 15000
110x + 30y <= 4000
x + y <= 75
x >= 0
y >= 0
We will be solving this problem in both excel and R.
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.
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.
Next we set the objective function to maximization.
We define the objective function and the constraints
Let’s have a look at the LP problem that we have defined.
Getting the optimized profit value
Let’s finally get the values for how many acres of Wheat and Barley to be planted.
Thus, to achieve the maximum profit ($6315.625), the farmer should plant 21.875 acres of wheat and 53.125 acres of barley.
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.
- A Primer on Web Scraping in R
- Learn Generalized Linear Models (GLM) using R
- 10 Tools to Help You Learn R
Top Stories Past 30 Days