Giải mô hình PuLP

Phân tích Chuỗi Cung Ứng với Python

Aaren Stubberfield

Supply Chain Analytics Mgr.

Quy trình mô hình hóa với PuLP

  1. Khởi tạo mô hình
  2. Định nghĩa biến quyết định
  3. Định nghĩa hàm mục tiêu
  4. Định nghĩa ràng buộc
  5. Giải mô hình
    • gọi phương thức solve()
    • kiểm tra trạng thái nghiệm
    • in biến quyết định tối ưu
    • in giá trị hàm mục tiêu tối ưu
Phân tích Chuỗi Cung Ứng với Python

Giải mô hình - phương thức solve

.solve(solver=None)
  • solver = Tùy chọn: bộ giải cụ thể sẽ dùng, mặc định là bộ giải mặc định.
Phân tích Chuỗi Cung Ứng với Python
# Initialize, Define Decision Vars., Objective Function, and Constraints
from pulp import *
import pandas as pd
model = LpProblem("Minimize Transportation Costs", LpMinimize)
cust = ['A','B','C']
warehouse = ['W1','W2']
demand = {'A': 1500, 'B': 900, 'C': 800}
costs = {('W1','A'): 232, ('W1','B'): 255, ('W1','C'): 264, 
         ('W2','A'): 255, ('W2','B'): 233, ('W2','C'): 250}
ship = LpVariable.dicts("s_", [(w,c) for w in warehouse for c in cust], 
                         lowBound=0, cat='Integer')
model += lpSum([costs[(w, c)] * ship[(w, c)] for w in warehouse for c in cust])
for c in cust: model += lpSum([ship[(w, c)] for w in warehouse]) == demand[c]

# Solve Model
model.solve()
Phân tích Chuỗi Cung Ứng với Python

Giải mô hình - trạng thái nghiệm

LpStatus[model.status]
  • Not Solved: Trạng thái trước khi giải bài toán.
  • Optimal: Đã tìm được nghiệm tối ưu.
  • Infeasible: Không có nghiệm khả thi (ví dụ: đặt ràng buộc x ≤ 1 và x ≥ 2).
  • Unbounded: Hàm mục tiêu không bị chặn; tối đa hóa hoặc tối thiểu hóa sẽ tiến tới vô cực (ví dụ: chỉ có ràng buộc x ≥ 3).
  • Undefined: Có thể tồn tại nghiệm tối ưu nhưng chưa tìm thấy.
1 Keen, Ben Alex. “Linear Programming with Python and PuLP 2 Part 2.” _Ben Alex Keen_, 1 Apr. 2016, benalexkeen.com/linear-programming-with-python-and-pulp-part-2/._{{5}}
Phân tích Chuỗi Cung Ứng với Python
# Initialize, Define Decision Vars., Objective Function, and Constraints
from pulp import *
import pandas as pd
model = LpProblem("Minimize Transportation Costs", LpMinimize)
cust = ['A','B','C']
warehouse = ['W1','W2']
demand = {'A': 1500, 'B': 900, 'C': 800}
costs = {('W1','A'): 232, ('W1','B'): 255, ('W1','C'): 264,
         ('W2','A'): 255, ('W2','B'): 233, ('W2','C'): 250}
ship = LpVariable.dicts("s_", [(w,c) for w in warehouse for c in cust], lowBound=0, cat='Integer')
model += lpSum([costs[(w, c)] * ship[(w, c)] for w in warehouse for c in cust])
for c in cust: model += lpSum([ship[(w, c)] for w in warehouse]) == demand[c]
# Solve Model
model.solve()
print("Status:", LpStatus[model.status])
Status: Optimal
Phân tích Chuỗi Cung Ứng với Python

In biến ra chuẩn đầu ra:

for v in model.variables():
    print(v.name, "=", v.varValue)

Cấu trúc dữ liệu Pandas:

o = [{A:ship[(w,'A')].varValue, B:ship[(w,'B')].varValue, C:ship[(w,'C')].varValue}
     for w in warehouse]
print(pd.DataFrame(o, index=warehouse))
  • lặp qua biến của mô hình
  • lưu giá trị vào DataFrame của pandas
Phân tích Chuỗi Cung Ứng với Python
# Solve Model
model.solve()
print(LpStatus[model.status])
o = [{A:ship[(w,'A')].varValue, B:ship[(w,'B')].varValue, C:ship[(w,'C')].varValue}
     for w in warehouse]
print(pd.DataFrame(o, index=warehouse))

  Kết quả:

Status: Optimal
|       |A      |B      |C      |
|:------|:------|:------|:------|
|W1     |1500.0 |0.0    |0.0    |
|W2     |0.0    |900.0  |800.0  |
Phân tích Chuỗi Cung Ứng với Python

Giải mô hình - hàm mục tiêu tối ưu

In giá trị hàm mục tiêu tối ưu:

print("Objective = ", value(model.objective))
Phân tích Chuỗi Cung Ứng với Python
# Solve Model
model.solve()
print(LpStatus[model.status])
output = []
for w in warehouse: t = [ship[(w,c)].varValue for c in cust] output.append(t)
opd = pd.DataFrame.from_records(output, index=warehouse, columns=cust)
print(opd)
print("Objective = ", value(model.objective))
Status: Optimal
|       |A      |B      |C      |
|:------|:------|:------|:------|
|W1     |1500.0 |0.0    |0.0    |
|W2     |0.0    |900.0  |800.0  |
Objective = 757700.0
Phân tích Chuỗi Cung Ứng với Python

Tóm tắt

Giải mô hình

  • Gọi phương thức solve()
  • Kiểm tra trạng thái nghiệm
  • In giá trị biến quyết định
  • In giá trị hàm mục tiêu
Phân tích Chuỗi Cung Ứng với Python

Ayo berlatih!

Phân tích Chuỗi Cung Ứng với Python

Preparing Video For Download...