Hi Bryan.

Thanks for reviewing the proposed (flawed) solution. Here's the corrected solution which does produce the expected result. I should have been more careful and not posted before doing a real check against the facts.

As you suggest, this is clearly enough on this topic ....

Regards,

Win

The corrections are:

34c34
<   (avh (a drinks) (v coffee) (h ?d2&~?d1&?c2&~?c3&~?c1))
---
>   (avh (a drinks) (v coffee) (h ?d2&~?d1&?c2&~?c1))
105,107c105,107
<           (solution drinks coffee ?d1)
<           (solution drinks milk ?d2)
<           (solution drinks tea ?d3)
---
>           (solution drinks tea ?d1)
>           (solution drinks coffee ?d2)
>           (solution drinks milk ?d3)

Bryan Talbot wrote:

This is straying off topic, but ...

From the output of this program, I don't think the solution is correct. Here are the facts stated by the program:

1)  The Englishman lives in the red house.
2)  The Swede keeps dogs as pets.
3)  The Dane drinks tea.
4)  The green house is on the left of the white house.
5)  The green house owner drinks coffee.
6)  The person who smokes Pall Mall rears birds.
7)  The owner of the yellow house smokes Dunhill.
8)  The man living in the house right in the center drinks milk.
9)  The Norwegian lives in the first house.
10) The man who smokes Blends lives next to the one who keeps cats.
11) The man who keeps horses lives next to the one who smokes Dunhill.
12) The owner who smokes Bluemaster drinks beer.
13) The German smokes Prince.
14) The Norwegian lives next to the blue house.



And here is the solution provided:

HOUSE | Nationality Color Pet Drink Smokes
--------------------------------------------------------------------
  1   |norwegian yellow cats water dunhill
  2   |dane blue horses COFFEE blends
  3   |englishman red birds TEA pall-mall
  4   |german green fish MILK prince
  5   |swede white dogs beer bluemaster


However,


Fact 3 states that the Dane drinks tea - the solution claims it's coffee.
Fact 5 states that the Green house owner drinks coffee - the solution puts the coffee drinker in a blue house.
Fact 8 states that the center house owner drinks milk - the solution has him drinking tea.



So the solution should be:


HOUSE | Nationality Color Pet Drink Smokes
--------------------------------------------------------------------
  1   |norwegian yellow cats water dunhill
  2   |dane blue horses TEA blends
  3   |englishman red birds MILK pall-mall
  4   |german green fish COFFEE prince
  5   |swede white dogs beer bluemaster



Clearly the drinks are getting mixed up someplace; the bug fix for this is left as an exercise for the reader. :)


-Bryan





-----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, June 11, 2003 9:24 AM To: [EMAIL PROTECTED] Subject: JESS: Einstein's Riddle in Jess


Hi David.


Here is a Jess solution to Einstein's Riddle as presented in your E-mail. The
approach to this problem and the implementation is taken directly from the
zebra.clp example supplied by the Jess distribution (which is taken, in turn,
directly from the Giarratano and Riley's "Expert Systems: Principles and
Programming").


Regards,

Win Carus
> Dear Jessers,
> > I am a newbie to Jess. I would like to solve the following riddle using
> Jess. I have previously tried to use Prolog to solve it but my attempts
> were futile.
> Anyone has any idea how to approach it ? > > Who owns the fish? This is the question that Einstein posed last century
> when he stated that 98% of the population would be unable to solve it.
> Find out if you are one of the 2% who can...
> There are 5 houses in 5 different colors.
> In each house lives a person with a different nationality.
> These 5 owners drink a certain drink, smoke a certain brand of cigar,
> and keep a certain pet.
> No owners have the same pet, smoke the same brand of cigar or drink the
> same drink.
> The question is -- WHO OWNS THE FISH?
> >
> HINTS:
> - the Brit lives in the red house
> - the Swede keeps dogs as pets
> - the Dane drinks tea
> - the green house is on the left of the white house
> - the green house owner drinks coffee
> - the person who smokes Pall Mall rears birds


> - the owner of the yellow house smokes Dunhill
> - the man living in the house right in the center drinks milk
> - the Norwegian lives in the first house
> - the man who smokes blends lives next to the one who keeps cats
> - the man who keeps horses lives next to the one who smokes Dunhill
> - the owner who smokes Bluemaster drinks beer
> - the German smokes prince
> - the Norwegian lives next to the blue house
> - the man who smokes blends has a neighbor who drinks water.
> <http://www.tektron.co.uk/riddlesolution.html> Einstein
> > ========================================================================
> ========================================
> > >



;;;======================================================
;;;   Who Owns the Fish?
;;;
;;;     Jess version 
;;;
;;;     To execute, merely load, reset and run.
;;;======================================================

;(set-node-index-hash 13)
    
(deftemplate avh (slot a) (slot v) (slot h (type INTEGER)))

(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 Swede keeps dogs as pets.

  (avh (a nationality) (v swede) (h ?n2&~?n1))
  (avh (a pet) (v dogs) (h ?p1&?n2&~?n1))
  
  ; The Dane drinks tea.
  
  (avh (a nationality) (v dane) (h ?n3&~?n2&~?n1))
  (avh (a drinks) (v tea) (h ?d1&?n3&~?n2&~?n1))

  ; The green house is on the left of the white house.
  ; The green house owner drinks coffee.

  (avh (a color) (v green) (h ?c2&~?c1))
  (avh (a color) (v white) (h ?c3&~?c2&~?c1&:(= ?c3 (+ ?c2 1))))
  (avh (a drinks) (v coffee) (h ?d2&~?d1&?c2&~?c1))
  
  ; The person who smokes Pall Mall rears birds.

  (avh (a smokes) (v pall-mall) (h ?s1))
  (avh (a pet) (v birds) (h ?p2&~?p1&?s1))

  ; The owner of the yellow house smokes Dunhill.

  (avh (a color) (v yellow) (h ?c4&~?c3&~?c2&~?c1))
  (avh (a smokes) (v dunhill) (h ?s2&~?s1&?c4&~?c3&~?c2&~?c1))

  ; The man living in the house right in the center drinks milk.

  (avh (a drinks) (v milk) (h ?d3&~?d2&~?d1&3))
  
  ; The Norwegian lives in the first house.

  (avh (a nationality) (v norwegian) (h ?n4&~?n3&~?n2&~?n1&1))

  ; The man who smokes Blends lives next to the one who keeps cats.

  (avh (a smokes) (v blends) (h ?s3&~s2&~?s1))
  (avh (a pet) (v cats) (h ?p3&~?p2&~?p1&:(or (= ?s3 (- ?p3 1)) (= ?s3 (+ ?p3 1)))))

  ; The man who keeps horses lives next to the one who smokes Dunhill.
  
  (avh (a pet) (v horses) (h ?p4&~?p3&~?p2&~?p1&:(or (= ?p4 (- ?s2 1)) (= ?p4 (+ ?s2 
1)))))

  ; The owner who smokes Bluemaster drinks beer.

  (avh (a smokes) (v bluemaster) (h ?s4&~?s3&~?s2&~?s1))
  (avh (a drinks) (v beer) (h ?d4&~?d3&~?d2&~?d1&?s4&~?s3&~?s2&~?s1)) 

  ; The German smokes Prince

  (avh (a nationality) (v german) (h ?n5&~?n4&~?n3&~?n2&~?n1))
  (avh (a smokes) (v prince) (h ?s5&~?s4&~?s3&~?s2&~?s1&?n5&~?n4&~?n3&~?n2&~?n1))

  ; 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)))))

  ; The man who smokes Blends has a neighbor who drinks water.
 
  (avh (a drinks) (v water) (h ?d5&~?d4&~?d3&~?d2&~?d1))
  (avh (a smokes) (v blends) (h ?s3&:(or (= ?s3 (- ?d5 1)) (= ?s3 (+ ?d5 1)))))

  ; Who owns the fish?
  
  (avh (a pet) (v fish) (h ?p5&~?p4&~?p3&~?p2&~?p1))
  
  => 
  (assert (solution nationality englishman ?n1)
          (solution nationality swede ?n2)
          (solution nationality dane ?n3)
          (solution nationality norwegian ?n4)
          (solution nationality german ?n5)
  
          (solution color red ?c1)
          (solution color green ?c2)
          (solution color white ?c3)
          (solution color yellow ?c4)
          (solution color blue ?c5)
          
          (solution pet dogs ?p1)
          (solution pet birds ?p2)
          (solution pet cats ?p3)
          (solution pet horses ?p4) 
          (solution pet fish ?p5)
          
          (solution drinks tea ?d1)
          (solution drinks coffee ?d2)
          (solution drinks milk ?d3) 
          (solution drinks beer ?d4) 
          (solution drinks water ?d5)
          
          (solution smokes pall-mall ?s1)
          (solution smokes dunhill ?s2)
          (solution smokes blends ?s3)
          (solution smokes bluemaster ?s4)
          (solution smokes prince ?s5)))

(defrule print-solution
  ?f1 <- (solution nationality ?n1 1)
  ?f2 <- (solution color ?c1 1)
  ?f3 <- (solution pet ?p1 1)
  ?f4 <- (solution drinks ?d1 1)
  ?f5 <- (solution smokes ?s1 1)
  
  ?f6 <- (solution nationality ?n2 2)
  ?f7 <- (solution color ?c2 2)
  ?f8 <- (solution pet ?p2 2)
  ?f9 <- (solution drinks ?d2 2)
  ?f10 <- (solution smokes ?s2 2)
  
  ?f11 <- (solution nationality ?n3 3)
  ?f12 <- (solution color ?c3 3)
  ?f13 <- (solution pet ?p3 3)
  ?f14 <- (solution drinks ?d3 3)
  ?f15 <- (solution smokes ?s3 3)
  
  ?f16 <- (solution nationality ?n4 4)
  ?f17 <- (solution color ?c4 4)
  ?f18 <- (solution pet ?p4 4)
  ?f19 <- (solution drinks ?d4 4)
  ?f20 <- (solution smokes ?s4 4)
  
  ?f21 <- (solution nationality ?n5 5)
  ?f22 <- (solution color ?c5 5)
  ?f23 <- (solution pet ?p5 5)
  ?f24 <- (solution drinks ?d5 5)
  ?f25 <- (solution smokes ?s5 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)
           
  (printout t "HOUSE | Nationality Color Pet Drink Smokes" crlf)
  (printout t "--------------------------------------------------------------------" 
crlf)
  (printout t "  1   |" ?n1 " " ?c1 " " ?p1 " " ?d1 " " ?s1 crlf)
  (printout t "  2   |" ?n2 " " ?c2 " " ?p2 " " ?d2 " " ?s2 crlf)
  (printout t "  3   |" ?n3 " " ?c3 " " ?p3 " " ?d3 " " ?s3 crlf)
  (printout t "  4   |" ?n4 " " ?c4 " " ?p4 " " ?d4 " " ?s4 crlf)
  (printout t "  5   |" ?n5 " " ?c5 " " ?p5 " " ?d5 " " ?s5 crlf)
  (printout t crlf crlf))

(defrule startup
   =>
   (printout t 
        "The Englishman lives in the red house." crlf
        "The Swede keeps dogs as pets." crlf
        "The Dane drinks tea." crlf
        "The green house is on the left of the white house." crlf
        "The green house owner drinks coffee." crlf
        "The person who smokes Pall Mall rears birds." crlf
        "The owner of the yellow house smokes Dunhill." crlf
        "The man living in the house right in the center drinks milk." crlf
        "The Norwegian lives in the first house." crlf
        "The man who smokes Blends lives next to the one who keeps cats." crlf
        "The man who keeps horses lives next to the one who smokes Dunhill." crlf
        "The owner who smokes Bluemaster drinks beer." crlf
        "The German smokes Prince." crlf 
        "The Norwegian lives next to the blue house." crlf crlf)

   (assert (value color red)
           (value color green) 
           (value color white) 
           (value color yellow)
           (value color blue)
           
           (value nationality englishman)
           (value nationality swede)
           (value nationality dane)
           (value nationality norwegian)
           (value nationality german)
            
           (value pet dogs)
           (value pet birds)
           (value pet cats)
           (value pet horses)
           (value pet fish)
           
           (value drinks coffee)
           (value drinks milk)
           (value drinks beer)
           (value drinks tea)
           (value drinks water)
           
           (value smokes dunhill)
           (value smokes pall-mall) 
           (value smokes blends)
           (value smokes bluemaster) 
           (value smokes prince)) 
   )

(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))))

(defglobal ?*time* = (time))
(set-reset-globals FALSE)
(deffunction run-n-times (?n)
  (while (> ?n 0) do
         (reset)
         (run)
         (bind ?n (- ?n 1))))

(run-n-times 1)

(printout t "Elapsed time: " (integer (- (time) ?*time*)) crlf)

Reply via email to