funkelab / ilpy

Unified python wrappers for popular ILP solvers
https://funkelab.github.io/ilpy/
MIT License
3 stars 2 forks source link

Add expression objects, convertible to LinearConstraints #9

Closed tlambert03 closed 1 year ago

tlambert03 commented 1 year ago

this is now ready @funkey

from ilpy import Relation
from ilpy.expressions import Variable

u = Variable("u", index=0)
v = Variable("v", index=1)
e = Variable("e", index=2)

expr = 2 * u - 5 * v + e / 2 >= -3
print(expr)  # "2 * u - 5 * v + e / 2 >= -3"

constraint = expr.constraint()
assert constraint.get_value() == -3
assert constraint.get_relation() == Relation.GreaterEqual
assert constraint.get_coefficients() == {0: 2.0, 1: -5.0, 2: 0.5}

see https://github.com/funkelab/motile/pull/30 for an example of how this could be used in motile