Linear constrained biscuits
Congratulations! Your biscuit business has expanded. You now have two bakeries, \(A\) and \(B\), to help you deliver your biscuits nationwide.
Your bakeries can each make 100 biscuits a day and the cost of making a biscuit in bakery \(A\) is \(1.5\) the quantity \(q\) while in bakery \(B\) it is \(1.75q\).
Price is defined by \(150 - q\).
Your business is booming and you already have 140 biscuit pre-orders for the day. You want to maximize your profit for the day. How many biscuits should you make in each bakery?
minimize, Bounds, and LinearConstraint have been loaded for you and the revenue function R is already defined.
Bài tập này là một phần của khóa học
Introduction to Optimization in Python
Hướng dẫn bài tập
- Define the cost function
Cusingq[0]for the quantities in bakery \(A\) andq[1]for bakery \(B\). - Define the
profitfunction. - Define the
boundsandconstraintsfor your optimization problem. - Perform optimization, saving to
result.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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}')