integer linear programming - adam furmanek...declarative programming at a glance. some theory. real...
TRANSCRIPT
![Page 2: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/2.jpg)
About me
.NET developer for 5 years.
Scala Developer at Amazon.
Blogger, public speaker.
Author of .NET Internals Cookbook.
http://blog.adamfurmanek.pl
furmanekadam
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 2
![Page 3: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/3.jpg)
AgendaDeclarative programming at a glance.
Some theory.
Real life example.
Implementation consideration.
Summary.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 3
![Page 4: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/4.jpg)
Declarative programming
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 4
![Page 5: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/5.jpg)
Declarative programmingParadigm that expresses the logic of a computation without describing its control flow.
Declarative programming often considers programs as theories of a formal logic, and computations as deductions in that logic space.
A high-level program that describes what a computation should perform.
SELECT * FROM Orders
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 5
![Page 6: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/6.jpg)
Declarative programming — why?Decouples problem formulation from solving process.
Solving part can be replaced without modifying the formulation.
Easier to optimise algorithms for because „the goal” is understandable for the machine.
Can be used with little to no programming skills.
Sometimes we don’t know how to solve it.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 6
![Page 7: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/7.jpg)
Example — RTS gameWe are playing RTS game.
We are allowed to hire footmen and archers.
Every footman costs 10 gold and 30 food. Every archer costs 20 gold, 25 food, and 10 wood.
Footman’s attack is equal to 5, archer’s attack is equal to 7.
Our population limit is set to 200 units. Every footman „costs” 1 unit, every archer „costs” 2 units.
We have 1000 gold, 1000 food, and 200 wood. We want to get strongest possible army.
07.02.2020 09:11 ADAM FURMANEK 7
![Page 8: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/8.jpg)
UnitsGold Food Wood
Footman 10 30 0
Archer 20 25 10
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 8
Attack Unit count
Footman 5 1
Archer 7 2
Available gold Available food Available wood Max units count
Constraint 1000 1000 200 200
![Page 9: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/9.jpg)
VariablesVariables:
𝑓 − 𝑓𝑜𝑜𝑡𝑚𝑒𝑛𝑎 − 𝑎𝑟𝑐ℎ𝑒𝑟𝑠
Constraints:
𝑓 + 2 ⋅ 𝑎 ≤ 200 // population10 ⋅ 𝑓 + 20 ⋅ 𝑎 ≤ 1000 // gold30 ⋅ 𝑓 + 25 ⋅ 𝑎 ≤ 1000 // food
10 ⋅ 𝑎 ≤ 200 // wood
Target value:5 ⋅ 𝑓 + 7 ⋅ 𝑎
07.02.2020 09:11 ADAM FURMANEK 9
![Page 10: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/10.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 10
![Page 11: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/11.jpg)
Example
07.02.2020 09:11 ADAM FURMANEK 11
![Page 12: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/12.jpg)
Example
07.02.2020 09:11 ADAM FURMANEK 12
![Page 13: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/13.jpg)
ExampleVariables:
𝑆, 𝐸, 𝑁, 𝐷,𝑀, 𝑂, 𝑅, 𝑌 ∈ 0,… , 9 and all different
Constraints:1000𝑆 + 100𝐸 + 10𝑁 + 𝐷 +1000𝑀 + 100𝑂 + 10𝑅 + 𝐸 =10000𝑀 + 1000𝑂 + 100𝑁 + 10𝐸 + 𝑌
07.02.2020 09:11 ADAM FURMANEK 13
![Page 14: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/14.jpg)
Example
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 14
![Page 15: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/15.jpg)
Real life examplesRTS game = factory production allocation:
◦ We have available resources
◦ We have facitilies
◦ We want to plan work to maximize the production
Send more money = pattern recognition:◦ We know structure of a problem but doesn’t understand the latent factors
Floor tiling = microchip transistor layout:◦ We have transistors and gates of given size
◦ We want to minimize heat emission and the board size
Others: BTS placement, power plant rooms layout, scheduling systems, items personalization and many more.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 15
![Page 16: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/16.jpg)
Some theoryMIXED INTEGER LINEAR PROGRAMMING
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 16
![Page 17: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/17.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 17
![Page 18: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/18.jpg)
Mixed Integer Linear ProgrammingProgramming in mathematics means finding a solution to optimization problem.
Linear Programming is a class of a problems with only linear constraints and with linear cost function.
Mixed Integer Linear Programming is a class of a problems with integer constraint for some of variables.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 18
![Page 19: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/19.jpg)
ConstraintsWe can add two variables:
𝑎 + 𝑏
We can multiply variable by constant:
5 ⋅ 𝑎
We can constrain variable with lower/upper bound:
𝑎 ≤ 7
We cannot multiply variables:𝑎 ⋅ 𝑏
We cannot use strict constraings (greater than, less than, not equal)
𝑎 < 10
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 19
![Page 20: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/20.jpg)
Cost functionNotice that not hiring anyone was also a solution!
Problem may have zero, one, multiple or infinitely many solutions. We need to compare them.
We use cost function to specify which one of them is the best.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 20
![Page 21: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/21.jpg)
Cutting plane method
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 21
![Page 22: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/22.jpg)
AlgorithmsBasic algorithms:◦ Cutting plane methods – we solve the problem without integer constraints
(continuous version of the problem), and next we cut the plane to get smaller problem
◦ Branch and bound – we solve the problem without integer constraints, and next we bound some variables and perform next iteration
MILP is NP-complete and we sometimes use heuristics:◦ Tabu search
◦ Simulated annealing
◦ Ant colony optimization
07.02.2020 09:11 ADAM FURMANEK 22
![Page 23: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/23.jpg)
What can we do?WE WILL IMPLEMENT
Boole’s logic.
Multiplication.
Comparison operators.
WE WILL NOT IMPLEMENT
Arithmetic: division, remainder, exponentation, roots.
Comparisons: min, max, absolute value.
Number theory: factorial, GCD.
Algorithms: if condition, sorting, loops, lexicographical comparisons, Gray’s code, linear regression.
Set operators: SOS type 1 and 2, approximation.
Graph operators: MST, vertex/edge cover, max flow, connectivity, shortest path, TSP.
Turing machine.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 23
![Page 24: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/24.jpg)
Conjunction — AND operator (&&)
We have two variables: 𝑎, 𝑏.
We want to create variable 𝑥 which is 𝑥 = 𝑎 && 𝑏.
Formula:
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 24
![Page 25: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/25.jpg)
Conjunction — AND operator (&&)
If both 𝑎 and 𝑏 are 1 then 𝑥 must be 1.
If 𝑥 was 0:
0 ≤ 1 + 1 − 2 ⋅ 0 ≤ 1
0 ≤ 2 − 0 ≤ 1
0 ≤ 2 ≤ 1
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 25
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
![Page 26: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/26.jpg)
Conjunction — AND operator (&&)
If both 𝑎 and 𝑏 are 1 then 𝑥 must be 1.
If 𝑥 is 1:
0 ≤ 1 + 1 − 2 ⋅ 1 ≤ 1
0 ≤ 2 − 2 ≤ 1
0 ≤ 0 ≤ 1
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 26
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
![Page 27: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/27.jpg)
Conjunction — AND operator (&&)
If either 𝑎 or 𝑏 is 1 then 𝑥 must be 0.
If 𝑥 was 1:
0 ≤ 1 + 0 − 2 ⋅ 1 ≤ 1
0 ≤ 1 − 2 ≤ 1
0 ≤ −1 ≤ 1
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 27
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
![Page 28: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/28.jpg)
Conjunction — AND operator (&&)
If either 𝑎 or 𝑏 is 1 then 𝑥 must be 0.
If 𝑥 is 0:
0 ≤ 1 + 0 − 2 ⋅ 0 ≤ 1
0 ≤ 1 − 0 ≤ 1
0 ≤ 1 ≤ 1
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 28
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
![Page 29: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/29.jpg)
Conjunction — AND operator (&&)
If both 𝑎 or 𝑏 are 0 then 𝑥 must be 0.
If 𝑥 was 1:
0 ≤ 0 + 0 − 2 ⋅ 1 ≤ 1
0 ≤ 0 − 2 ≤ 1
0 ≤ −2 ≤ 1
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 29
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
![Page 30: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/30.jpg)
Conjunction — AND operator (&&)
If both 𝑎 or 𝑏 are 0 then 𝑥 must be 0.
If 𝑥 is 0:
0 ≤ 0 + 0 − 2 ⋅ 0 ≤ 1
0 ≤ 0 − 0 ≤ 1
0 ≤ 0 ≤ 1
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 30
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
![Page 31: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/31.jpg)
Conjunction — AND operator (&&)
If both 𝑎 and 𝑏 are 1 then 𝑥 must be 1.
If either 𝑎 or 𝑏 is 1 then 𝑥 must be 0.
If both 𝑎 or 𝑏 are 0 then 𝑥 must be 0.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 31
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
![Page 32: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/32.jpg)
Conjunction & DisjunctionTwo variables conjunction:
0 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 1
𝑛 variables conjunction:0 ≤ 𝑎1 + 𝑎2 + 𝑎3 + …+ 𝑎𝑛 − 𝑛𝑥 ≤ 𝑛 − 1
Two variables disjunction:−1 ≤ 𝑎 + 𝑏 − 2𝑥 ≤ 0
𝑛 variables disjunction goes the same way
07.02.2020 09:11 ADAM FURMANEK 32
![Page 33: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/33.jpg)
Negation, exclusive or, implication
Negation:𝑥 = 1 − 𝑎
Implication:𝑎 ⇒ 𝑏 ≡ ~𝑎 ∨ 𝑏
Exclusive or:~𝑎 ∧ 𝑏 ∨ (𝑎 ∧ ~𝑏)
07.02.2020 09:11 ADAM FURMANEK 33
![Page 34: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/34.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 34
Multiplication of two binary variables is their conjunction.
𝑎 ⋅ 𝑏 ≡ 𝑎 ∧ 𝑏
![Page 35: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/35.jpg)
Multiplication of integer variablesMultiplication is possible when we know the maximum possible value of a variable.
We set the upper bound and perform the long multiplication.
It is rather slow approach, it requires 𝑂(𝑛2) temporary variables.
07.02.2020 09:11 ADAM FURMANEK 35
![Page 36: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/36.jpg)
Value decompositionWe decompose the variable to extract digits.
We assume the maximum possible value, because we need to know the number of digits.
Decomposition is straghtforward:
𝑏 = 𝑏0 + 2 ⋅ 𝑏1 + 4 ⋅ 𝑏2 + 8 ⋅ 𝑏3 + …+ 2𝑛−1 ⋅ 𝑏𝑛−1
07.02.2020 09:11 ADAM FURMANEK 36
![Page 37: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/37.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 37
![Page 38: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/38.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 38
![Page 39: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/39.jpg)
Multiplication𝑎 ⋅ 𝑏 =𝑎0 ∧ 𝑏0 + 2 ⋅ 𝑎0 ∧ b1 + 4 ⋅ a0 ∧ 𝑏2 + …+ 2𝑛−1 ⋅ 𝑎0 ∧ 𝑏𝑛−1
+2 ⋅ 𝑎1 ∧ 𝑏0 + 2 ⋅ 𝑎1 ∧ 𝑏1 + 4 ⋅ 𝑎1 ∧ 𝑏2 +⋯2𝑛−1 ⋅ (𝑎1 ∧ 𝑏𝑛−1)+4 ⋅ 𝑎2 ∧ 𝑏0 + 2 ⋅ 𝑎2 ∧ 𝑏1 + 4 ⋅ 𝑎2 ∧ 𝑏2 + …+ 2𝑛−1 ⋅ 𝑎2 ∧ bn−1+⋯+ 2𝑛−1( 𝑎𝑛−1 ∧ 𝑏0 + 2 ⋅ 𝑎𝑛−1 ∧ 𝑏1 + 4 ⋅ 𝑎𝑛−1 ∧ 𝑏2 + …+ 2𝑛−1 ⋅ 𝑎𝑛−1 ∧ 𝑏𝑛−1 )
It can be represented as:
𝑎 ⋅ 𝑏 =
𝑖=0
𝑛−1
2𝑖
𝑗=0
𝑛−1
2𝑗𝑎𝑖 ∧ 𝑏𝑗
07.02.2020 09:11 ADAM FURMANEK 39
![Page 40: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/40.jpg)
Comparison operators
To calculate whether 𝑎 > 𝑏 we can use:
0 ≤ 𝑏 − 𝑎 + 2𝑛−1𝑥 ≤ 2𝑛−1 − 1
To check whether numbers differ:𝑥 = 𝑎 > 𝑏 ∨ (𝑏 > 𝑎)
07.02.2020 09:11 ADAM FURMANEK 40
![Page 41: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/41.jpg)
There are libraries doing that!
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 41
https://github.com/afish/MilpManager
![Page 42: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/42.jpg)
Scheduling SystemREAL LIFE EXAMPLE
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 42
![Page 43: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/43.jpg)
IdeaAt the beginning of every term students must be assigned to classes.
It is hard to make them happy because some of them work, some of them prefer to sleep long, some of them prefer to have lots of days off.
We can try to represent the requirements as a MILP program and find the optimal solution.
07.02.2020 09:11 ADAM FURMANEK 43
![Page 44: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/44.jpg)
UsageWe ask students to assign preferences points to every possible class.
The more points assigned the more student wants to be assigned to that class.
We need to take care of rooms occupancy limits, collisions etc.
07.02.2020 09:11 ADAM FURMANEK 44
![Page 45: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/45.jpg)
ScheduleWe are given a schedule of classes in courses
Every class has associated day (Monday – Friday), hour (e.g., 9:30 AM) and duration (e.g., 1:30 hrs).
Every class has associated room with occupancy limit.
07.02.2020 09:11 ADAM FURMANEK 45
![Page 46: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/46.jpg)
ConstraintsVariables.
Exactly one class in one course.
Collisions.
Rooms occupancy limits.
Cost function.
07.02.2020 09:11 ADAM FURMANEK 46
![Page 47: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/47.jpg)
VariablesFor every person, every course, every class we declare binary variable.
Value 1 means that the student is assigned to this class.
We define:𝑥𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠,𝑠𝑡𝑢𝑑𝑒𝑛𝑡
07.02.2020 09:11 ADAM FURMANEK 47
![Page 48: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/48.jpg)
Exactly one class for a studentEvery student must attend exactly one class in course.
For every course we need to assign student to exactly one class.
ሥ
𝑐𝑜𝑢𝑟𝑠𝑒,𝑠𝑡𝑢𝑑𝑒𝑛𝑡
𝑐𝑙𝑎𝑠𝑠
𝑥𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠,𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 1
07.02.2020 09:11 ADAM FURMANEK 48
![Page 49: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/49.jpg)
CollisionsStudent cannot be in two places at the same time.
ሥ 𝑐𝑜𝑢𝑟𝑠𝑒1, 𝑐𝑙𝑎𝑠𝑠1 , 𝑐𝑜𝑢𝑟𝑠𝑒2, 𝑐𝑙𝑎𝑠𝑠2 𝑐𝑜𝑙𝑙𝑖𝑑𝑖𝑛𝑔
ሥ
𝑠𝑡𝑢𝑑𝑒𝑛𝑡
𝑥𝑐𝑜𝑢𝑟𝑠𝑒1,𝑐𝑙𝑎𝑠𝑠1,𝑠𝑡𝑢𝑑𝑒𝑛𝑡 + 𝑥𝑐𝑜𝑢𝑟𝑠𝑒2,𝑐𝑙𝑎𝑠𝑠2,𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ≤ 1
07.02.2020 09:11 ADAM FURMANEK 49
![Page 50: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/50.jpg)
Room occupancy limitEvery room has an occupancy limit.
ሥ
𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠
𝑠𝑡𝑢𝑑𝑒𝑛𝑡
𝑥𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠,𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ≤ 𝑠𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠
where 𝑠𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠 means the size of the room
07.02.2020 09:11 ADAM FURMANEK 50
![Page 51: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/51.jpg)
Preferences cost functionEvery student assigned points to classes. We want to maximize the sum of those points.
𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠,𝑠𝑡𝑢𝑑𝑒𝑛𝑡
𝑝𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠,𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ⋅ 𝑥𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠,𝑠𝑡𝑢𝑑𝑒𝑛𝑡
where 𝑝𝑐𝑜𝑢𝑟𝑠𝑒,𝑐𝑙𝑎𝑠𝑠,𝑠𝑡𝑢𝑑𝑒𝑛𝑡 means number of points
07.02.2020 09:11 ADAM FURMANEK 51
![Page 52: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/52.jpg)
What now?Students: 150
Courses: 15
Classes: 9 per course
Problem size: ~40 000 variables.
Solving time: 2 seconds.
This solution is optimal, we won’t get any better than that!
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 52
![Page 53: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/53.jpg)
Implementation consideration
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 53
![Page 54: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/54.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 54
![Page 55: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/55.jpg)
Licenses
Various approaches:◦License per workstation◦License per person◦Licensing server with tickets
Licenses for universities and research work.
Licenses for students.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 55
![Page 56: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/56.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 56
![Page 57: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/57.jpg)
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 57
https://neos-server.org/neos/solvers/index.html
![Page 58: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/58.jpg)
Beyond MILP
SAT/SMT.
Quadratic programming.
Prolog.
Specialized models for various problems.
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 58
![Page 59: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/59.jpg)
SummaryDeclarative programming allows you to focus on a problem, not on an algorithm!
If something is slow — just replace the solver.
There are many models, choose as powerful as you can (to make modelling easy) and as primitive as possible (to make solving fast).
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 59
![Page 60: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/60.jpg)
Q&A
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 60
![Page 61: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/61.jpg)
ReferencesAlexander Schrijver — „Theory of Linear and Integer Programming”
Dennis Yurichev — „SAT/SMT by example”
Adam Furmanek – „.NET Internals Cookbook”
http://blog.adamfurmanek.pl/2015/08/22/ilp-part-1/ — a lot about ILP
https://github.com/afish/MilpManager — library for modelling
https://neos-server.org/neos/solvers/index.html — NEOS cloud for solving problems
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 61
![Page 62: Integer Linear Programming - Adam Furmanek...Declarative programming at a glance. Some theory. Real life example. Implementation consideration. Summary. 07.02.2020 INTEGER LINEAR PROGRAMMING,](https://reader036.vdocuments.site/reader036/viewer/2022062602/5edd3302ad6a402d666833ef/html5/thumbnails/62.jpg)
HT TP://BLOG.ADAMFURMANEK.PL
@FURMANEKADAM
07.02.2020 INTEGER LINEAR PROGRAMMING, ADAM FURMANEK 62