I have to get the value (in a range of them) that gives the maximum of a function, using z3py.
For example: I have a function (foo(x,y) = 2*x + 1 + y
), and I want to find the value of [x,y]
(using the constraint lo <= foo(x,y) <= hi
) such that foo(x,y)
is maximum.
In other words I have to solve a maximum problem within a range, is that possible?
I have wrote this simple script in python:
s = Solver()
# hi is given by the user
# expr is foo(x)
s.add(expr <= hi)
s.add(expr >= lo)
s.add(X >= 0)
s.add(Y >= 0)
if s.check() == sat:
print('_upper_bound: got model %s' % s.model())
If I use only a variable (e.g., foo(x) = 2*x + 1
), the given model is correct and the value of foo(x)
is the highest. If I use more than one variable (e.g., foo(x,y) = 2*x+y
) the found result is the minimum.
All the points in the range are in the domain of the function.
EDIT: Apparently, if I remove the two constraints about X and Y, I get the maximum value.