# Extended data tecdat: R language to solve optimization problem linear programming (LP) problem

Time：2021-7-7

## Source of the original text:Tuo end data tribal official account # Introduction to linear programming

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

To start with the optimization problem, it is very important to determine the goal first. The goal is the quantitative measurement of performance. For example: maximize profit, minimize time, minimize cost, maximize sales.

## The optimization problem can be divided into two groups

1. Linear programming (LP): it is also called linear optimization. In this problem, the goal is to obtain the best result in the mathematical model, where the goal and all constraints are linear functions of the decision variables.
2. Quadratic programming (QP): in quadratic programming, objectives are quadratic functions of decision variables and constraints, which are linear functions of variables. Quadratic function is also a kind of nonlinear programming.

# Linear optimization in R:

Examples of built-in functions for general optimization problems:

• The general parameter structure of the program is as follows
``optimizer(objective, constraints, bounds=NULL, types=NULL, maximum=FALSE)``

Example:

• Define objective function
``f <- function(x) 2 * (x\[1\]-1)^2 + 5 * (x\[2\]-3)^2 + 10``
• optimization
``r < -  optim（c（1,1），f）``
• Check whether the optimization converges to the minimum
``````r  \$  convergence  ==  0  ## Returns true if it converges to the minimum

## \[1\]TRUE``````
• Optimal input parameters
``````R \$par

## \[1\] 1.000168 3.000232``````
• The value of objective function
``````R \$value

## \[1\] 10``````

### Linear programming (LP)

Linear programming is expressed as:

min c T   = min（c 1 x 1 + … + c n x n）

Constraints:

x> = B，x  \> = 0

Linear programming example:

• One company wants to maximize the profits of two products a and B, which are sold for 25 yuan and 20 yuan respectively. There are 1800 units of resources per day. Product a needs 20 units and product B needs 12 units. Both products require 4 minutes of production time, and the total working time available is 8 hours per day. How many products should be produced to maximize profit.

The objective function of the above problem is as follows:

Max (Sales) = max (25 x1  + 20 x2）

Among them,
X1 is the production quantity of product a
X2 is the production quantity of product B
x   1 and X   2 is also called decision variable

Constraints (resources and time) in the problem:

20×1  + 12 x2  <= 1800 (resource constraints)
4×1  + 4×2  <= 8 * 60 (time constraint)

Solve the above problems in R:

Since this is a linear programming problem, we will use_ lp（）  _ Function to find the optimal solution_ lp（）  _ Syntax of function    Yes:

``lp（direction =“min”，objective.in，const.mat，const.dir，const.rhs）``
• _ Direction_ Is it minimizing or maximizing
• Objective function_ objective.in_
• Constraints_ A_ As a matrix_ Const. Mat gives ﹣ U, The direction is_ const.dir_
• constraintbAs a vector_ const.rhs_ insert
``````##Set the coefficients of the decision variables
objective.in < - c（25,20）

##Create constraint martix
const.mat < - martix（c（20,12,4,4） ）

##   Define constraints
time_constraint < - （8 * 60）

##Find the best solution
Best<  -    lp（direction  =" max"，objective.in，const.mat，const.dir，const.rhs）``````
``````##The best values of X1 and X2 are shown

## \[1\] 45 75

##Check the value of the objective function at the best point

## \[1\] 2625``````

From the above output, we can see that the company should produce 45 products a and 75 products B in order to obtain the sales of 2625 yuan.

After establishing the objective function and constraints, we can extend the same method to solve other LP problems in R. 