Skip to content

Instantly share code, notes, and snippets.

@summerofgeorge
Created November 12, 2025 22:18
Show Gist options
  • Select an option

  • Save summerofgeorge/390f87a735c4ef929857ca8fd41a3bc8 to your computer and use it in GitHub Desktop.

Select an option

Save summerofgeorge/390f87a735c4ef929857ca8fd41a3bc8 to your computer and use it in GitHub Desktop.
Production optimization example
#Optimal production quantities for products A and B
import pandas as pd
from scipy.optimize import linprog
# Coefficients for the objective function (negative for maximization)
c = [-40, -30]
# Coefficients for the inequality constraints
A = [
[2, 4], # Labor constraint
[3, 2] # Material constraint
]
b = [100, 90]
# Bounds for A and B (non-negative)
bounds = [(0, None), (0, None)]
# Solve the linear program
result = linprog(c, A_ub=A, b_ub=b, bounds=bounds, method='highs')
# Extract optimal values for A and B, and the maximum profit
optimal_A, optimal_B = result.x
max_profit = -result.fun
# Prepare results as a DataFrame for easy viewing
solution_df = pd.DataFrame({
'Product': ['A', 'B'],
'Optimal Units': [optimal_A, optimal_B]
})
solution_df['Optimal Units'] = solution_df['Optimal Units'].round(2)
solution_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment