Converting to "set-option" SMTLib format - z3

I want to convert this z3py code (online code) to standard SMTLib format. Everything is converted to SMTLib format except the set options properties "(set-option :produce-models true) (set-option :timeout 4000)". Why isn't working? The conversion code was suggested by Leonardo de Moura.
I want output to be like -
; benchmark
(set-info :status unknown)
(set-option :produce-models true)
(set-option :timeout 4000)
(set-logic QF_UFLIA)
(assert true)
(check-sat)
Thanks

Options are not printed by the SMT-LIB2 pretty printer.
The pretty printer returns a string and you
should be able to pre-pend options of your choice.

I am running your code and I am obtaining
; benchmark
(set-info :status unknown)
(set-logic QF_UFLIA)
(assert true)
(check-sat)

Related

Z3Py: randomized results (phase-selection) not random?

I tried to use bit vectors to get randomized results in model values like suggested by de Moura here but then with Z3Py instead of SMTLIB. I translated his example to:
from z3 import *
s = Solver()
x = BitVec('x', 16)
set_option('auto_config', False)
set_option('smt.phase_selection',5)
s.add(ULT(x,100))
s.check()
s.model()
s.check()
s.model()
However, the result seems to always be the same, i.e.
- repetitive checking with s.check() does not alter the result.
- even after a restart of the python interactive shell the result of the execution will be the same
Adding a change of the random seed did not alter the results: set_option('smt.random_seed', 123)
Does anyone have an idea why is not working as desired?
Thanks in advance!
Carsten
This case is simply too simple. It's essentially solved by the preprocessor and never gets to a point where it needs to select a phase, so random phase selection has no effect. Leo's answer to the cited post is now a little bit out of date and Z3 has changed, so it doesn't immediately replicate using the latest unstable version because Z3 chooses to use a different solver. We can still get the random behavior if we force it to use the incremental solver by adding a (push) command though; here's an updated example that is seed dependent:
(set-option :auto_config false)
(set-option :smt.phase_selection 5)
(set-option :smt.random_seed 456)
(declare-const x (_ BitVec 16))
(assert (bvult x (_ bv100 16)))
(push)
(check-sat)
(get-model)
;; try again to get a different model
(check-sat)
(get-model)
;; try again to get a different model
(check-sat)
(get-model)

Z3 Polarity using Z3 as SAT Solver

I am trying to solve a SAT problem with 12000+ boolean variables using Z3.
I expect that most of the variables will evaluate to false in the solution.
Is there a way to guide or hint Z3 as SAT solver to try "polarity false" first?
I've tried it with cryptominisat 2 and got good results.
Z3 is a collection of solvers and preprocessors. We can provide hints for some of the solvers.
When the command (check-sat) is used, Z3 will select the solver automatically for us.
We should (check-sat-using <strategy>) if we want to select the solver ourselves.
For example, the following command will instruct Z3 to use a Boolean SAT solver.
(check-sat-using sat)
We can force it to always try "polarity false" first by using:
(check-sat-using (with sat :phase always-false))
We can also control the preprocessing steps. If we want to put the formula in CNF before invoking sat, we should use:
(check-sat-using (then tseitin-cnf (with sat :phase always-false)))
EDIT: if you are using the DIMACS input format and Z3 v4.3.1, then you can't set parameters for all available solvers using the command line. The next release will address this limitation. In the meantime, you can download the work-in-progress branch using:
git clone https://git01.codeplex.com/z3 -b unstable
and compile Z3. Then, to force polarity false, we use the command line option
sat.phase=always_false
The command z3 -pm:sat will display all available options for this module.
END EDIT
Here is a complete example in SMT 2.0 (also available online):
(declare-const p Bool)
(declare-const q Bool)
(declare-const r Bool)
(declare-const s Bool)
(assert (or (not p) (not q) (not r)))
(assert (or r s))
(assert (or r (not s)))
(assert (or r (and p q)))
(echo "With always false")
(check-sat-using (then tseitin-cnf (with sat :phase always-false)))
(get-model)
(echo "With always true")
(check-sat-using (then tseitin-cnf (with sat :phase always-true)))
(get-model)

z3 with SMTlib2 input

I just upgraded z3 from 4.1 to 4.3.1, and it seems that the smtlib2 input has changed :
now, a function/constant declaration is not deleted by a pop statement.
Here is the SMTlib2 input that works fine with z3 4.1 (and other SMT solvers...), but return with an error with z3 4.3.1 : (error "line 19 column 25: invalid declaration, constant 'bs_2' (whith the given signature) already declared")
(set-option :produce-models true)
(set-option :produce-unsat-cores true)
(set-option :interactive-mode true)
(set-option :print-success false)
(push 1)
(declare-fun bs_1 () Bool)
(push 1)
(declare-fun bs_2 () Bool)
(assert (and bs_1 (not bs_2)))
(check-sat)
(pop 1)
(push 1)
(declare-fun bs_2 () Bool)
(assert (and bs_1 (not bs_2)))
(check-sat)
(pop 1)
(pop 1)
(exit)
Removing the last bs_2 declaration works fine with z3 4.3.1, but not for z3 4.1.
Am I using the push/pop wrong ?
In Z3 4.3.1, we tried to relax some of the SMT-LIB 2.0 restrictions to make Z3 more convenient to use. For example, we can now write (+ x 2) instead of (+ x 2.0) when x is Real. Declarations are global instead of scoped like in Z3 4.1. The motivation was to allow users to encode problems more succinctly. We can use the following option to enable scoped declarations like in Z3 4.1
(set-option :global-decls false)
That being said, I understand this change is very confusing and counter-intuitive for users that are used to other SMT solvers, or read a manual describing the SMT-LIB standard. Thus, we will change the default back to (set-option :global-decls false).

Rewriting of Formulas

In this paper (Section 3.2), it says that z3 applies rewriting/simplification of formulas before it does any other steps.
Suppose I have a formula in QF_UF, that consists of multiple assert statements. Is there any rewriting rule that would somehow "break the barrier" between different assert statements? Or, asking the other way round: Can I be sure that rewrite rules are only applied locally, "within" one assert statement?
For example, consider the following formula:
(set-logic QF_UF)
(set-option :auto-config false)
(set-option :PROOF_MODE 2)
(declare-fun a () Bool)
(assert a)
(assert (not a))
(check-sat)
(get-proof)
Can I be sure that the proof will contain a resolution step to prove False, or is it possible that False will be concluded by a rewrite/simplification step?
The reason I am asking is that for my application, every assert statement has a special semantics. Rewriting/Simplification across several assert statements would render the resulting proof of unsatisfiability unusable (or at least: very hard to use) for me.
Z3 3.2 applies several preprocessing steps. Using (set-option :auto-config false) will disable most of them. You should also include the following two options:
(set-option :propagate-booleans false)
(set-option :propagate-values false)

Is there an option to pretty-print bit vectors as signed decimals?

How can you pretty-print bit vectors as signed decimals in Z3?
You can use the command (set-option :pp-bv-literals false) to force Z3 to display the bit-vector literals in a decimal based format. Actually, it will display them using the SMT 2.0 format: (_ bv<decimal> <size>). Consider the following example:
(simplify #x00f8)
(set-option :pp-bv-literals false)
(simplify #x00f8)
Z3 will print
#x00f8
(_ bv248 16)
Z3 has no support for signed decimals. We can add an option to display a bit-vector n as (bvneg (_ bv<decimal> <size>) if the most significant bit of n is 1. Is this enough for your purposes?

Resources