I’m using the available Python Z3 API and using pretty standard code to print out the SAT model(s).
I’ll leave out the extraneous bits (Full Example Here), regardless here is the big picture:
for bits in range(HIGH, LOW, -1):
var1, var2 ... varn = BitVecs('var1 var2 ... varn', bits)
s = Solver();
s.add(...)
...
if(s.check() == sat):
print "Sat, %d," %(bits),
m = s.model()
for d in m.decls():
print "%s," % (d.name()),
print " "
print "ASSIGN, %d," %(bits),
for d in m.decls():
print "%s," % (m[d]),
else:
print "NotSat, %d," %(bits),
print " "
break
print " "
To which I get results that looks like this (95% of the time):
Sat, HIGH, VAR1, VAR2, VAR3 ... VARn
ASSIGN, HIGH, VAL1, VAL2, VAL3 ... VALn
Sat, HIGH-1, VAR1, VAR2, VAR3 ... VARn
ASSIGN, HIGH-1, VAL1, VAL2, VAL3 ... VALn
...
NotSat, SOMEVAL
But pseudo-randomly this happens (it only happens for specific problems, but reoccurs exactly at the same point each time):
Sat, HIGH, VAR1, VAR2, VAR3 ... VARn
ASSIGN, HIGH, VAL1, VAL2, VAL3 ... VALn
Sat, HIGH-1, VAR1, VAR2
At which point, python continues to run endlessly.
Any thoughts or suggestions would be appreciated.
There is no problem with the script you put in the link above. It is just hard to solve the instance for
bits=6.It is probably unsatisfiable, but Z3 will take a long time to prove it.
I attached the output I got in the end of the post.
If the output is truncated in your system, you may try to use
in the end of each iteration. It will force Python to flush the results to standard output.
You may also use a timeout to interrupt Z3 when it can’t solve the problem in less than x milliseconds. For example, for setting a timeout of 60 seconds, you should include the following command after
s = Solver()Z3 will return
unknowninstead ofunsatwhen the timeout is reached. You may decide to interpret the timeout as “probably”unsat.You may also force Z3 to display verbose messages. You can use them to check whether is dead or not. To enable verbose messages, we should include
Z3 will display many messages such as:
Here is the output produced by your script on my machine: