I am trying to solve a CLP problem in SWI-Prolog. The task is very similar to the zebra problem. There are a total of 25 variables with the domains between 1 to 5. So the related variables will get the same tags. (The program is written in hungarian.)
I want the output to show not only the labels assigned to the variables, but the related variables in a table. Is there a way to do this?
% Constraint Logic Programming
:- use_module(library(clpfd)).
:- use_module(library(lists)).
% Your program goes here
egyetemista(X, All):-
All = [Egressy,Fenyvesi,Gallyas,Jeney,Vadkerti,
Edina, Frida, Gabriella, Jozsef, Vince,
Budapest,Debrecen,Miskolc,Pecs,Szeged,
Biologia,Informatika,Jog,Kemia,Magyar],
All ins 1..5,
all_different([Egressy,Fenyvesi,Gallyas,Jeney,Vadkerti]),
all_different([Edina, Frida, Gabriella, Jozsef, Vince]),
all_different([Budapest,Debrecen,Miskolc,Pecs,Szeged]),
all_different([Biologia,Informatika,Jog,Kemia,Magyar]),
Fenyvesi #= Jog,
Fenyvesi #\= Debrecen,
Fenyvesi #\= Jozsef,
Fenyvesi #\= Vince,
Jozsef #\= Gallyas,
Jozsef #\= Biologia,
Jozsef #= Budapest,
Vadkerti #= Gabriella,
Vadkerti #\= Kemia,
Vadkerti #\= Szeged,
Gabriella #\= Kemia,
Gabriella #\= Szeged,
Kemia #= Szeged,
Jeney #= Pecs,
Jeney #\= Vince,
Frida #= Magyar,
Edina #= Egressy #\ Edina #= Miskolc,
Informatika #\= Edina,
Informatika #\= Frida,
Informatika #\= Gabriella,
labeling([], All),
%Szak:
nth0(N, All, Szeged),
nth0(N, All, X).
%egyetemista(X, All)
If you run the program like this, the output is: All = [1, 2, 3, 4, 5, 2, 4, 5, 1, 3, 1, 5, 2, 4, 3, 5, 1, 2, 3, 4], X = 3
Which means that the label assigned to the variable 'Szeged' is 3, and the associated variables are obtained by substituting variables from the All list that also have a label of 3 on the output. So, for example, the first row of the table could be: 'gallyas vince szeged kemia'
Thank you so much in advance.
Prolog cannot map from a variable back to its name in the source code, so you need to keep a mapping yourself. A simple way to do this is like this:
egyetemista(X, AllPairs):-
AllPairs = [
egressy-Egressy, fenyvesi-Fenyvesi, gallyas-Gallyas, jeney-Jeney, vadkerti-Vadkerti,
edina-Edina, frida-Frida, gabriella-Gabriella, jozsef-Jozsef, vince-Vince,
budapest-Budapest, debrecen-Debrecen, miskolc-Miskolc, pecs-Pecs, szeged-Szeged,
biologia-Biologia, informatika-Informatika, jog-Jog, kemia-Kemia, magyar-Magyar
],
pairs_values(AllPairs, All),
All ins 1..5,
% ... the rest of your definition unchanged
The result visible in a call of this is a list of Name-Value
pairs:
?- egyetemista(X, All).
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] ;
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] ;
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] ;
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] . % etc.
You can then filter out only those entries that have their value equal to X
, and print those entries in whatever format you like.