學號3020
4.3
點選檢視程式碼
import matplotlib.pyplot as plt
import numpy as np
import cvxpy as cp
x=cp.Variable(6,pos=True)
obj=cp.Minimize(x[5])
a1=np.array([0.025, 0.015, 0.055, 0.026])
a2=np.array([0.05, 0.27, 0.19, 0.185, 0.185])
a3=np.array([1, 1.01, 1.02, 1.045, 1.065])
k=0.05; kk=[]; qq=[]
while k<0.27:
con=[cp.multiply(a1,x[1:5])-x[5]<=0,a2@x[:-1]>=k, a3@x[:-1]==1]
prob=cp.Problem(obj,con)
prob.solve(solver='GLPK_MI')
kk.append(k); qq.append(prob.value)
k=k+0.005
plt.rc('text',usetex=False); plt.rc('font',size=16); plt.rc('font',family='SimHei')
plt.plot(kk,qq,'k')
plt.plot(kk,qq,'b.')
plt.xlabel("收益 k"); plt.ylabel("風險 Q",rotation=0)
plt.show()
print("學號:3020")
4.4
點選檢視程式碼
MAX_A = 15
MAX_B = 24
MAX_DEBUG = 5
products = [
{"name": "Ⅰ", "A_hours": 1, "B_hours": 6, "debug_hours": 1, "profit": 2}, # 假設產品Ⅰ至少使用1小時裝置A
{"name": "Ⅱ", "A_hours": 5, "B_hours": 2, "debug_hours": 1, "profit": 1}
]
max_profit = 0
best_plan = {}
for i in range(MAX_A // products[0]["A_hours"] + 1):
for j in range(MAX_B // products[1]["B_hours"] + 1):
# 計算除錯時間是否足夠
if (i + j) * max(products[0]["debug_hours"], products[1]["debug_hours"]) > MAX_DEBUG:
continue
total_A_hours = i * products[0]["A_hours"] + j * products[1]["A_hours"]
total_B_hours = i * products[0]["B_hours"] + j * products[1]["B_hours"]
if total_A_hours > MAX_A or total_B_hours > MAX_B:
continue
total_profit = i * products[0]["profit"] + j * products[1]["profit"]
if total_profit > max_profit:
max_profit = total_profit
best_plan = {"Ⅰ": i, "Ⅱ": j}
print(f"最優生產計劃:產品Ⅰ生產{best_plan['Ⅰ']}件,產品Ⅱ生產{best_plan['Ⅱ']}件")
print(f"最大利潤為:{max_profit}元")
print("學號:3020")