;;;====================================================== ;;; Who Drinks Coors? And Who owns the Zebra? ;;; ;;; Another puzzle problem in which there are five ;;; houses, each of a different color, inhabited by ;;; men of different nationalities, with different ;;; pets, drinks, and spouses. Given the initial ;;; set of conditions, it must be determined which ;;; attributes are assigned to each man. ;;; ;;; CLIPS Version 6.0 Example ;;; ;;; To execute, merely load, reset and run. ;;;====================================================== (deftemplate avh (field a) (field v) (field h)) (defrule find-solution ; The Englishman lives in the red house. (avh (a nationality) (v englishman) (h ?n1)) (avh (a color) (v red) (h ?c1&?n1)) ; The Spaniard owns the dog. (avh (a nationality) (v spaniard) (h ?n2&~?n1)) (avh (a pet) (v dog) (h ?p1&?n2)) ; The ivory house is immediately to the left of the green house, ; where the Bud drinker lives. (avh (a color) (v ivory) (h ?c2&~?c1)) (avh (a color) (v green) (h ?c3&~?c2&~?c1&=(+ ?c2 1))) (avh (a drink) (v Bud) (h ?d1&?c3)) ; The Evian drinker lives in the middle house. (avh (a drink) (v Evian) (h ?d2&~?d1&3)) ; The man who is married to Alyson also keeps cats. (avh (a married) (v Alyson) (h ?m1)) (avh (a pet) (v cats) (h ?p2&~?p1&?m1)) ; The Ukrainian drinks icedTea. (avh (a nationality) (v ukrainian) (h ?n3&~?n2&~?n1)) (avh (a drink) (v icedTea) (h ?d3&~?d2&~?d1&?n3)) ; The Norwegian resides in the first house on the left. (avh (a nationality) (v norwegian) (h ?n4&~?n3&~?n2&~?n1&1)) ; man married to Beth lives next door to the fish owner. (avh (a married) (v Beth) (h ?m2&~?m1)) (avh (a pet) (v fish) (h ?p3&~?p2&~?p1&:(or (= ?m2 (- ?p3 1)) (= ?m2 (+ ?p3 1))))) ; The man married to Celia drinks Heineken. (avh (a married) (v Celia) (h ?m3&~?m2&~?m1)) (avh (a drink) (v Heineken) (h ?d4&~?d3&~?d2&~?d1&?m3)) ; The Japanese is married to Diane (avh (a nationality) (v japanese) (h ?n5&~?n4&~?n3&~?n2&~?n1)) (avh (a married) (v Diane) (h ?m4&~?m3&~?m2&~?m1&?n5)) ; The hamster owner lives next to the man married to Elise, ; whose house is yellow. (avh (a pet) (v hamster) (h ?p4&~?p3&~?p2&~?p1)) (avh (a married) (v Elise) (h ?m5&~?m4&~?m3&~?m2&~?m1&:(or (= ?p4 (- ?m5 1)) (= ?p4 (+ ?m5 1))))) (avh (a color) (v yellow) (h ?c4&~?c3&~?c2&~?c1&?m5)) ; The Norwegian lives next to the blue house. (avh (a color) (v blue) (h ?c5&~?c4&~?c3&~?c2&~?c1&:(or (= ?c5 (- ?n4 1)) (= ?c5 (+ ?n4 1))))) ; Who drinks Coors? And Who owns the zebra? (avh (a drink) (v Coors) (h ?d5&~?d4&~?d3&~?d2&~?d1)) (avh (a pet) (v zebra) (h ?p5&~?p4&~?p3&~?p2&~?p1)) => (assert (solution nationality englishman ?n1) (solution color red ?c1) (solution nationality spaniard ?n2) (solution pet dog ?p1) (solution color ivory ?c2) (solution color green ?c3) (solution drink Bud ?d1) (solution drink Evian ?d2) (solution married Alyson ?m1) (solution pet cats ?p2) (solution nationality ukrainian ?n3) (solution drink icedTea ?d3) (solution nationality norwegian ?n4) (solution married Beth ?m2) (solution pet fish ?p3) (solution married Celia ?m3) (solution drink Heineken ?d4) (solution nationality japanese ?n5) (solution married Diane ?m4) (solution pet hamster ?p4) (solution married Elise ?m5) (solution color yellow ?c4) (solution color blue ?c5) (solution drink Coors ?d5) (solution pet zebra ?p5)) ) (defrule print-solution ?f1 <- (solution nationality ?n1 1) ?f2 <- (solution color ?c1 1) ?f3 <- (solution pet ?p1 1) ?f4 <- (solution drink ?d1 1) ?f5 <- (solution married ?m1 1) ?f6 <- (solution nationality ?n2 2) ?f7 <- (solution color ?c2 2) ?f8 <- (solution pet ?p2 2) ?f9 <- (solution drink ?d2 2) ?f10 <- (solution married ?m2 2) ?f11 <- (solution nationality ?n3 3) ?f12 <- (solution color ?c3 3) ?f13 <- (solution pet ?p3 3) ?f14 <- (solution drink ?d3 3) ?f15 <- (solution married ?m3 3) ?f16 <- (solution nationality ?n4 4) ?f17 <- (solution color ?c4 4) ?f18 <- (solution pet ?p4 4) ?f19 <- (solution drink ?d4 4) ?f20 <- (solution married ?m4 4) ?f21 <- (solution nationality ?n5 5) ?f22 <- (solution color ?c5 5) ?f23 <- (solution pet ?p5 5) ?f24 <- (solution drink ?d5 5) ?f25 <- (solution married ?m5 5) => (retract ?f1 ?f2 ?f3 ?f4 ?f5 ?f6 ?f7 ?f8 ?f9 ?f10 ?f11 ?f12 ?f13 ?f14 ?f15 ?f16 ?f17 ?f18 ?f19 ?f20 ?f21 ?f22 ?f23 ?f24 ?f25) (format t "HOUSE | %-11s | %-6s | %-8s | %-12s | %-13s%n" Nationality Color Pet Drink Spouse) (format t "----------------------------------------------------------------------%n") (format t " 1 | %-11s | %-6s | %-8s | %-12s | %-13s%n" ?n1 ?c1 ?p1 ?d1 ?m1) (format t " 2 | %-11s | %-6s | %-8s | %-12s | %-13s%n" ?n2 ?c2 ?p2 ?d2 ?m2) (format t " 3 | %-11s | %-6s | %-8s | %-12s | %-13s%n" ?n3 ?c3 ?p3 ?d3 ?m3) (format t " 4 | %-11s | %-6s | %-8s | %-12s | %-13s%n" ?n4 ?c4 ?p4 ?d4 ?m4) (format t " 5 | %-11s | %-6s | %-8s | %-12s | %-13s%n" ?n5 ?c5 ?p5 ?d5 ?m5) (printout t crlf crlf)) (defrule startup => (printout t "There are five houses, each of a different color, inhabited by men of" crlf "different nationalities, with different pets, drinks, and spouses." crlf crlf "The Englishman lives in the red house. The Spaniard owns the dog." crlf "The ivory house is immediately to the left of the green house, where" crlf "the Bud drinker lives. The Evian drinker lives in the middle house." crlf "The man who is married to Alyson also keeps cats. The Ukrainian drinks" crlf "icedTea. The Norwegian resides in the first house on the left. The" crlf) (printout t "man married to Beth lives next door to the fish owner. The man married" crlf "to Celia drinks Heineken. The Japanese is married to Diane." crlf "The hamster owner lives next to the man married to Elise, whose house is yellow." crlf "The Norwegian lives next to the blue house." t crlf "Now, who drinks Coors? And who owns the zebra?" crlf crlf) (assert (value color red) (value color green) (value color ivory) (value color yellow) (value color blue) (value nationality englishman) (value nationality spaniard) (value nationality ukrainian) (value nationality norwegian) (value nationality japanese) (value pet dog) (value pet cats) (value pet fish) (value pet hamster) (value pet zebra) (value drink Coors) (value drink Bud) (value drink Evian) (value drink Heineken) (value drink icedTea) (value married Alyson) (value married Beth) (value married Celia) (value married Diane) (value married Elise)) ) (defrule generate-combinations ?f <- (value ?s ?e) => (retract ?f) (assert (avh (a ?s) (v ?e) (h 1)) (avh (a ?s) (v ?e) (h 2)) (avh (a ?s) (v ?e) (h 3)) (avh (a ?s) (v ?e) (h 4)) (avh (a ?s) (v ?e) (h 5))))