📖 Scenario: You are working as a data scientist helping a company optimize their product pricing to maximize profit. The prices must stay within certain limits, and some relationships between prices must hold true.
🎯 Goal: Build a Python program using SciPy to find the best prices for two products that maximize profit, while respecting given bounds and constraints.
📋 What You'll Learn
Create a function to calculate profit based on product prices
Set bounds for product prices
Add a constraint that one product price must be at least 1.5 times the other
Use SciPy's minimize function with bounds and constraints
Print the optimized product prices and maximum profit
💡 Why This Matters
🌍 Real World
Companies often need to find the best prices for products to maximize profit while respecting market or production limits.
💼 Career
Understanding how to apply bounds and constraints in optimization is key for data scientists working in pricing, operations, and resource allocation.
Progress0 / 4 steps
1
Create the profit function
Create a function called profit that takes a list prices with two elements: price of product A and price of product B. The function should return the total profit calculated as 5 * prices[0] + 3 * prices[1] minus the cost 2 * prices[0] + prices[1].
SciPy
Hint
Use def profit(prices): to define the function and return the profit formula.
2
Set bounds for product prices
Create a variable called bounds that sets the price of product A between 1 and 10, and the price of product B between 2 and 8. Use a list of tuples for the bounds.
SciPy
Hint
Use a list of tuples like [(1, 10), (2, 8)] to set bounds for each product price.
3
Add constraint and optimize
Create a dictionary called constraint with key type set to 'ineq' and key fun set to a lambda function that takes prices and returns prices[0] * 1.5 - prices[1]. Then, use scipy.optimize.minimize to minimize the negative profit function starting from initial prices [2, 3], with the bounds and constraints set to a list containing constraint. Store the result in result. Remember to import minimize from scipy.optimize. Negate the profit function by defining neg_profit = lambda prices: -profit(prices).
SciPy
Hint
Use constraint = {'type': 'ineq', 'fun': lambda prices: prices[0] * 1.5 - prices[1]} and call minimize with neg_profit, initial guess, bounds, and constraints.
4
Print optimized prices and profit
Print the optimized prices from result.x with the message "Optimized prices:". Then print the maximum profit by calling profit with result.x and the message "Maximum profit:".
SciPy
Hint
Use print("Optimized prices:", result.x) and print("Maximum profit:", profit(result.x)).
Practice
(1/5)
1. What is the main purpose of bounds in scipy.optimize functions?
easy
A. To set the maximum number of iterations
B. To define the objective function to minimize
C. To limit the range of values each variable can take
D. To specify the initial guess for variables
Solution
Step 1: Understand the role of bounds
Bounds restrict each variable to a specific range, for example, between 0 and 1.
Step 2: Differentiate bounds from other parameters
Bounds are not about the objective function, initial guess, or iteration limits; they only limit variable values.
Final Answer:
To limit the range of values each variable can take -> Option C
Quick Check:
Bounds = variable limits [OK]
Hint: Bounds always restrict variable ranges, not function or iterations [OK]
Common Mistakes:
Confusing bounds with constraints
Thinking bounds set initial guesses
Assuming bounds limit iterations
2. Which of the following is the correct way to define bounds for two variables x and y where x is between 0 and 5, and y is between -1 and 1 in scipy.optimize.minimize?
easy
A. bounds = [(0, 5), (-1, 1)]
B. bounds = {0:5, -1:1}
C. bounds = [0, 5, -1, 1]
D. bounds = (0, 5, -1, 1)
Solution
Step 1: Recall bounds format in scipy.optimize
Bounds are given as a list of tuples, each tuple for one variable's (min, max).
Step 2: Check each option's format
bounds = [(0, 5), (-1, 1)] uses a list of tuples correctly. Options A (flat list), B (dictionary), and D (flat tuple) are invalid formats.
Final Answer:
bounds = [(0, 5), (-1, 1)] -> Option A
Quick Check:
Bounds = list of (min, max) tuples [OK]
Hint: Use list of (min, max) tuples for bounds [OK]
Common Mistakes:
Using dictionary instead of list
Passing flat list instead of tuples
Confusing tuple and list syntax
3. What will be the output of the following code snippet?
B. Bounds should be a list of tuples, not a flat list
C. Objective function must return a scalar
D. Bounds must be specified as a dictionary
Solution
Step 1: Check bounds format
The code uses bounds = [0, 1, 0, 1], which is a flat list. scipy expects a list of tuples for bounds.
Step 2: Confirm other parameters
Initial guess is inside bounds, function returns scalar, and bounds as dictionary is not required.
Final Answer:
Bounds should be a list of tuples, not a flat list -> Option B
Quick Check:
Bounds = list of tuples, not flat list [OK]
Hint: Use list of tuples for bounds, not flat list [OK]
Common Mistakes:
Using flat list instead of list of tuples
Setting initial guess outside bounds
Misunderstanding bounds format
5. You want to minimize the function f(x, y) = (x-3)^2 + (y+1)^2 with the constraints that x + y = 2 and bounds 0 ≤ x ≤ 4, -2 ≤ y ≤ 2. Which of the following correctly sets up the constraints and bounds for scipy.optimize.minimize?