A more convenient interface to doing Binary Linear Programming with PuLP
Project description
Citrus
Intended to work like PuLP, but with a few convenience functions thrown in.
pip install citrus
Comparisons
ANDing two variables
# without citrus
import pulp
p = pulp.LpProblem('and example', pulp.LpMinimize)
x = pulp.LpVariable('x', cat=pulp.LpBinary)
y = pulp.LpVariable('y', cat=pulp.LpBinary)
x_and_y = pulp.LpVariable('x_and_y', cat=pulp.LpBinary)
model.addConstraint(x_and_y >= x + y - 1)
model.addConstraint(x_and_y <= x)
model.addConstraint(x_and_y <= y)
# with citrus
import citrus
p = citrus.Problem('and example', pulp.LpMinimize)
x = p.make_var('x', cat=pulp.LpBinary)
y = p.make_var('y', cat=pulp.LpBinary)
x_and_y = x & y
# alternatively, x_and_y = citrus.logical_and(x, y)
ORing two variables
# without citrus
import pulp
p = pulp.LpProblem('or example', pulp.LpMinimize)
x = pulp.LpVariable('x', cat=pulp.LpBinary)
y = pulp.LpVariable('y', cat=pulp.LpBinary)
x_or_y = pulp.LpVariable('x_or_y', cat=pulp.LpBinary)
model.addConstraint(x_or_y <= x + y)
model.addConstraint(x_or_y >= x)
model.addConstraint(x_or_y >= y)
# with citrus
import citrus
p = citrus.Problem('or example', pulp.LpMinimize)
x = p.make_var('x', cat=pulp.LpBinary)
y = p.make_var('y', cat=pulp.LpBinary)
x_or_y = x | y
# alternatively, x_or_y = citrus.logical_or(x, y)
Negating a variable
# without citrus
p = pulp.LpProblem('negation test', pulp.LpMinimize)
x = pulp.LpVariable('x', cat=pulp.LpBinary)
not_x = pulp.LpVariable('not_x', cat=pulp.LpBinary)
p.addConstraint(not_x == 1 - x)
# With citrus
import citrus
p = citrus.Problem('negation test', pulp.LpMinimize)
x = p.make_var('x', cat=pulp.LpBinary)
not_x = citrus.negate(x)
Tips & Tricks
Sometimes, you'll have many variables that you want to AND or OR together:
p = citrus.Problem('vacation at some point', pulp.Maximize)
vacation_in_x_month = [
p.make_var('vacation in ' + month, cat=pulp.LpBinary)
for month in MONTHS
]
take_a_vacation = reduce(citrus.logical_or, vacation_in_x_month)
p.addConstraint(take_a_vacation)
API
Classes
-
Variable
is a subclass ofpulp.LpVariable
. It adds the following methods:- (classmethod)
from_lp_var
. Upgrade apulp.LpVariable
to a Variable. __or__(self, other)
Compute thelogical_or
of two binaryVariable
s__and__(self, other)
Compute thelogical_and
of two binaryVariable
s__and__(self, other)
Compute thelogical_and
of two binaryVariable
s
- (classmethod)
-
Problem
A subclass ofpulp.LpProblem
. It adds the following methodmake_var()
accepts same arguments aspulp.LpVariable
, but produces aVariable
Functions
negate(x: Variable)
Produce a newVariable
with the opposite value ofx
.logical_and(x: Variable, y: Variable)
Produce a newVariable
constrained to take on the AND ofx
andy
.logical_or(x: Variable, y: Variable)
Produce a newVariable
constrained to take on the OR ofx
andy
.logical_xor(x: Variable, y: Variable)
Produce a newVariable
constrained to take on the XOR ofx
andy
.implies(x: Variable, y: Variable)
Produce a new variable constrained to take on the value ofx => y
minimum(*xs)
Produce a newVariable
that can be no larger than the smallest inxs
maximum(*xy)
Produce a newVariable
that can be no smaller than the largest inxs
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
citrus-0.0.2.tar.gz
(6.0 kB
view hashes)
Built Distributions
citrus-0.0.2-py3-none-any.whl
(5.0 kB
view hashes)
citrus-0.0.2-py2.7.egg
(8.3 kB
view hashes)