Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 5.90 KB | None | 0 0
  1. :- use_module(library(clpfd)).
  2.  
  3. % ---------------------------------------------------------------------- %
  4. %                                                                        %
  5. %                        PREDICAT PRINCIPAL                              %
  6. %                                                                        %
  7. % ---------------------------------------------------------------------- %
  8.  
  9. go :- Lconvives = [manon,alice,juliette,lucie,charlotte,olivia,margaux,jules,hugo,tom,louis,paul,jean,antoine],
  10.       length([anne,martin|Lconvives],Nb),
  11.       placer_table(Lconvives,Nb).
  12.  
  13. placer_table(Lconvives,Nb) :-
  14.     length([anne,martin|Lconvives],Nb), % Renvoie dans Nb la taille de la liste
  15.     produire_lassoc([anne,martin|Lconvives],Nb,Lassoc), %lst, nb et tape tout dans Lassoc
  16.     alternance_cercles(Lassoc,Nb),
  17.     meme_hobby(Lassoc,Nb),
  18.     en_couple_pas_cote_a_cote(Lassoc,Nb),
  19.     pas_incompatibilite(Lassoc,Nb),
  20.     places_differentes(Lassoc),
  21.     label_places(Lassoc),
  22.     sort(2,@<,Lassoc,Lassoc_trie),
  23.     impression_table(Lassoc_trie).
  24.  
  25. % ---------------------------------------------------------------------- %
  26. %                                                                        %
  27. %                          LISTE ASSOCIATIVE                             %
  28. %                                                                        %
  29. % ---------------------------------------------------------------------- %
  30.  
  31. % produire_lassoc(L, Nb, Lassoc) : Renvoie la liste L reçu en entrée
  32. % ajouté du numéro Nb. La liste de sortie Lassoc.
  33. produire_lassoc(LPers, Nb, Lassoc) :-
  34.     numlist(1,Nb,LNb), %LNb = Nbr de 1 à 14 (sans les chefs!)
  35.     zip(LPers,LNb,Lassoc). % On zip les ppl avec leur num
  36.  
  37. zip([], [], []).
  38. zip([X|Xs], [Y|Ys], [(X,Y)|Zs]) :- zip(Xs,Ys,Zs).
  39.  
  40. getPers([], _, _).
  41. getPers(L, Nom, Place) :- member((Nom, Place), L).
  42.    
  43. % ---------------------------------------------------------------------- %
  44. %                                                                        %
  45. %                               CONTRAINTES                              %
  46. %                                                                        %
  47. % ---------------------------------------------------------------------- %
  48.    
  49. %alternance_cercle(Lassoc, Nb) : Reçoit Lassoc en entrée, qui représente la liste
  50. %des étudiants avec leur numéro et va analyser chaque élève afin de choisir un
  51. %placement de type PHILO-INFO-PHILO ou INFO-PHILO-INFO, mais pas INFO-INFO...
  52. % Deux membres d'un même cercle ne doivent pas être assis l'un à coté de l'autre !
  53. alternance_cercles(Lassoc, Nb) :-
  54.     getPers(Lassoc, N1, V1), %N = Nom de l'étudiant, V = Numéro de la place
  55.     getPers(Lassoc, N2, V2),
  56.     N1 \= N2,   % Si les noms sont les mêmes, problème, on backtrack !
  57.     V1 \= V2,   % Si les numéros sont les mêmes, problème, on backtrack !
  58.     cercle(N1, C1),
  59.     cercle(N2, C2),
  60.     (C1 = C2 -> mod(V2, Nb) #\= mod(V1, Nb) + 1; true),
  61.     (C1 = C2 -> mod(V2, Nb) #\= mod(V1, Nb) - 1; true).
  62.  
  63. % meme_hobby([L], Nb) : Recherche les personnes ayant un hobby commun
  64. % et renvoie true si c'est le cas !
  65. meme_hobby(Lassoc, Nb) :-  
  66.     getPers(Lassoc, N1, V1),
  67.     getPers(Lassoc, N2, V2),
  68.     N1 \= N2,
  69.     V1 \= V2,
  70.     hobby(N1, Lhobby1),
  71.     hobby(N2, Lhobby2),
  72.     member(Hobby, Lhobby1),
  73.     member(Hobby2, Lhobby2),
  74.     (Hobby \= Hobby2 -> mod(V2, Nb) #\= mod(V1, Nb) - 1 ; true), %Si les hobbys sont communs, alors, cela est bon !
  75.     (Hobby \= Hobby2 -> mod(V2, Nb) #\= mod(V1, Nb) + 1 ; true).
  76.  
  77. %en_couple_pas_cote_a_cote/2 : Recherche les personnes en couple et permet
  78. %de les séparer
  79. en_couple_pas_cote_a_cote(Lassoc, Nb) :-
  80.     getPers(Lassoc, N1, V1),
  81.     getPers(Lassoc, N2, V2),
  82.     en_couple(N1, N2),      
  83.     mod(V1, Nb) #\= mod(V2, Nb) - 1,
  84.     mod(V1, Nb) #\= mod(V1, Nb) + 1.
  85.  
  86. % pas_incompatibilite/2 : Recherche les personnes ayant des incompatibilités !
  87. % (s'ils ne s'aiment pas par exemple...)
  88. pas_incompatibilite(Lassoc, Nb) :-
  89.     getPers(Lassoc, N1, V1),
  90.     getPers(Lassoc, N2, V2),
  91.     incompatible(N1, N2),
  92.     mod(V1, Nb) #\= mod(V2, Nb) - 1,
  93.     mod(V1, Nb) #\= mod(V2, Nb) + 1.
  94.  
  95. % places_differentes/1
  96. places_differentes(Lassoc) :-
  97.     findall((N1, N2), (getPers(Lassoc, N1, _), getPers(Lassoc, N2, _), N1 \= N2), R),
  98.     getPers(R, N3, V3),
  99.     getPers(R, N4, V4),
  100.     N3 #\= N4,
  101.     V3 #\= V4.
  102.  
  103. %label_places
  104. label_places(Lassoc) :- labeling([ff], Lassoc).
  105.  
  106. % impression_table :
  107. impression_table([]) :- !, nl.
  108. impression_table([(P,V)|T]) :-
  109.     nl, format('~w ~d ~w ~a',['Place ',V, ' :', P]),
  110.     impression_table(T).
  111.  
  112. % ---------------------------------------------------------------------- %
  113. %                                                                        %
  114. %                            BASE DE DONNEES                             %
  115. %                                                                        %
  116. % ---------------------------------------------------------------------- %
  117.  
  118. incompatible(manon,louis).
  119. incompatible(charlotte,antoine).
  120. incompatible(margaux,hugo).
  121.  
  122. en_couple(anne,martin).
  123. en_couple(manon,jules).
  124. en_couple(juliette,tom).
  125. en_couple(charlotte,paul).
  126.  
  127. cercle(martin,info).
  128. cercle(jules,info).
  129. cercle(hugo,info).
  130. cercle(tom,info).
  131. cercle(louis,info).
  132. cercle(paul,info).
  133. cercle(jean,info).
  134. cercle(antoine,info).
  135. cercle(anne,philo).
  136. cercle(manon,philo).
  137. cercle(alice,philo).
  138. cercle(juliette,philo).
  139. cercle(lucie,philo).
  140. cercle(charlotte,philo).
  141. cercle(olivia,philo).
  142. cercle(margaux,philo).
  143.  
  144. hobby(anne,[sport,lecture,voyages]).
  145. hobby(manon,[lecture,voyages]).
  146. hobby(alice,[lecture,voyages]).
  147. hobby(juliette,[sport,voyages]).
  148. hobby(lucie,[lecture]).
  149. hobby(charlotte,[lecture]).
  150. hobby(olivia,[sport,lecture]).
  151. hobby(margaux,[sport]).
  152. hobby(martin,[sport,lecture,voyages]).
  153. hobby(jules,[sport,lecture]).
  154. hobby(hugo,[sport,lecture]).
  155. hobby(tom,[sport,voyages]).
  156. hobby(louis,[sport,lecture]).
  157. hobby(paul,[sport]).
  158. hobby(jean,[sport,lecture]).
  159. hobby(antoine,[sport,lecture]).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement