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
-
Variableis a subclass ofpulp.LpVariable. It adds the following methods:- (classmethod)
from_lp_var. Upgrade apulp.LpVariableto a Variable. __or__(self, other)Compute thelogical_orof two binaryVariables__and__(self, other)Compute thelogical_andof two binaryVariables__and__(self, other)Compute thelogical_andof two binaryVariables
- (classmethod)
-
ProblemA subclass ofpulp.LpProblem. It adds the following methodmake_var()accepts same arguments aspulp.LpVariable, but produces aVariable
Functions
negate(x: Variable)Produce a newVariablewith the opposite value ofx.logical_and(x: Variable, y: Variable)Produce a newVariableconstrained to take on the AND ofxandy.logical_or(x: Variable, y: Variable)Produce a newVariableconstrained to take on the OR ofxandy.logical_xor(x: Variable, y: Variable)Produce a newVariableconstrained to take on the XOR ofxandy.implies(x: Variable, y: Variable)Produce a new variable constrained to take on the value ofx => yminimum(*xs)Produce a newVariablethat can be no larger than the smallest inxsmaximum(*xy)Produce a newVariablethat 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file citrus-0.0.3.tar.gz.
File metadata
- Download URL: citrus-0.0.3.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0842a7ecb6292545f39e6167261d4a761aeed968d316b66e93665396e4e69f9
|
|
| MD5 |
a54dabf1e38ee1dc1d096a4020435979
|
|
| BLAKE2b-256 |
275bdff6a9af25d031fa22d5487e33e2468137ae0df0fe6989a10f184e19b90f
|
File details
Details for the file citrus-0.0.3-py3-none-any.whl.
File metadata
- Download URL: citrus-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
563c1c6053176ca75b1152635a2f8484ae13e6674f218fdd500d8a71e4bc21be
|
|
| MD5 |
a81b7804623b156a247a3b3ae57f1ee7
|
|
| BLAKE2b-256 |
242abf3795e34e867bea77a027301ecc06b59eacb006829a17360e1da94421ec
|