Another question on the maximization API in Z3. I get wrong answers if I switch maximization objectives midway through:
from z3 import Real, Optimize
x = Real('x')
y = Real('y')
opt = Optimize()
opt.add(x >= 0)
opt.add(y >= 0)
opt.add(x + y <= 15)
print "Optimizing", x
h = opt.maximize(x)
print opt.check()
print opt.upper(h)
print opt.model()
print "Optimizing", y
h = opt.maximize(y)
print opt.check()
print opt.upper(h)
print opt.model()
The latter call to opt.model()
returns y = 0
, whereas clearly the answer should be 15
.
Is it a bug or simply unsupported feature? (and should I manually re-add the constraints each time I want to switch the objective?)
Moreover, there is a separate bug which comes out when I remove the non-negativity constraint, but that's a separate issue (bad handling for unbounded objectives, I presume?)
from z3 import Real, Optimize
x = Real('x')
y = Real('y')
opt = Optimize()
opt.add(x + y <= 15)
print "Optimizing", x
h = opt.maximize(x)
print opt.check()
print opt.upper(h)
print opt.model()
Dies with
Optimizing x
terminate called after throwing an instance of 'std::bad_typeid'
what(): std::bad_typeid
fish: Job 1, 'python opt.py' terminated by signal SIGABRT (Abort)