Material 2
Material 2
Material 2
with Python
• Linear Programming
• Mixed-Integer Linear Programming
• Nonlinear Programming
• Mixed-Integer Nonlinear Programming
• Heuristics (GA and Particle Swarm)
• Constraint Programming
Rafael Silva Pinto
rafaelcxc@gmail.com
• Installing Python and Packages
• Starting with Python
• Linear Programming (LP)
• Mixed-Integer Linear Programming (MILP)
2𝑥 + 𝑦 ≤ 14
Constraints 2𝑥 − 𝑦 ≤ 10
2y <= x+8 (time of production)
2x + y <= 14 (raw material) 0 ≤ 𝑥 ≤ 10
PROBLEM PROBLEM
RESOLUTION RESULTS
UNDERSTANDING MODELING
• Search for the optimal decision
• Any problem of planning:
long, medium, short term, operational
• Applied in the decision-making for investments,
operations, route problems, cost reduction...
What is • Ex.: We want to maximize the revenue for
max 𝑥 + 𝑦
the sale of 2 products (x and y), each
optimization product costs 1 dollar. What is the required
daily production?
−𝑥 + 2𝑦 ≤ 8
𝟐𝒙 + 𝒚𝒙 ≤ 14
Constraints 2𝑥 − 𝑦 ≤ 10
2y <= x+8 (time of production)
x (2 + y) <= 14 (raw material) 0 ≤ 𝑥 ≤ 10
PROBLEM PROBLEM
RESOLUTION RESULTS
UNDERSTANDING MODELING
• Search for the optimal decision
• Any problem of planning:
long, medium, short term, operational
• Applied in the decision-making for investments,
operations, route problems, cost reduction...
What is • Ex.: We want to maximize the revenue for
max 𝑥 + 𝑦
the sale of 2 products (x and y), each
optimization product costs 1 dollar. What is the required
daily production?
−𝑥 + 2𝑦 ≤ 8
2𝑥 + 𝑦 ≤ 14
Constraints 2𝑥 − 𝑦 ≤ 10
2y <= x+8 (time of production)
0 ≤ 𝑥 ≤ 10
2x + y <= 14 (raw material)
2x <= y+10 (historical sales) 0 ≤ 𝑦 ≤ 10
x,y <= 10 (maximum daily production)
𝑥 and 𝑦 integers
PROBLEM PROBLEM
RESOLUTION RESULTS
UNDERSTANDING MODELING
INTRODUCTION TO
MATHEMATICAL
MODELING
- What is mathematical modeling
- How do we solve mathematical problems?
- Type of variables
- Objective Function and Constraints
- How to model your problem?
- Some examples
- How to learn more?
WHAT IS I wish to minimize the total cost of
𝑪𝒐𝒔𝒕(𝒊) = …..
Real-world problem → Mathematics
OPTIMIZATION
UNDERSTANDING
PROBLEM
PROBLEMS? MODELING
FRAMEWORK
RESOLUTION
SOLVER
First, understand the problem!
RESULTS
Second, convert your problem to math
What is a set?
OBJECTIVE
FUNCTION AND
CONSTRAINTS
max 𝑥 + 𝑦
How to define if a solution is better than other?
−𝑥 + 2𝑦 ≤ 8
What defines if a solution is feasible? 2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
𝑥, 𝑦 ≥ 0
HOW TO MODEL PROBLEM
UNDERSTANDING
RESOLUTION
RESULTS
EXAMPLE 1
Mayke wish to define the best investments that he should make
with his money. He has a total of 100,000 USD and the
following options for investment.
Mayke wish to define the best investments that he should make Constraints (rules)
with his money. He has a total of 100,000 USD and the 𝐶𝐴 + 𝐶𝐵 + 𝐶𝐶 = 100,000
following options for investment.
𝑅𝐴 = 0.05𝐶𝐴
A) Low risk fund with historical gains of 5% per year 𝑅𝐵 = 0.10𝐶𝐵
B) Medium risk fund with historical gains 10% per year
C) High risk fund with historical gains of 12% per year 𝑅𝐶 = 0.12𝐶𝐶
0 ≤ 𝐶𝐵 ≤ 0.2 ∗ 100,000
Mayke wish to control the risk of his investments with
maximum of 10% of his money in the investment with high risk, 0 ≤ 𝐶𝐶 ≤ 0.1 ∗ 100,000
20% in the investment with medium risk.
Objective Function
Which is the decision of investments in A, B, and C that
maximize the return of investment for Mayke? max(𝑅𝐴 + 𝑅𝐵 + 𝑅𝐶 )
EXAMPLE 2
Mayke wish to define the best investments that he should make
with his money. He has a total of 100,000 USD and the
following options for investment.
EXAMPLE 2 𝐴, 𝐵, 𝐶, 𝐷 → funds
Variables
Mayke wish to define the best investments that he should make 𝐶𝐴 , 𝐶𝐵 , 𝐶𝐶 , 𝐶𝐷 → Invested capital in the fund
with his money. He has a total of 100,000 USD and the
following options for investment. 𝑅𝐴 , 𝑅𝐵 , 𝑅𝐶 , 𝑅𝐷 → Return of investment from the fund
EXAMPLE 2 max 𝑅𝐴 + 𝑅𝐵 + 𝑅𝐶 + 𝑅𝐷
EXAMPLE 2 max 𝑅𝐴 + 𝑅𝐵 + 𝑅𝐶 + 𝑅𝐷
Constraints (rules)
Mayke wish to define the best investments that he should make 𝐶𝐴 + 𝐶𝐵 + 𝐶𝐶 + 𝐶𝐷 = 100,000
with his money. He has a total of 100,000 USD and the
𝑅𝐴 = 0.05𝐶𝐴
following options for investment.
𝑅𝐵 = 0.10𝐶𝐵
A) Low risk fund with historical gains of 5% per year
𝑅𝐶 = 0.12𝐶𝐶
B) Medium risk fund with historical gains 10% per year
C) High risk fund with historical gains of 12% per year 𝑅𝐷 = 10−6 𝐶𝐷 2
EXAMPLE 2 max 𝑅𝐴 + 𝑅𝐵 + 𝑅𝐶 + 𝑅𝐷
Same as
Mayke wish to define the best investments that he should make
with his money. He has a total of 100,000 USD and the
max 𝑅𝑓
following options for investment.
𝑓∈𝐹
EXAMPLE 2 𝐶𝐴 + 𝐶𝐵 + 𝐶𝐶 + 𝐶𝐷 = 100,000
𝐶𝑓 = 100,000
𝑓∈𝐹
Mayke wish to define the best investments that he should make 𝑅𝐴 = 0.05𝐶𝐴
with his money. He has a total of 100,000 USD and the 𝑅𝐵 = 0.10𝐶𝐵
following options for investment.
𝑅𝐶 = 0.12𝐶𝐶
A) Low risk fund with historical gains of 5% per year 𝑅𝐷 = 10−6 𝐶𝐷 2
B) Medium risk fund with historical gains 10% per year
C) High risk fund with historical gains of 12% per year 𝑅𝑓 = 𝑟𝑒𝑡𝑢𝑟𝑛 𝐶𝑓 ∀𝑓
D) Especial fund: 10−6 ∗ 𝐶𝐷 2
Mayke wish to control the risk of his investments with 0 ≤ 𝐶𝐵 ≤ 0.2 ∗ 100,000
maximum of 10% of his money in the investment with high risk,
0 ≤ 𝐶𝐶 ≤ 0.1 ∗ 100,000
20% in the investment with medium risk, and 30% in the
especial fund. 0 ≤ 𝐶𝐷 ≤ 0.3 ∗ 100,000
EXAMPLE 2 max 𝑅𝑓
𝑓∈𝐹
Constraints (rules)
Mayke wish to define the best investments that he should make 𝐶𝑓 = 100,000
with his money. He has a total of 100,000 USD and the 𝑓∈𝐹
following options for investment.
𝑅𝑓 = 𝑟𝑒𝑡𝑢𝑟𝑛 𝐶𝑓 ∀𝑓
A) Low risk fund with historical gains of 5% per year
𝐶𝑓𝑚𝑖𝑛 ≤ 𝐶𝑓 ≤ 𝐶𝑓𝑚𝑎𝑥 ∀𝑓
B) Medium risk fund with historical gains 10% per year
C) High risk fund with historical gains of 12% per year
D) Especial fund: 10−6 ∗ 𝐶𝐷 2
You have a company of shoes with 3 very large machines, and you
wish to minimize the total cost of production.
You have a company of shoes with 3 very large machines, and you
Constraints (rules)
wish to minimize the total cost of production.
𝑃𝐴 + 𝑃𝐵 + 𝑃𝐶 = 10,000
The total cost of production of each machine is:
𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 0.1
A) 𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 0.1
B) 𝐶𝐵 = 0.3𝑃𝐵 + 0.5 𝐶𝐵 = 0.3𝑃𝐵 + 0.5
C) 𝐶𝐶 = 0.01𝑃𝐶3
𝐶𝐶 = 0.01𝑃𝐶3
where 𝐶 is the cost of production of 𝑃 products for each machine 𝑃𝐴 , 𝑃𝐵 , 𝑃𝐶 ≥ 0
You have a company of shoes with 3 very large machines, and you
Constraints (rules)
wish to minimize the total cost of production.
𝑃𝑚 = 10,000
The total cost of production of each machine is: 𝑚
A) 𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 0.1
𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 0.1
B) 𝐶𝐵 = 0.3𝑃𝐵 + 0.5
C) 𝐶𝐶 = 0.01𝑃𝐶3 𝐶𝐵 = 0.3𝑃𝐵 + 0.5
𝐶𝐶 = 0.01𝑃𝐶3
where 𝐶 is the cost of production of 𝑃 products for each machine
𝑃𝐴 , 𝑃𝐵 , 𝑃𝐶 ≥ 0
In the next month, you have a demand of 10,000 shoes. What is
the number of products that should be assigned to each machine
in order to minimize the total cost? Objective Function
min 𝐶𝑚
𝑚 𝑚 = {𝐴, 𝐵, 𝐶}
Variables
You have a company of shoes with 3 very large machines, and you
Constraints (rules)
wish to minimize the total cost of production.
𝑃𝑚 = 10,000
The total cost of production of each machine is: 𝑚
A) 𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 0.1
𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 0.1
B) 𝐶𝐵 = 0.3𝑃𝐵 + 0.5
C) 𝐶𝐶 = 0.01𝑃𝐶3 𝐶𝐵 = 0.3𝑃𝐵 + 0.5
𝐶𝐶 = 0.01𝑃𝐶3
where 𝐶 is the cost of production of 𝑃 products for each machine
𝑃𝐴 , 𝑃𝐵 , 𝑃𝐶 ≥ 0
In the next month, you have a demand of 10,000 shoes. What is
the number of products that should be assigned to each machine
in order to minimize the total cost? Objective Function
min 𝐶𝑚
𝑚 𝑚 = {𝐴, 𝐵, 𝐶}
Objective Function
EXAMPLE 3 min 𝐶𝑚
𝑚
Constraints (rules)
You have a company of shoes with 3 very large machines, and you
wish to minimize the total cost of production. 𝑃𝑚 = 10,000
𝑚
The total cost of production of each machine is: 𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 𝛽𝐴 0.1
A) 𝐶𝐴 = 0.1𝑃𝐴2 + 0.5𝑃𝐴 + 0.1 𝐶𝐵 = 0.3𝑃𝐵 + 𝛽𝐵 0.5
B) 𝐶𝐵 = 0.3𝑃𝐵 + 0.5
C) 𝐶𝐶 = 0.01𝑃𝐶3 𝐶𝐶 = 0.01𝑃𝐶3
𝑃𝐴 ≤ 𝛽𝐴 𝑀
where 𝐶 is the cost of production of 𝑃 products for each machine
𝑃𝐵 ≤ 𝛽𝐵 𝑀
In the next month, you have a demand of 10,000 shoes. What is 𝑃𝐴 , 𝑃𝐵 , 𝑃𝐶 ≥ 0
the number of products that should be assigned to each machine
in order to minimize the total cost?
2 P1
30
B
A 10 10
5
7 P3
P2 8
2 P1
30
B
A 10 10
5
7 P3
P2 8
Parameters
𝐷𝑖,𝑗 → Distance from point i to j
Create a generic formulation to minimize the path from point A to B
2 P1
30
B
A 10 10
5
7 P3
P2 8
Parameters
𝐷𝑖,𝑗 → Distance from point i to j
Create a generic formulation to minimize the path from point A to B
Sets
Ω𝑖𝑛
𝑖 → set of nodes that connect to arcs entering node 𝑖
2 P1
30
B
Ω𝑜𝑢𝑡
𝑖 → set of nodes that connect to arcs exiting node 𝑖
A 10 10
5 Example:
7 P3
Ω𝑖𝑛
𝑃1 = {𝐴, 𝑃2} Ω𝑖𝑛
𝐵 = {𝑃1, 𝑃3}
P2 8 Ω𝑜𝑢𝑡
𝑃1 = {𝑃2, 𝐵} Ω𝐴𝑜𝑢𝑡 = {𝑃1, 𝑃2}
Ω𝑜𝑢𝑡
𝑖 → set of nodes that connect to arcs exiting node 𝑖
Create a generic formulation to minimize the path from point A to B
Objective Function
A 10 10 Constraints
5
7 P3 𝑥𝐴,𝑗 = 1
P2 8 𝑗∈Ω𝑜𝑢𝑡
𝐴
𝑥𝑖,𝐵 = 1
𝑖∈Ω𝑖𝑛
𝐵
The numbers are the distances from one point to another
𝑥𝑖,𝑗 = 𝑥𝑗,𝑖 ∀𝑖\{𝐴, 𝐵}
𝑗∈Ω𝑜𝑢𝑡
𝑖 𝑗∈Ω𝑖𝑛
𝑖
EXAMPLE 5
Petter has a construction company. He needs to assign 5 of the
company’s teams to work in some of the constructions below:
Parameters
𝑅𝑜 → Revenue of construction 𝑜
Petter has a construction company. He needs to assign 5 of the 𝑁𝑇𝑜 → Number of teams required for the construction 𝑜
company’s teams to work in some of the constructions below:
𝑁𝑇 𝑚𝑎𝑥 → Total number of teams (available) (5)
A) Revenue of 500, requires 1 team
B) Revenue of 4,000, requires 3 teams
C) Revenue of 3,000, requires 2 teams
D) Revenue of 2,000, requires 1 team
E) Revenue of 2,000, requires 5 teams
Parameters
𝑅𝑜 → Revenue of construction 𝑜
Petter has a construction company. He needs to assign 5 of the 𝑁𝑇𝑜 → Number of teams required for the construction 𝑜
company’s teams to work in some of the constructions below:
𝑁𝑇 𝑚𝑎𝑥 → Total number of teams (available) (5)
A) Revenue of 500, requires 1 team
B) Revenue of 4,000, requires 3 teams Objective Function
C) Revenue of 3,000, requires 2 teams
D) Revenue of 2,000, requires 1 team max 𝑥𝑜 𝑅𝑜
E) Revenue of 2,000, requires 5 teams 𝑜
Constraints
Select the constructions that would maximize the revenue.
- Each construction can be made just once
𝑥𝑜 𝑁𝑇𝑜 ≤ 𝑁𝑇 𝑚𝑎𝑥
- Not all constructions will be selected
𝑜
Variables
Parameters
𝑅𝑜 → Revenue of construction 𝑜
Petter has a construction company. He needs to assign 5 of the 𝑁𝑇𝑜 → Number of teams required for the construction 𝑜
company’s teams to work in some of the constructions below:
𝑁𝑇 𝑚𝑎𝑥 → Total number of teams (available) (5)
A) Revenue of 500, requires 1 team
B) Revenue of 4,000, requires 3 teams Objective Function
C) Revenue of 3,000, requires 2 teams
D) Revenue of 2,000, requires 1 team max 𝑥𝑜 𝑅𝑜
E) Revenue of 2,000, requires 5 teams 𝑜
Constraints
Select the constructions that would maximize the revenue.
- Each construction can be made just once
𝑥𝑜 𝑁𝑇𝑜 ≤ 𝑁𝑇 𝑚𝑎𝑥
- Not all constructions will be selected
𝑜
Parameters
𝑅𝑜 → Revenue of construction 𝑜
Petter has a construction company. He needs to assign 5 of the 𝑁𝑇𝑜 → Number of teams required for the construction 𝑜
company’s teams to work in some of the constructions below:
𝑁𝑇 𝑚𝑎𝑥 → Total number of teams (available) (5)
A) Revenue of 500, requires 1 team
B) Revenue of 4,000, requires 3 teams Objective Function
C) Revenue of 3,000, requires 2 teams
D) Revenue of 2,000, requires 1 team max 𝑥𝑜 𝑅𝑜
E) Revenue of 2,000, requires 5 teams 𝑜
Constraints
Select the constructions that would maximize the revenue.
- Each construction can be made just once
𝑥𝑜 𝑁𝑇𝑜 ≤ 𝑁𝑇 𝑚𝑎𝑥
- Not all constructions will be selected
𝑜
- Construction C can only be selected if A is selected
- Construction D can only be selected if A and C are selected
Variables
𝑥𝑜 → Binary decision on selecting (or not) construction 𝑜
EXAMPLE 6 Parameters
𝑅𝑜 → Revenue of construction 𝑜
𝑁𝑇𝑜 → Number of teams required for the construction 𝑜
Petter has a construction company. He needs to assign 5 of the
company’s teams to work in some of the constructions below: 𝑁𝑇 𝑚𝑎𝑥 → Total number of teams (available) (5)
Select the constructions that would maximize the revenue. 𝑥𝑜 𝑁𝑇𝑜 ≤ 𝑁𝑇 𝑚𝑎𝑥
- Each construction can be made just once 𝑜
𝑥𝐷 ≤ 𝑥𝐶
Solution: Constructions A, C, and D
Variables
Parameters
𝑅𝑜 → Revenue of construction 𝑜
Petter has a construction company. He needs to assign 5 of the 𝑁𝑇𝑜 → Number of teams required for the construction 𝑜
company’s teams to work in some of the constructions below: 𝑁𝑇 𝑚𝑎𝑥 → Total number of teams (available)
The list of jobs (demands) with the duration of job and its profit is
defined below:
A) duration 2h, profit 200 USD
B) duration 3h, profit 500 USD
C) duration 5h, profit 300 USD
D) duration 2h, profit 100 USD
E) duration 6h, profit 1,000 USD
F) duration 4h, profit 300 USD
Mark wants to maximize the profit for the next 3 days working 6h
per day. Which demands he should attend per day?
- Neglect the traveling time
- Each demand only can be attended once
Variables
Parameters
𝑃𝑗 → Profit for the job
Mark wishes to define the scheduling of costumers that he must 𝐷𝑗 → Duration of the job in hours
attend in the next 3 days. 𝑇ℎ → Number of hours in a working day (6)
The list of jobs (demands) with the duration of job and its profit is
defined below:
A) duration 2h, profit 200 USD
B) duration 3h, profit 500 USD
C) duration 5h, profit 300 USD
D) duration 2h, profit 100 USD
E) duration 6h, profit 1,000 USD
F) duration 4h, profit 300 USD
Mark wants to maximize the profit for the next 3 days working 6h
per day. Which demands he should attend per day?
- Neglect the traveling time
- Each demand only can be attended once
Variables
Parameters
𝑃𝑗 → Profit for the job
Mark wishes to define the scheduling of costumers that he must 𝐷𝑗 → Duration of the job in hours
attend in the next 3 days. 𝑇ℎ → Number of hours in a working day (6)
The list of jobs (demands) with the duration of job and its profit is
defined below: Objective Function
A) duration 2h, profit 200 USD
max 𝑥𝑗,𝑑 𝑃𝑗
B) duration 3h, profit 500 USD
𝑗 𝑑
C) duration 5h, profit 300 USD
D) duration 2h, profit 100 USD Constraints
E) duration 6h, profit 1,000 USD
F) duration 4h, profit 300 USD 𝑥𝑗,𝑑 𝐷𝑗 ≤ 𝑇ℎ ∀𝑑
𝑗
Mark wants to maximize the profit for the next 3 days working 6h
𝑥𝑗,𝑑 ≤ 1 ∀𝑗
per day. Which demands he should attend per day?
𝑑
- Neglect the traveling time
- Each demand only can be attended once
Variables
Parameters
𝑃𝑗 → Profit for the job
Mark wishes to define the scheduling of costumers that he must 𝐷𝑗 → Duration of the job in hours
attend in the next 3 days. 𝑇ℎ → Number of hours in a working day (6)
The list of jobs (demands) with the duration of job and its profit is
defined below: Objective Function
A) duration 2h, profit 200 USD
max 𝑥𝑗,𝑑 𝑃𝑗
B) duration 3h, profit 500 USD
𝑗 𝑑
C) duration 5h, profit 300 USD
D) duration 2h, profit 100 USD Constraints
E) duration 6h, profit 1,000 USD
F) duration 4h, profit 300 USD 𝑥𝑗,𝑑 𝐷𝑗 ≤ 𝑇ℎ ∀𝑑
Solution: Profit Total = 2100.0
Mark wants to maximize the profit for the next 3 days working 6h
𝑗
Parameters
𝑃𝑗 → Profit for the job
Mark wishes to define the scheduling of costumers that he must 𝐷𝑗 → Duration of the job in hours
attend in the next 3 days. 𝑇ℎ → Number of hours in a working day (6)
The list of jobs (demands) with the duration of job and its profit is
defined below: Objective Function
A) duration 2h, profit 200 USD
max 𝑥𝑗,𝑑 𝑃𝑗
B) duration 3h, profit 500 USD
𝑗 𝑑
C) duration 5h, profit 300 USD
D) duration 2h, profit 100 USD Constraints
E) duration 6h, profit 1,000 USD
F) duration 4h, profit 300 USD 𝑥𝑗,𝑑 𝐷𝑗 ≤ 𝑇ℎ ∀𝑑
𝑗
Mark wants to maximize the profit for the next 3 days working 6h
per day. Which demands he should attend per day? 𝑥𝑗,𝑑 ≤ 1 ∀𝑗
- Neglect the traveling time 𝑑
- Each demand only can be attended once
- Mark wish to do a maximum of 1 job per day
Variables
𝑥𝑗,𝑑 → Binary decision on attending (or not) job j in day d
EXAMPLE 8 Parameters
𝑃𝑗 → Profit for the job
𝐷𝑗 → Duration of the job in hours
Mark wishes to define the scheduling of costumers that he must
attend in the next 3 days. 𝑇ℎ → Number of hours in a working day (6)
The list of jobs (demands) with the duration of job and its profit is
Objective Function
defined below:
A) duration 2h, profit 200 USD max 𝑥𝑗,𝑑 𝑃𝑗
B) duration 3h, profit 500 USD 𝑗 𝑑
C) duration 5h, profit 300 USD
D) duration 2h, profit 100 USD Constraints
E) duration 6h, profit 1,000 USD
𝑥𝑗,𝑑 𝐷𝑗 ≤ 𝑇ℎ ∀𝑑
F) duration 4h, profit 300 USD 𝑗
Mark wants to maximize the profit for the next 3 days working 6h 𝑥𝑗,𝑑 ≤ 1 ∀𝑗
𝑑
per day. Which demands he should attend per day?
𝑥𝑗,𝑑 ≤ 1 ∀𝑑
- Neglect the traveling time
𝑗
- Each demand only can be attended once
- Mark wish to do a maximum of 1 job per day
HOW TO
LEARN MORE?
PRACTICE!!!
Installing Python
and Packages
WinPython
Portable
How to https://winpython.github.io/
Installing • Go to https://www.python.org/
• Download and install
• Check version!
• Linux
• Check your version:
python --version
• Command Prompt:
pip install PACKAGE_NAME
Packages
IDE Spyder pip install spyder
Jupyter pip install jupyterlab
Notebook
Starting with Python
Starting with Lists
Python Tuples
Dictionaries
Starting with If
Python For
While
Inline commands
Starting with Functions
Python
Starting with Numpy
Python
Starting with Pandas
Python
Starting with Pandas
Python Reading from Excel
and some functions
Starting with Matplotlib
Python
Linear Programming (LP)
Introduction
max 𝑥 + 𝑦
−𝑥 + 2𝑦 ≤ 8
2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
0 ≤ 𝑥 ≤ 10
0 ≤ 𝑦 ≤ 10
Linear Solver
vs
Programming Framework
PROBLEM
UNDERSTANDING
PROBLEM
MODELING
Solver PROGRAMMING
FRAMEWORK
vs LANGUAGE
Framework PROBLEM
SOLVE SOLVER
RESULTS
Linear Or-Tools
Programming https://developers.google.com/optimization
Linear Scip
Programming https://www.scipopt.org/
Package documentation
https://github.com/SCIP-Interfaces/PySCIPOpt
Gurobi
https://www.gurobi.com/
Download and install Gurobi
Activate Gurobi
Linear CPLEX
https://www.ibm.com/products/ilog-
Programming cplex-optimization-studio
Download and install
GLPK
http://sourceforge.net/projects/winglpk/
Download
Add GLPK/win64 to the
environment variable PATH
Linear Pyomo
Documentation
https://pyomo.readthedocs.io/
Linear PuLP
More information in
https://github.com/coin-or/pulp
Linear Which solver
should I choose?
Programming
Which framework and solver
How easy to How easy to configure a new
Framework (AML) Linear Problems Nonlinear Problems
start with solver and about documentation
Pyomo X X High High
Ortools X Very High Low
PuLP X High High
SCIP X X Very High Not possible / Low
SciPy X X Low Medium
min −4𝑥 − 2𝑦
Try to solve it by your self
𝑥+𝑦 ≤8
8𝑥 + 3𝑦 ≥ −24 Check the solution in the
−6𝑥 + 8𝑦 ≤ 48 resource of this class
3𝑥 + 5𝑦 ≤ 15
𝑥≤3 Estimated time: 30min
𝑦≥0
Pyomo CBC:
https://projects.coin-or.org/Cbc
https://bintray.com/coin-or/download/Cbc/
Working with Arrays and
Pyomo Summations
Arrays and Summation
Power Generation (Pg) 4
min 𝐶𝑔 𝑖𝑔 𝑃𝑔 𝑖𝑔
ID Cost Power Generation
𝑖𝑔 =0
0 0,10 20 kW
4 2
1 0,05 10 kW
𝑃𝑔 (𝑖𝑔 ) = 𝑃𝑑 (𝑖𝑑 )
2 0,30 40 kW
𝑖𝑔 =0 𝑖𝑐 =0
3 0,40 50 kW
4 0,01 5 kW 𝑃𝑑 0 ≤ 𝑃𝑔 0 + 𝑃𝑔 3
- continuos²
x ** 2
Mixed-Integer Linear
Programming (MILP)
Introduction
max 𝑥 + 𝑦
−𝑥 + 2𝑦 ≤ 8
2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
0 ≤ 𝑥 ≤ 10
0 ≤ 𝑦 ≤ 10
Introduction
max 𝑥 + 𝑦
−𝑥 + 2𝑦 ≤ 𝟕
7
(4.2, 5.6) 2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
0 ≤ 𝑥 ≤ 10
0 ≤ 𝑦 ≤ 10
Introduction max 𝑥 + 𝑦
−𝑥 + 2𝑦 ≤ 7
2𝑥 + 𝑦 ≤ 14
7
(4, 5.5) 2𝑥 − 𝑦 ≤ 10
D
0 ≤ 𝑥 ≤ 10
E
C 0 ≤ 𝑦 ≤ 10
A B
𝑥 as integer
Pyomo
MILP
model.x = pyo.Var(within=Integers)
model.x = pyo.Var(within=Binary)
https://pyomo.readthedocs.io/en/stable/p
yomo_modeling_components/Sets.html#p
redefined-virtual-sets
Ortools
MILP
Change the Solver (from GLOP to):
CBC
Gurobi
Cplex
x = solver.IntVar(0,10,'x’)
SCIP
MILP
x = model.addVar('x', vtype=‘INTEGER’)
Exercise
Find the optimal solution for the following problem
5
min 𝑥𝑖 + 𝑦
𝑖=1
Try to solve it by your self
5
Tip 1
𝑥𝑖 + 𝑦 ≤ 20 Check the solution in the
From 1 to 5
𝑖=1
Tip 2 next class
𝑥𝑖 + 𝑦 ≥ 15 , ∀𝑖
For all i
5
2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
0 ≤ 𝑥 ≤ 10
0 ≤ 𝑦 ≤ 10
Introduction max 𝑥 + 𝑥𝑦
−𝑥 + 2𝑦𝒙 ≤ 8
2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
0 ≤ 𝑥 ≤ 10
0 ≤ 𝑦 ≤ 10
Pyomo: IPOPT
Search for ipopt binaries
https://www.coin-
NLP or.org/download/binary/Ipopt
Unzip in C:\
Pyomo
opt = SolverFactory(
‘ipopt',
executable='C:\\ipopt\\bin\\ipopt.exe')
NLP SCIP
Exercise
Find the optimal solution for the following problem
−5 ≤ 𝑥 ≤ 5
−5 ≤ 𝑦 ≤ 5
2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
0 ≤ 𝑥 ≤ 10
0 ≤ 𝑦 ≤ 10
Introduction max 𝑥 + 𝑥𝑦
−𝑥 + 2𝑦𝑥 ≤ 8
2𝑥 + 𝑦 ≤ 14
2𝑥 − 𝑦 ≤ 10
0 ≤ 𝑥 ≤ 10
0 ≤ 𝑦 ≤ 10
𝒙 integer
Pyomo: Couenne
https://projects.coin-or.org/Couenne
MINLP https://www.coin-
or.org/download/binary/Couenne/
Unzip in C:\
opt = SolverFactory('couenne',
executable='C:\\couenne\\bin\\couenne.exe')
Decomposition
MINLP Pyomo + MindtPy
opt = SolverFactory('mindtpy’)
opt.solve(model, mip_solver=‘gurobi',
nlp_solver='ipopt')
MINLP SCIP
Genetic Algorithm
https://pypi.org/project/genet
icalgorithm/
Particle Swarm
MINLP pip install pyswarm
https://pythonhosted.org/pyswarm/
Ortools
https://developers.google.com/opti
mization/cp/integer_opt_cp
CP
Constraint Programming (CP)
Ortools
https://developers.google.com/opti
mization/cp/integer_opt_cp
CP
Special Cases
• Linear or NonLinear Models
• Linearizations
SCOP
Second-Order Cone Programming
Pyomo + Gurobi
SCOP Example
Suppose that you have 3 machines to manufacture shoes, and the cost of each machine is:
𝐶1 = 0.01𝑛12 + 2𝑛1
𝐶2 = 6𝑛2
𝐶3 = 7𝑛3
where 𝐶𝑖 is cost for production of machine i, 𝑛𝑖 is the number of shoes manufactured in machine i
min 𝐶1 + 𝐶2 + 𝐶3
𝑛1 + 𝑛2 + 𝑛3 = 2100
𝐶1 = 0.01𝑛12 + 2𝑛1
𝐶2 = 6𝑛2
𝐶3 = 7𝑛3
0 ≤ 𝑛1 , 𝑛2 , 𝑛3 ≤ 1000
𝑛1 , 𝑛2 , 𝑛3 𝑎𝑠 𝑖𝑛𝑡𝑒𝑔𝑒𝑟𝑠
https://www.gurobi.com/resource/
non-convex-quadratic-optimization/
NonConvex QP
NonConvex Quadratic Programming
NonConvex QP Example
Suppose that you have 3 machines to manufacture shoes, and the cost of each machine is:
𝐶1 = 0.01𝑛12 + 2𝑛1 𝐶2 = 6𝑛2 𝒏𝟏 𝐶3 = 7𝑛3
where 𝐶𝑖 is cost for production of machine i, 𝑛𝑖 is the number of shoes manufactured in machine i
min 𝐶1 + 𝐶2 + 𝐶3
𝑛1 + 𝑛2 + 𝑛3 = 2100
𝐶1 = 0.01𝑛12 + 2𝑛1
𝑪𝟐 = 𝟔𝒏𝟐 𝒏𝟏
𝐶3 = 7𝑛3
0 ≤ 𝑛1 , 𝑛2 , 𝑛3 ≤ 1000
𝑛1 , 𝑛2 , 𝑛3 𝑎𝑠 𝑖𝑛𝑡𝑒𝑔𝑒𝑟𝑠
https://developers.google.com
/optimization/routing/vrp
Routing
Problems
OR-TOOLS VRP
Linearization 𝐶 =𝑏∗𝑥
𝑏 𝑖𝑠 𝑏𝑖𝑛𝑎𝑟𝑦
BigM Similar to
Binary * Continuos −𝑏 ∗ 𝑀 ≤ 𝐶 ≤ 𝑏 ∗ 𝑀
− 1−𝑏 ∗𝑀 ≤𝐶−𝑥 ≤ 1−𝑏 ∗𝑀
𝑏 𝑖𝑠 𝑏𝑖𝑛𝑎𝑟𝑦
Linearization 𝐶 =𝑏∗𝑥
𝑏 𝑖𝑠 𝑏𝑖𝑛𝑎𝑟𝑦
BigM Similar to
Binary * Continuos −0 ∗ 𝑀 ≤ 𝐶 ≤ 0 ∗ 𝑀
− 1−0 ∗𝑀 ≤𝐶−𝑥 ≤ 1−0 ∗𝑀
Linearization 𝐶 =𝑏∗𝑥
𝑏 𝑖𝑠 𝑏𝑖𝑛𝑎𝑟𝑦
BigM Similar to
BigM Similar to
BigM Similar to
Binary * Continuos −1 ∗ 𝑀 ≤ 𝐶 ≤ 1 ∗ 𝑀
− 1−1 ∗𝑀 ≤𝐶−𝑥 ≤ 1−1 ∗𝑀
Linearization 𝐶 =𝑏∗𝑥
𝑏 𝑖𝑠 𝑏𝑖𝑛𝑎𝑟𝑦
BigM Similar to
Binary * Continuos −𝑀 ≤ 𝐶 ≤ 𝑀
0≤𝐶−𝑥 ≤0
Linearization 𝐶 =𝑏∗𝑥
𝑏 𝑖𝑠 𝑏𝑖𝑛𝑎𝑟𝑦
BigM Similar to
Binary * Continuos −𝑀 ≤ 𝐶 ≤ 𝑀
𝐶−𝑥 =0
Linearization 𝐶 =𝑏∗𝑥
𝑏 𝑖𝑠 𝑖𝑛𝑡𝑒𝑔𝑒𝑟
BigM Similar to
where 𝐶𝑖 is cost for production of machine i, 𝑛𝑖 is the number of shoes manufactured in machine i
min 𝐶1 + 𝐶2 + 𝐶3
𝑛1 + 𝑛2 + 𝑛3 = 2100 b as binary
𝑏=1 represents that machine 2 is ON
𝑪𝟏 = 𝟐𝒏𝟏 𝑏=0 represents that machine 2 is OFF
𝑪𝟐 = 𝒃 ∗ 𝟔𝒏𝟐 + 𝟏𝟎𝟎𝟎
𝒏𝟐 ≤ 𝒃 ∗ 𝟏𝟎𝟎𝟎
𝐶3 = 7𝑛3
0 ≤ 𝑛1 , 𝑛2 , 𝑛3 ≤ 1000 −𝒃 ∗ 𝑴 ≤ 𝑪𝟐 ≤ 𝒃 ∗ 𝑴
− 𝟏 − 𝒃 ∗ 𝑴 ≤ 𝑪𝟐 − 𝟔𝒏𝟐 + 𝟏𝟎𝟎𝟎 ≤ 𝟏 − 𝒃 ∗ 𝑴
𝑛1 , 𝑛2 , 𝑛3 𝑎𝑠 𝑖𝑛𝑡𝑒𝑔𝑒𝑟𝑠
𝐶 = 𝑏1 ∗ 𝑏2
𝐶=𝑧
𝑧 ≤ 𝑏1
𝑧 ≤ 𝑏2
𝑧 ≥ 𝑏1 + 𝑏2 − 1
𝑧, 𝑏1 , 𝑏2 𝑎𝑟𝑒 𝑏𝑖𝑛𝑎𝑟𝑖𝑒𝑠
Binary*Binary Example
Suppose that you have 3 machines to manufacture shoes, and the cost of each machine is:
𝑪𝟏 = 𝟐𝒏𝟏 𝐶2 = 6𝑛2 𝐶3 = 7𝑛3
where 𝐶𝑖 is cost for production of machine i, 𝑛𝑖 is the number of shoes manufactured in machine i
min 𝐶1 + 𝐶2 + 𝐶3
𝑛1 + 𝑛2 + 𝑛3 = 2100
𝑏𝑖 =1 represents that machine i is ON
𝑏𝑖 =0 represents that machine i is OFF
𝑪𝟏 = 𝟐𝒏𝟏
𝒏𝟏 ≤ 𝒃𝟏 ∗ 𝟏𝟎𝟎𝟎
𝐶2 = 6𝑛2 𝒏𝟐 ≤ 𝒛 ∗ 𝟏𝟎𝟎𝟎
𝒏𝟐 ≤ 𝒃𝟏 ∗ 𝒃𝟐 ∗ 𝟏𝟎𝟎𝟎 𝒛 ≤ 𝒃𝟏
𝐶3 = 7𝑛3 𝒛 ≤ 𝒃𝟐
0 ≤ 𝑛1 , 𝑛2 , 𝑛3 ≤ 1000
𝒛 ≥ 𝒃𝟏 + 𝒃𝟐 − 𝟏
𝑛1 , 𝑛2 , 𝑛3 𝑎𝑠 𝑖𝑛𝑡𝑒𝑔𝑒𝑟𝑠
𝒛 𝒂𝒔 𝒃𝒊𝒏𝒂𝒓𝒚
𝑏1 , 𝑏2 as binary
Binary*Binary Example
A good alternative would be using b2<b1, as the following example.
Using this alternative, you do not need to use b2*b1; however, for this class, I will continue with b2*b1 so we can
practice how to work with the multiplication of two binaries variables
min 𝐶1 + 𝐶2 + 𝐶3
𝑛1 + 𝑛2 + 𝑛3 = 2100
𝑪𝟏 = 𝟐𝒏𝟏
𝒏𝟏 ≤ 𝒃𝟏 ∗ 𝟏𝟎𝟎𝟎
𝐶2 = 6𝑛2
𝒏𝟐 ≤ 𝒃𝟐 ∗ 𝟏𝟎𝟎𝟎
𝒃𝟐 ≤ 𝒃𝟏
𝐶3 = 7𝑛3
0 ≤ 𝑛1 , 𝑛2 , 𝑛3 ≤ 1000
𝑛1 , 𝑛2 , 𝑛3 𝑎𝑠 𝑖𝑛𝑡𝑒𝑔𝑒𝑟𝑠
𝑏1 , 𝑏2 as binary
Advanced Features for Pyomo
Case Study
Case Study
Suppose you have 4 machines on your computer’s factory
You have to define the number of computers that each machine must produce during the next 10 hours in order to maximize the total production. The
objective function of our problem is given by
𝑚𝑎𝑥 𝑥𝑚,𝑡
𝑚 𝑡
where 𝑥𝑚,𝑡 represents the number of computers produced by a machine m at hour t.
Some machines have dependencies on others and the constraints are (for each 𝑡)
2𝑥
2𝑥2,𝑡−
2,𝑡 −8𝑥 ≤ 00
8𝑥3,𝑡 ≤ (1)
𝑥2,𝑡 − 2𝑥3,𝑡−2 + 𝑥4,𝑡 ≥ 1 (2)
𝑥2,𝑡 − 2𝑥3,𝑡−2 + 𝑥4,𝑡 ≥ 1 (2)
And the capacity production is given by (for all 𝑡)
𝑥𝑥𝑚,𝑡
≤ 50
𝑚,𝑡 ≤ 50 (3)
𝑚
𝑚
0 ≤ 𝑥𝑚,𝑡 ≤ 10 (5)
Solve the problem and find the optimal production of computers for each machine for each hour of the next 10 hours.
Note the 𝑥2,𝑡−1 , in the last constraint, represents the second machine at hour t-1
Obs.: Constraints (2) and (4) does not exist for t<3 and t<2, respectively
Case Study
𝑚𝑎𝑥 𝑥𝑚,𝑡
𝑚 𝑡
2𝑥2,𝑡 − 8𝑥3,𝑡 ≤ 0 ∀𝑡
𝑥𝑚,𝑡 ≤ 50 ∀𝑡
𝑚
0 ≤ 𝑥𝑚,𝑡 ≤ 10 ∀𝑚, ∀𝑡
Solver
progress
opt.solve(model, tee=True)
Gap Limit opt.options['MIPgap'] = 0.0001
https://www.gurobi.com/documentation/9.1/refman/
mipgap2.html
Time Limit opt.options[‘TimeLimit'] = 60
Inequallity 𝐴≤𝑥≤𝐵
model.C = pyo.Constraint(pyo.inequality(A,x,B))
𝑥𝑚,𝑡
summation 𝑚 𝑡
pyo.summation(x)
Parameters pyo.Param(initialize=VALUE)
and Sets
pyo.Set(initialize=LIST)
pyo.RangeSet(BEGIN,END)
Constraint’s Rules SAME AS
Rules in the Objective Function
SAME AS
pyo.Objective(rule=myobj, sense=pyo.maximize)
def myobj(model):
return pyo.summation(model.x)
model.C1 = pyo.ConstraintList()
for t in model.setT:
Constraint’s Rules model.C1.add(expr = 2*x[2,t] - 8*x[3,t] <= 0)
Warmstart
Differential https://pyomo.readthedocs.io/en/stable/
modeling_extensions/dae.html
Algebraic
Equations
(DAE)
Practical Examples
Fence in the Garden
What is the largest area that we can fence in a garden using 100 meters of fence?
Define the dimensions of this garden as well.
Note: The garden is already fenced by a wall of rocks in one of its sides.
m𝑎𝑥 𝑥𝑦
2𝑥 + 𝑦 ≤ 100
𝑥, 𝑦 > 0
Maximize Revenue
From the historical sales data, it is known that practicing a rent price (p) between 50 and 200 dollar,
the number of cars rented per day is N(p) = 1001-5p.
m𝑎𝑥 𝑝 𝑁
𝑁 = 1001 − 5𝑝
50 ≤ 𝑝 ≤ 200
𝑁 𝑖𝑛𝑡𝑒𝑔𝑒𝑟
Route Optimization
650 m
DISTRICT FOUR 500 m
DISTRICT TWO
DISTRICT SEVEN
220 m 900 m DISRTICT FIVE
DISTRICT ONE
400 m
1500 m
DISTRICT SIX
400 m
DISTRICT THREE
500 m
Route Optimization min 𝑥𝑖𝑗 𝐷𝑥𝑖𝑗
X57
𝑥𝑖𝑗 ≤ 1 ∀𝑛𝑜𝑑𝑒/(𝑜𝑟𝑖𝑔𝑖𝑛, 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛)
X36
𝑖𝑛
min 𝐶𝑔 𝑃𝑔
Pg1 <= 20 kW Pg2 <= 30 kW
𝑔
Cg = 0,2 $/kWh Cg = 0,5 $/kWh
~ ~
B0
L0
B1 𝑃𝑔 − 𝑃𝑙 + 𝑃𝑙 = 𝑃𝑑 ∀𝑛
Pl = 15 kW 𝑔∈Ω𝐺
𝑛 𝑙∈Ω𝐿𝑛=𝑙 𝑠 𝑙∈Ω𝐿𝑛=𝑙 𝑟 𝑑∈Ω𝐷
𝑛
L1 L2
Pl = 15 kW Pl = 15 kW
𝑃𝑙 = 𝐵𝑙 (𝜃𝑙 𝑛=𝑠 − 𝜃𝑙 𝑛=𝑟 ) ∀𝑙
B2
0 ≤ 𝑃𝑔 ≤ 𝑃𝑔𝑚𝑎𝑥 ∀𝑔
Pd = 25 kW
−𝑃𝑙𝑚𝑎𝑥 ≤ 𝑃𝑙 ≤ 𝑃𝑙𝑚𝑎𝑥 ∀𝑙
Bl = 100 pu −𝜋 ≤ 𝜃𝑛 ≤ 𝜋 ∀𝑛
Sbase = 1 kW
𝜃𝑛 = 0 𝑛: 𝑟𝑒𝑓(0)
Congratulations!!
Challenges:
https://math.libretexts.org/Courses/Mount_Royal_University/MATH_1200%3A_Calculus_for_Scientists_I/3%3A_Appli
cations_of_Derivatives/3.6%3A_Applied_Optimization_Problems