作业4.3 4.4

zhhhhha / 2025-01-25 / 原文

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("3010")
 

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("3010")