```python import numpy as np from scipy.optimize import linprog # 定义参数 orders = np.array([ [10, 1.6, 2.2], # 订单1:学校教学楼 [20, 1.8, 2.4], # 订单2:酒店客房 [20, 1.7, 2.3], # 订单3:医院病房 [15, 1.5, 2.0] # 订单4:办公楼 ]) raw_materials = np.array([ [5.5, 18], # 原材料1:长度5.5米,单价18元/根 [6.2, 22], # 原材料2:长度6.2米,单价22元/根 [7.8, 28] # 原材料3:长度7.8米,单价28元/根 ]) saw_width = 0.005 # 锯口宽度 tolerance = 0.01 # 允许误差范围 prices = np.array([480, 680, 550, 420]) # 窗框单价(元/套) # 初始化变量 num_orders = orders.shape[0] num_materials = raw_materials.shape[0] # 构建目标函数矩阵 f = [] # 成本最小化的目标函数系数 A = [] # 不等式约束矩阵 b = [] # 不等式约束右侧向量 # 遍历每个订单和原材料组合 for i in range(num_orders): order_quantity = orders[i, 0] # 当前订单数量 width_target = orders[i, 1] # 当前订单宽度目标 height_target = orders[i, 2] # 当前订单高度目标 # 考虑允许误差范围 width_min = width_target - tolerance width_max = width_target + tolerance height_min = height_target - tolerance height_max = height_target + tolerance for j in range(num_materials): material_length = raw_materials[j, 0] # 当前原材料长度 material_cost = raw_materials[j, 1] # 当前原材料成本 # 计算可能的切割方案 max_width_cuts = int((material_length - saw_width) // (width_max + saw_width)) max_height_cuts = int((material_length - saw_width) // (height_max + saw_width)) # 添加决策变量 f.append(material_cost) # 目标函数添加成本 # 添加订单需求约束 constraint_row = np.zeros(len(f)) constraint_row[-1] = -(max_width_cuts * 2 + max_height_cuts * 2) # 每根材料可生产的窗框数 if len(A) == 0: A = constraint_row else: A = np.vstack([A, constraint_row]) b.append(-order_quantity) # 添加原材料长度约束 constraint_row = np.zeros(len(f)) constraint_row[-1] = (width_max * 2 + height_max * 2 + 3 * saw_width) # 总需切割长度 A = np.vstack([A, constraint_row]) b.append(material_length) # 添加非负约束 bounds = [(0, None) for _ in range(len(f))] # 决策变量非负 # 求解线性规划问题 result = linprog(f, A_ub=A, b_ub=b, bounds=bounds, method='highs') solution = result.x cost = result.fun # 计算总收益和利润 total_revenue = np.sum(prices * orders[:, 0]) # 总收益 total_profit = total_revenue - cost # 利润 # 输出结果 print(f'总收益: {total_revenue:.2f} 元') print(f'总成本: {cost:.2f} 元') print(f'总利润: {total_profit:.2f} 元') # 计算利用率和损失率 used_materials = np.dot(solution, raw_materials[:, 0]) # 已使用原材料总长度 total_materials = np.sum(raw_materials[:, 0] * solution) # 原材料总长度 utilization_rate = used_materials / total_materials * 100 # 利用率 waste_rate = (total_materials - used_materials) / total_materials * 100 # 损失率 print(f'原材料利用率: {utilization_rate:.2f}%') print(f'切割损失率: {waste_rate:.2f}%') ``` [2025-05-10 14:23:41 | AI问答 | 1036点数解答]