斑马斑纹的年龄比较



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

一个乐队有6位爵士音乐家,没有一位年龄小于70岁。每个艺术家都创作了不同的歌曲。

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

艺术家的名字是:安迪,科尼利厄斯,弗里茨,马库斯,皮特,沃尔特
艺术家的姓氏是:Bramkamp、Franke、Karolewicz、Lueg、Schlüter、Weidemann
艺术家的年龄分别为:76岁、77岁、78岁、79岁、80岁、82岁。
歌曲名称分别为:Im Bermudadreieck、Jupps Eck Blues、Krösken Tanz、Legend vom Bergmann、Sally‘s Dog、Wanne Eickel Blues。

线索1:钢琴演奏者比低音提琴演奏者大至少两年
线索2:在Lueg的右边的一个位置是WalterWalter也是比Sally‘s Dog作曲家更左边的一个位置
线索3:5号位的男子比Pete小一岁,比Im Bermudadreieck的作曲家大一岁
线索4:比77岁的艺术家靠右的一个位置是Wanne Eickel Blues的作曲家
线索5:比Fritz靠右的一个位置是Franke<弗兰克比传奇冯·伯格曼的作曲家大至少3岁。这位作曲家的年龄比施吕特大>
线索6:比Cornelius靠右的一个位置是80岁的WeidemannWeidemann也是比Karolewicz更靠左的一个位置。他们都不是Krösken Tanz的作曲家
Clue 7:SchlüterJupps-Eck Blues的作曲家,比Markus靠右一位Schlüter比Andi更靠左一个位置。

我对Prolog完全陌生,这要归功于http://www.hakank.org/bprolog/和http://www.hakank.org/bprolog/a_round_of_golf.pl我能够完成大部分代码。目前我停留在线索4,我不知道如何将这位77岁艺术家的位置(1..6(与年龄列表(76..80:82(相结合。

go :-

N = 6,

Piano = 1,
Flute = 2,
Drums = 3,
DoubleBass = 4,
Saxophon = 5,
Trumpet = 6,
Instrument = [Piano, Flute, Drums, DoubleBass, Saxophon, Trumpet],
InstrumentS = ['Piano', 'Flute', 'Drums', 'DoubleBass', 'Saxophon', 'Trumpet'],

FirstName = [Andi, Cornelius, Fritz, Markus, Pete, Walter], 
FirstNameS = ['Andi', 'Cornelius', 'Fritz', 'Markus', 'Pete', 'Walter'],
FirstName :: 1..N,

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

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 :: 1..N,

length(Age ,N),
Age :: [76, 77, 78, 79, 80, 82],
Age = [Age_Piano, Age_Flute, Age_Drums, Age_DoubleBass, Age_Saxophon, Age_Trumpet],
alldifferent(FirstName), 
alldifferent(LastName),
alldifferent(Age),
alldifferent(Song),
% Clue 1 ---
% The man at the piano ist at least two years older than the double bass user.
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.
element(Pete, Age, Age_Pete),
element(Bermudadreieck, Age, Age_Bermudadreieck),

Pete #= 5, 
Bermudadreieck #= 5,
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.
element(Wanne, Age, Age_Wanne),

Age_Wanne #= 77, 

% 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.

element(Franke, Age, Age_Franke),
element(Legende, Age, Age_Legende),
element(Schlueter, Age, 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.

element(Weidemann, Age, 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).

由于http://www.hakank.org/eclipse/.诀窍是创建另一个具有所有年龄和给定位置范围[1..N]的列表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('nPositionttFirst NamettLast NamettAgettSongn'),
write('---------tt---------tt---------tt---------tt---------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('tt%w', [FS])
;
true
),
( foreach(L, LastName),
foreach(LS, LastNameS),
param(I) do

L == I ->
printf('tt%w', [LS])
;
true
),

nth1(I, AgeList, A),
printf('tt%w', [A]), 

( foreach(S, Song),
foreach(SS, SongS),
param(I) do

S == I ->
printf('tt%wn', [SS])
;
true
)
).

相关内容

  • 没有找到相关文章

最新更新