Question

I have the following example code for the knapsack problem in a file called "knapsack.smt2", which I believe is in smt2 format and I have the latest version of Z3:

(declare-const s1 Int)
(declare-const o1 Int)
(declare-const b1 Bool)

(declare-const s2 Int)
(declare-const o2 Int)
(declare-const b2 Bool)

(declare-const s3 Int)
(declare-const o3 Int)
(declare-const b3 Bool)


(declare-const sack-size Int)
(declare-const filled Int)

(assert (< o1 sack-size))
(assert (< o2 sack-size))
(assert (< o3 sack-size))

(assert (>= o1 0))
(assert (>= o2 0))
(assert (>= o3 0))

(assert (=> (not b1)(= o1 o2)))
(assert (=> (not b2)(= o2 o3)))

(assert (=> b1 (= (+ o1 s1) o2)))
(assert (=> b2 (= (+ o2 s2) o3)))
(assert (=> b3 (= (+ o3 s3) filled)))

(assert (=> (not b3) (= o3 filled)))
(assert (<= filled sack-size))

(assert ( = o1 0))


(assert ( = s1 3))
(assert ( = s2 4))
(assert ( = s3 5))
(assert ( = sack-size 20))

(assert ( = filled 13))

(check-sat)
(get-model)

However, when I run "z3 -m knapsack.smt2" I get the following error message:

>> z3 -m knapsack.smt2
unsat
(error "line 46 column 10: model is not available")

where line 46 is the last line "(get-model)".

Can anyone tell me why this isn't working?

Thanks.

Was it helpful?

Solution

Z3 produces models for satisfiable instances. Your formula is not satisfiable. Using the constraints

(assert ( = o1 0))
(assert ( = s1 3))
(assert ( = s2 4))
(assert ( = s3 5))
(assert ( = filled 13))

we have that o1 = 0 and s1 = 3, s2 = 3, s3 = 5, and filled = 13, Using the constraints

(assert (=> (not b1)(= o1 o2)))
(assert (=> b1 (= (+ o1 s1) o2)))

We have that o2 = 0 or o2 = 3.

Using

(assert (=> (not b2)(= o2 o3)))
(assert (=> b2 (= (+ o2 s2) o3)))

We have that o3 = o2 or o3 = o2 + 3 Using

(assert (=> b3 (= (+ o3 s3) filled)))
(assert (=> (not b3) (= o3 filled)))

We have that o3 = 8 or o3 = 13

Now, we can see the conflict

o2 = 0 or o2 = 3
o3 = 8 or o3 = 13
o3 = o2 or o3 = o2 + 3 

If we try o2 = 0 we get the unsatisfiable formulas

o3 = 8 or o3 = 13
o3 = 0 or o3 = 3 

If we try o2 = 3 we get the unsatisfiable formulas

o3 = 8 or o3 = 13
o3 = 3 or o3 = 6 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top