Created
June 26, 2020 14:18
-
-
Save yangyushi/7953acd10d1ac9187e4ac3feb35f293c to your computer and use it in GitHub Desktop.
introductory of linear programming in using python or Pulp
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
""" | |
This module is a demonstartion of using scipy to solve LP problem | |
following this tutorial: https://realpython.com/linear-programming-python | |
The problem os as following | |
maximize z = x + 2 y, same as minimizing -z = -x - 2y | |
subject to 2x + y <= 20 | |
-4x + 5y <= 10 | |
-x + 2y >= -2, same as x - 2y <= 2 for scipy | |
-x + 5y = 15 | |
x >= 0 | |
y >= 0 | |
the variable x and y are called decision variables | |
the output will be | |
con: array([0.]) -> equality constraints residuals | |
fun: -16.818181818181817 -> optimum cost | |
message: 'Optimization terminated successfully.' | |
nit: 3 -> the number of iterations | |
slack: array([ 0. , 18.18181818, 3.36363636]) | |
-> the differences between values of | |
the left side and right side of the | |
constrains | |
status: 0 -> 0 = found optimum solution | |
success: True | |
x: array([7.72727273, 4.54545455]) -> optimum values of the | |
decision variables x & y | |
""" | |
from scipy.optimize import linprog | |
obj = [-1, -2] | |
lhs_ineq = [ | |
[2, 1], | |
[-4, 5], | |
[1, -2] | |
] | |
rhs_ineq = [ 20, 10, 2 ] | |
lhs_eq = [[-1, 5]] | |
rhs_eq = [15] | |
bounds = [ | |
(0, float("inf")), # for x | |
(0, float("inf")), # for y | |
] | |
opt = linprog(c=obj, | |
A_ub=lhs_ineq, b_ub=rhs_ineq, | |
A_eq=lhs_eq, b_eq=rhs_eq, | |
bounds=bounds, | |
method="revised simplex") | |
print(opt) |
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
""" | |
This module is a demonstartion of using PuLP to solve LP problem | |
following this tutorial: https://realpython.com/linear-programming-python | |
The problem os as following | |
maximize z = x + 2 y | |
subject to 2x + y <= 20 | |
-4x + 5y <= 10 | |
-x + 2y >= -2 | |
-x + 5y = 15 | |
x >= 0 | |
y >= 0 | |
where x are integers | |
the variable x and y are called decision variables | |
the output will be | |
con: array([0.]) -> equality constraints residuals | |
fun: -16.818181818181817 -> optimum cost | |
message: 'Optimization terminated successfully.' | |
nit: 3 -> the number of iterations | |
slack: array([ 0. , 18.18181818, 3.36363636]) | |
-> the differences between values of | |
the left side and right side of the | |
constrains | |
status: 0 -> 0 = found optimum solution | |
success: True | |
x: array([7.72727273, 4.54545455]) -> optimum values of the | |
decision variables x & y | |
""" | |
from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable | |
from pulp import GLPK | |
model = LpProblem(name="example", sense=LpMaximize) | |
x = LpVariable(name="x", lowBound=0, cat="Integer") | |
y = LpVariable(name="y", lowBound=0) | |
constraint_1 = 2 * x + 1 * y <= 20 | |
constraint_2 = 4 * x - 5 * y >= -10 | |
constraint_3 = -x + 2 * y >= -2 | |
constraint_4 = -x + 5 * y == 15 | |
cost_func = x + 2 * y | |
model += constraint_1 | |
model += constraint_2 | |
model += constraint_3 | |
model += constraint_4 | |
model += cost_func | |
model.solve(solver=GLPK(msg=False)) | |
for var in model.variables(): | |
print(f"{var.name}: {var.value()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment