Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 7.62 KB | None | 0 0
  1. :- use_module(library(clpfd)).
  2.  
  3.  
  4. % ---------------------------------------------------------------------- %
  5. %                                                                        %
  6. %                        PREDICAT PRINCIPAL                              %
  7. %                                                                        %
  8. % ---------------------------------------------------------------------- %
  9.    
  10. go :- Lconvives = [  manon, alice, juliette, lucie, charlotte, olivia, margaux,
  11.                      jules, hugo, tom, louis, paul, jean ,antoine ],
  12.  
  13.       table(Lconvives).
  14.  
  15.  
  16. table(Lconvives) :-
  17.  
  18.     length([anne,martin|Lconvives],Nb),
  19.     produire_lassoc([anne,martin|Lconvives],Lv,Nb,Lassoc),
  20.  
  21.     alternance_cercles(Lassoc,Nb),
  22.     meme_hobby(Lassoc,Nb),
  23.     en_couple_pas_cote_a_cote(Lassoc,Nb),
  24.     pas_incompatibilite(Lassoc,Nb),
  25.     places_differentes(Lv),
  26.     label_places(Lv),
  27.  
  28.     sort(2,@<,Lassoc,Lassoc_trie),
  29.     impression_table(Lassoc_trie).
  30.  
  31.  
  32.  
  33.  
  34. % ---------------------------------------------------------------------- %
  35. %                                                                        %
  36. %                          LISTE ASSOCIATIVE                             %
  37. %                                                                        %
  38. % ---------------------------------------------------------------------- %
  39.    
  40. produire_lassoc([] , [] , _ , [] ).
  41. produire_lassoc([anne  |T],[Var|Lv],Nb,[pers_var(anne  ,Var)|Sol]) :-
  42.                                     Var #= 1 ,
  43.                                     produire_lassoc(T,Lv,Nb,Sol),!.
  44. produire_lassoc([martin|T],[Var|Lv],Nb,[pers_var(martin,Var)|Sol]) :-
  45.                                     Check is Nb mod 4 ,
  46.                                     Check == 0 ,
  47.                                     Q is div(Nb,2),
  48.                                     Var #= Q  ,
  49.                                     produire_lassoc(T,Lv,Nb,Sol),!.
  50. produire_lassoc([martin|T],[Var|Lv],Nb,[pers_var(martin,Var)|Sol]) :-
  51.                                     Q is div(Nb,2),
  52.                                     Var #= Q+1,
  53.                                     produire_lassoc(T,Lv,Nb,Sol),!.
  54. produire_lassoc([H     |T],[Var|Lv],Nb,[pers_var(H,Var)     |Sol]) :-
  55.                                     Var in 2..Nb ,
  56.                                     produire_lassoc(T,Lv,Nb,Sol).
  57.  
  58.  
  59.    
  60. % ---------------------------------------------------------------------- %
  61. %                                                                        %
  62. %                               CONTRAINTES                              %
  63. %                                                                        %
  64. % ---------------------------------------------------------------------- %
  65.  
  66. addconstraint(Nb,pers_var(_,Var1)-pers_var(_,Var2)):-
  67.                                             Var1 #\= (Var2 mod Nb) + 1 ,
  68.                                             Var2 #\= (Var1 mod Nb) + 1.
  69.  
  70. %var can't be Odd
  71. notOddConstraint(_,0).
  72. notOddConstraint(Var,NB):- Var #\= NB-1,NB2 is NB - 2 , notOddConstraint(Var,NB2).
  73.  
  74. notEvenConstraint(_,0).
  75. notEvenConstraint(Var,NB):- Var #\= NB, NB2 is NB - 2 , notEvenConstraint(Var,NB2).
  76.  
  77. % Alternance cercles
  78. % ------------------
  79.  
  80. alternance_cercles([],_).
  81. alternance_cercles([pers_var(Nom,Var)|T],Nb):- cercle(Nom,info ) , notOddConstraint(Var,Nb) ,alternance_cercles(T,Nb).
  82. alternance_cercles([pers_var(Nom,Var)|T],Nb):- cercle(Nom,philo) , notEvenConstraint(Var,Nb),alternance_cercles(T,Nb).
  83.  
  84. % Meme hobby
  85. % ----------
  86.    
  87. meme_hobby( Lassoc,Nb) :- bagof(pers_var(P1,Var1)-pers_var(P2,Var2) ,
  88.                                             C1^C2^(member(pers_var(P1,Var1),Lassoc),
  89.                                             member(pers_var(P2,Var2),Lassoc), P1\=P2,
  90.                                             hobby(P1,C1) , hobby(P2,C2) ,
  91.                                             intersection(C1,C2,[])
  92.                                             ),L),
  93.                                       maplist(addconstraint(Nb),L).
  94.                                      
  95.  
  96.  
  97. % En couple pas cote a cote
  98. % -------------------------
  99.    
  100. en_couple_pas_cote_a_cote( Lassoc,Nb  ) :- bagof(pers_var(P1,Var1)-pers_var(P2,Var2) ,
  101.                                                 (member(pers_var(P1,Var1),Lassoc),
  102.                                                 member(pers_var(P2,Var2),Lassoc), P1\=P2,
  103.                                                 en_couple(P1,P2)
  104.                                                 ),L),
  105.                                       maplist(addconstraint(Nb),L).
  106.  
  107.  
  108.                                                
  109.  
  110. % Pas d incompatibilite
  111. % ---------------------    
  112.  
  113. pas_incompatibilite( Lassoc,Nb) :-   bagof(pers_var(P1,Var1)-pers_var(P2,Var2) ,
  114.                                                 (member(pers_var(P1,Var1),Lassoc),
  115.                                                 member(pers_var(P2,Var2),Lassoc), P1\=P2,
  116.                                                 incompatible(P1,P2)
  117.                                                 ),L),
  118.                                       maplist(addconstraint(Nb),L).
  119.  
  120.    
  121.    
  122. % Personnes a des places differentes
  123. % ----------------------------------
  124.    
  125. places_differentes( Vs ):-  all_distinct(Vs).
  126.  
  127.        
  128.  
  129.  
  130. % ---------------------------------------------------------------------- %
  131. %                                                                        %
  132. %                                LABELING                                %
  133. %                                                                        %
  134. % ---------------------------------------------------------------------- %
  135.  
  136.    
  137. label_places(Lv ) :-  label(Lv).
  138.        
  139.    
  140.  
  141. % ---------------------------------------------------------------------- %
  142. %                                                                        %
  143. %                                IMPRESSION                              %
  144. %                                                                        %
  145. % ---------------------------------------------------------------------- %
  146.    
  147. impression_table([]) :- !, nl.
  148. impression_table([pers_var(P,V)|T]) :-
  149.     nl, format('~w ~d ~w ~a',['Place ',V, ' :', P]),
  150.     impression_table(T).
  151.  
  152.    
  153.  
  154. % ---------------------------------------------------------------------- %
  155. %                                                                        %
  156. %                            BASE DE DONNEES                             %
  157. %                                                                        %
  158. % ---------------------------------------------------------------------- %
  159.  
  160. en_couple(anne,martin).
  161. en_couple(manon,jules).
  162. en_couple(juliette,tom).
  163. en_couple(charlotte,paul).
  164.  
  165. cercle(anne,philo).
  166. cercle(manon,philo).
  167. cercle(alice,philo).
  168. cercle(juliette,philo).
  169. cercle(lucie,philo).
  170. cercle(charlotte,philo).
  171. cercle(olivia,philo).
  172. cercle(margaux,philo).
  173.  
  174. cercle(martin,info).
  175. cercle(jules,info).
  176. cercle(hugo,info).
  177. cercle(tom,info).
  178. cercle(louis,info).
  179. cercle(paul,info).
  180. cercle(jean,info).
  181. cercle(antoine,info).
  182.  
  183. hobby(anne,[sport,lecture,voyages]).
  184. hobby(manon,[lecture,voyages]).
  185. hobby(alice,[lecture,voyages]).
  186. hobby(juliette,[sport,voyages]).
  187. hobby(lucie,[lecture]).
  188. hobby(charlotte,[lecture]).
  189. hobby(olivia,[sport,lecture]).
  190. hobby(margaux,[sport]).
  191.    
  192. hobby(martin,[sport,lecture,voyages]).
  193. hobby(jules,[sport,lecture]).
  194. hobby(hugo,[sport,lecture]).
  195. hobby(tom,[sport,voyages]).
  196. hobby(louis,[sport,lecture]).
  197. hobby(paul,[sport]).
  198. hobby(jean,[sport,lecture]).
  199. hobby(antoine,[sport,lecture]).
  200.  
  201. incompatible(manon,louis).
  202. incompatible(charlotte,antoine).
  203. incompatible(margaux,hugo).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement