首页 > 其他分享 >第五章习题

第五章习题

时间:2024-11-17 23:44:35浏览次数:1  
标签:cost production 第五章 demand x2 import 习题 x1

学号后四位:3018
5.4:

点击查看代码
import cvxpy as cp
import numpy as np
import pandas as pd
from scipy.optimize import minimize
import sympy as sp
sp.init_printing(use_unicode=True)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='cm'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
a = np.arange(100, 0, -1)
x = cp.Variable(100, nonneg=True)
obj = cp.Maximize(cp.sum(cp.sqrt(x)))
cons = [
    x[0] <= 10,
    x[0] + 2*x[1] <= 20,
    x[0] + 2*x[1] + 3*x[2] <= 30,
    x[0] + 2*x[1] + 3*x[2] + 4*x[3] <= 40,
    cp.sum(cp.multiply(a, x)) <= 1000,
]
prob = cp.Problem(obj, cons)
prob.solve(solver='ECOS')
print(f'最优解为:{x.value}'); print(f'最优值为:{prob.value}')
print("xuehao3018")

5.5:

点击查看代码
import cvxpy as cp
import numpy as np
import pandas as pd
from scipy.optimize import minimize
import sympy as sp
sp.init_printing(use_unicode=True)
import matplotlib.pyplot as plt
def obj(x):
    x1, x2, x3 = x
    return (-1)*(2*x1 + 3*x1**2 + 3*x2 + x2**2 + x3)

def ineq(x):
    x1, x2, x3 = x
    return [
        10 - (x1 + 2*x1**2 + x2 + 2*x2**2 + x3),
        50 - (x1 + x1**2 + x2 + x2**2 - x3),
        40 - (2*x1 + x1**2 + 2*x2 + x3),
        x1 + 2*x2 - 1,
    ]

def eq(x):
    x1, x2, x3 = x
    return x1**2 + x3 - 2

x0 = np.random.randn(3)
cons = [
    {'type': 'ineq', 'fun': ineq},
    {'type': 'eq', 'fun': eq},
]
bd = [(0, None), (None,None), (None, None)]
ret = minimize(obj, x0, constraints=cons, bounds=bd)
print(ret)
print('-'*100)
print("最优值为:", -ret.fun)
print("xuehao3018")

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

标签:cost,production,第五章,demand,x2,import,习题,x1
From: https://www.cnblogs.com/wdew/p/18509443

相关文章

  • 第二章习题
    学号后四位:30182.1:点击查看代码importmathimportpylabaspltimportnumpyasnpplt.rc('text',usetex=True)#调用字库x=np.linspace(-10,10,100)y1=np.cosh(x)y2=np.sinh(x)y3=math.e**x/2plt.plot(x,y1,label='$\\mathrm{cosh}(x)$'......
  • python基础练习题----练手
    python—练手题—40题#01-helloworldprint('helloworld!')#如果3大于0,则打印'ok'和'yes'if3>0:print('ok')print('yes')x=3y=4print(x+y)#02-printprint('helloworld!')print(�......
  • HDLBIts习题(7):状态机
    (1)较难习题1:134题(fsm_ps2data)    有个积攒数据的过程。(1)较难习题2:135题(fsm_serial)        读清题意,有一个检验选择是否发送的进程。(3)较难习题3:137题(fsm_serialdp)    加入了奇偶校验位检测机制(4)较难习题4:138题(fsm_hdlc)   ......
  • HDLBIts习题(2):位操作,For循环(generate与integer)
    (1)冷门习题1:VerilogLanguage-MoreVerilogFeatures-Reductionoperators    一个矢量的位操作,多比特矢量操作会变得方便。(2)冷门习题2:VerilogLanguage-MoreVerilogFeatures-Combinationfor-loop:Vectorreversal2     Verilog中的for循环(3......
  • 习题7.7
    1.代码实现点击查看代码importnumpyasnpfromscipy.optimizeimportcurve_fit,least_squaresfromscipy.linalgimportlstsqimportmatplotlib.pyplotaspltdefg(x,a,b):return(10*a)/(10*b+(a-10*b)*np.exp(-a*np.sin(x)))#给定参数a......
  • 习题7.4
    1.代码实现点击查看代码importnumpyasnpfromscipy.interpolateimportgriddataimportmatplotlib.pyplotasplt#定义函数deff(x,y):return(x**2-2*x)*np.exp(-x**2-y**2-x*y)#生成随机点np.random.seed(0)x_random=np.random.uni......
  • 习题7.3
    1.代码实现点击查看代码importnumpyasnpfromscipy.interpolateimportinterp1d,CubicSplineimportmatplotlib.pyplotasplt#给定数据T=np.array([700,720,740,760,780])V=np.array([0.0997,0.1218,0.1406,0.1551,0.1664])#要插值的温度点T_interp......
  • 数学建模习题8.5
    `importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.integrateimportsolve_ivp定义微分方程模型defmodel(t,y):f,df_dm,d2f_dm2,T,dT_dm=yd3f_dm3=-3*f*d2f_dm2+2*(df_dm)**2-Td2T_dm2=-2*f*dT_dmreturn[df_dm,d2f_dm2,d3f_dm......
  • 习题7.10(3)
    1.代码实现点击查看代码importnumpyasnpimportpylabaspltfromscipy.optimizeimportcurve_fit#原始数据点x0=np.array([-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9])y0=np.......
  • 习题8.5
    1.代码实现点击查看代码importnumpyasnpfromscipy.integrateimportodeintimportmatplotlib.pyplotasplt#定义微分方程组defsystem(state,eta):f,df,d2f,T,dT=stated3f=-3*d2f+2*df**2-Td2T=-2.1*df*dTreturn[df,......