Last active
August 23, 2022 02:45
-
-
Save vinovator/2e89fd84bc9071ce19e8 to your computer and use it in GitHub Desktop.
Python script to solve Linear Programming problems using pulp library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# simpleLP2.py | |
# Python 2.7.6 | |
""" | |
Python script to solve Linear Programming problems | |
Uses Pulp library | |
Problem statement: (src - http://fisher.osu.edu/~croxton.4/tutorial/) | |
ChemCo produces two fertilizers, FastGro and ReallyFastGro. Each is made of | |
a mix of two growth agents. FastGro is a 50/50 mix of the two, and it sells | |
for $13 per pound. ReallyFastGro is a 25/75 mix (i.e., its 25% Agent 1 and | |
75% Agent 2), and sells for $15/pound. Agent 1 costs $2/pound and Agent 2 costs | |
$3/pound. ChemCo can purchase up to 250 pounds of Agent 1 and up to 350 pounds | |
of Agent 2 each day. What is ChemCo's optimal production strategy? i.e., | |
How much of each product should it produce in order to maximize its profits? | |
""" | |
import pulp as p | |
""" | |
What is the objective? | |
- To maximise profits, | |
- how much of each products (pounds per day) should be produced | |
so variables are products here, | |
FG - volume of FastGro per day (pounds/ day) | |
RFG - volume of ReallyFastGro per day (pounds / day) | |
Objective function | |
Total Revenue = 13FG + 15RFG | |
Total cost = | |
- Agent 1 = 2(0.5FG + 0.25RFG) | |
- Agent 2 = 3(0.5FG + 0.75RFG) | |
So total profit = | |
13FG + 15RFG - 2(0.5FG + 0.25RFG) - 3(0.5FG + 0.75RFG) | |
= 10.5FG + 12.25RFG | |
""" | |
# declare all variables | |
X = p.LpVariable("X", 0) # FG >= 0 | |
Y = p.LpVariable("Y", 0) # RFG >= 0 | |
# Define the problem | |
# Objective is to maximize profit | |
prob = p.LpProblem("problem", p.LpMaximize) | |
# Define constraints | |
prob += 0.5 * X + 0.25 * Y <= 250 | |
prob += 0.5 * X + 0.75 * Y <= 350 | |
prob += X >= 0 | |
prob += Y >= 0 | |
# Define objective function | |
prob += 10.5 * X + 12.25 * Y | |
# Solve the problem | |
status = prob.solve() | |
print("This is the {} solution".format(p.LpStatus[status])) | |
# make sure we got an optimal solution | |
assert status == p.LpStatusOptimal | |
# print the value of X and Y | |
print("To maximize profitability you have to sell {} FG and {} RFG". | |
format(p.value(X), p.value(Y))) | |
print("The maximum profit to be derived is $ {}".format( | |
prob.objective.value())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment