0
0
SciPydata~30 mins

Constrained optimization in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Constrained Optimization with SciPy
📖 Scenario: Imagine you are managing a small factory that produces two products: chairs and tables. You want to maximize your profit, but you have limited resources like wood and labor hours. You need to find the best number of chairs and tables to produce without exceeding your resource limits.
🎯 Goal: You will build a program that uses constrained optimization to find the number of chairs and tables to produce that maximizes profit while respecting resource limits.
📋 What You'll Learn
Create a function to calculate total profit based on quantities of chairs and tables
Set up constraints for wood and labor availability
Use SciPy's optimization function to find the best production plan
Print the optimal number of chairs and tables and the maximum profit
💡 Why This Matters
🌍 Real World
Factories and businesses often need to decide how much of each product to make to maximize profit without wasting resources.
💼 Career
Understanding constrained optimization is useful for roles in operations research, data science, and business analytics to make smart decisions.
Progress0 / 4 steps
1
Create the profit function
Create a function called profit that takes a list x with two elements: the number of chairs (x[0]) and tables (x[1]). The function should return the total profit calculated as 50 * x[0] + 80 * x[1].
SciPy
Need a hint?

Use def to create the function and return the sum of profits for chairs and tables.

2
Set up resource constraints
Create a list called constraints with two dictionaries. The first dictionary should have 'type': 'ineq' and a 'fun' key with a lambda function that returns 100 - (5 * x[0] + 10 * x[1]) representing the wood limit. The second dictionary should have 'type': 'ineq' and a 'fun' key with a lambda function that returns 80 - (2 * x[0] + 4 * x[1]) representing the labor limit.
SciPy
Need a hint?

Use a list of dictionaries with 'type': 'ineq' and lambda functions for constraints.

3
Use SciPy to find the optimal solution
Import minimize from scipy.optimize. Use minimize with the function lambda x: -profit(x), initial guess [0, 0], and the constraints list. Save the result in a variable called result.
SciPy
Need a hint?

Remember to import minimize and use a lambda to minimize the negative profit.

4
Print the optimal production and profit
Print the optimal number of chairs and tables from result.x rounded to 2 decimals. Then print the maximum profit by calling profit with result.x rounded to 2 decimals.
SciPy
Need a hint?

Use round() to limit decimals and print() to show results.