# You want to evaluate your savi

You want to evaluate your savings of 2.500.000 $. If you deposityour money in the A bank, you get 9% interest income in every threeyears. The B bank provides 4% interest income per year. The B bankalso provides 6% annual income to the 4-year private account and 6%annual income to the 7-year private account. Also, if you investyour money in a 4-year government bond, 7.5% interest income peryear. However, the government deducts 15% tax in every two yearsfrom the interest income you get from the bank and you have to pay200.000 $ (rising 4% in every two years) every three years for theinstallment of the house you buy. How should you invest 2.500.000 $to maximize the earnings at the end of 9 years.

**(Create only the LP model, do not solve theproblem)**

Answer:

Thank u, please like this answer and support us please,

and please dont give us any hate, this is the best answer foryour question,

Answer::

Introduction

Optimization is the way of life. We all have finite resourcesand time and we want to make the most of them. From using your timeproductively to solving supply chain problems for your company –everything uses optimization. It’s an especially interesting andrelevant topic in data science.

It is also a very interesting topic – it starts with simpleproblems, but it can get very complex. For example, sharing a barof chocolate between siblings is a simple optimization problem. Wedon’t think in mathematical terms while solving it. On the otherhand, devising inventory and warehousing strategy for an e-tailercan be very complex. Millions of SKUs with different popularity indifferent regions to be delivered in defined time and resources –you see what I mean!

Linear programming (LP) is one of the simplest ways to performoptimization. It helps you solve some very complex optimizationproblems by making a few simplifying assumptions. As an analyst,you are bound to come across applications and problems to be solvedby Linear Programming.

For some reason, LP doesn’t get as much attention as it deserveswhile learning data science. So, I thought let me do justice tothis awesome technique. I decided to write an article that explainsLinear programming in simple English. I have kept the content assimple as possible. The idea is to get you started and excitedabout Linear Programming.

**Note-** If you want to learn this in a courseformat, we have curated this free course for you- LinearProgramming for Data Science Professionals

Table of Content

- What is Linear Programming?
- Basic Terminologies
- The process to define an LP problem

- Solve Linear Program by Graphical Method
- Solve Linear Program using R
- Solve Linear Program using OpenSolver
- Simplex Method
- Northwest Corner Method and Least Cost Method
- Applications of Linear programming

1. What is Linear Programming?

Now, what is linear programming? Linear programming is a simpletechnique where we **depict** complex relationshipsthrough linear functions and then find the optimum points. Theimportant word in the previous sentence is depicted. The realrelationships might be much more complex – but we can simplify themto linear relationships.

Applications of linear programming are everywhere around you.You use linear programming at personal and professional fronts. Youare using linear programming when you are driving from home to workand want to take the shortest route. Or when you have a projectdelivery you make strategies to make your team work efficiently foron-time delivery.

Example of a linear programming problem

Let’s say a FedEx delivery man has 6 packages to deliver in aday. The warehouse is located at point A. The 6 deliverydestinations are given by U, V, W, X, Y, and Z. The numbers on thelines indicate the distance between the cities. To save on fuel andtime the delivery person wants to take the shortest route.

So, the delivery person will calculate different routes forgoing to all the 6 destinations and then come up with the shortestroute. This technique of choosing the shortest route is calledlinear programming.

In this case, the objective of the delivery person is to deliverthe parcel on time at all 6 destinations. The process of choosingthe best route is called Operation Research. Operation research isan approach to decision-making, which involves a set of methods tooperate a system. In the above example, my system was the Deliverymodel.

Linear programming is used for obtaining the most optimalsolution for a problem with given constraints. In linearprogramming, we formulate our real-life problem into a mathematicalmodel. It involves an objective function, linear inequalities withsubject to constraints.

Is the linear representation of the 6 points aboverepresentative of the real-world? Yes and No. It is anoversimplification as the real route would not be a straight line.It would likely have multiple turns, U-turns, signals and trafficjams. But with a simple assumption, we have reduced the complexityof the problem drastically and are creating a solution that shouldwork in most scenarios.

Formulating a problem – Let’s manufacture some chocolates

**Example:** Consider a chocolate manufacturingcompany that produces only two types of chocolate – A and B. Boththe chocolates require Milk and Choco only. To manufacture eachunit of A and B, the following quantities are required:

- Each unit of A requires 1 unit of Milk and 3 units ofChoco
- Each unit of B requires 1 unit of Milk and 2 units ofChoco

The company kitchen has a total of 5 units of Milk and 12 unitsof Choco. On each sale, the company makes a profit of

- Rs 6 per unit A sold
- Rs 5 per unit B sold.

Now, the company wishes to maximize its profit. How many unitsof A and B should it produce respectively?

**Solution:** The first thing I’m gonna do isrepresent the problem in a tabular form for betterunderstanding.

Milk | Choco | Profit per unit | |

A | 1 | 3 | Rs 6 |

B | 1 | 2 | Rs 5 |

Total | 5 | 12 |

Let the total number of units produced by A be = X

Let the total number of units produced by B be = Y

Now, the total profit is represented by Z

The total profit the company makes is given by the total numberof units of A and B produced multiplied by its per-unit profit ofRs 6 and Rs 5 respectively.

**Profit: Max Z = 6X+5Y**

which means we have to maximize Z.

The company will try to produce as many units of A and B tomaximize the profit. But the resources Milk and Choco are availablein a limited amount.

As per the above table, each unit of A and B requires 1 unit ofMilk. The total amount of Milk available is 5 units. To representthis mathematically,

**X+Y ≤ 5**

Also, each unit of A and B requires 3 units & 2 units ofChoco respectively. The total amount of Choco available is 12units. To represent this mathematically,

**3X+2Y ≤ 12**

Also, the values for units of A can only be integers.

So we have two more constraints, **X ≥ 0 & Y ≥0**

For the company to make maximum profit, the above inequalitieshave to be satisfied.

**This is called formulating a real-world problem into amathematical model.**

Common terminologies used in Linear Programming

Let us define some terminologies used in Linear Programmingusing the above example.

**Decision Variables:**The decision variables arethe variables that will decide my output. They represent myultimate solution. To solve any problem, we first need to identifythe decision variables. For the above example, the total number ofunits for A and B denoted by X & Y respectively are my decisionvariables.**Objective Function:**It is defined as theobjective of making decisions. In the above example, the companywishes to increase the total profit represented by Z. So, profit ismy objective function.**Constraints:**The constraints are therestrictions or limitations on the decision variables. They usuallylimit the value of the decision variables. In the above example,the limit on the availability of resources Milk and Choco are myconstraints.**Non-negativity restriction:**For all linearprograms, the decision variables should always take non-negativevalues. This means the values for decision variables should begreater than or equal to 0.

The process to formulate a Linear Programming problem

Let us look at the steps of defining a Linear Programmingproblem generically:

- Identify the decision variables
- Write the objective function
- Mention the constraints
- Explicitly state the non-negativity restriction

For a problem to be a linear programming problem, the decisionvariables, objective function and constraints all have to be linearfunctions.

If all the three conditions are satisfied, it is called a**Linear Programming Problem**.

2. Solve Linear Programs by Graphical Method

A linear program can be solved by multiple methods. In thissection, we are going to look at the Graphical method for solving alinear program. This method is used to solve a two-variable linearprogram. If you have only two decision variables, you should usethe graphical method to find the optimal solution.

A graphical method involves formulating a set of linearinequalities subject to the constraints. Then the inequalities areplotted on an X-Y plane. Once we have plotted all the inequalitieson a graph the intersecting region gives us a feasible region. Thefeasible region explains what all values our model can take. And italso gives us the optimal solution.

Let’s understand this with the help of an example.

**Example:** A farmer has recently acquired a 110hectares piece of land. He has decided to grow Wheat and barley onthat land. Due to the quality of the sun and the region’s excellentclimate, the entire production of Wheat and Barley can be sold. Hewants to know how to plant each variety in the 110 hectares, giventhe costs, net profits and labor requirements according to the datashown below:

Variety | Cost (Price/Hec) | Net Profit (Price/Hec) | Man-days/Hec |

Wheat | 100 | 50 | 10 |

Barley | 200 | 120 | 30 |

The farmer has a budget of US$10,000 and availability of 1,200man-days during the planning horizon. Find the optimal solution andthe optimal value.

**Solution:** To solve this problem, first we gonnaformulate our linear program.

Formulation of Linear Problem

**Step 1: Identify the decision variables**

The total area for growing Wheat = X (in hectares)

The total area for growing Barley = Y (in hectares)

X and Y are my decision variables.

**Step 2: Write the objective function**

Since the production from the entire land can be sold in themarket. The farmer would want to maximize the profit for his totalproduce. We are given net profit for both Wheat and Barley. Thefarmer earns a net profit of US$50 for each hectare of Wheat andUS$120 for each Barley.

Our objective function (given by Z) is, **Max Z = 50X +120Y**

**Step 3: Writing the constraints**

1. It is given that the farmer has a total budget of US$10,000.The cost of producing Wheat and Barley per hectare is also given tous. We have an upper cap on the total cost spent by the farmer. Soour equation becomes:

**100X + 200Y ≤ 10,000**

2. The next constraint is the upper cap on the availability ofthe total number of man-days for the planning horizon. The totalnumber of man-days available is 1200. As per the table, we aregiven the man-days per hectare for Wheat and Barley.

**10X + 30Y ≤ 1200**

3. The third constraint is the total area present forplantation. The total available area is 110 hectares. So theequation becomes,

**X + Y ≤ 110**

**Step 4: The non-negativity restriction**

The values of X and Y will be greater than or equal to 0. Thisgoes without saying.

**X ≥ 0, Y ≥ 0**

We have formulated our linear program. It’s time to solveit.

Solving an LP through Graphical method

Since we know that X, Y ≥ 0. We will consider only the firstquadrant.

To plot for the graph for the above equations, first I willsimplify all the equations.

100X + 200Y ≤ 10,000 can be simplified to X + 2Y ≤ 100 bydividing by 100.

10X + 30Y ≤ 1200 can be simplified to X + 3Y ≤ 120 by dividingby 10.

The third equation is in its simplified form, X + Y ≤ 110.

Plot the first 2 lines on a graph in the first quadrant (likeshown below)

The optimal feasible solution is achieved at the point ofintersection where the budget & man-days constraints areactive. This means the point at which the equations X + 2Y ≤ 100and X + 3Y ≤ 120 intersect gives us the optimal solution.

The values for X and Y which gives the optimal solution is at(60,20).

To maximize profit the farmer should produce Wheat and Barley in60 hectares and 20 hectares of land respectively.

The maximum profit the company will gain is,

Max Z = 50 * (60) + 120 * (20)

= US$5400

**Note:** Everything taught here has also beentaught in a course format in this free course- Linear Programmingfor Data Science Professionals

3. Solve Linear Program Using R

R is an open-source tool that is very popular among the datascientists for essential data science tasks. Performing linearprogramming is very easy and we can attain an optimum solution invery few steps. Come let’s learn.

**Example:** A toy manufacturing organizationmanufactures two types of toys A and B. Both the toys are sold atRs.25 and Rs.20 respectively. There are 2000 resource unitsavailable every day from which the toy A requires 20 units whiletoy B requires 12 units. Both of these toys require a productiontime of 5 minutes. Total working hours are 9 hours a day. Whatshould be the manufacturing quantity for each of the pipes tomaximize the profits?

Here:

The objective function is:Max.Z=25x+20y

where x are the units of pipe A

y are the units of pipe B

Constraints:20x+12y<=2000

5x+5y<=540

Let’s see the code part now:

install.packages(“lpSolve”) | |

library(lpSolve) | |

#Setting the coefficients of decision variables | |

objective.in=c(25,20) | |

#Constraint Matrix | |

const.mat=matrix(c(20,12,5,5),nrow = 2,byrow = T) | |

#defining constraints | |

const_time=540 #in minutes | |

const_res=2000 | |

#RHS for constraints | |

const.rhs=c(const_res,const_time) | |

#Direction for constraints | |

const.dir=c(“<=”,”<=”) | |

#Finding the optimum solution | |

opt=lp(direction =”max”,objective.in,const.mat,const.dir,const.rhs) | |

summary(opt) | |

#Objective values of x and y | |

opt$solution | |

#Value of objective function at optimal point | |

opt$objval |

view rawlpp.R hosted with ❤ by GitHub

Output

summary(opt)Length Class Mode direction 1 -none- numeric x.count 1 -none- numeric objective 2 -none- numeric const.count 1 -none- numeric constraints 8 -none- numeric int.count 1 -none- numeric int.vec 1 -none- numeric bin.count 1 -none- numeric binary.vec 1 -none- numeric num.bin.solns 1 -none- numeric objval 1 -none- numeric solution 2 -none- numeric presolve 1 -none- numeric compute.sens 1 -none- numeric sens.coef.from 1 -none- numeric sens.coef.to 1 -none- numeric duals 1 -none- numeric duals.from 1 -none- numeric duals.to 1 -none- numeric scale 1 -none- numeric use.dense 1 -none- numeric dense.col 1 -none- numeric dense.val 1 -none- numeric dense.const.nrow 1 -none- numeric dense.ctr 1 -none- numeric use.rw 1 -none- numeric tmp 1 -none- characterstatus 1 -none- numeric > opt$solution[1] 88 20> opt$objval[1] 2600

Therefore from the output, we see that the organization shouldproduce 88 units of toy A and 20 units of toy B and the maximumprofit for the organization will be Rs.2600.