Solving this type of problems using Jess is not very easy, basically
because you need disjunction (or) to encode your knowledge easily. This
in turn implies you need backtracking/search to solve the problem. Both
features are not actually offered by Jess. You can easily solve the
puzzle using Constraint Handling Rules, a forward chaining rule paradigm
close to Jess' rules (www.cs.kuleuven.be/~dtai/projects/CHR/),
implemented in Prolog (there are some Java implementations as well with
built-in search support). It took me about 15 minutes to write up a
simple solution (cf. attachment). Doing the same in Jess (or any rule
engine without backtracking support) is of course possible (rule engines
are turing complete), but will take more time and effort since you
basically have to code the backtracking manually. An alternative to
backtracking are of course always generate-and-test or look-ahead, but I
then encoding the given knowledge requires a lot more thought...


Jeff Brown wrote:
> I am trying to solve the puzzle at
> http://www.puzzlechoice.com/pc/Band_Logicx.html with Jess as a learning
> exercise and I can't seem to come up with a working solution.  If you
> are interested in working up a solution and would share it, I would be
> interested in seeing how that is done.
> 
> Thanks.
> 
> 
> 
> jb
> 

:- use_module(library(chr)).

:- chr_constraint age/2, lives/2, usedtobe/2.

name(greg).
name(darren).
name(patrick).
name(daniel).

age(19).
age(20).
age(21).
age(22).

job(truckdriver).
job(pizzadeliveror).
job(carpenter).
job(waiter).

city(sanfransisco).
city(newyork).
city(cleveland).
city(seattle).

age(X, Y) ==> name(X), age(Y).
lives(X, Y) ==> name(X), city(Y).
usedtobe(X, Y) ==> name(X), job(Y).

age(X, Z) \ age(Y, Z) <=> X = Y.
lives(X, Z) \ lives(Y, Z) <=> X = Y.
usedtobe(X, Z) \ usedtobe(Y, Z) <=> X = Y.

age(X, Y) \ age(X, Z) <=> Y = Z.
lives(X, Y) \ lives(X, Z) <=> Y = Z.
usedtobe(X, Y) \ usedtobe(X, Z) <=> Y = Z.

lives(X, cleveland) ==> usedtobe(X, truckdriver).
usedtobe(X, truckdriver) ==> lives(X, cleveland).
age(greg, X), age(Y, Z), lives(Y, cleveland), usedtobe(Y, truckdriver) 
        ==> (Z is X+2 ; Z is X-2).

query :- 
        age(X, 22), lives(X, sanfransisco), usedtobe(X, _),
        person(Y), person(Z), person(W),
        alldiff([X,Y,Z,W]),
        ( usedtobe(darren, pizzadeliveror) ; ( lives(X, seatle), usedtobe(X, 
pizzadeliveror ))).
person(X) :- lives(X, _), usedtobe(X, _), age(X, _).

lives(X, newyork), age(X, Y), age(daniel, Z) ==> Y < Z.
lives(X, newyork), age(X, Y), usedtobe(Z, carpenter), age(Z, W) ==> Y > W.
age(X, 22), lives(X, sanfransisco), usedtobe(X, waiter) ==> fail.

alldiff([]).
alldiff([X|Xs]) :-
        alldiff(X, Xs),
        alldiff(Xs).
alldiff(_, []).
alldiff(X, [Y|Ys]) :-
        X \== Y,
        alldiff(X, Ys).
begin:vcard
fn:Peter Van Weert
n:Van Weert;Peter
org:K.U.Leuven;Computer Science
adr:room 01.08;;Celestijnenlaan 200A;Heverlee;;3001;Belgium
email;internet:[EMAIL PROTECTED]
title:DTAI (Declarative Languages and Artificial Intelligence)
tel;work:+ 32 16 327064
x-mozilla-html:TRUE
url:http://www.cs.kuleuven.be/~petervw/
version:2.1
end:vcard

Reply via email to