Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1.  
  2. :-dynamic geracoes/1.
  3. :-dynamic populacao/1.
  4. :-dynamic prob_cruzamento/1.
  5. :-dynamic prob_mutacao/1.
  6. :-dynamic seleciona_ind2/1.
  7. :-dynamic seleciona_pos/3.
  8.  
  9.  
  10. % tarefa(Id,TempoProcessamento,TempConc,PesoPenalizacao).
  11. tarefa(t1,2,5,1).
  12. tarefa(t2,4,7,6).
  13. tarefa(t3,1,11,2).
  14. tarefa(t4,3,9,3).
  15. tarefa(t5,3,8,2).
  16.  
  17. % tarefas(NTarefas).
  18. tarefas(5).
  19.  
  20. % parameteriza��o
  21. inicializa:-write('Numero de novas Geracoes: '),read(NG), (retract(geracoes(_));true), asserta(geracoes(NG)),
  22. write('Dimensao da Populacao: '),read(DP),
  23. (retract(populacao(_));true), asserta(populacao(DP)),
  24. write('Probabilidade de Cruzamento (%):'), read(P1),
  25. PC is P1/100,
  26. (retract(prob_cruzamento(_));true), asserta(prob_cruzamento(PC)),
  27. write('Probabilidade de Mutacao (%):'), read(P2),
  28. PM is P2/100,
  29. (retract(prob_mutacao(_));true), asserta(prob_mutacao(PM)).
  30.  
  31.  
  32. gera:-
  33. inicializa,
  34. gera_populacao(Pop),
  35. write('Pop='),write(Pop),nl,
  36. avalia_populacao(Pop,PopAv),
  37. write('PopAv='),write(PopAv),nl,
  38. ordena_populacao(PopAv,PopOrd),
  39. geracoes(NG),
  40. gera_geracao(0,NG,PopOrd).
  41.  
  42. gera_populacao(Pop):-
  43. populacao(TamPop),
  44. tarefas(NumT),
  45. findall(Tarefa,tarefa(Tarefa,_,_,_),ListaTarefas),
  46. gera_populacao(TamPop,ListaTarefas,NumT,Pop).
  47.  
  48. gera_populacao(0,_,_,[]):-!.
  49.  
  50. gera_populacao(TamPop,ListaTarefas,NumT,[Ind|Resto]):-
  51. TamPop1 is TamPop-1,
  52. gera_populacao(TamPop1,ListaTarefas,NumT,Resto),
  53. gera_individuo(ListaTarefas,NumT,Ind),
  54. not(member(Ind,Resto)).
  55. gera_populacao(TamPop,ListaTarefas,NumT,L):-
  56. gera_populacao(TamPop,ListaTarefas,NumT,L).
  57.  
  58. gera_individuo([G],1,[G]):-!.
  59.  
  60. gera_individuo(ListaTarefas,NumT,[G|Resto]):-
  61. NumTemp is NumT + 1, % To use with random
  62. random(1,NumTemp,N),
  63. retira(N,ListaTarefas,G,NovaLista),
  64. NumT1 is NumT-1,
  65. gera_individuo(NovaLista,NumT1,Resto).
  66.  
  67. retira(1,[G|Resto],G,Resto).
  68. retira(N,[G1|Resto],G,[G1|Resto1]):-
  69. N1 is N-1,
  70. retira(N1,Resto,G,Resto1).
  71.  
  72. avalia_populacao([],[]).
  73. avalia_populacao([Ind|Resto],[Ind*V|Resto1]):-
  74. avalia(Ind,V),
  75. avalia_populacao(Resto,Resto1).
  76.  
  77. avalia(Seq,V):-
  78. avalia(Seq,0,V).
  79.  
  80. avalia([],_,0).
  81. avalia([T|Resto],Inst,V):-
  82. tarefa(T,Dur,Prazo,Pen),
  83. InstFim is Inst+Dur,
  84. avalia(Resto,InstFim,VResto),
  85. (
  86. (InstFim =< Prazo,!, VT is 0)
  87. ;
  88. (VT is (InstFim-Prazo)*Pen)
  89. ),
  90. V is VT+VResto.
  91.  
  92. ordena_populacao(PopAv,PopAvOrd):-
  93. bsort(PopAv,PopAvOrd).
  94.  
  95. bsort([X],[X]):-!.
  96. bsort([X|Xs],Ys):-
  97. bsort(Xs,Zs),
  98. btroca([X|Zs],Ys).
  99.  
  100.  
  101. btroca([X],[X]):-!.
  102.  
  103. btroca([X*VX,Y*VY|L1],[Y*VY|L2]):-
  104. VX>VY,!,
  105. btroca([X*VX|L1],L2).
  106.  
  107. btroca([X|L1],[X|L2]):-btroca(L1,L2).
  108.  
  109.  
  110. gera_geracao(G,G,Pop):-!,
  111. write('Geracao '), write(G), write(':'), nl, write(Pop), nl.
  112.  
  113. gera_geracao(N,G,Pop):-
  114. write('Geracao '), write(N), write(':'), nl, write(Pop), nl,
  115. cruzamentos(Pop,[],NPop1),
  116. mutacao(NPop1,NPop),
  117. avalia_populacao(NPop,NPopAv),
  118. ordena_populacao(NPopAv,NPopOrd),
  119. efetua_torneios(NPopOrd,[],NFinal),
  120. N1 is N+1,
  121. gera_geracao(N1,G,NFinal).
  122.  
  123. gerar_pontos_cruzamento(P1,P2):-
  124. gerar_pontos_cruzamento1(P1,P2).
  125.  
  126. gerar_pontos_cruzamento1(P1,P2):-
  127. tarefas(N),
  128. NTemp is N+1,
  129. random(1,NTemp,P11),
  130. random(1,NTemp,P21),
  131. P11\==P21,!,
  132. ((P11<P21,!,P1=P11,P2=P21);(P1=P21,P2=P11)).
  133. gerar_pontos_cruzamento1(P1,P2):-
  134. gerar_pontos_cruzamento1(P1,P2).
  135.  
  136. %Efetua os Cruzamentos
  137.  
  138. cruzamentos([],Result,Result).
  139. cruzamentos([_*_|[]],Result,Result).
  140. cruzamentos([Ind1*_|Rest],Results,Result):-
  141. cruzamento([Ind1*_|Rest],Results2),
  142. append(Results,Results2,FinalResults),
  143. cruzamentos(Rest,FinalResults,Result).
  144.  
  145. cruzamento([],[]).
  146. cruzamento([Ind*_],[Ind]).
  147. cruzamento([Ind1*_|Resto],[NInd1,NInd2|Resto1]):-
  148. seleciona_ind2(Resto,Ind2,NovoResto),
  149. gerar_pontos_cruzamento(P1,P2),
  150. prob_cruzamento(Pcruz),random(0.0,1.0,Pc),
  151. ((Pc =< Pcruz,!,
  152. cruzar(Ind1,Ind2,P1,P2,NInd1),
  153. cruzar(Ind2,Ind1,P1,P2,NInd2))
  154. ;
  155. (NInd1=Ind1,NInd2=Ind2)),
  156. cruzamento(NovoResto,Resto1).
  157.  
  158. seleciona_ind2([Ind*_|[]],Ind,[]).
  159. seleciona_ind2(Resto,Ind2,NovoResto):-
  160. length(Resto,Num),
  161. random(1,Num,Pos),
  162. seleciona_pos(Resto,Pos,Ind2),
  163. % delete(Resto,Ind2,NovoResto).
  164. delete(Resto,Ind2*_,NovoResto).
  165.  
  166.  
  167. seleciona_pos([Ind1*_|_], 0, Ind1).
  168. seleciona_pos([_|Resto], Pos, Ind2):-
  169. Pos1 is Pos - 1,
  170. seleciona_pos(Resto, Pos1, Ind2).
  171.  
  172.  
  173. preencheh([],[]).
  174.  
  175. preencheh([_|R1],[h|R2]):-
  176. preencheh(R1,R2).
  177.  
  178.  
  179. sublista(L1,I1,I2,L):-
  180. I1 < I2,!,
  181. sublista1(L1,I1,I2,L).
  182.  
  183. sublista(L1,I1,I2,L):-
  184. sublista1(L1,I2,I1,L).
  185.  
  186. sublista1([X|R1],1,1,[X|H]):-!,
  187. preencheh(R1,H).
  188.  
  189. sublista1([X|R1],1,N2,[X|R2]):-!,
  190. N3 is N2 - 1,
  191. sublista1(R1,1,N3,R2).
  192.  
  193. sublista1([_|R1],N1,N2,[h|R2]):-
  194. N3 is N1 - 1,
  195. N4 is N2 - 1,
  196. sublista1(R1,N3,N4,R2).
  197.  
  198. rotate_right(L,K,L1):-
  199. tarefas(N),
  200. T is N - K,
  201. rr(T,L,L1).
  202.  
  203. rr(0,L,L):-!.
  204.  
  205. rr(N,[X|R],R2):-
  206. N1 is N - 1,
  207. append(R,[X],R1),
  208. rr(N1,R1,R2).
  209.  
  210.  
  211. elimina([],_,[]):-!.
  212.  
  213. elimina([X|R1],L,[X|R2]):-
  214. not(member(X,L)),!,
  215. elimina(R1,L,R2).
  216.  
  217. elimina([_|R1],L,R2):-
  218. elimina(R1,L,R2).
  219.  
  220. insere([],L,_,L):-!.
  221. insere([X|R],L,N,L2):-
  222. tarefas(T),
  223. ((N>T,!,N1 is N mod T);N1 = N),
  224. insere1(X,N1,L,L1),
  225. N2 is N + 1,
  226. insere(R,L1,N2,L2).
  227.  
  228.  
  229. insere1(X,1,L,[X|L]):-!.
  230. insere1(X,N,[Y|L],[Y|L1]):-
  231. N1 is N-1,
  232. insere1(X,N1,L,L1).
  233.  
  234. cruzar(Ind1,Ind2,P1,P2,NInd11):-
  235. sublista(Ind1,P1,P2,Sub1),
  236. tarefas(NumT),
  237. R is NumT-P2,
  238. rotate_right(Ind2,R,Ind21),
  239. elimina(Ind21,Sub1,Sub2),
  240. P3 is P2 + 1,
  241. insere(Sub2,Sub1,P3,NInd1),
  242. eliminah(NInd1,NInd11).
  243.  
  244.  
  245. eliminah([],[]).
  246.  
  247. eliminah([h|R1],R2):-!,
  248. eliminah(R1,R2).
  249.  
  250. eliminah([X|R1],[X|R2]):-
  251. eliminah(R1,R2).
  252.  
  253. %Efetua as mutações
  254.  
  255. mutacao([],[]).
  256. mutacao([Ind|Rest],[NInd|Rest1]):-
  257. prob_mutacao(Pmut),
  258. random(0.0,1.0,Pm),
  259. ((Pm < Pmut,!,mutacao1(Ind,NInd));NInd = Ind),
  260. mutacao(Rest,Rest1).
  261.  
  262. mutacao1(Ind,NInd):-
  263. gerar_pontos_cruzamento(P1,P2),
  264. mutacao22(Ind,P1,P2,NInd).
  265.  
  266. mutacao22([G1|Ind],1,P2,[G2|NInd]):-
  267. !, P21 is P2-1,
  268. mutacao23(G1,P21,Ind,G2,NInd).
  269. mutacao22([G|Ind],P1,P2,[G|NInd]):-
  270. P11 is P1-1, P21 is P2-1,
  271. mutacao22(Ind,P11,P21,NInd).
  272.  
  273. mutacao23(G1,1,[G2|Ind],G2,[G1|Ind]):-!.
  274. mutacao23(G1,P,[G|Ind],G2,[G|NInd]):-
  275. P1 is P-1,
  276. mutacao23(G1,P1,Ind,G2,NInd).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement