funkelab / ilpy

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

Add conversion from Expression to Objective #13

Closed tlambert03 closed 1 year ago

tlambert03 commented 1 year ago

This extends Expressions added in #9 to convert to Objective objects (with an optional sense argument).

Note @funkey : this also changes the name of the method constraint() to as_constraint() ... (and as_objective here). I think it reads a bit clearer, you?

from ilpy import Sense
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

objective = expr.as_objective(Sense.Maximize)  # optional arg ... default is Minimize
assert objective.get_constant() == -3
assert objective.get_sense() == Sense.Maximize
assert objective.get_coefficients() == [2.0, -5.0, 0.5]
tlambert03 commented 1 year ago

@funkey ... i'll take care of the cleanup here. you can just check if the general pattern looks ok

funkey commented 1 year ago

...too late ;)

funkey commented 1 year ago

Haha, I see, I missed a few more. Sorry for the noise, and thanks for cleaning up :)