Optimasi cekung dengan kendala ketaksamaan

Pengantar Optimasi di Python

Jasmin Ludolf

Content Developer

Solusi corner vs. interior

  • Alexia hanya punya 5 jam kerja

$$ w\leq 5$$

  • Dua kendala:
    • 24 jam per hari
    • 5 jam kerja
  • Kendala membentuk sudut (corner)
  • Solusi interior bukan titik perpotongan

Kurva indiferensi dan kendala. Kurva indiferensi pada utilitas optimal melalui titik perpotongan kendala.

Pengantar Optimasi di Python

Produsen dengan batas kapasitas

  • Produsen mobil membuat mobil identik di dua pabrik $A$, $B$
    • Kuantitas: $q_A$, $q_B$
    • Kapasitas: $q_A\leq 90$, $q_B\leq 90$
    • Biaya: $C_A(q)=3q$, $C_B(q)=3.5q$
  • Permintaan:
    • $P=120-Q$
  • Kontrak:
    • $Q\geq 92$
  • Tujuan: maksimalkan laba
    • $\displaystyle\max \Pi(q_A,q_B)$

Lini perakitan mobil

Pengantar Optimasi di Python

Memaksimalkan laba

  • Tujuan:
    • $\Pi = R-C$
      • $R = PQ$
Pengantar Optimasi di Python

Memaksimalkan laba

  • Tujuan:
    • $\Pi = R-C$
      • $R = PQ = (120-Q)Q $
Pengantar Optimasi di Python

Memaksimalkan laba

  • Tujuan:

    • $\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$
  • Batas

    • $0\leq q_A, q_B\leq 90$
  • Kendala
    • $Q\geq92\Leftrightarrow 92\leq q_A+q_B$
Pengantar Optimasi di Python

Formulasi

$$\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)
Pengantar Optimasi di Python

Maksimalkan laba dengan 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
Pengantar Optimasi di Python

Kendala nonlinier di 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)
Pengantar Optimasi di Python

Solusi dengan 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 
  • Gunakan LinearConstraint() untuk masalah linear sederhana
  • Selain itu, gunakan NonlinearConstraint()
Pengantar Optimasi di Python

Ayo berlatih!

Pengantar Optimasi di Python

Preparing Video For Download...