การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น
Jasmin Ludolf
Content Developer
อุปสงค์:
การผลิตชุดราตรี:


$C$: ต้นทุนผ้า + ค่าแรงคุณ S + ต้นทุนค่าเสียโอกาสของคุณ T
ต้นทุนค่าเสียโอกาส: ต้นทุนของการเลือกตัดเย็บแทนที่จะทำงานอื่น
$C=110g+240g+105g+75t+160t+35t$
$C=455g+270t$
| ต้นทุน | ผ้า | คุณ S | คุณ T |
|---|---|---|---|
| ชุดราตรี | $\$110$ | $\$40/h \times 6h = \$240$ | $\$35/h \times 3h = \$105$ |
| ชุดทักซิโด้ | $\$75$ | $\$40/h \times 4h = \$160$ | $\$35/h \times 1h = \$35$ |
ข้อจำกัด:
อุปสงค์: $g\leq20$, $t\leq12$
อุปทาน: $6g+4t\leq40$, $3g+t\leq20$
from scipy.optimize import milp, Bounds, LinearConstraintresult = milp([-545, -330],integrality=[1, 1],bounds=Bounds([0, 0], [20, 12]),constraints=LinearConstraint([[6, 4], [3, 1]], ub=[40, 20]))
print(result.message)
print(f'The optimal number of gowns produced is: {result.x[0]:.2f}')
print(f'The optimal number of tuxedos produced is: {result.x[1]:.2f}')
Optimization terminated successfully. (HiGHS Status 7: Optimal)
The optimal number of gowns produced is: 6.00
The optimal number of tuxedos produced is: 1.00
result = milp([-545, -330],
bounds=Bounds([0, 0], [20, 12]),
constraints=LinearConstraint([[6, 4], [3, 1]], ub=[40, 20]))
...
The optimal number of gowns produced is: 6.67
The optimal number of tuxedos produced is: 0.00
คำตอบที่ได้คือ 6.67 ชุดราตรี และ 0.00 ชุดทักซิโด้ $\rightarrow$
ปัดขึ้น เป็น 7 ชุดราตรี และ 0 ชุดทักซิโด้
ปัดลง เป็น 6 ชุดราตรี และ 0 ชุดทักซิโด้
การหาค่าที่เหมาะสมที่สุดใน Python เบื้องต้น