HiGHS icon indicating copy to clipboard operation
HiGHS copied to clipboard

highspy: Highs.val should work for highs_linear_expression

Open few opened this issue 1 year ago • 1 comments

In my modeling, I'm carrying around several linear expressions where sometimes I'm only interested in the value of the expressions and not the individual variables. It would be nice if that could be supported.

h = highspy.Highs()
x = h.addVariable(ub=3, obj=1)
y = h.addVariable(ub=4, obj=1)
h.maximize()

assert abs(h.val(x)-3) < 1e-6
assert abs(h.val(y)-4) < 1e-6
assert abs(h.val(x+y)-7) < 1e-6 #AttributeError: 'highs_linear_expression' object has no attribute 'index'

few avatar Aug 24 '24 08:08 few

This is a simple feature (if we limit to linear expressions). I've already added support in #1891. Will commit when I've finished with all the new tests and suitable highspy coverage.

Note: it also supports constant addition and all in/equality variants. Strict inequalities (<,>) aren't supported.

h.val(x - 3)
h.val(x + y - 7)
h.val(x + y <= 7)  # returns True/False
...

mathgeekcoder avatar Aug 30 '24 17:08 mathgeekcoder

Closed by #1942

jajhall avatar Sep 26 '24 09:09 jajhall