习题5.4
点击查看代码
import numpy as np
import math
from scipy.optimize import minimize,Bounds
def func(x):return sum(math.sqrt(x[i]) for i in range(100))
def con(x):return 1000-np.sum(x[i]*(101-i+1) for i in range(100))
con1={'type':'ineq','fun': lambda x: 10-x[0]}
con2={'type':'ineq','fun': lambda x: 20-x[0]-2*x[1]}
con3={'type':'ineq','fun': lambda x: 30-x[0]-x[1]*2-x[2]*3}
con4={'type':'ineq','fun': lambda x: 40-x[0]-x[1]*2-x[2]*3-x[3]*4}
con5={'type':'ineq','fun': con}
cons=[con1,con2,con3,con4,con5]
# 变量的初始猜测
x0 = np.zeros(100)
# 边界条件,所有变量都必须大于等于0
bnds = [(0, None) for _ in range(100)]
res=minimize(func,x0,constraints=cons,bounds=bnds)
def negative_func(x): return -func(x) # 假设 func 是您要最大化的原始目标函数
res = minimize(negative_func, x0, constraints=cons, bounds=bnds)
# 注意:res.fun 现在是原始目标函数的相反数,所以您需要取其相反数来得到真正的最大值
max_func_value = -res.fun
print(res.x)
print(max_func_value)
print("学号:3001")
习题5.5
点击查看代码
import numpy as np
from scipy.optimize import minimize
# 定义目标函数
def objective(x): return - (2 * x[0] + 3 * x[0]**2 + 3 * x[1] + x[1]**2 + x[2])
# 定义约束条件
def con1(x): return 10 - (x[0] + 2 * x[0]**2 + x[1] + 2 * x[1]**2 + x[2])
def con2(x): return 50 - (x[0] + x[0]**2 + x[1] + x[1]**2 - x[2])
def con3(x): return 40 - (2 * x[0] + x[0]**2 + 2 * x[1] + x[2])
def con4(x): return x[0] + 2 * x[1] - 1
def con5(x): return x[0]
def con6(x): return x[0]**2 + x[2] - 2
# 定义约束条件的字典
cons = ({'type': 'ineq', 'fun': con1}, {'type': 'ineq', 'fun': con2}, {'type': 'ineq', 'fun': con3}, {'type': 'ineq', 'fun': lambda x: -con4(x)}, {'type': 'ineq', 'fun': lambda x: -con5(x)}, {'type': 'eq', 'fun': con6}) # 定义初始点
x0 = -np.random.rand(3)
bounds = [(0, None) for _ in range(1)]
solution = minimize(objective, x0, method='SLSQP', bounds=bounds, constraints=cons)
# 输出结果
print('Optimal solution:', solution.x)
print('Objective value at optimal solution:', -solution.fun)
print("学号:3001")
习题5.7
点击查看代码
import numpy as np demands = [40, 60, 80]
max_production = 100
total_demand = sum(demands) dp = np.full((4, total_demand + 1), float('inf'))
dp[0][0] = 0 prev_production = np.full((4, total_demand + 1), -1) for i in range(1, 4): prev_demand = sum(demands[:i-1]) for j in range(total_demand + 1): if j < prev_demand + demands[i-1]: continue for x in range(max(0, j - prev_demand - demands[i-1] + 1), min(max_production + 1, j - prev_demand + 1)): production_cost = 50 * x + 0.2 * x**2 storage_cost = 4 * (j - prev_demand - x) total_cost = dp[i-1][j-x] + production_cost + storage_cost if total_cost < dp[i][j]: dp[i][j] = total_cost prev_production[i][j] = x min_cost = float('inf')
final_state = -1
for j in range(total_demand, total_demand + 1): if dp[3][j] < min_cost: min_cost = dp[3][j] final_state = j production_plan = [0] * 3
current_state = final_state
for i in range(3, 0, -1): production_plan[i-1] = prev_production[i][current_state] current_state -= prev_production[i][current_state] print(f"最小总费用为: {min_cost} 元")
print("生产计划为:")
for i, plan in enumerate(production_plan, 1): print(f"第{i}季度生产: {plan} 台")print("学号:3001")