开始使用免费开始使用

线性约束:饼干生产

恭喜!您的饼干业务已扩大。现在您拥有两个烘焙厂,\(A\) 和 $B$,可以帮助您将饼干配送到全国各地。

两个烘焙厂每天各能生产 100 块饼干。在烘焙厂 $A$,每块饼干的成本为 \(1.5\) 乘以数量 $q$;在烘焙厂 $B$,成本为 $1.75q$。

售价由 \(150 - q\) 给出。

业务蒸蒸日上,今天您已经有 140 块饼干的预订单。您希望最大化今天的利润。每个烘焙厂应各生产多少块饼干?

minimizeBoundsLinearConstraint 已为您加载,收入函数 R 也已定义。

本练习是课程的一部分

Python 优化入门

查看课程

练习说明

  • 使用 q[0] 表示烘焙厂 \(A\) 的产量、q[1] 表示烘焙厂 \(B\) 的产量,定义成本函数 C
  • 定义 profit 函数。
  • 为优化问题定义 boundsconstraints
  • 执行优化,并将结果保存到 result

交互式实操练习

通过完成这段示例代码来试试这个练习。

def R(q):
    return (150 - q[0] - q[1]) * (q[0] + q[1])

# Define the cost function
def C(q): 
  return ____

# Define the profit function
def profit(q): 
  return ____

# Define the bounds and constraints
bounds = Bounds(____, ____) 
constraints = LinearConstraint([1, 1], ____) 

# Perform optimization
result = ____(lambda q: ____,                 
                  [50, 50],              
                  bounds=bounds,
                  constraints=constraints)

print(result.message)
print(f'The optimal number of biscuits to bake in bakery A is: {result.x[0]:.2f}')
print(f'The optimal number of biscuits to bake in bakery B is: {result.x[1]:.2f}')
print(f'The bakery company made: ${-result.fun:.2f}')
编辑并运行代码