Tối ưu lồi với ràng buộc bất đẳng thức

Nhập môn Tối ưu hóa với Python

Jasmin Ludolf

Content Developer

Nghiệm góc so với nghiệm bên trong

  • Alexia chỉ có 5 giờ làm việc

$$ w\leq 5$$

  • Hai ràng buộc:
    • 24 giờ mỗi ngày
    • 5 giờ làm việc
  • Các ràng buộc tạo thành một góc
  • Nghiệm bên trong không phải giao điểm

Đường bàng quan và ràng buộc. Đường bàng quan tại mức hữu dụng tối ưu đi qua giao điểm của các ràng buộc.

Nhập môn Tối ưu hóa với Python

Nhà sản xuất với ràng buộc công suất

  • Hãng xe sản xuất mẫu xe giống nhau tại hai nhà máy $A$, $B$
    • Sản lượng: $q_A$, $q_B$
    • Công suất: $q_A\leq 90$, $q_B\leq 90$
    • Chi phí: $C_A(q)=3q$, $C_B(q)=3.5q$
  • Cầu:
    • $P=120-Q$
  • Hợp đồng:
    • $Q\geq 92$
  • Mục tiêu tối đa hóa lợi nhuận:
    • $\displaystyle\max \Pi(q_A,q_B)$

Dây chuyền lắp ráp ô tô

Nhập môn Tối ưu hóa với Python

Tối đa hóa lợi nhuận

  • Mục tiêu:
    • $\Pi = R-C$
      • $R = PQ$
Nhập môn Tối ưu hóa với Python

Tối đa hóa lợi nhuận

  • Mục tiêu:
    • $\Pi = R-C$
      • $R = PQ = (120-Q)Q $
Nhập môn Tối ưu hóa với Python

Tối đa hóa lợi nhuận

  • Mục tiêu:

    • $\Pi = R-C$
      • $R = PQ = (120-Q)Q=\left[120-(q_A+q_B)\right](q_A+q_B)$
      • $C=C_A+C_B=3q_A+3.5q_B$
  • Giới hạn

    • $0\leq q_A, q_B\leq 90$
  • Ràng buộc
    • $Q\geq92\Leftrightarrow 92\leq q_A+q_B$
Nhập môn Tối ưu hóa với Python

Mô hình hóa

$$\max_{q_A,q_B}R(q_A,q_B)-C(q_A,q_B)$$

$$s.t.$$

$$R(q_A,q_B)=\left[120-(q_A+q_B)\right](q_A+q_B)$$

$$\ \ \ \ \ C(q_A,q_B)=3q_A+3.5q_B$$

$$\ \ \ 0\leq q_A, q_B\leq 90$$

$$ \ \ \ \ 92\leq q_A+q_B$$

from scipy.optimize import minimize,\
        Bounds, LinearConstraint


def R(q): return (120 - (q[0] + q[1] )) * (q[0] + q[1])
def C(q): return 3*q[0] + 3.5*q[1]
def profit(q): return R(q) - C(q)
bounds = Bounds([0, 0], [90, 90])
constraints = LinearConstraint([1, 1], lb=92)
Nhập môn Tối ưu hóa với Python

Tối đa hóa lợi nhuận với SciPy

result = minimize(lambda q: -profit(q),

[50, 50],
bounds=bounds, constraints=constraints)
print(result.message) print(f'The optimal number of cars produced in plant A is: {result.x[0]:.2f}') print(f'The optimal number of cars produced in plant B is: {result.x[1]:.2f}') print(f'The firm made: ${-result.fun:.2f}')
Optimization terminated successfully
The optimal number of cars produced in plant A is: 90.00
The optimal number of cars produced in plant B is: 2.00
The firm made: $2299.00
Nhập môn Tối ưu hóa với Python

Ràng buộc phi tuyến trong SciPy

from scipy.optimize import NonlinearConstraint 
import numpy as np


constraints = NonlinearConstraint(lambda q: q[0] + q[1], lb=92, ub=np.inf)
result = minimize(lambda q: -profit(q), [50, 50], bounds=Bounds([0, 0], [90, 90]), constraints=constraints)
Nhập môn Tối ưu hóa với Python

Lời giải với NonlinearConstraint

print(result.message)
print(f'The optimal number of cars produced in plant A is: {result.x[0]:.2f}')
print(f'The optimal number of cars produced in plant B is: {result.x[1]:.2f}')
print(f'The firm made: ${-result.fun:.2f}') 
Optimization terminated successfully
The optimal number of cars produced in plant A is: 90.00
The optimal number of cars produced in plant B is: 2.00
The firm made: $2299.00 
  • Dùng LinearConstraint() cho bài toán tuyến tính, đơn giản
  • Nếu không, dùng NonlinearConstraint()
Nhập môn Tối ưu hóa với Python

Ayo berlatih!

Nhập môn Tối ưu hóa với Python

Preparing Video For Download...