One way to add the constraint for the first assertation:
% 1. The English man lives in the red house.
% S: english --> red house <==> red house OR not english
% not S: not (red house OR not english) <==> not red house AND english
% ie. it is not so, that the english man doesn't live in the red house
:- not 1 { house(X, red, english, A, J, B) :
number(X) : animal(A) : job(J) : beverage(B) }.
and as another example the seventh assertation:
% 7. The green house is immediately right of the white one.
% (number_green = number_white + 1)
:- house(NG, green, _, _, _, _), house(NW, white, _, _, _, _), NG!=NW+1.
This will however lead to long solving times and large memory requirements (gigabytes), because the grounded program (output of gringo) is massive. You can see this with gringo -t yourcode.asp
. This is because the "do not care" variables _
(and X, A, J, B
in the constraint for the first assertation above). Each rule will be written in at least 5*5*5*5 ways.
M. Gebser adviced me that the predicates (relations) should be kept short. The problem with this encoding of the instance is that house/6
is so long. One way to combat this would be to encode it in the following style:
house(1..5).
elem(color, red;green;white;yellow;blue).
elem(nationality, english;spanish;japanese;italian;norwegian).
...
and start from there. Now the arity of elem is only 2. When the instance is defined this way, the program becomes simpler eg. the constraints for the assertations do not need aggregates (the 1{ ... }N
syntax).
:- not chosen(H, nationality, english), chosen(H, color, red).
M. Gebser also suggested that the solver (clasp) might benefit form the rule being written in the other way too:
:- not chosen(H, nationality, english), chosen(H, color, red).
:- chosen(H, nationality, english), not chosen(H, color, red).
You also need some additional restrictions, like that two different elements of the same type shouldn't be mapped to one house.
For nicer output, you can make a relation that gives the output like house/6
did.
Note that I used gringo3 and clasp2, which are not the newest versions. If you have the new clingo, there might be modifications that are needed.