Guest User

Untitled

a guest
Sep 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. % Constraints satisfaction problem from Albert Einstein's Puzzle
  2. % Author : Feng Di @K.U.Leuven
  3.  
  4. % Please run this program by ECLiPSe
  5. % ECLiPSe is a constraint processing tool using Prolog
  6.  
  7. :-lib(ic).
  8.  
  9. einstein_problem:-
  10. % I use 5 list of 5 variables
  11. Nat = [English,Swedes,Danish,Norwegian,German],
  12. Color = [Red,White,Green,Yellow,Blue],
  13. Pat = [Dog,Bird,Cat,Horse,Fish],
  14. Drink = [Tea,Coffee,Milk,Beer,Water],
  15. Cigarette = [PallMall,Dunhill,Blends,BlueMaster,Prince],
  16.  
  17. %domain of variables
  18. Nat :: 1..5,
  19. Color :: 1..5,
  20. Pat :: 1..5,
  21. Drink :: 1..5,
  22. Cigarette :: 1..5,
  23.  
  24. %constraints
  25. alldifferent(Nat),
  26. alldifferent(Color),
  27. alldifferent(Pat),
  28. alldifferent(Drink),
  29. alldifferent(Cigarette),
  30.  
  31. English $= Red,
  32. Swedes $= Dog,
  33. Danish $= Tea,
  34. White #= Green+1,
  35. Green $= Coffee,
  36. PallMall $= Bird,
  37. Yellow $= Dunhill,
  38. Milk $= 3,
  39. Norwegian $= 1,
  40. abs(Blends-Cat) $= 1,
  41. abs(Horse-Dunhill) $= 1,
  42. BlueMaster $= Beer,
  43. German $= Prince,
  44. abs(Norwegian-Blue) $= 1,
  45. abs(Blends-Water) $=1,
  46.  
  47. %search
  48. flatten([Nat,Color,Pat,Drink,Cigarette],List),
  49. labeling(List),
  50.  
  51. %print solution
  52. NatNames = [English-english,Swedes-swedes,Danish-danish,Norwegian-norwegian,German-german],
  53. memberchk(Fish-FishNat,NatNames),
  54. write('The '),write(FishNat),write(' owns fish.'),nl.
Add Comment
Please, Sign In to add comment