Could any one can tell me how I can implement minimizing integer problem like the below one by Z3py? How can I define for all statement? Here all variables are int sort.
Is there any dedicated solver within Z3 is available to solve such kind of problem? If there any, then how can I set configuration for that solver?
Thanks
Here are some relevant/similar questions and answers:
Minimum and maximum value of variable
Determine upper/lower bound for variables in an arbitrary propositional formula
How to optimize a piece of code in Z3? (PI_NON_NESTED_ARITH_WEIGHT related)
Does Z3 have support for optimization problems
Related
I have written two Sudoku solvers in Z3, once using 81 variables, and once using a function that maps x and y coordinates to the number in square[x][y].
I guess one could also use an Array instead.
What is the difference between having a python array of Z3 variables, having a Z3 array or having a function in Z3?
When should I use which?
There is no generally applicable answer to this question. There is usually more than one way to model a problem and it's never clear which one will be the best in practice. As a general rule it makes sense to keep it within one theory as that avoids costly cross-theory reasoning; i.e., stick with bit-vectors or (bounded) integers, but don't try to translate integers to bit-vectors (e.g., the int2bv term is essentially treated as uninterpreted by Z3). Also, it's known that there are better solutions for solving problems with arrays, than the one implemented in Z3, so if they are not really necessary it helps to eliminate them.
This is a follow-up question of my earlier question "Is it possible to intrinsically reduce the search space of Function".
I am thinking if it is possible that I can define a sort that contains a set of integers, e.g., integers 1-10.
My intuition is that to reduce search space of Function, instead of defining a Function whose domain sort and range sort are IntSort, I want to define a Function whose domain sort and range sort are a sort that only contains a set of integers of my interest.
Suppose
This sounds like bit-vectors might be a good choice for modelling your problems, at least of your sets of integers are of relatively small and finite size. Z3 can handle bit-vectors combined with quantifiers and uninterpreted functions and it has some custom preprocessors for that logic so that it performs quite well for some problems; it's definitely worth a try. For details see this paper: Efficiently Solving Quantified Bit-Vector Formulas.
As far as I understand, Z3, when encountering quantified linear real/rational arithmetic, applies a form of quantifier elimination described in Bjørner, IJCAR 2010 and more recent work by Bjørner and Monniaux (that's what qe_sat_tactic.cpp says, at least).
I was wondering
Whether it still works if the formula is multilinear, in the sense that the "constants" are symbolic. E.g. ∀x, ax≤b ⇒ ax ≤ 0 can be dealt with by separating the cases a<0, a=0 and a>0. This is possible using Weispfenning's virtual substitution approach, but I don't know what ended up being implemented in Z3 (that is, whether it implements the general approach or the one restricted to constant coefficients).
Whether it is possible, in Z3, to output the result of elimination instead of just solving for one model. There might be a Z3 tactic to do so but I don't know how this is supposed to be requested.
Whether it is possible, in Z3, to perform elimination as described above, then use the new nonlinear solver to obtain a model. Again, a succession of tactics might do the trick, but I don't know how this is supposed to be requested.
Thanks.
After long travels (including a travel where I met David at a conference), here is a short summary to answer the questions as they are posed.
There is no specific support for multi-linear forms.
The 'qe' tactic produces results of elimination, but may as a side-effect decide satisfiablity.
This is a very interesting problem to investigate, but it is not supported out of the box.
Since the question is a little hard to describe, I will use a small example to describe my question.
Suppose there is a propositional formula set whose elements are Boolean variables a,b and c.
When using z3 to get a truth assignment of this formula set, does there exist some way to set the priority of the variables? I mean that if the priority is a>b>c, then during the searching process z3 firstly assumes a is true and if a is impossible to be true it assumes b is true and so on. In another words, if z3 gives a truth assignment: not a,b,c under the aforementioned priority it means a is impossible to be true because a is high-priority compared with b. Hope I describe the question clearly.
There is not easy way to do it in the current release (v4.3.1). The only way I can see is to hack/modify the Z3 source code (http://z3.codeplex.com). We agree that setting priorities is a useful feature for some applications, however there are some problems.
First, Z3 applies several transformations (aka preprocessing steps) before solving a problem. Variables are created and eliminated. Thus, a case-split priority for the original problem may be meaningless for the actual problem (the one generated after applying all transformations) that is solved by Z3.
One dramatic example is a formula containing only Bit-vectors. By default, Z3 will reduce this formula into Propositional logic and invoke a Propositional SAT solver. In this reduction, all Bit-vector variables are eliminated.
Z3 is a collection of solvers and preprocessors. By default, Z3 will select a solver automatically for the user. Some of these solvers use completely different algorithms. So, the provided priority may be useless for the solver being used.
As GManNickG pointed out, it is possible to set the phase selection strategy for a particular solver. See the post provided in his comment for additional details.
Can SMT solver efficiently find a solution (or an assignment) for the pseudo-Boolean problem as described as follows:
\sum {i..m} f_i x1 x2.. xn *w_i
where f_i x1 x2 .. xn is a Boolean function, and w_i is a weight of Int type.
For your convenience, I highlight the contents in page 1 and 3, which is enough for specifying
the pseudo-Boolean problem.
SMT solvers typically address the question: given a logical formula, optionally using functions and predicates from underlying theories (such as the theory of arithmetic, the theory of bit-vectors, arrays), is the formula satisfiable or not.
They typically don't expose a way for you specify objective functions
and typically don't have built-in optimization procedures.
Some special cases are formulas that only use Booleans or a combination of Booleans and either bit-vectors or integers. Pseudo Boolean constraints can be formulated with either integers or encoded (with some care taking overflow semantics into account) using bit-vectors, or they can be encoded directly into SAT. For some formulas using bounded integers that fall in the class of psuedo-boolean problems, Z3 will try automatic reductions into bit-vectors. This applies only to benchmkars in the SMT-LIB2 format tagged as QF_LIA or applies if you explicitly invoke a tactic that performs this reduction (the "qflia" tactic should apply).
While Z3 does not directly expose objective functions, the question of augmenting
SMT solvers with objective functions is actively pursued in the research community.
One approach suggested by Nieuwenhuis and Oliveras in SAT 2006 was to build in
solving for the "weighted max SMT" problem as a custom theory. Yices comes with built-in
features for weighted max SMT, Z3 does not, but it is possible to write a custom
theory that performs the backtracking search of a weighted max SMT solver, but nothing
out of the box.
Sometimes people try to specify objective functions using quantified formulas.
In theory one could hope that quantifier elimination procedures then can solve
for the objective.
This is generally pretty bad when it comes to performance. Quantifier elimination
is an overfit and the routines (that we have) will not be efficient.
For your problem, if you want to find an optimized (maximum or minimum) result from the sum, yes Z3 has this ability. You can use the Optimize class of Z3 library instead of Solver class. The class provides two methods for 'maximization' and 'minimization' respectively. You can pass the SMT variable that is needed to be optimized and Optimization class model will give the solution for you. It actually worked with C# API using Microsoft.Z3 library. For your inconvenience, I am attaching a snippet:
Optimize opt; // initializing object
opt.MkMaximize(*your variable*);
opt.MkMinimize(*your variable*);
opt.Assert(*anything you need to do*);