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.
What is optimization?
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. To start with an optimization problem, it is important to first identify an objective. An objective is a quantitative measure of performance. For example: to maximize profits, minimize time, minimize costs, maximize sales.
There are a variety of optimization techniques -
In certain cases the variable can be freely selected within it’s full range. The optim() function in R can be used for 1- dimensional or n-dimensional problems. The general format for the optim() function is -
We start off with an example, let’s define the objective function what we are looking to solve -
Setting up the constraints next
The optimization function is invoked
Next we check if the optimization converged to a minimum or not. The easy way to do this is to check if
The optimization has converged to minimum. Finding out the input arguments for the optimization function can be obtained by
The value of objective function at the minimum is obtained by
Here is a good definition from technopedia - “Linear programming is a mathematical method that is used to determine the best possible outcome or solution from a given set of parameters or list of requirements, which are represented in the form of linear relationships. It is most often used in computer modeling or simulation in order to find the best solution in allocating finite resources such as money, energy, manpower, machine resources, time, space and many other variables. In most cases, the "best outcome" needed from linear programming is maximum profit or lowest cost.”
An example of a LP problem is -
Maximize or Minimize objective function: f(y1, y2) = g1.y1 + g2.y2
Subjected to inequality constraints:
- g11.y1 + g12.y2 <= p1
- g21.y1 + g22.y2 <= p2
- g31.y1 + g32.y2 <= p3
- y1 >= 0, y2 >=0
A company wants to maximize the profit for two products A and B which are sold at $ 25 and $ 20 respectively. There are 1800 resource units available every day and product A requires 20 units while B requires 12 units. Both of these products require a production time of 4 minutes and total available working hours are 8 in a day. What should be the production quantity for each of the products to maximize profits?
A LP problem can either be a maximization problem or a minimization problem. The Above problem is a maximization problem. Some of the steps that should be followed while defining a LP problem are -
- Identify the decision variables
- Write the objective function
- Mention the constraints
- Explicitly state the non-negativity restriction
Lets walk through the above example -
As already defined this is a maximization problem, first we define the objective function.
max(Sales) = max(25 y1 + 20 y2)
- y1 is the units of Product A produced
- y2 is the units of Product B produced
- y1 and y2 are called the decision variables
- 25 and 20 are the selling price of the products
We are trying to maximize the sales while finding out the optimum number of products to manufacture. Now we set the constraints for this particular LP problem. We are dealing with both resource and time constraints.
20y1 + 12 y2 <= 1800 (Resource Constraint)
4y1 + 4y2 <= 8*60 (Time constraint)
There are two ways to solve a LP problem
- Graphical Method
- Simplex Method
We will be solving this problem using the simplex method but in R. We shall also explain another example with excel’s solver. There are a couple of packages in R to solve LP problems. Some of the popular ones are -
Implementation in R using Lpsolve
Let’s use lpsolve for this problem. First we need to set the objective function, this has already been defined.
Creating a matrix for the constraints, we create a 2 by 2 matrix for this. And then setting constraints.
Now we are basically creating the equations that we have already defined by setting the rhs and the direction of the constraints.
The final step is to find the optimal solution. The syntax for the lpsolve package is -
lp(direction , objective, const.mat, const.dir, const.rhs)
Now we get the optimum values for y1 and y2, i.e the number of product A and product B that should be manufactured.
The maximum sales figure is -
Top Stories Past 30 Days