I've just start learning z3 solver. I've seen some puzzles solved by z3 i.e sudoku and Eight Queens. I'm just wondering if any one solved River crossing problem in z3. z3 seems very good in problem solving.
Regards
I tried to model it and I came up with the following. You can run it in the interactive rise4fun environment and see the answer:
(declare-datatypes () ((Object Human Fox Rabbit Cabbage)))
(declare-datatypes () ((Location Left Right OnBoat)))
(declare-const n Int)
(declare-fun objectLocation (Int Object) Location)
(assert (= n 14))
(assert (forall ((x Object) (t Int)) (=> (= t 0) (= (objectLocation t x) Left))))
(assert (forall ((x Object) (t Int)) (=> (= t n) (= (objectLocation t x) Right))))
(assert
(forall
((t Int))
(=>
(and (>= t 0) (<= t n))
(<=
(+
(ite (= (objectLocation t Human) OnBoat) 1 0)
(ite (= (objectLocation t Fox) OnBoat) 1 0)
(ite (= (objectLocation t Rabbit) OnBoat) 1 0)
(ite (= (objectLocation t Cabbage) OnBoat) 1 0)
)
2
)
)
)
)
(assert
(forall
((x Object) (t Int))
(=>
(and (>= t 1) (<= t (- n 1)))
(iff
(= (objectLocation t x) OnBoat)
(and
(not (= (objectLocation (+ t 1) x) OnBoat))
(not (= (objectLocation (- t 1) x) OnBoat))
(not (= (objectLocation (+ t 1) x) (objectLocation (- t 1) x)))
)
)
)
)
)
(assert
(forall
((x Object) (t Int))
(=>
(and (>= t 1) (<= t n))
(=>
(not (= (objectLocation t x) (objectLocation (- t 1) x)))
(or
(= (objectLocation t x) OnBoat)
(= (objectLocation (- t 1) x) OnBoat)
)
)
)
)
)
(assert
(forall
((x Object) (y Object) (t Int))
(=>
(and (>= t 1) (<= t n))
(=>
(and (= (objectLocation t x) OnBoat) (= (objectLocation t y) OnBoat))
(= (objectLocation (- t 1) x) (objectLocation (- t 1) y))
)
)
)
)
(assert
(forall
((x Object) (t Int))
(=>
(and (>= t 0) (<= t n))
(=>
(= (objectLocation t x) OnBoat)
(= (objectLocation t Human) OnBoat)
)
)
)
)
(assert
(forall
((t Int))
(=>
(and (>= t 0) (<= t n))
(=>
(= (objectLocation t Fox) (objectLocation t Rabbit))
(= (objectLocation t Human) (objectLocation t Rabbit))
)
)
)
)
(assert
(forall
((t Int))
(=>
(and (>= t 0) (<= t n))
(=>
(= (objectLocation t Cabbage) (objectLocation t Rabbit))
(= (objectLocation t Human) (objectLocation t Rabbit))
)
)
)
)
(check-sat)
(echo "step 0")
(eval (objectLocation 0 Human))
(eval (objectLocation 0 Fox))
(eval (objectLocation 0 Rabbit))
(eval (objectLocation 0 Cabbage))
(echo "step 1")
(eval (objectLocation 1 Human))
(eval (objectLocation 1 Fox))
(eval (objectLocation 1 Rabbit))
(eval (objectLocation 1 Cabbage))
(echo "step 2")
(eval (objectLocation 2 Human))
(eval (objectLocation 2 Fox))
(eval (objectLocation 2 Rabbit))
(eval (objectLocation 2 Cabbage))
(echo "step 3")
(eval (objectLocation 3 Human))
(eval (objectLocation 3 Fox))
(eval (objectLocation 3 Rabbit))
(eval (objectLocation 3 Cabbage))
(echo "step 4")
(eval (objectLocation 4 Human))
(eval (objectLocation 4 Fox))
(eval (objectLocation 4 Rabbit))
(eval (objectLocation 4 Cabbage))
(echo "step 5")
(eval (objectLocation 5 Human))
(eval (objectLocation 5 Fox))
(eval (objectLocation 5 Rabbit))
(eval (objectLocation 5 Cabbage))
(echo "step 6")
(eval (objectLocation 6 Human))
(eval (objectLocation 6 Fox))
(eval (objectLocation 6 Rabbit))
(eval (objectLocation 6 Cabbage))
(echo "step 7")
(eval (objectLocation 7 Human))
(eval (objectLocation 7 Fox))
(eval (objectLocation 7 Rabbit))
(eval (objectLocation 7 Cabbage))
(echo "step 8")
(eval (objectLocation 8 Human))
(eval (objectLocation 8 Fox))
(eval (objectLocation 8 Rabbit))
(eval (objectLocation 8 Cabbage))
(echo "step 9")
(eval (objectLocation 9 Human))
(eval (objectLocation 9 Fox))
(eval (objectLocation 9 Rabbit))
(eval (objectLocation 9 Cabbage))
(echo "step 10")
(eval (objectLocation 10 Human))
(eval (objectLocation 10 Fox))
(eval (objectLocation 10 Rabbit))
(eval (objectLocation 10 Cabbage))
(echo "step 11")
(eval (objectLocation 11 Human))
(eval (objectLocation 11 Fox))
(eval (objectLocation 11 Rabbit))
(eval (objectLocation 11 Cabbage))
(echo "step 12")
(eval (objectLocation 12 Human))
(eval (objectLocation 12 Fox))
(eval (objectLocation 12 Rabbit))
(eval (objectLocation 12 Cabbage))
(echo "step 13")
(eval (objectLocation 13 Human))
(eval (objectLocation 13 Fox))
(eval (objectLocation 13 Rabbit))
(eval (objectLocation 13 Cabbage))
(echo "step 14")
(eval (objectLocation 14 Human))
(eval (objectLocation 14 Fox))
(eval (objectLocation 14 Rabbit))
(eval (objectLocation 14 Cabbage))
You can also run "(get-model)" to see a definition of the function "objectLocation" but I warn you that it will be very unintelligible.
Here is a short description of what is asserted:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With