We’re able to solve Einstein’s Riddle using a straightforward “we either know this variable’s value or we don’t” model of thought. It’s blatantly suboptimal. But it’s too fast to be noticeable. So we need a better problem.
This in installment 2 in this series.
There’s a nice set of this class of problems over at brainzilla.1 Our old friend, Einstein’s, is there too, rated medium. There are five puzzles available in the “very hard” category.
Here’s the plan:
- upgrade my code to be able to read a puzzle in the site’s format
- check it works using Einstein
- proceed to time various ways of solving them on a harder one.
Of course, I may get bored halfway through, or find something else shiny enough to distract me, or it may just plain old be too hard for me. We’ll see how that pans out…
The first thing to notice is Einstein there isn’t the same one as Wikipedia’s. Sigh. Oh well. The gist of it is similar enough.
Brainzilla features a nifty PDF export of their puzzles. It’s the cleanest way I’ve found to extract the relevant information our of them. So, what do we have?
Color: blue, green, red, white, yellow Nationality: Brit, Dane, German, Norwegian, Swede Drink: beer, coffee, milk, tea, water Cigarette: Blends, Blue Master, Dunhill, Pall Mall, Prince Pet: birds, cats, dogs, horses, fish - The Brit lives in the Red house. - The Swede keeps Dogs as pets. - The Dane drinks Tea. - The Green house is exactly to the left of the White house. - The owner of the Green house drinks Coffee. - The person who smokes Pall Mall rears Birds. - The owner of the Yellow house smokes Dunhill. - The man living in the centre house 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 man who smokes Dunhill. - The man who smokes Blue Master drinks Beer. - The German smokes Prince. - The Norwegian lives next to the Blue house. - The man who smokes Blends has a neighbour who drinks Water.
They’re kind enough to give us the property names and values. Having to guess them is fun when you’re solving by hand; it’s a nuisance when automating. So I’m glad they do. We notice two-word “orange juice” is gone, but there’s still “Pall Mall” and “Blue Master”. I’ll search-and-replace them to single words. I’ll also add a period at the end of the property list lines, so I can mostly use the same word-based tokenizer between both parts. And remove the leading dashes in the constraint listing.
Then I’ll highlight the actually interesting remaining parts of the text, to see what we really need to keep from this.
Color: blue, green, red, white, yellow.
Nationality: Brit, Dane, German, Norwegian, Swede.
Drink: beer, coffee, milk, tea, water.
Cigarette: Blends, BlueMaster, Dunhill, PallMall, Prince.
Pet: birds, cats, dogs, horses, fish.
The Brit lives in the Red house.
The Swede keeps Dogs as pets.
The Dane drinks Tea.
The Green house is exactly to the left of the White house.
The owner of the Green house drinks Coffee.
The person who smokes PallMall rears Birds.
The owner of the Yellow house smokes Dunhill.
The man living in the centre house 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 man who smokes Dunhill.
The man who smokes BlueMaster drinks Beer.
The German smokes Prince.
The Norwegian lives next to the Blue house.
The man who smokes Blends has a neighbour who drinks Water.
That text doesn’t have much efficiency, huh?
I’m keeping the colon and period punctuation marks as individual token to help at:
- distinguishing feature lines from constrain lines
- future extensibility when we’ll have more complex sentences
The features’ actual names aren’t needed per se, but I do need to know which values go together, so that name is as good as another to group them under.
Now to implement this. I’ll use the usual Prolog DCGs.
First to split on words.
:- [library(dcg/basics)].
W|Ws]) --> word(W), blanks, !, words(Ws).
words([--> eos. words([])
To scan for a word, take anything that’s not ignored. We’re ignoring whitespace as a separator, and some punctuation marks as tokens of their own. I’m defining those lists in separate predicates to help further evolution.
".:").
punct(",").
skip(" \n").
whitespace(
Cs) :-
word_complement(P), skip(S), whitespace(W),
punct(P,S,T), string_concat(T,W,Cs).
string_concat(
T) -->
word({ word_complement(Cp) },
Cp, Codes),
string_without({ Codes \= [] },
{ read_codes(Codes,T) }.
T) --> [C], { punct(P), string_codes(P,Cs), member(C,Cs), atom_codes(T,[C]) }.
word(T) --> [C], { skip(S), string_codes(S,Cs), member(C,Cs) }, blanks, word(T).
word(
Codes,Token) :- atom_codes(Token,Codes). read_codes(
Let’s try it out.
?- string_codes(“:hello, world.”, Codes),
phrase(words(Words), Codes).
Codes = [58, 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 46],
Words = [:, hello, world, ‘.’].
It’s extracted the two interesting punctuation marks as individual tokens. It skipped spaces and commas. It converted the two words to atoms. Seems all right for now.
Let’s see what it makes of the entire input file.
?- phrase_from_file(words(Words), “einstein.txt”).
Words = [color, :, blue, green, red, white, yellow, ‘.’, nationality, :, brit, dane, german, norwegian, swede, ‘.’, drink, :, beer, coffee, milk, tea, water, ‘.’, cigarette, :, blends, bluemaster, dunhill, pallmall, prince, ‘.’, pet, :, birds, cats, dogs, horses, fish, ‘.’, the, brit, lives, in, the, red, house, ‘.’, the, swede, keeps, dogs, as, pets, ‘.’, the, dane, drinks, tea, ‘.’, the, green, house, is, exactly, to, the, left, of, the, white, house, ‘.’, the, owner, of, the, green, house, drinks, coffee, ‘.’, the, person, who, smokes, pallmall, rears, birds, ‘.’, the, owner, of, the, yellow, house, smokes, dunhill, ‘.’, the, man, living, in, the, centre, house, 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, man, who, smokes, dunhill, ‘.’, the, man, who, smokes, bluemaster, drinks, beer, ‘.’, the, german, smokes, prince, ‘.’, the, norwegian, lives, next, to, the, blue, house, ‘.’, the, man, who, smokes, blends, has, a, neighbour, who, drinks, water, ‘.’].
Ok.
Now to strip anything we’re not interested in. We’ll need to extract our domain first. The domain is a list of features, where each feature presents as its name, a colon, a list of value words, and a period.
F|Fs]) --> feature(F), domain(Fs).
domain([--> [].
domain([]) Name,Vs)) --> [Name,':'], values(Vs).
feature(feature(
--> ['.'], !.
values([]) V|Vs]) --> [V], values(Vs). values([
?- phrase_from_file(words(Words), “einstein.txt”),
phrase(domain(Dom), Words, _).
Dom = [feature(color, [blue, green, red, white, yellow]), feature(nationality, [brit, dane, german, norwegian, swede]), feature(drink, [beer, coffee, milk, tea, water]), feature(cigarette, [blends, bluemaster, dunhill, pallmall, prince]), feature(pet, [birds, cats, dogs, horses, fish])]
Sweet.
Flattening that and referencing a few connector words now lets us filter the constraints.
D,Vs) :- maplist(feature_values,D,Vss), append(Vss,Vs).
domain_values(_,Vs),Vs).
feature_values(feature(
'.']).
connectors([left,next,neighbour,
D,Ts,C) :-
cleanse(Cs), domain_values(D,Vs), append(Cs,Vs,K),
connectors(include(member_(K),Ts,C).
L,E) :- member(E,L). member_(
?- phrase_from_file(words(Words), “einstein.txt”),
phrase(domain(Dom), Words, Verbose),
cleanse(Dom, Verbose, Terse).
Terse = [brit, red, ‘.’, swede, dogs, ‘.’, dane, tea, ‘.’, green, left, white, ‘.’, green, coffee, ‘.’, pallmall, birds, ‘.’, yellow, dunhill, ‘.’, milk, ‘.’, norwegian, ‘.’, blends, next, cats, ‘.’, horses, next, dunhill, ‘.’, bluemaster, beer, ‘.’, german, prince, ‘.’, norwegian, next, blue, ‘.’, blends, neighbour, water, ‘.’]
It’s reduced to all that’s needed. Parsing is now trivial:
Dom,[C|Cs]) --> constraint(Dom,C), ['.'], constraints(Dom,Cs).
constraints(_,[]) --> [].
constraints(
Dom,same(A,B)) --> attribute(Dom,A), attribute(Dom,B).
constraint(Dom,seq([A,B])) --> attribute(Dom,A), [left], attribute(Dom,B).
constraint(Dom,neighbors(A,B)) -->
constraint(Dom,A),
attribute(N], { member(N,[next,neighbour]) },
[Dom,B). attribute(
This recognizes three kinds of constraints:
- the most commone one: “feature A is in the same place as feature B”
- ordered sequence: “features A directly precedes feature B”
- unordered sequence: “features A and B are directly next to each other”
It needs an attribute parser, recognizing values from the domain:
Dom,attr(F,V)) --> [V], { member(feature(F,Vs),Dom), member(V,Vs) }.
attribute(_,attr(position,P)) --> [P], { positions(Ps), member(P,Ps) }. attribute(
We can now parse the entire constraint list:
?- phrase_from_file(words(Words), “einstein.txt”),
phrase(domain(Dom), Words, Verbose),
cleanse(Dom, Verbose, Terse),
phrase(constraints(Dom,Constraints),Terse).
Constraints = [
same(attr(nationality, brit),attr(color, red)),
same(attr(nationality, swede),attr(pet, dogs)),
same(attr(nationality, dane),attr(drink, tea)),
seq([attr(color, green),attr(color, white)]),
same(attr(color, green),attr(drink, coffee)),
same(attr(cigarette, pallmall),attr(pet, birds)),
same(attr(color, yellow),attr(cigarette, dunhill)),
same(attr(position, centre),attr(drink, milk)),
same(attr(nationality, norwegian),attr(position, first)),
neighbors(attr(cigarette, blends),attr(pet, cats)),
neighbors(attr(pet, horses),attr(cigarette, dunhill)),
same(attr(cigarette, bluemaster),attr(drink, beer)),
same(attr(nationality, german),attr(cigarette, prince)),
neighbors(attr(nationality, norwegian),attr(color, blue)),
neighbors(attr(cigarette, blends),attr(drink, water))
]
Let’s package that.
FileName,puzzle(Dom,Constraints)) :-
read_zebra(Words),FileName),
phrase_from_file(words(phrase(domain(Dom),Words,Verbose),
Dom,Verbose,Terse),
cleanse(phrase(constraints(Dom,Constraints),Terse).
With a parsed AST, all we have left to do is convert it to a search, in the same form as last installment’s. In a nutshell: give the search space a shape, then unify it with the constraints, in order.
Dom,Constraints),Sol) :-
solve(puzzle(Dom,Sol),
shape(Dom,Sol),Constraints). maplist(apply(
The shaping phase will unify the solution with a list as long as the
domain’s value count per feature. Each list element will be a
place
functor, with as many arguments as there are features
in the puzzle’s domain.
Dom,Sol) :-
shape(Dom,N),
inner_length(Sol,N),
length(Dom,F),
length(F),Sol).
maplist(place(
F,P) :- length(X,F), P =.. [place|X].
place(
_,H)|T],N) :-
inner_length([feature(H,N),
length(\+ ( member(feature(_,X),T), length(X,N2), N2 \= N ).
It’s probably easier to visualize than decode:
?- read_zebra(“einstein.txt”,puzzle(D,_)),shape(D,S).
S = [place(_2154, _2160, _2166, _2172, _2178), place(_2202, _2208, _2214, _2220, _2226), place(_2250, _2256, _2262, _2268, _2274), place(_2298, _2304, _2310, _2316, _2322), place(_2346, _2352, _2358, _2364, _2370)]
Constraint application unifies the places’ attributes, depending on the connector. As a special case, “position” attributes are special-cased so that they can apply to a specific place instead of trying them all.
Dom,Sol,same(attr(position,Pos),A)) :-
apply(Pos = centre -> length(Sol,L), M is L//2 ;
( Pos = first -> M = 0 ;
throw(invalid_position(Pos)) ),
M,Sol,P),
nth0(Dom,P,A).
unify(Dom,Sol,same(A,attr(position,Pos))) :-
apply(Dom,Sol,same(attr(position,Pos),A)).
apply(Dom,Sol,same(A,B)) :-
apply(P,Sol),
member(Dom,P,A),
unify(Dom,P,B).
unify(Dom,Sol,seq(A,B)) :-
apply(Pa,Pb,Sol),
nextto(Dom,Pa,A),
unify(Dom,Pb,B).
unify(Dom,Sol,neighbors(A,B)) :-
apply(Dom,Sol,seq(A,B)) ;
apply(Dom,Sol,seq(B,A)). apply(
Unification simply locates the place
functor argument by
cross-referencing the domain.
Dom,P,attr(F,V)) :-
unify(N,Dom,feature(F,_)), !,
nth0(P =.. [place|Vs],
N,Vs,V). nth0(
And we’re done! Let’s try it out.
?- read_zebra(“einstein.txt”,P),solve(P,Sol).
Sol = [place(yellow, norwegian, water, dunhill, cats), place(blue, dane, tea, blends, horses), place(red, brit, milk, pallmall, birds), place(green, german, coffee, prince, _25176), place(white, swede, beer, bluemaster, dogs)]
A solution is found instantly. As was the case previously, the actual pet the statement asks for isn’t reified: it’s still a free variable. As it’s the only one left in the solution space, it’s unambiguous.
With this new generic infrastructure at the ready, let’s try for a harder puzzle: delightful dresses. Parsing difficulties:
- new relations: “somewhere to the right/left”, “somewhere between”
- new positions: “at one of the ends”
- indirect attributes: “oldest”, “youngest”, “smallest discount”
I’ll introduce a new constraint type, ordered
, to
account for the “somewhere” class of new relations. Recognition will
consist in adding “right”, “somewhere”, “between” and “and” to the list
of connector words. Parsing is a few additional clauses of the
constraint
predicate.
'.',somewhere,between,and]).
connectors([left,right,next,neighbour,
Dom,ordered([A,B])) -->
constraint(Dom,A), [somewhere,left], attribute(Dom,B).
attribute(Dom,ordered([A,B])) -->
constraint(Dom,B), [somewhere,right], attribute(Dom,A).
attribute(
Dom,seq(A,B)) --> attribute(Dom,B), [right], attribute(Dom,A). constraint(
The “at-end” one will be a new position pseudo-feature value by virtue of adding a keyword to the list.
. positions([centre,first,ends])
The indirect attributes are going to be slightly trickier. We know the domain, so we can resolve them at parse time. To do it cleanly, I’ll need to interpret their values as numbers, instead of the symbols I currently have. So I’ll update the tokenizer slightly:
",%").
skip(
Codes,Number) :-
read_codes(catch(number_codes(Number,Codes),
,_),
error(syntax_error(illegal_number)fail).
Codes,Token) :- atom_codes(Atom,Codes), downcase_atom(Atom,Token). read_codes(
I added %
to the list of skipped characters so it
wouldn’t confuse the number parser. The puzzle author was nice enough
not to make the discounts collide with the ages. Then I inserted a
number parsing clause above the regular atom one: if it’s a number it’ll
parse as such, else it will be a simple symbol.
I’ll new list the relatives as recognized words, and update the word purge to save those too.
.
relatives([youngest,oldest])
D,Ts,C) :-
cleanse(Cs),
connectors(Ps),
positions(Rs),
relatives(D,Vs),
domain_values(Cs,Ps,Rs,Vs],K),
append([include(member_(K),Ts,C).
I’ll need to read them accurately. “Youngest” and “oldest” can reasonably be assumed to always refer to an “age” feature.
Dom,attr(age,Young)) --> [youngest], !, { feature_min(Dom,age,Young) }.
attribute(Dom,attr(age,Young)) --> [oldest], !, { feature_max(Dom,age,Young) }.
attribute(
Dom,F,Min) :-
feature_min(F,Vs),Dom),
menber(feature(Min,Vs).
min_member(Dom,F,Min) :-
feature_max(F,Vs),Dom),
menber(feature(Min,Vs). max_member(
“Smallest discount” is tricker. Interpreting both words in the current framework would be quite nasty. Interpreting “smallest” to imply “discount” would be very oddly specific.
So I’ll take the side road. I’ll introduce an optional, puzzle-specific predicate to substitute a term for a word. It’ll serve as a hatch to help language recognition in those cases.
:- dynamic subst/2.
D,Ts,C) :-
cleanse(Cs),
connectors(Ps),
positions(Rs),
relatives(S,subst(S,_),Ss),
findall(D,Vs),
domain_values(Cs,Ps,Rs,Vs,Ss],K),
append([include(member_(K),Ts,Tmp),
,Tmp,C).
maplist(perform_substF,T) :- once(subst(F,T)) ; F = T. perform_subst(
Here’s how to use it to interpret the smallest discount without leaking too much abstraction away:
,min(discount).
subst(smallest
Dom,attr(F,Min)) --> [min(F)], { feature_min(Dom,F,Min) }. attribute(
And we’ve got the whole file parsed. Of course, it’s parsed wrong because I’m not interpreting “immediately before” correctly yet. But this is trivial now I’ve got a substitution facility.
,left). subst(before
?- read_zebra(“dresses.txt”,Puzzle).
Puzzle = puzzle([feature(dress, [black, blue, purple, red, white]),
feature(name, [anna, erica, lauren, megan, sara]),
feature(profession, [actress, electrician, programmer, psychologist, surgeon]),
feature(type, [‘a-line’, bodycon, sheath, sundress, wrap]),
feature(discount, [5, 10, 15, 20, 25]),
feature(age, [30, 33, 36, 39, 41])],
[seq(attr(profession, surgeon), attr(profession, programmer)),
ordered([attr(discount, 10), attr(dress, black)]),
neighbors(attr(profession, actress), attr(dress, black)),
neighbors(attr(profession, psychologist), attr(type, bodycon)),
same(attr(name, megan), attr(age, 41)),
ordered([attr(dress, white), attr(age, 36)]),
neighbors(attr(type, sheath), attr(discount, 15)),
ordered([attr(age, 30), attr(dress, purple), attr(dress, white)]),
seq(attr(discount, 5), attr(discount, 15)),
ordered([attr(dress, white), attr(discount, 20)]),
seq(attr(profession, surgeon), attr(type, sundress)),
same(attr(name, anna), attr(dress, white)),
seq(attr(name, megan), attr(discount, 5)),
same(attr(type, sheath), attr(dress, red)),
same(attr(position, ends), attr(age, 33)),
seq(attr(type, bodycon), attr(name, sara)),
same(attr(name, lauren), attr(age, 33)),
same(attr(position, ends), attr(type, sheath)),
seq(attr(discount, 10), attr(discount, 5)),
same(attr(type, ‘a-line’), attr(dress, purple))])
I love the smell of a freshly roasted parsed puzzle in the
morning.
To be able to solve it, we’ll need to implement the new
ordered
subsequence predicate.
Dom,Sol,ordered(S)) :- apply_list(Dom,Sol,S).
apply(
Dom,[Sh|St],[Ah|At]) :- unify(Dom,Sh,Ah), apply_list(Dom,St,At).
apply_list(Dom,[_|St],As) :- apply_list(Dom,St,As).
apply_list(_,_,[]). apply_list(
And we’ll need to implement the new “at one of the ends” position.
Dom,Sol,same(attr(position,Pos),A)) :-
apply(Pos = centre -> length(Sol,L), M is (L+1)//2 ;
( Pos = first -> M = 1 ;
Pos = ends -> (M = 1 ; length(Sol,M) ) ;
throw(invalid_position(Pos)) ),
M,Sol,P),
nth1(Dom,P,A). unify(
And merrily solve our puzzle.
?- read_zebra(“dresses.txt”,Puzzle), solve(Puzzle,Sol).
Sol = [place(red, lauren, surgeon, sheath, 10, 33), place(black, megan, programmer, sundress, 5, 30), place(purple, megan, psychologist, ‘a-line’, 15, 41), place(white, anna, actress, bodycon, 15, _17180), place(black, sara, _17218, sheath, 20, 36)]
Wait a second. This won’t do! Notwithstanding the remaining free variables, this solution is incorrect! Spot the problem yet?
Allow me to reformat to make it more apparent:
- place(red, lauren, surgeon, sheath, 10, 33)
- place(black, megan, programmer, sundress, 5, 30)
- place(purple, megan, psychologist, ‘a-line’, 15, 41)
- place(white, anna, actress, bodycon, 15, Age)
- place(black, sara, Job, sheath, 20, 36)
Yup. Two black dresses, two Megans, two sheathes, two 15% discounts. Contrary to Einstein, this puzzle’s constraints aren’t sufficient to rule out duplication.
Well, at least that isn’t not too hard to include.
Dom,Sol) :-
assert_domain(Dom,L),
length(1,L,X),assert_feature(X,Dom,Sol)).
foreach(between(X,Dom,Sol) :-
assert_feature(X,Dom,feature(_,Vs)),
nth1(X,Sol),Vs).
maplist(assert_value(X,Sol,V) :-
assert_value(P,Sol),
member(P =.. PVs,
X,PVs,V).
nth0(
Dom,Constraints),Sol) :-
solve(puzzle(Dom,Sol),
shape(Dom,Sol),Constraints),
maplist(apply(Dom,Sol). assert_domain(
The astute reader will have noticed I’m actually checking the converse, namely that each feature value is represented. As there are as many women as values, they’re equivalent.
?- read_zebra(“dresses.txt”,Puzzle), solve(Puzzle,Sol).
Sol = [place(blue, erica, electrician, wrap, 25, 30), place(purple, megan, psychologist, ‘a-line’, 10, 41), place(white, anna, surgeon, bodycon, 5, 39), place(black, sara, programmer, sundress, 15, 36), place(red, lauren, actress, sheath, 20, 33)] ;
Victory!
Let’s implement blood donation, another “very hard” puzzle from the site, to make sure it wasn’t an accident.
I’m not going to delve too much into the details this time. There were two hurdles.
- Donors can be referred to as “universal donor” or “universal recipient”. This is completely out-of-the-box information. I just inserted “O-” and “AB+” in the relevant places in the constraints. This leads to…
- The Rhesus signs in the PDF are U+00AD SOFT HYPHENs instead of the more usual U+0045 HYPHEN-MINUS. Don’t get mixed up!
- 2 Oh, and the usual: “feature names are expected to be single words”.
Barring that, it all runs as smoothly as expected:
?- read_zebra(“blood-donation.txt”,Puzzle), solve(Puzzle,Sol).
Sol = [place(black, brooke, ‘b-’, 45, 130, actress), place(green, nichole, ‘o-’, 35, 160, chef), place(purple, andrea, ‘ab+’, 30, 120, policewoman), place(blue, meghan, ‘a+’, 25, 150, florist), place(red, kathleen, ‘b+’, 40, 140, engineer)]
Victory again. This will conclude today’s installment.
So where are we, exactly?
We’ve got Prolog code to solve the Einstein riddle. With a bit of careful generalization, we can mock NLP and parse brainzilla’s puzzles, and solve two “very hard” of those too. Performance, on the other hand, is debatable. Einstein is instant, but delightful dresses and blood donation take entire minutes.
So that gives us something to work with for next time. Don’t you love it when a plan comes together?
The code is available on my Github.