0
0
SciPydata~30 mins

Integer programming in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Integer Programming with SciPy
📖 Scenario: You are managing a small factory that produces two products. Each product requires a certain amount of resources, and you want to maximize your profit. However, you can only produce whole units of each product (no fractions). You will use integer programming to find the best number of units to produce.
🎯 Goal: Build a program using SciPy to solve an integer programming problem that maximizes profit while respecting resource limits.
📋 What You'll Learn
Create arrays for profit coefficients and resource constraints
Set up integer constraints for the decision variables
Use SciPy's milp function to solve the integer programming problem
Print the optimal number of units to produce for each product
💡 Why This Matters
🌍 Real World
Integer programming helps businesses decide how many whole units of products to make when resources are limited.
💼 Career
Many data science and operations research jobs require solving optimization problems like this to improve efficiency and profits.
Progress0 / 4 steps
1
Set up profit and resource data
Create a NumPy array called c with values [-20, -30] representing the negative profits for two products. Create a 2D NumPy array called A_ub with values [[1, 2], [3, 1]] representing resource usage per product. Create a NumPy array called b_ub with values [40, 30] representing resource limits.
SciPy
Need a hint?

Use np.array to create arrays with the exact values given.

2
Define integer constraints
Create a NumPy array called integrality with values [1, 1] to specify that both decision variables must be integers.
SciPy
Need a hint?

Use np.array with [1, 1] to indicate integer variables.

3
Solve the integer programming problem
Import milp and Bounds from scipy.optimize. Create a Bounds object called bounds with lower bounds 0 and no upper bounds for both variables. Use milp with arguments c=c, A_ub=A_ub, b_ub=b_ub, integrality=integrality, and bounds=bounds to solve the problem. Store the result in a variable called result.
SciPy
Need a hint?

Use Bounds to set lower bounds to 0 and upper bounds to infinity. Call milp with all required arguments.

4
Print the optimal production quantities
Print the string "Optimal production quantities:" followed by the result.x array which contains the number of units to produce for each product.
SciPy
Need a hint?

Use print to show the message and the result.x array.