Writing Constraints for At least one assignment from 1 to N to a set of variables in Sat Solver

StackOverflow https://stackoverflow.com/questions/12586811

  •  03-07-2021
  •  | 
  •  

Question

I am asking this question in context of sat solver. Lets suppose I have 100 integer variables x1, x2, x3 ... x100 which are assigned a value randomly between 1 to N. I want to make sure that at least one variable of x1 to x100 should have each of value from 1 to N.

Now I would like to encode this problem in sat solver constraints. Since while writing the constraints I don't know the value N, it is difficult to me to code as below -

(assert (x1 = 0 or x2 = 0 or ... x100 = 0))
(assert (x1 = 1 or x2 = 1 or ... x100 = 1))
(assert (x1 = 2 or x2 = 2 or ... x100 = 2))
...
(assert (x1 = N or x2 = N or ... x100 = N))

Lets say, that at the end, I assert the value of N to be 2, then the above constraints will not work. Further to that, I would not like to use arrays or un-interpreted functions for performance reasons.

Update :

In Short, the constraints are as follows -

  1. N < 100
  2. (Lets say N = 20), then there are 20 variables which maybe be any of them from x_1 to x_100 which are distinct. So this constraint will ensure that there will be assignment of at least one variable for each of values from 1 to N.
  3. The values of remaining variables (100-N) can overlap each other.

Can anyone give me some suggestions?

Was it helpful?

Solution

How about combining Kyle's answer with distinct, for up to n of the x_i variables (randomly chosen)?

This will give a model like (for N = 50 and 100 x_i variables):

 x = [0 -> 1,
  1 -> 11,
  2 -> 50,
  3 -> 1,
  4 -> 2,
  5 -> 1,
  6 -> 36,
  7 -> 1,
  8 -> 34,
  9 -> 1,
  10 -> 13,
  11 -> 5,
  12 -> 7,
  13 -> 23,
  14 -> 1,
  15 -> 40,
  16 -> 42,
  17 -> 1,
  18 -> 1,
  19 -> 1,
  20 -> 16,
  21 -> 33,
  22 -> 1,
  23 -> 17,
  24 -> 20,
  25 -> 1,
  26 -> 9,
  27 -> 44,
  28 -> 1,
  29 -> 49,
  30 -> 26,
  31 -> 1,
  32 -> 29,
  33 -> 46,
  34 -> 8,
  35 -> 1,
  36 -> 27,
  37 -> 1,
  38 -> 1,
  39 -> 32,
  40 -> 1,
  41 -> 31,
  42 -> 1,
  43 -> 1,
  44 -> 14,
  45 -> 1,
  46 -> 1,
  47 -> 1,
  48 -> 1,
  49 -> 1,
  50 -> 35,
  51 -> 19,
  52 -> 43,
  53 -> 22,
  54 -> 1,
  55 -> 1,
  56 -> 1,
  57 -> 1,
  58 -> 21,
  59 -> 1,
  60 -> 1,
  61 -> 39,
  62 -> 28,
  63 -> 12,
  64 -> 1,
  65 -> 1,
  66 -> 1,
  67 -> 1,
  68 -> 1,
  69 -> 41,
  70 -> 1,
  71 -> 25,
  72 -> 1,
  73 -> 6,
  74 -> 1,
  75 -> 1,
  76 -> 1,
  77 -> 1,
  78 -> 1,
  79 -> 24,
  80 -> 1,
  81 -> 30,
  82 -> 38,
  83 -> 3,
  84 -> 4,
  85 -> 1,
  86 -> 1,
  87 -> 1,
  88 -> 1,
  89 -> 1,
  90 -> 18,
  91 -> 1,
  92 -> 47,
  93 -> 37,
  94 -> 1,
  95 -> 45,
  96 -> 1,
  97 -> 15,
  98 -> 48,
  99 -> 10,
  else -> 1],

Here's a Z3Py script accomplishing this, assuming the first N indices can be constrained, instead of random ones (and using a function for x instead of constants so it was faster to write): http://rise4fun.com/Z3Py/M3TG

Next is code for doing this for a random set of indices, but you can't run this on Z3Py@Rise, because it does not allow using imports, so you'll have to run it locally.

from random import *
from z3 import *

x = Function('x', IntSort(), IntSort())

M = 100
N = 50

s = Solver()
idxs = sample(xrange(M),N) # get N random ids from sequence {1,...M}
print idxs

distinctlist = []
for i in range(M):
  s.add(And(x(i) >= 1, x(i) <= N))
  if i in idxs:
    distinctlist.append(x(i))

print distinctlist

s.add(Distinct(distinctlist))

print "checking..."

r = s.check()
print r
if r == sat:
  print s.model()

(Beware if you make this query unsat, it may timeout.)

OTHER TIPS

Use the distinct predicate. See: http://smtlib.cs.uiowa.edu/theories/Core.smt2

I'd write

(assert (or (and (> x1 0) (<= x1 n))
            (and (> x2 0) (<= x2 n))
            ...same for x3 thru x99...
            (and (> x100 0) (<= x100 n))))

which will work no matter what value of n is asserted later so long as it is greater than or equal to 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top