1

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)
George Karpenkov
  • 2,094
  • 1
  • 16
  • 36
  • Can you check, what happens if you tell opt.add(x <= 14)? Is answer now 1? I mean does it apply the both optizations separately or in addition to each other. – mico Oct 14 '14 at 20:16
  • @mico thanks for the comment! What would it mean though to apply both optimizations in addition? Optimize for "x" and then optimize for "y" without making "x" smaller? – George Karpenkov Oct 15 '14 at 08:48
  • If there were x, y, z it would make sense optimize both, maybe. Still, this was wild guess that comment, I just wanted to point how to get more info what it is making. – mico Oct 15 '14 at 11:50
  • If it returns 0 anyways -> no clue what is going on there and if 1 it optimizes things somehow additively by not resetting situation between, like is my assumption. – mico Oct 15 '14 at 12:07

1 Answers1

0

Thanks for the bug report on the crash. That should not happen.

For the first problem you report. the semantics of adding objectives is additive. That is, you instruct the engine to optimize both x and also y (in the second call). It chooses by default the lexicographic combination of x, y. In the lexicographic ordering the value (15,0) dominates other values.

Nikolaj Bjorner
  • 8,229
  • 14
  • 15