PuLP 모델 풀기

Python으로 배우는 공급망 분석

Aaren Stubberfield

Supply Chain Analytics Mgr.

PuLP의 일반적 모델링 절차

  1. 모델 초기화
  2. 의사결정변수 정의
  3. 목적함수 정의
  4. 제약식 정의
  5. 모델 풀기
    • solve() 메서드 호출
    • 해의 상태 확인
    • 최적 의사결정변수 출력
    • 최적 목적함수 값 출력
Python으로 배우는 공급망 분석

모델 풀이 - solve 메서드

.solve(solver=None)
  • solver = 선택 사항: 사용할 특정 솔버. 기본값은 기본 솔버입니다.
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()
Python으로 배우는 공급망 분석

모델 풀이 - 해의 상태

LpStatus[model.status]
  • Not Solved: 문제를 풀기 전 상태입니다.
  • Optimal: 최적해를 찾았습니다.
  • Infeasible: 실행 가능한 해가 없습니다(예: 제약식 x ≤ 1 및 x ≥ 2).
  • Unbounded: 목적함수가 무한대로 발산합니다(예: 유일한 제약식이 x ≥ 3).
  • Undefined: 최적해가 있을 수 있으나 찾지 못했습니다.
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}}
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
Python으로 배우는 공급망 분석

변수를 표준 출력에 인쇄:

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

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))
  • 모델 변수 반복
  • 값을 pandas DataFrame에 저장
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))

  출력:

Status: Optimal
|       |A      |B      |C      |
|:------|:------|:------|:------|
|W1     |1500.0 |0.0    |0.0    |
|W2     |0.0    |900.0  |800.0  |
Python으로 배우는 공급망 분석

모델 풀이 - 최적 목적함수 값

최적화된 목적함수 값 출력:

print("Objective = ", value(model.objective))
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
Python으로 배우는 공급망 분석

요약

모델 풀기

  • solve() 메서드 호출
  • 해의 상태 확인
  • 의사결정변수 값 출력
  • 목적함수 값 출력
Python으로 배우는 공급망 분석

연습해 봅시다!

Python으로 배우는 공급망 분석

Preparing Video For Download...