Thursday, August 3, 2017

The snail puzzle problem made me overlook another BrainBashers puzzle I liked. Actually it was a series of puzzles (or could be seen as such). They are called brain bashers' quizzes: there are some multiple choice question with answers a-D and you are supposed to find the answers that are consistent. The original puzzle was here:


Here is the gist. If we know then answer to a question we know something about the other answers. The right answer to question 1 tells us something about question 2 and so on:

1. The answer to Question 2 is:
            A. B      B. A      C. D      D. C
2. The answer to Question 3 is:
            A. C      B. D      C. B      D. A
3. The answer to Question 4 is:
            A. D      B. A     C. C      D. B
4. The answer to Question 1 is:
            A. D      B. C     C. A      D. B

Not too hard to solve. Here the choice of one answer  constrains the other answers. We just want a consistent set of answers. We can work trough following the statements one-by-one recording our deductions:
1=A => 2=B => 3=D => 4=B => 1=C
1=B => 2=A => 3=C => 4=C => 1=A
1=C => 2=D => 3=A => 4=D => 1=B
The first three result in contradictions. We have performed a reductio on each of the first three possibilities.

The only remaining is consistent:     1=D => 2=C ->3=B => 4=A => 1=D

So we can solve this by simple deduction. We could also solve it by examining all the possible sets of answers A,A,A,A ... D,D,D,D. Ltes try the deduction for now.

The descriptions of the answers for each problem can be seen as giving a collection of if-then conditionals.  For example, "If the answer to 1 is a then the answer to 2 is b and so forth. We give them all without further comment:

(deffacts claims
   (ans 1 a => 2 b)(ans 1 b => 2 a)(ans 1 c => 2 d)(ans 1 d => 2 c)
   (ans 2 a => 3 c)(ans 2 b => 3 d)(ans 2 c => 3 b)(ans 2 d => 3 a) 
   (ans 3 a => 4 d)(ans 3 b => 4 a)(ans 3 c => 4 c)(ans 3 d => 4 b)
   (ans 4 a => 1 d)(ans 4 b => 1 c)(ans 4 c => 1 a)(ans 4 d => 1 b) 
   
   (world)
)

We have added a world fact at the end. This is because we will test a possible world with each choice for the true answer to question 1:

 (defrule startWorld
   (world)
   (ans 1 ?x => ? ?)
=>
   (assert (world # 1 ?x)))  ;  # separates world facts

We find out more about the worlds by comparing what the say so far to the if-then facts we recorded s data. The if-ten clains were given above. Logician speak of a conditional as be "IF antecedent THEN consequent". If the antecedent is true in a world then its consequent must also be. Our rule just adds a consequent if an antecedent is known - so long as the test shows the consequent is new information. We also must ignore things like (ans  1 a  1 a) or our rule could be caught in a infinite deduction! If we do extend a world, we update what we know about if to be complete by retracting the old and asserting the new.

(defrule extendWorld                "= Modus Ponens"
?f<-(world $?W1  # ?n ?a $?W2)
    (ans ?n ?a => ?n2 ?a2)
    (test (and (or (neq ?n ?n2)
                   (neq ?a ?a2))   
               (not (member$ (create$ ?n2 ?a2) $?W1))
               (not (member$ (create$ ?n2 ?a2) $?W2))))
=>
   (retract ?f)
   (assert (world $?W1  # ?n ?a $?W2 # ?n2 ?a2)))


Of course we are looking for worlds that are consistent. We can immediately reject any that are inconsistent. Four our representation that means the have # ?n ?a and later on have a different value for ?n,  i.e.:  ?n ?b&~?a.  So the code below retract at once any fact with such a contradiction:

(defrule contradictory
    (declare (salience 10))
?f<-(world $? # ?i ?a $? # ?i ?b&~?a $?)
=>

    (retract ?f))

Now our code will only retain consistent, that is possible worlds. There might be more than 1.  If the puzzle setter erred - or more likely, we erred collecting the conditionals. It might be nice to have a rule to print any worlds still left after all the above worlds have done their work:

(defrule possible
   (declare (salience -20))
?f<-(world $?W)
    (test (> (length$ $?W) 0))  ; ignore world where we know nothing
=>
   (printout t "Possible: " $?W " " ?f crlf))

Here is the result:

CLIPS> Loading Selection...
Defining deffacts: claims
Defining defrule: startWorld +j+j+j
Defining defrule: extendWorldDescribed +j+j+j
Defining defrule: contradictory +j+j
Defining defrule: possible +j+j
CLIPS> (reset)
CLIPS> (run)
Possible: (# 1 d # 2 c # 3 b # 4 a) <Fact-33>
CLIPS> 

next puzzle TBA


Do we still have code that solves it?

No comments:

Post a Comment