Let's look at the LP algorithm for finding the optimal solution with an example.
LP is a methodology for solving problems in the form of first-order functions with objective functions and constraints
5. LP: Linear Programming
Objective Function
Constraints
Decision Variable
Methodology for solving problems in the form of first-order functions
with objective functions and constraints
6. LP: Linear Programming
Some cafes want to sell Americano and Latte.
Americano is made from water and coffee, and latte is made from coffee and milk.
At this time, water is assumed to be 0 won because it is close to free.
Coffee ingredients and milks have to be paid separately.
A total of 80g of coffee is used to make a cup of Americano.
Suppose, on the other hand, for a cup of latte,
60g of coffee raw material and 100ml of milk are used.
One cup of Americano is 300 won, and one cup of latte is 450 won.
Today, our store has 2kg of raw coffee and 3L of milk.
How many cups of Americano and latte each should you make
if you want to make the most of your current ingredients?
7. LP: Linear Programming
Redefine the problem with linear programming
Maximize Z = 300 * X_1 + 450 * X_2
Coffee ingredient: 80 * X_1 + 60 * X_2 <= 2000
Milk: 100 * X_2 <= 3000
X_1(cups of Americano), X_2(cups of Latte)
Objective Function
Constraints
Decision Variable
8. LP: Linear Programming
Using Pulp to make LP problems easier
pip install pulp
https://coin-or.github.io/pulp/main/installing_pulp_at_home.html
9. LP: Linear Programming
Problem definition
from pulp import *
# Objective Function
prob = LpProblem('Make_coffee_revenue', LpMaximize)
# Decision Variable
x1 = LpVariable('Americano', 0, None, LpInteger)
x2 = LpVariable('Milk', 0, None, LpInteger)
prob += 300 * x1 + 450 * x2
# Constraints
prob += 80 * x1 + 60 * x2 <= 2000
prob += 100 * x2 <= 3000
prob.solve()
for v in prob.variables():
print(f'{v.name} = {v.varValue}')
10. LP: Linear Programming
Welcome to the CBC MILP Solver
Version: 2.9.0
Build Date: Feb 12 2015
…
TwoMirCuts was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Result - Optimal solution found
Objective value: 14100.00000000
Enumerated nodes: 0
Total iterations: 0
Time (CPU seconds): 0.00
Time (Wallclock seconds): 0.00
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.00 (Wallclock seconds): 0.00
Americano = 2.0
Milk = 30.0
12. LP: Linear Programming
LP is used for the optimization algorithm and mainly uses natural numbers
because it uses a linear function.
● It’s “Optimization Algorithm” which is finding optimal value by using linear functions
● Optimize operation by adjusting linear variables and making all decisions at once
● Usually suitable for natural number arithmetic, writing objective functions and constraints on
natural numbers
13. DP: Dynamic Programming
DP is a problem that divides and solves big problems into smaller problems.
● DP divides and executes small decisions to find the optimal solution
● DP can define problems from small to complex problems in real world
● The target of DP can define various parts from arrays to strings and trees as problems.
15. LeetCode Problems
● Easy: 1232. Check If It is a Straight Line
● Medium: 11. Container With Most Water
● Medium: 973. K Closest Points to Origin
● Hard: 552. Student Attendance Record II