Home > Net >  Python Z3, rule to make 2 numbers be 2 certain numbers in a 2D array
Python Z3, rule to make 2 numbers be 2 certain numbers in a 2D array

Time:11-24

If i have 2 z3 Ints for examaple x1 and x2, and a 2d array of numbers for example: list = [[1,2],[12,13],[45,7]]

i need to right a rule so that x1 and x2 are any of the pairs of numbers in the list for example x1 would be 1 and x2 would be 2 or x1 is 12 and x2 is 13

im guessing it would be something like:
solver = Solver()
for i in range(o,len(list)):
      solver.add(And((x1==list[i][0]),(x2==list[i][1])))

but this would obviously just always be unsat, so i need to right it so that x1 and x2 can be any of the pairs in the list. It's worth noting that the number of pairs in the list could be anything not just 3 pairs.

CodePudding user response:

You're on the right track. Simply iterate and form the disjunction instead. Something like:

from z3 import *

list = [[1,2],[12,13],[45,7]]

s = Solver()
x1, x2 = Ints('x1 x2')
s.add(Or([And(x1 == p[0], x2 == p[1]) for p in list]))

while s.check() == sat:
  m = s.model()
  print("x1 = -, x2 = -" % (m[x1].as_long(), m[x2].as_long()))
  s.add(Or(x1 != m[x1], x2 != m[x2]))

When run, this prints:

x1 =  1, x2 =  2
x1 = 12, x2 = 13
x1 = 45, x2 =  7
  • Related