0
votes

We can use the following code to solve the Dog,Cat,Mouse puzzle in the tutorial.

dog, cat, mouse = Ints('dog cat mouse')

s = Solver();
s.add(dog>=1)
s.add(cat>=1)
s.add(mouse>=1)
s.add(dog+cat+mouse==100)
s.add(1500 * dog + 100 * cat + 25 * mouse == 10000)

print s.check()
print s.model()

Well, I know I can use

m=s.model
for d in m.decls():
    print  "%s = %s" % (d.name(), m[d])

to get the names and values of the variables. For example, cat = 41. I wonder can I create a new assertion from the names and values such as cat != 41. I used

s.add(d.name != m[d]) 
s.add("%s != %s" % (d.name(), m[d]))

But, none of them can work. Any one knows how to add a new assertion from reading the names and values of the model? Many thanks.

1

1 Answers

0
votes

In for d in m.decls():, d is a func_decl, i.e., only a declaration, not a variable (constant function) yet, so we need to apply it to it's arguments, which here are empty. Thus we can do:

m = s.model()
for d in m.decls():
    v = d() # <-- Note parenthesis ()
    print("%s != %s" % (v, m[d]))
    s.add(v != m[d])

print(s)
print(s.check())

to get

...
cat != 41
mouse != 56
dog != 3
[dog >= 1,
 cat >= 1,
 mouse >= 1,
 dog + cat + mouse == 100,
 1500*dog + 100*cat + 25*mouse == 10000,
 41 != cat,
 56 != mouse,
 3 != dog]
unsat

to get