Basic Usage
Variable¶
Create a variable with flipy.LpVariable
:
x = flipy.LpVariable('x', low_bound=1, up_bound=5)
By default, variables are created as continous type, meaning it can take any value between its lower and upper bound.
Integer and binary variable types are also available:
y = flipy.LpVariable('y', var_type=flipy.VarType.Integer, low_bound=1, up_bound=5)
z = flipy.LpVariable('z', var_type=flipy.VarType.Binary)
y
can take any integer value from 1 to 5 (1, 2, 3, 4, 5), while z
can only take 0 or 1.
Expression¶
An LP expression is a mapping from variables to coefficients, with an optional constant.
For example, the above LP expression can be created by:
expr = flipy.LpExpression(expression={x: 3, y: 2}, constant=5)
Constraint¶
A constraint has 3 compontents: lhs
, sense
and rhs
.
lhs
: The left-hand side expression of the constraintsense
: The sense of the constraint, can be one of:leq
: The left-hand side expression must be less than or equal to the right hand side expressioneq
: The left-hand side expression must be equal to the right hand side expressiongeq
: The left-hand side expression must be greater than or equal to the right hand side expression
rhs
: The right-hand side expression of the constraint
For example, the above constraint can be created by:
lhs = flipy.LpExpression(expression={x: 1})
rhs = flipy.LpExpression(expression={y: 2}, constant=10)
cons = flipy.LpConstraint(lhs, 'leq', rhs)
Objective¶
The purpose of linear programming is to find the optimal values of the variables that maximize or minimize the objective function. An objective function defines the numeric value that we want to optimize.
In flipy, an objective function is an expression with a sense indicator. The sense indicator can be either flipy.Minimize
or flipy.Maximize
.
To create an objective function:
obj = flipy.LpObjective(expression={x: 3, y: 2}, constant=5, sense=flipy.Maximize)
Problem¶
To create a problem, we need to provide the objective and the constraints:
problem = flipy.LpProblem('test_problem', lp_objective=obj, lp_constraints=[cons])
We can also create an empty problem and add the objective function and constraints later
problem = flipy.LpProblem('test_problem')
problem.add_constraint(cons)
problem.set_objective(obj)
Solving¶
Flipy currently supports two linear solver: CBC and Gurobi
To solve a problem, create a solver instance first:
solver = flipy.CBCSolver()
Solve the created problem:
status = solver.solve(problem)
status
is the
There are 4 possible status codes:
flipy.SolutionStatus.Optimal
: The solver has succesfully solved the problem and an optimal solution is returnedflipy.SolutionStatus.Infeasible
: The solver couldn't find a solution for the problem as there is no feasible regionflipy.SolutionStatus.Unbounded
: The solver couldn't find a solution for the problem as the feasible region is unbouned and the objective function is infiniteflipy.SolutionStatus.NotSolved
: An error has occurred and the problem is not solved
To retrieve the values of the variables, call LpVariable.evaluate
:
val_x = x.evaluate()
val_y = y.evaluate()
To get the value of objective function: call LpObjective.evalaute()
:
val_obj = obj.evaluate()