A Python package that solves linear programming problems. https://code.google.com/p/pulp-or/ https://pythonhosted.org/PuLP/index.html
A linear programming problem is an optimization problem in which an objective function and constraints are expressed by a linear expression. For example
Is one of the linear programming problems.
Since it was introduced at PyConJP 2014, when I investigated how to use it, It's pretty easy to use, so I summarized it.
sudo pip install pulp
This expresses all the problems. For the constructor, choose the name of the problem and whether to minimize or maximize the objective function.
import pulp
problem = pulp.LpProblem('Problem Name', pulp.LpMinimize) #When minimizing
problem = pulp.LpProblem('Problem Name', pulp.LpMaximize) #When maximizing
Create variables for use in linear programming. There are two ways to make it. You will often use 2 when you have a large number of variables.
(1) Make one by one
pulp.LpVariable('name', 0, 1, 'Continuous')
Variables are in order from the front
With this code, you can generate a variable with a variable name that takes a continuous value from 0 to 1.
(2) Pass the list and make it all together
var = pulp.LpVariable.dicts('VAR', ([1,2,3], ['a', 'b']), 0, 1, 'Continuous')
Variables are in order from the front
3-5 is the same as before.
When this code is executed, it takes continuous values with a minimum value of 0 and a maximum value of 1,
You can get a dictionary with the variables VAR_1_a, VAR_1_b, VAR_2_a, ...
.
The access method is var [1] ['a']
like this.
This will be used in a future article.
Create an expression using variables and add it to the Mathematical Planning Problem object ** This is amazing.
For example, if you want to maximize the sum of ʻaand
b`
import pulp
problem = pulp.LpProblem('test', pulp.LpMinimize)
a = pulp.LpVariable('a', 0, 1)
b = pulp.LpVariable('b', 0, 1)
problem += a + b
If you print the problem in this state, the problem will be displayed.
test:
MINIMIZE
1*a + 1*b + 0
VARIABLES
a <= 1 Continuous
b <= 1 Continuous
Make a comparison using variables and ** add it to a mathematical planning problem object ** This is also great.
For example, for the variables ʻaand
b`
b
is 0.1 or moreand
b` is 0.5
When you want to create a constraintproblem += a >= 0
problem += b >= 0.1
problem += a + b == 0.5
Only the anti-inequality sign (<=, =>) could be used.
problem.solve()
only this.
Depending on the constraints, it may not be possible to solve it, so if you want to know the result,
status = problem.solve()
print pulp.LpStatus[status]
OK if Optimal appears in
You can get the value by calling the value
method of the variable added to the problem
.
import pulp
problem = pulp.LpProblem('sample', pulp.LpMinimize)
a = pulp.LpVariable('a', 0, 1)
b = pulp.LpVariable('b', 0, 1)
problem += a + b
problem += a >= 0
problem += b >= 0.1
problem += a + b == 0.5
status = problem.solve()
print "Status", pulp.LpStatus[status]
print problem
print "Result"
print "a", a.value()
print "b", b.value()
Result is,
Status Optimal
sample:
MINIMIZE
1*a + 1*b + 0
SUBJECT TO
_C1: a >= 0
_C2: b >= 0.1
_C3: a + b = 0.5
VARIABLES
a <= 1 Continuous
b <= 1 Continuous
Result
a 0.4
b 0.1