Bắt đầu ngayBắt đầu miễn phí

Nonlinear constrained biscuits

That was some excellent baking!

Now can you solve the same problem again using NonlinearConstraint?

Recall the constraint for the bakeries is they need to fulfill a minimum of 140 pre-orders and each factory can make 100 biscuits daily.

minimize, Bounds, and NonlinearConstraint have been loaded for you as well as the revenue function R, cost function C, and profit function profit.

Bài tập này là một phần của khóa học

Introduction to Optimization in Python

Xem khóa học

Hướng dẫn bài tập

  • Define the constraints using the lambda function q, setting the lower and upper bounds.
  • Perform optimization by adding the optimization function, bounds, and constraints to miminize().

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.

# Redefine the problem with NonlinearConstraint
constraints = NonlinearConstraint(lambda q: ____, ____, ___)

# Perform optimization
result = minimize(lambda q: ____, 
                  [50, 50], 
                  bounds=____,
                  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}')
Chỉnh sửa và Chạy Mã