Question

In the following working example , I'm trying to retrieve the matched models, in this case there are two satisfying models:

 t1= cl7          t2= cl4      t3= cl5

and

 t1= cl4          t2= cl3      t3= cl9 

The problem is repeating the matched models until time-out of the running solver. How can I retrieve the satisfied models without repetition.

Many thanks,

        S, (cl1, cl2, cl3, cl4, cl5, cl6, cl7,cl8,cl9) = EnumSort('S', ['cl1', 'cl2', 'cl3', 'cl4', 'cl5','cl6' ,'cl7', 'cl8', 'cl9'])



       s = Solver()

       x = Const('x', S)
      def fun1(x):
        return Or(x == cl1, x == cl2, x == cl3, x == cl4, x == cl5, x == cl6, x == cl7, x == cl8, x == cl9)


       y1, y2 = Consts('y1 y2', S)
      def fun2(y1, y2):
          return Or(And(y1 == cl7, y2 == cl4), And(y1 == cl4, y2 == cl3), And(y1 == cl3, y2 == cl2),And(y1 == cl8, y2 == cl9))


      q1,q2 = Consts('q1 q2', S)
      def fun3(q1,q2):
        return Or(And(q1 == cl7, q2 == cl5), And(q1 == cl4, q2 == cl9))



 t1, t2 ,t3 = Consts('t1 t2 t3', S)

 s.add(fun1(t1))
 s.add(fun2(t1,t2))
 s.add(fun3(t1,t3))


while s.check() == sat:
s.add(Or(t1 != s.model()[t1], t2 != s.model()[t2],t3 != s.model()[t2]))
print s.model()
Was it helpful?

Solution

You appear to have a small typo in the part that prevents the same models from being used again in new satisfiability checks, particularly the second to last line should have t3 != s.model()[t3] instead of t3 != s.model()[t2].

Here's the updated example that generates the two models you put at the beginning (z3py link: http://rise4fun.com/Z3Py/AxJv ):

S, (cl1, cl2, cl3, cl4, cl5, cl6, cl7,cl8,cl9) = EnumSort('S', ['cl1', 'cl2', 'cl3', 'cl4', 'cl5','cl6' ,'cl7', 'cl8', 'cl9'])

s = Solver()

x = Const('x', S)
def fun1(x):
  return Or(x == cl1, x == cl2, x == cl3, x == cl4, x == cl5, x == cl6, x == cl7, x == cl8, x == cl9)

y1, y2 = Consts('y1 y2', S)
def fun2(y1, y2):
  return Or(And(y1 == cl7, y2 == cl4), And(y1 == cl4, y2 == cl3), And(y1 == cl3, y2 == cl2),And(y1 == cl8, y2 == cl9))

q1,q2 = Consts('q1 q2', S)
def fun3(q1,q2):
  return Or(And(q1 == cl7, q2 == cl5), And(q1 == cl4, q2 == cl9))

t1, t2 ,t3 = Consts('t1 t2 t3', S)

s.add(fun1(t1))
s.add(fun2(t1,t2))
s.add(fun3(t1,t3))

while s.check() == sat:
  s.add(Or(t1 != s.model()[t1], t2 != s.model()[t2], t3 != s.model()[t3]))
  print s.model()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top