Linear Programming is defined as a method of achieving the best outcome using a mathematical model whose relationships are represented by linear relationships. In contrast, to Non-Linear Programming a basic assumption of linear programming is that the relationships between the decision variables, constraints and the objective functions are linear - meaning they can be represented by a straight line. [1] [2] [3]
The variables (integer
, or continuos
, if all the variables are
integer
then it is known as Integer Programming, however, most of the
real world problems are "Mixed Integer Programming"
abbreviated as MILP
. The utility functions for linear programming is available under linprog.py and the
non-linear programming defination is currently in development.
The code is publicly available at optim
by
ZenithClown. The module requires external libraries
pulp
and scipy
, to initialize:
# install pip libraries:
pip install pulp # linear programming using pulp
pip install scipy # scientific computing library
# clone the code like:
git clone https://gist.github.com/ZenithClown/277ddc84fa10239b4a24a366813761a5.git optim
export PYTHONPATH="${PYTHONPATH}:optim"
Currently, a objective function is defined for PuLP
, that aims to simplify large variables problem defination using a
combination of scipy.optimize.linprog
and pulp
module approach. TODO: Documentation!!
import pulp as p
import linprog as lp
n = 3 # no. of variables
defination = lp.LpObject(n, cost = [10, 15, 25], sense = "minimize", var_lb = [0] * n)
print(repr(defination)) # development representation
>> LP(2402254029488, lp-problem, <n = 3, vars = dict_keys(['X1', 'X2', 'X3'])>)
print(f"Defined Objective Function: {defination.problem.objective}")
>> Defined Objective Function: 10*X1 + 15*X2 + 25*X3
# add constraints
defination.problem += defination.variables["X1"] + defination.variables["X2"] + defination.variables["X3"] >= 1_000
defination.problem += defination.variables["X1"] - 2 * defination.variables["X2"] >= 0
defination.problem += defination.variables["X3"] >= 340
status = defination.problem.solve()
print(p.LpStatus[status])
>> Optimal
print("X1 =", p.value(defination.variables["X1"]))
print("X2 =", p.value(defination.variables["X2"]))
print("X3 =", p.value(defination.variables["X3"]))
print("Objective = ", p.value(defination.problem.objective))
>> X1 = 660.0
>> X2 = 0.0
>> X3 = 340.0
>> Objective = 15100.0