Get startedGet started for free

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.

This exercise is part of the course

Introduction to Optimization in Python

View Course

Exercise instructions

  • Define the cost function C using q[0] for the quantities in bakery \(A\) and q[1] for bakery \(B\).
  • Define the profit function.
  • Define the bounds and constraints for your optimization problem.
  • Perform optimization, saving to result.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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 A is: {result.x[1]:.2f}')
print(f'The bakery company made: ${-result.fun:.2f}')
Edit and Run Code