--- id: wiki-2026-0508-linear-programming title: Linear Programming category: 10_Wiki/Topics status: verified canonical_id: self aliases: [LP, Linear Optimization, Simplex] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [optimization, lp, simplex, scipy, pulp, or-tools] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: { language: Python, framework: scipy/PuLP/OR-Tools } --- # Linear Programming ## 매 한 줄 > **"매 LP는 선형 목적함수 + 선형 제약 → 최적해는 vertex에 있다"**. Simplex가 vertex 사이를 움직이고, interior point는 내부를 가로지른다. ## 매 핵심 ### 매 표준형 - minimize cᵀx s.t. Ax ≤ b, x ≥ 0 - Feasible region = polytope (convex). 최적해는 항상 corner(꼭짓점) 또는 edge. - LP relaxation → IP/MIP의 lower bound. ### 매 알고리즘 비교 - **Simplex**: vertex hopping. exponential worst case지만 실제는 빠름. - **Interior point (Karmarkar)**: polynomial time. 큰 LP에 유리. - **Dual simplex**: 제약 추가 후 warm start에 강함 (branch-and-bound 내부). ### 매 응용 1. Resource allocation, transportation, assignment 2. Diet/blending, production scheduling 3. Network flow (max-flow, min-cost-flow) 4. ML: SVM (QP지만 LP-like), L1 regression, portfolio optimization ## 💻 패턴 ### scipy.optimize.linprog ```python from scipy.optimize import linprog # minimize -x0 - 2x1 s.t. x0+x1<=4, x0+3x1<=6, x>=0 res = linprog(c=[-1, -2], A_ub=[[1,1],[1,3]], b_ub=[4,6], bounds=[(0, None), (0, None)], method="highs") print(res.x, res.fun) # [3, 1] -> -5 ``` ### PuLP (선언적) ```python from pulp import LpProblem, LpVariable, LpMaximize, lpSum prob = LpProblem("prod", LpMaximize) x = LpVariable.dicts("x", ["A", "B"], lowBound=0) prob += 40*x["A"] + 30*x["B"] # objective prob += 2*x["A"] + x["B"] <= 100 # labor prob += x["A"] + x["B"] <= 80 # material prob.solve() ``` ### OR-Tools (production) ```python from ortools.linear_solver import pywraplp solver = pywraplp.Solver.CreateSolver("GLOP") # LP. "CBC" for MIP x = solver.NumVar(0, solver.infinity(), "x") y = solver.NumVar(0, solver.infinity(), "y") solver.Add(x + 2*y <= 14); solver.Add(3*x - y >= 0) solver.Maximize(3*x + 4*y) solver.Solve() ``` ### Integer/Mixed-Integer LP ```python # PuLP with integer variables x = LpVariable("x", lowBound=0, cat="Integer") y = LpVariable("y", lowBound=0, upBound=1, cat="Binary") # branch-and-bound: LP relaxation → branch on fractional vars ``` ### Transportation problem ```python # min sum c_ij x_ij s.t. sum_j x_ij = supply_i, sum_i x_ij = demand_j costs = [[8, 6, 10], [9, 12, 13], [14, 9, 16]] supply = [20, 30, 25]; demand = [10, 35, 30] # flatten to linprog ``` ### Sensitivity / dual ```python # scipy returns marginals via res.ineqlin.marginals (HiGHS) # Dual variable = shadow price of constraint ``` ## 매 결정 기준 | 상황 | Tool | |---|---| | 빠른 prototype | scipy.linprog (HiGHS) | | 선언적/큰 모델 | PuLP | | Production / MIP | OR-Tools, Gurobi, CPLEX | | 정수 변수 多 | CBC, Gurobi (commercial) | | Network flow | NetworkX, OR-Tools min_cost_flow | **기본값**: scipy HiGHS → 부족하면 PuLP+CBC → 상용 Gurobi. ## 🔗 Graph - 부모: [[Optimization]] - 변형: [[Integer-Programming]] - Adjacent: [[SVM]] ## 🤖 LLM 활용 **언제**: LP 모델링 (변수/제약 도출), code 생성, 결과 해석. **언제 X**: 대규모 commercial solver tuning, numerical stability 진단은 전문가. ## ❌ 안티패턴 - 비선형 제약을 LP로 모델링 (→ NLP/QP 필요) - 정수 변수에 LP 그대로 적용 (반올림 ≠ 최적) - 큰 dense A matrix를 sparse 변환 없이 사용 - Unbounded/infeasible 모델 진단 없이 결과 신뢰 ## 🧪 검증 / 중복 - Verified (Bertsimas Tsitsiklis "Intro to LO", scipy/PuLP/OR-Tools docs). 신뢰도 A. - 중복: 없음 (LP는 독립 토픽). ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — 매 prefix, scipy/PuLP/OR-Tools 패턴 |