Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Prolog 斑马谜的年龄比较_Prolog_Zebra Puzzle_Eclipse Clp - Fatal编程技术网

Prolog 斑马谜的年龄比较

Prolog 斑马谜的年龄比较,prolog,zebra-puzzle,eclipse-clp,Prolog,Zebra Puzzle,Eclipse Clp,我正试图通过CLP用ECLiPSe Prolog解决一个类似于爱因斯坦之谜的逻辑之谜: 一个乐队有6名爵士音乐家,没有人年龄小于70岁。每个艺术家都写了一首不同的歌 使用的乐器有:位置1(乐队左外侧)的钢琴、位置2(位置1右侧)的长笛、位置3(位置2右侧)的鼓、位置4(位置3右侧)的低音提琴、位置5(位置4右侧)的萨克斯管和位置6的小号(束带的右外侧) 艺术家的名字是:安迪、科尼利厄斯、弗里茨、马库斯、皮特、沃尔特。 艺术家的姓氏是:布拉姆坎普、弗兰克、卡罗莱维茨、卢格、施吕特、魏德曼。 艺术家

我正试图通过CLP用ECLiPSe Prolog解决一个类似于爱因斯坦之谜的逻辑之谜:

一个乐队有6名爵士音乐家,没有人年龄小于70岁。每个艺术家都写了一首不同的歌

使用的乐器有:位置1(乐队左外侧)的钢琴、位置2(位置1右侧)的长笛、位置3(位置2右侧)的鼓、位置4(位置3右侧)的低音提琴、位置5(位置4右侧)的萨克斯管和位置6的小号(束带的右外侧)

艺术家的名字是:安迪、科尼利厄斯、弗里茨、马库斯、皮特、沃尔特。
艺术家的姓氏是:布拉姆坎普、弗兰克、卡罗莱维茨、卢格、施吕特、魏德曼。
艺术家的年龄是:76、77、78、79、80、82。
歌曲的标题是:Im百慕大德雷耶克、朱普斯·埃克布鲁斯、克罗斯肯·坦茨、勒根德·沃姆·伯格曼、莎莉的狗、瓦恩·艾克尔布鲁斯

线索1:钢琴演奏者比低音提琴演奏者至少大两岁。
线索2:吕格右边的一个位置是沃尔特。沃尔特也比萨利的狗作曲家更左边一个位置。
线索3:第五位的男子比皮特小一岁,比《百慕大群岛》的作曲家大一岁。
线索4:比这位77岁的艺术家更右边的一个位置是Wanne Eikel Blues的作曲家。
线索5:比弗里茨靠右的一个位置是弗兰克。弗兰克比莱根德·冯·伯格曼的作曲家至少年长3岁。这位作曲家比施吕特年长。
线索6:比科尼利厄斯靠右的一个位置是80岁的魏德曼。魏德曼也比卡罗莱维茨靠左的一个位置。他们中没有一个是克鲁斯肯·坦茨的作曲家。
线索7:朱普斯·埃克·布鲁斯的作曲家施吕特比马库斯靠右一个位置,施吕特比安迪靠左一个位置

多亏了,我对Prolog完全是新手,而且我能够完成大部分代码。目前我坚持线索4,我不知道如何将77岁艺术家的位置(1..6)与年龄列表(76..80:82)结合起来


由于,实现了工作解决方案并很好地展示了结果。诀窍是创建另一个包含所有年龄和给定职位范围的列表
AgePos

[...]
AgePos = [AP76, AP77, AP78, AP79, AP80, AP82],
AgePos :: 1..N,
[...]
然后我将此列表与
Age
链接

[...]
nth1(AP76, Age, Age_AP76),
Age_AP76 #= 76,
[...]
完整的代码,有一些调整

:- lib(ic).
:- lib(listut).

go :-
    
N = 6,
Range = 1..N,

Piano = 1,
Flute = 2,
Drums = 3,
DoubleBass = 4,
Saxophon = 5,
Trumpet = 6,
Instrument = [Piano, Flute, Drums, DoubleBass, Saxophon, Trumpet],
InstrumentS = ['piano', 'flute', 'drums', 'double bass', 'saxophon', 'trumpet'],

FirstName = [Andi, Cornelius, Fritz, Markus, Pete, Walter], 
FirstNameS = ['Andi', 'Cornelius', 'Fritz', 'Markus', 'Pete', 'Walter'],
FirstName :: Range,

LastName = [Bramkamp, Franke, Karolewicz, Lueg, Schlueter, Weidemann],
LastNameS = ['Bramkamp', 'Franke', 'Karolewicz', 'Lueg', 'Schlüter', 'Weidemann'],
LastName :: Range,

Song = [Bermudadreieck, Jupps, Kroesken, Legende, Sally, Wanne],
SongS = ['Im Bermudadreieck', 'Jupps-Eck-Blues', 'Krösken-Tanz', 
'Legende vom Bergmann', 'Sally`s Dog', 'Wanne-Eickel-Blues'],
Song :: Range,

AgePos = [AP76, AP77, AP78, AP79, AP80, AP82],
AgePos :: Range,

dim(Age, [N]),
Age :: [76, 77, 78, 79, 80, 82],
collection_to_list(Age, AgeList),

alldifferent(FirstName), 
alldifferent(LastName),
alldifferent(Age),
alldifferent(AgePos),
alldifferent(Song),

% Age Setting

nth1(AP76, AgeList, Age_AP76),
nth1(AP77, AgeList, Age_AP77),
nth1(AP78, AgeList, Age_AP78),
nth1(AP79, AgeList, Age_AP79),
nth1(AP80, AgeList, Age_AP80),
nth1(AP82, AgeList, Age_AP82),

Age_AP76 #= 76,
Age_AP77 #= 77,
Age_AP78 #= 78,
Age_AP79 #= 79,
Age_AP80 #= 80,
Age_AP82 #= 82,

% Clue 1 ---
% The man at the piano ist at least two years older than the double bass user.

nth1(Piano, AgeList, Age_Piano),
nth1(DoubleBass, AgeList, Age_DoubleBass),

Age_Piano #>= Age_DoubleBass + 2, 

% Clue 2 ---
% One position further to Lueg's right is Walter. 
% Walter is also one position further to the left than Sally's Dog composer.

Walter #= Lueg + 1, 
Walter #= Sally - 1, 

% Clue 3 ---
% The man on position 5 is one year younger than Pete 
% and one year older than the composer of Im Bermudadreieck.

nth1(Pete, AgeList, Age_Pete),
nth1(Bermudadreieck, AgeList, Age_Bermudadreieck),

Age_Saxophon #= Age_Pete - 1,
Age_Saxophon #= Age_Bermudadreieck + 1,

% Clue 4 ---
% One position further to the right than the 77 year old artist is 
% the composer of Wanne-Eickel-Blues.

Wanne #= AP77 + 1,

% Clue 5 ---
% One position further to right than Fritz is Franke. 
% Franke is at least 3 years older than the composer of Legende von Bergmann. 
% This composer is older than Schlüter.

nth1(Franke, AgeList, Age_Franke),
nth1(Legende, AgeList, Age_Legende),
nth1(Schlueter, AgeList, Age_Schlueter),

Franke #= Fritz + 1,
Franke #\= Legende,
Franke #\= Schlueter,
Age_Franke #>= Age_Legende + 3,
Age_Legende #> Age_Schlueter,

% Clue 6 ---
% One position further to the right than Cornelius is the 80 years old Weidemann. 
% Weidemann is also one position further to the left than Karolewicz. 
% None of them is the composer of Krösken-Tanz.

nth1(Weidemann, AgeList, Age_Weidemann),

Age_Weidemann #= 80,
Weidemann #= Cornelius + 1,
Weidemann #= Karolewicz - 1,
Weidemann #\= Kroesken,
Cornelius #\= Kroesken,
Karolewicz #\= Kroesken,

% Clue 7 ---
% Schlüter, composer of Jupps-Eck-Blues, is one position further to the right than Markus. 
% Schlüter is one position further to the left than Andi.

Schlueter #= Jupps,
Schlueter #= Markus + 1,
Schlueter #= Andi - 1,

term_variables([FirstName, LastName, Age, Song], Vars),

labeling(Vars),

write('\nPosition\t\tFirst Name\t\tLast Name\t\tAge\t\tSong\n'),
write('---------\t\t---------\t\t---------\t\t---------\t\t---------\n'),

( foreach(I, Instrument),
    foreach(IS, InstrumentS),
    param(FirstName, FirstNameS, LastName, LastNameS, AgeList, Song, SongS)
    do 
    
    printf('%w', [IS]),
    
    ( foreach(F, FirstName),
            foreach(FS, FirstNameS),
            param(I) do
            
            F == I ->
            printf('\t\t%w', [FS])
    ;
            true
    ),
    ( foreach(L, LastName),
            foreach(LS, LastNameS),
            param(I) do
            
            L == I ->
            printf('\t\t%w', [LS])
    ;
            true
    ),
    
    nth1(I, AgeList, A),
    printf('\t\t%w', [A]), 
    
    ( foreach(S, Song),
            foreach(SS, SongS),
            param(I) do
            
            S == I ->
            printf('\t\t%w\n', [SS])
    ;
            true
    )
).

如果将所有出现的
nth1
替换为
element
,您将看到显著的加速。原因是
nth1
是不确定的,而
element
使用约束传播。对
nth1
的多次调用会导致相当大的搜索树。@jschimpf谢谢您的建议,我会的看看这个
:- lib(ic).
:- lib(listut).

go :-
    
N = 6,
Range = 1..N,

Piano = 1,
Flute = 2,
Drums = 3,
DoubleBass = 4,
Saxophon = 5,
Trumpet = 6,
Instrument = [Piano, Flute, Drums, DoubleBass, Saxophon, Trumpet],
InstrumentS = ['piano', 'flute', 'drums', 'double bass', 'saxophon', 'trumpet'],

FirstName = [Andi, Cornelius, Fritz, Markus, Pete, Walter], 
FirstNameS = ['Andi', 'Cornelius', 'Fritz', 'Markus', 'Pete', 'Walter'],
FirstName :: Range,

LastName = [Bramkamp, Franke, Karolewicz, Lueg, Schlueter, Weidemann],
LastNameS = ['Bramkamp', 'Franke', 'Karolewicz', 'Lueg', 'Schlüter', 'Weidemann'],
LastName :: Range,

Song = [Bermudadreieck, Jupps, Kroesken, Legende, Sally, Wanne],
SongS = ['Im Bermudadreieck', 'Jupps-Eck-Blues', 'Krösken-Tanz', 
'Legende vom Bergmann', 'Sally`s Dog', 'Wanne-Eickel-Blues'],
Song :: Range,

AgePos = [AP76, AP77, AP78, AP79, AP80, AP82],
AgePos :: Range,

dim(Age, [N]),
Age :: [76, 77, 78, 79, 80, 82],
collection_to_list(Age, AgeList),

alldifferent(FirstName), 
alldifferent(LastName),
alldifferent(Age),
alldifferent(AgePos),
alldifferent(Song),

% Age Setting

nth1(AP76, AgeList, Age_AP76),
nth1(AP77, AgeList, Age_AP77),
nth1(AP78, AgeList, Age_AP78),
nth1(AP79, AgeList, Age_AP79),
nth1(AP80, AgeList, Age_AP80),
nth1(AP82, AgeList, Age_AP82),

Age_AP76 #= 76,
Age_AP77 #= 77,
Age_AP78 #= 78,
Age_AP79 #= 79,
Age_AP80 #= 80,
Age_AP82 #= 82,

% Clue 1 ---
% The man at the piano ist at least two years older than the double bass user.

nth1(Piano, AgeList, Age_Piano),
nth1(DoubleBass, AgeList, Age_DoubleBass),

Age_Piano #>= Age_DoubleBass + 2, 

% Clue 2 ---
% One position further to Lueg's right is Walter. 
% Walter is also one position further to the left than Sally's Dog composer.

Walter #= Lueg + 1, 
Walter #= Sally - 1, 

% Clue 3 ---
% The man on position 5 is one year younger than Pete 
% and one year older than the composer of Im Bermudadreieck.

nth1(Pete, AgeList, Age_Pete),
nth1(Bermudadreieck, AgeList, Age_Bermudadreieck),

Age_Saxophon #= Age_Pete - 1,
Age_Saxophon #= Age_Bermudadreieck + 1,

% Clue 4 ---
% One position further to the right than the 77 year old artist is 
% the composer of Wanne-Eickel-Blues.

Wanne #= AP77 + 1,

% Clue 5 ---
% One position further to right than Fritz is Franke. 
% Franke is at least 3 years older than the composer of Legende von Bergmann. 
% This composer is older than Schlüter.

nth1(Franke, AgeList, Age_Franke),
nth1(Legende, AgeList, Age_Legende),
nth1(Schlueter, AgeList, Age_Schlueter),

Franke #= Fritz + 1,
Franke #\= Legende,
Franke #\= Schlueter,
Age_Franke #>= Age_Legende + 3,
Age_Legende #> Age_Schlueter,

% Clue 6 ---
% One position further to the right than Cornelius is the 80 years old Weidemann. 
% Weidemann is also one position further to the left than Karolewicz. 
% None of them is the composer of Krösken-Tanz.

nth1(Weidemann, AgeList, Age_Weidemann),

Age_Weidemann #= 80,
Weidemann #= Cornelius + 1,
Weidemann #= Karolewicz - 1,
Weidemann #\= Kroesken,
Cornelius #\= Kroesken,
Karolewicz #\= Kroesken,

% Clue 7 ---
% Schlüter, composer of Jupps-Eck-Blues, is one position further to the right than Markus. 
% Schlüter is one position further to the left than Andi.

Schlueter #= Jupps,
Schlueter #= Markus + 1,
Schlueter #= Andi - 1,

term_variables([FirstName, LastName, Age, Song], Vars),

labeling(Vars),

write('\nPosition\t\tFirst Name\t\tLast Name\t\tAge\t\tSong\n'),
write('---------\t\t---------\t\t---------\t\t---------\t\t---------\n'),

( foreach(I, Instrument),
    foreach(IS, InstrumentS),
    param(FirstName, FirstNameS, LastName, LastNameS, AgeList, Song, SongS)
    do 
    
    printf('%w', [IS]),
    
    ( foreach(F, FirstName),
            foreach(FS, FirstNameS),
            param(I) do
            
            F == I ->
            printf('\t\t%w', [FS])
    ;
            true
    ),
    ( foreach(L, LastName),
            foreach(LS, LastNameS),
            param(I) do
            
            L == I ->
            printf('\t\t%w', [LS])
    ;
            true
    ),
    
    nth1(I, AgeList, A),
    printf('\t\t%w', [A]), 
    
    ( foreach(S, Song),
            foreach(SS, SongS),
            param(I) do
            
            S == I ->
            printf('\t\t%w\n', [SS])
    ;
            true
    )
).