Guest User

Untitled

a guest
Jan 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. :- use_module(library(lists)).
  2. :- use_module(library(clpfd)).
  3.  
  4. % P 4.a)
  5. table6(L) :-
  6. %% Indices in list L
  7. % Asdrubal=1, Bernardete=2, Cristalinda=3
  8. % Demetrio=4, Eleuterio=5, Felismina=6
  9. L = [Asdr, Bern, Crist, Demet, Eleut, Felism],
  10. domain(L, 1, 6),
  11. all_distinct(L),
  12.  
  13. Asdr #= 1, Bern #= 2,
  14. together6(Asdr, Bern, 1),
  15. together6(Crist, Demet, 1),
  16.  
  17. together6(Eleut, Felism, 0),
  18. together6(Asdr, Eleut, 0),
  19.  
  20. labeling([], L).
  21.  
  22. together6(I1, I2, Bool) :-
  23. ((I1 #= 1 #/\ I2 #= 6) #\/
  24. (I1 #= I2 + 1) #\/
  25. (I1 #= I2 - 1)) #<=> Bool.
  26. %% table6(L).
  27.  
  28.  
  29. % P 4.b)
  30. squareTable(Ppl, N, Together, Apart) :-
  31.  
  32. %% Validating Input
  33. Res is N mod 2, Res = 0, %% N is even
  34. length(Ppl, N),
  35. length(Together, LenT),
  36. LenT > 0,
  37.  
  38. %% Domains
  39. domain(Ppl, 1, N),
  40.  
  41. %% Restrictions
  42. all_distinct(Ppl),
  43. apply_together_restrictions(N, Together, 1),
  44. apply_together_restrictions(N, Apart, 0),
  45.  
  46. %% Avoid Symmetries
  47. element(2, Ppl, Second),
  48. element(N, Ppl, Last),
  49. Second #< Last,
  50. %% (horizontal axis)
  51.  
  52. TableHeadIdx is (N // 2) + 1,
  53. element(TableHeadIdx, Ppl, TableHead),
  54. element(1, Ppl, First),
  55. First #< TableHead,
  56. %% (vertical axis)
  57.  
  58. %% Solution
  59. labeling([], Ppl).
  60.  
  61. apply_together_restrictions(_, [], _).
  62. apply_together_restrictions(N, [P1-P2 | Together], Bool) :-
  63. together(N, P1, P2, Bool),
  64. apply_together_restrictions(N, Together, Bool).
  65.  
  66. together(N, P1, P2, Bool) :-
  67. (
  68. (P1 #= P2 - 1 #\/ P1 #= P2 + 1) #\/
  69. (P1 #= 1 #/\ P2 #= N) #\/
  70. (P2 #= 1 #/\ P1 #= N) #\/
  71. ((P1 - 2 #= N - P2) #/\ /** on opposite sides of the table **/
  72. (P1 #< P2)) #\/ /** and not on the head of the table **/
  73. ((P2 - 2 #= N - P1) #/\
  74. (P2 #< P1))
  75. ) #<=> Bool.
  76.  
  77.  
  78. %% squareTable([A,B,C,D,E,F,G,H], 8, [A-B, C-D, E-F, G-H], [A-C, B-C, A-E, D-G, D-H, E-G, E-H]).
  79.  
  80. %% All solutions:
  81. %% squareTable([A,B,C,D,E,F,G,H], 8, [A-B, C-D, E-F, G-H], [A-C, B-C, A-E, D-G, D-H, E-G, E-H]), L=[A,B,C,D,E,F,G,H], write(L), nl, fail.
Add Comment
Please, Sign In to add comment