การหาค่าเหมาะสมแบบนูนที่มีเงื่อนไขอสมการ

การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

Jasmin Ludolf

Content Developer

คำตอบที่มุมเทียบกับคำตอบภายใน

  • Alexia มีเวลาทำงานเพียง 5 ชั่วโมง

$$ w\leq 5$$

  • เงื่อนไขสองข้อ:
    • 24 ชั่วโมงในหนึ่งวัน
    • 5 ชั่วโมงสำหรับการทำงาน
  • เงื่อนไขก่อตัวเป็นมุม
  • คำตอบภายในไม่ใช่จุดตัดกัน

เส้นความพึงพอใจเท่ากันและเงื่อนไข โดยเส้นความพึงพอใจเท่ากันที่ระดับอรรถประโยชน์สูงสุดผ่านจุดตัดของเงื่อนไข

การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

ผู้ผลิตที่มีข้อจำกัดด้านกำลังผลิต

  • ผู้ผลิตรถยนต์ผลิตรถที่เหมือนกันในโรงงานสองแห่ง $A$, $B$
    • ปริมาณ: $q_A$, $q_B$
    • กำลังผลิต: $q_A\leq 90$, $q_B\leq 90$
    • ต้นทุน: $C_A(q)=3q$, $C_B(q)=3.5q$
  • อุปสงค์:
    • $P=120-Q$
  • สัญญา:
    • $Q\geq 92$
  • เป้าหมายเพื่อเพิ่มกำไรสูงสุด:
    • $\displaystyle\max \Pi(q_A,q_B)$

สายการผลิตรถยนต์

การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

การเพิ่มกำไรสูงสุด

  • เป้าหมาย:
    • $\Pi = R-C$
      • $R = PQ$
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

การเพิ่มกำไรสูงสุด

  • เป้าหมาย:
    • $\Pi = R-C$
      • $R = PQ = (120-Q)Q $
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

การเพิ่มกำไรสูงสุด

  • เป้าหมาย:

    • $\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$
  • ขอบเขต

    • $0\leq q_A, q_B\leq 90$
  • เงื่อนไข
    • $Q\geq92\Leftrightarrow 92\leq q_A+q_B$
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

การกำหนดสูตร

$$\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)
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

เพิ่มกำไรสูงสุดด้วย 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
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

เงื่อนไขไม่เชิงเส้นใน 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)
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

คำตอบด้วย 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 
  • ใช้ LinearConstraint() สำหรับปัญหาเชิงเส้นที่ตรงไปตรงมา
  • หากไม่ใช่ ให้ใช้ NonlinearConstraint()
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

มาฝึกกันเถอะ!

การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น

Preparing Video For Download...