Programmation linéaire

Introduction à l'optimisation en Python

Jasmin Ludolf

Content Developer

Programmation linéaire

  • Objectif et contraintes liés linéairement

  • Contrairement aux contraintes linéaires mixtes : éléments linéaires et non linéaires

Introduction à l'optimisation en Python

Panachage de produits

 

Type Nombre Bouteilles (h/boîte) Gobelets (h/boîte)
$M_A$ 4 1,5 1,3
$M_B$ 3 0,8 2,1

 

  • Chaque machine fonctionne 30 h par semaine

 

  • Demande : bouteilles pour boissons, gobelets pour yogourt
Introduction à l'optimisation en Python

Maximiser le profit

  • Profit par boîte
B ($/boîte) C ($/boîte)
480 510

 

  • Fonction objectif : $480B + 510C$
  • Contraintes
    • Machine A : $1.5B+1.3C\leq 4 \times 30$
    • Machine B : $0.8B+ 2.1C \leq 3\times 30$
    • Non-négativité : $B, C \geq 0$
Introduction à l'optimisation en Python

Utiliser PuLP

from pulp import *


model = LpProblem('MaxProfit', LpMaximize)
B = LpVariable('B', lowBound=0) C = LpVariable('C', lowBound=0)
model += 480*B + 510*C
model += 1.5*B + 1.3*C - 120, "M_A" model += 0.8*B + 2.1*C - 90, "M_B"
  • LpMaximize ou LpMinimize
Introduction à l'optimisation en Python

Sortie du modèle PuLP

print(model)
ProductMix:
MAXIMIZE
480*B + 510*C + 0
SUBJECT TO
M_A: 1.5 B + 1.3 C <= 120

M_B: 0.8 B + 2.1 C <= 90

VARIABLES
B Continuous
C Continuous
Introduction à l'optimisation en Python

Résoudre avec PuLP

status = model.solve()
print(status)
Welcome to the CBC MILP Solver 
...
1
print(f"Profit = {value(model.objective):.2f}")
print(f"Tons of bottles = {B.varValue:.2f}, tons of cups = {C.varValue:.2f}")
Profit = 40137.44
Boîtes de bouteilles = 63.98, boîtes de gobelets = 18.48
Introduction à l'optimisation en Python

Gérer plusieurs variables et contraintes

variables = LpVariable.dicts("Product", ['B', 'C'], lowBound=0)

OU

variables = LpVariable.dicts("Product", range(2), 0)
A = LpVariable.matrix('A', (range(2), range(2)), 0)
box_profit = {'B': 480, 'C': 510}
model += lpSum([box_profit[i] * variables[i] for i in ['B', 'C']]) 

model += 1.5*variables['B'] + 1.3*variables['C'] <= 120, "M_A"
model += 0.8*variables['B'] + 2.1*variables['C'] <= 90, "M_B"
Introduction à l'optimisation en Python

Passons à la pratique !

Introduction à l'optimisation en Python

Preparing Video For Download...