3

An Example:

from z3 import *
p1,p2, p3 = Reals('p1 p2 p3')
s = Optimize()
s.add(And(Or(p1>=0, p2>0),Or(p1<=10,p3>0)))
print(s.check())
print(s.model())

run the code,and get output:

sat
[p3 = 1, p1 = 11, p2 = 1]

it's right. However, it's valuable to get expected result(satisfy the constraintAnd(Or(p1>=0, p2>0),Or(p1<=10,p3>0)), with setting less variables.

for example, only set p1=0 (or any value in range([0,10])), then the constraint is satisfied. Only one variable is neccessary to set.

so my question is, is there a common method to get the least number of neccessary variables?

Lucent
  • 33
  • 7
  • 1
    Does this answer your question? [Do you know how to set "weak" initial values to each of z3 Array element?](https://stackoverflow.com/questions/60666626/do-you-know-how-to-set-weak-initial-values-to-each-of-z3-array-element) – Malte Schwerhoff Jan 02 '21 at 20:33
  • thanks. helpful.I knew there is "add_soft" function in Z3 Optimize object. but in reality the constraint logic is composed of hundreds of variables and they are coupled in different clauses, for example: (p1>100) || ((p1<50) && p2==1).... surely i can add soft-clauses for each condition. but after the solver check, I can not get to know which varialbes are necessary and which are redundent – Lucent Jan 03 '21 at 05:38
  • Yes. maybe i can add soft-causes and check the solved model to get which variables are redundent. But when it comes to coupled variables, for example: `p1,p2 = Reals('p1 p2') s = Optimize() s.add(And((p1>=100),Or(p1<=150,p2<0)))` , how to set the soft-clauses? – Lucent Jan 03 '21 at 08:14
  • I have tried to edit the question. hope i make the point clear. Thanks. – Lucent Jan 03 '21 at 15:36
  • Oh, I see variables are no longer `booleans` but have become `Reals`. That's an entirely different question from the original one. AFAIK, that's not really possible with vanilla SMT/OMT solvers like Z3. – Patrick Trentin Jan 04 '21 at 12:29
  • sad for hearing that. it's common in reality for `Reals` type, so i re-edit the question like that. But **is the question reasonable**? Is there something we can do based on Z3 to find a way? is soft constraint possible? – Lucent Jan 04 '21 at 16:38

1 Answers1

3

What you are looking for are called "symbolic" models, i.e., where some variables are set to constants and others can be set to expressions. For instance, if you have a constraint like x > y, then a symbolic model might be y = 0, x > y. Unfortunately, SMT solvers do not provide "symbolic" models of this sort. The usual DPLL style constraint propagation do not allow for this sort of easy symbolic model construction.

One of the difficulties in this area is the fact that there is no "canonical" notion of what a minimal such model might be. (This question is undecidable in general.)

If you are interested in such models, your best bet is to use higher-level tools like mathematica, or theorem provers in general, to come up with such models. (Of course, these will be semi-automated at best.) BDD based solvers can also provide symbolic models as well, though most modern solvers use SAT engines and/or do not expose enough API to look into models generated by their BDD engines.

alias
  • 28,120
  • 2
  • 23
  • 40
  • Thanks alias. The question is just from symbolic execution. For some specific application, i have got a solution by programming. Now, i want to find a general way to deal with more common constraint, by giving the constraint to Z3 and find an answer. So here is the question I am working. Thanks for all discussions from @Patrick Trentin and Malte Schwerhoff. So far I got to knew the condition of this question, and I should work it out by some insight. Seems soft-constraint should be try first. – Lucent Jan 05 '21 at 12:47