-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultipleRoomsScheduling.pl
336 lines (292 loc) · 9.99 KB
/
MultipleRoomsScheduling.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
:- consult('AssignOperations.pl').
:- dynamic agenda_operation_room/3.
assign :-
write('Please Insert a date for the operation rooms schedulling (format: AAAAMMDD): '),
read(Date),
assign_operations(Date, Result),
write('Surgeries distribution: '), nl,
write(Result), nl,
store_surgeries(Date, Result).
store_surgeries(_, []).
store_surgeries(Date, [(Surgery, Room)|Rest]) :-
(retract(agenda_operation_room(Room, Date, Surgeries)) ; Surgeries = []),
assert(agenda_operation_room(Room, Date, [Surgery|Surgeries])),
store_surgeries(Date, Rest).
% Definir o predicado surgeries/1
surgeries(NumSurgeries) :-
get_all_surgeries(Surgeries),
length(Surgeries, NumSurgeries).
% Show rooms and apply genetic algorithm
show_rooms_and_apply_genetic_algorithm(Date) :-
get_all_operation_rooms(Date, Rooms),
write('Available rooms: '), write(Rooms), nl,
write('Select the room numbers to apply the genetic algorithm (e.g., [sR1, sR2]): '),
read(SelectedRooms),
% Convert single room to list if necessary
(is_list(SelectedRooms) -> RoomsList = SelectedRooms ; RoomsList = [SelectedRooms]),
apply_genetic_algorithm_to_rooms(Date, RoomsList).
apply_genetic_algorithm_to_rooms(_, []).
apply_genetic_algorithm_to_rooms(Date, [Room|Rest]) :-
% Fetch surgeries for the room
agenda_operation_room(Room, Date, Surgeries),
% Only apply genetic algorithm if there are surgeries
(Surgeries \= [] ->
(write('Applying genetic algorithm to room '), write(Room),
write(' with surgeries: '), write(Surgeries), nl,
apply_genetic_algorithm(Surgeries))
;
(write('No surgeries found for room '), write(Room), nl)
),
% Continue with remaining rooms if any
(Rest \= [] -> apply_genetic_algorithm_to_rooms(Date, Rest) ; true).
% Genetic algorithm
% Parameters initialization
initialize :-
write('Number of new generations: '), read(NG),
(retract(generations(_)); true),
asserta(generations(NG)),
write('Population size: '), read(PS),
(retract(population(_)); true),
asserta(population(PS)),
write('Probability of crossover (%):'),
read(P1),
PC is P1 / 100,
(retract(prob_crossover(_)); true),
asserta(prob_crossover(PC)),
write('Probability of mutation (%):'),
read(P2),
PM is P2 / 100,
(retract(prob_mutation(_)); true),
asserta(prob_mutation(PM)),
write('Time limit (seconds):'), read(TL),
(retract(time_limit(_)); true),
asserta(time_limit(TL)),
write('Target evaluation value:'), read(TV),
(retract(target_value(_)); true),
asserta(target_value(TV)).
% Population generation
apply_genetic_algorithm(Surgeries) :-
initialize,
current_time(StartTime),
generate_population(Surgeries, Pop),
write('Pop='), write(Pop), nl,
evaluate_population(Pop, PopValue),
write('PopValue='), write(PopValue), nl,
order_population(PopValue, PopOrd),
generations(NG),
write('Generation 0:'), nl,
write(PopOrd), nl,
generate_generation(1, NG, PopOrd, StartTime).
generate_population(Surgeries, Pop) :-
population(PopSize),
length(Surgeries, NumSurgeries),
generate_population(PopSize, Surgeries, NumSurgeries, Pop).
generate_population(0, _, _, []) :- !.
generate_population(PopSize, Surgeries, NumSurgeries, [Ind|Rest]) :-
PopSize1 is PopSize - 1,
generate_population(PopSize1, Surgeries, NumSurgeries, Rest),
generate_individual(Surgeries, NumSurgeries, Ind),
\+ member(Ind, Rest).
generate_population(PopSize, Surgeries, NumSurgeries, Pop) :-
generate_population(PopSize, Surgeries, NumSurgeries, Pop).
% Individual generation
generate_individual([G], 1, [G]) :- !.
generate_individual(Surgeries, NumSurgeries, [G|Rest]) :-
NumTemp is NumSurgeries + 1,
random(1, NumTemp, N),
remove(N, Surgeries, G, NewList),
NumSurgeries1 is NumSurgeries - 1,
generate_individual(NewList, NumSurgeries1, Rest).
remove(1, [G|Rest], G, Rest).
remove(N, [G1|Rest], G, [G1|Rest1]) :-
N1 is N - 1,
remove(N1, Rest, G, Rest1).
% Population evaluation
evaluate_population([], []).
evaluate_population([Ind|Rest], [Ind*V|Rest1]) :-
evaluate(Ind, V),
evaluate_population(Rest, Rest1).
evaluate(Seq, V) :-
evaluate_priority(Seq, 0, 0, V).
% Evaluate priority
evaluate_priority([], _, V, V).
evaluate_priority([Surgery|Rest], LastPriority, Acc, V) :-
surgery_id(Surgery, Type),
operation_priority(Type, Priority),
(Priority >= LastPriority ->
NewAcc is Acc + Priority ;
NewAcc is Acc + Priority + 20),
evaluate_priority(Rest, Priority, NewAcc, V).
% Population sorting
order_population(PopValue, PopValueOrd) :-
bsort(PopValue, PopValueOrd).
bsort([X], [X]) :- !.
bsort([X|Xs], Ys) :-
bsort(Xs, Zs),
bchange([X|Zs], Ys).
bchange([X], [X]) :- !.
bchange([X*VX, Y*VY|L1], [Y*VY|L2]) :-
VX > VY, !,
bchange([X*VX|L1], L2).
bchange([X|L1], [X|L2]) :-
bchange(L1, L2).
% Generation evolution
generate_generation(G, G, Pop, _) :- !,
write('Generation '), write(G), write(':'), nl,
write(Pop), nl,
Pop = [BestInd|_],
write('Best individual in final generation: '),
write(BestInd), nl.
generate_generation(N, G, Pop, StartTime) :-
N < G,
current_time(CurrentTime),
time_limit(TL),
MaxTime is StartTime + TL,
(CurrentTime > MaxTime ->
(write('Time limit reached.'), nl, Pop = [BestInd|_], write('Best individual: '), write(BestInd), nl) ;
(write('Generation '), write(N), write(':'), nl,
write(Pop), nl,
select_population_varied(Pop, SelectedPop),
crossover_population(SelectedPop, NPop1),
mutation(NPop1, NPop),
evaluate_population(NPop, NPopValue),
order_population(NPopValue, NPopOrd),
elitism(Pop, NPopOrd, BestInd),
target_value(TV),
(BestInd = _*BestVal, BestVal =< TV ->
(write('Target evaluation reached.'), nl, write('Best individual: '), write(BestInd), nl) ;
(N1 is N + 1, generate_generation(N1, G, NPopOrd, StartTime))))).
% Crossover surgeries
generate_crossover_points(P1, P2) :-
generate_crossover_points1(P1, P2).
generate_crossover_points1(P1, P2) :-
surgeries(N),
NTemp is N + 1,
random(1, NTemp, P11),
random(1, NTemp, P21),
P11 \== P21, !,
((P11 < P21, !, P1 = P11, P2 = P21); P1 = P21, P2 = P11).
generate_crossover_points1(P1, P2) :-
generate_crossover_points1(P1, P2).
% random permutation of elements
crossover_population(Pop, NewPop) :-
random_permutation(Pop, PermutedPop),
crossover(PermutedPop, NewPop).
crossover([], []).
crossover([Ind*_], [Ind]).
crossover([Ind1*_, Ind2*_|Rest], [NInd1, NInd2|Rest1]) :-
generate_crossover_points(P1, P2),
prob_crossover(Pcruz),
random(0.0, 1.0, Pc),
((Pc =< Pcruz, !,
cross(Ind1, Ind2, P1, P2, NInd1),
cross(Ind2, Ind1, P1, P2, NInd2)) ;
(NInd1 = Ind1, NInd2 = Ind2)),
crossover(Rest, Rest1).
fillh([], []).
fillh([_|R1], [h|R2]) :-
fillh(R1, R2).
sublist(L1, I1, I2, L) :-
I1 < I2, !,
sublist1(L1, I1, I2, L).
sublist(L1, I1, I2, L) :-
sublist1(L1, I2, I1, L).
sublist1([X|R1], 1, 1, [X|H]) :- !,
fillh(R1, H).
sublist1([X|R1], 1, N2, [X|R2]) :- !,
N3 is N2 - 1,
sublist1(R1, 1, N3, R2).
sublist1([_|R1], N1, N2, [h|R2]) :-
N3 is N1 - 1,
N4 is N2 - 1,
sublist1(R1, N3, N4, R2).
rotate_right(L, K, L1) :-
surgeries(N),
T is N - K,
rr(T, L, L1).
rr(0, L, L) :- !.
rr(N, [X|R], R2) :-
N1 is N - 1,
append(R, [X], R1),
rr(N1, R1, R2).
remove([], _, []) :- !.
remove([X|R1], L, [X|R2]) :-
\+ member(X, L), !,
remove(R1, L, R2).
remove([_|R1], L, R2) :-
remove(R1, L, R2).
insert([], L, _, L) :- !.
insert([X|R], L, N, L2) :-
surgeries(T),
((N > T, !, N1 is N mod T); N1 = N),
insert1(X, N1, L, L1),
N2 is N + 1,
insert(R, L1, N2, L2).
insert1(X, 1, L, [X|L]) :- !.
insert1(X, N, [Y|L], [Y|L1]) :-
N1 is N - 1,
insert1(X, N1, L, L1).
cross(Ind1, Ind2, P1, P2, NInd11) :-
sublist(Ind1, P1, P2, Sub1),
surgeries(NumSurgeries),
R is NumSurgeries - P2,
rotate_right(Ind2, R, Ind21),
remove(Ind21, Sub1, Sub2),
P3 is P2 + 1,
insert(Sub2, Sub1, P3, NInd1),
removeh(NInd1, NInd11).
removeh([], []).
removeh([h|R1], R2) :- !,
removeh(R1, R2).
removeh([X|R1], [X|R2]) :-
removeh(R1, R2).
% Mutation surgeries
mutation([], []).
mutation([Ind|Rest], [NInd|Rest1]) :-
prob_mutation(Pmut),
random(0.0, 1.0, Pm),
((Pm < Pmut, !, mutacao1(Ind, NInd)); NInd = Ind),
mutation(Rest, Rest1).
mutacao1(Ind, NInd) :-
generate_crossover_points(P1, P2),
mutacao22(Ind, P1, P2, NInd).
mutacao22([G1|Ind], 1, P2, [G2|NInd]) :- !,
P21 is P2 - 1,
mutacao23(G1, P21, Ind, G2, NInd).
mutacao22([G|Ind], P1, P2, [G|NInd]) :-
P11 is P1 - 1,
P21 is P2 - 1,
mutacao22(Ind, P11, P21, NInd).
mutacao23(G1, 1, [G2|Ind], G2, [G1|Ind]) :- !.
mutacao23(G1, P, [G|Ind], G2, [G|NInd]) :-
P1 is P - 1,
mutacao23(G1, P1, Ind, G2, NInd).
% Elitism
elitism(Pop, NPop, BestInd) :-
Pop = [BestInd|_],
(member(BestInd, NPop) -> NPop1 = NPop ; NPop1 = [BestInd|NPop]),
length(NPop1, Len),
population(PopSize),
(Len > PopSize -> remove_last(NPop1, NPop) ; NPop = NPop1),
write('Best individual carried over: '),
write(BestInd), nl.
remove_last([_], []) :- !.
remove_last([H|T], [H|T1]) :-
remove_last(T, T1).
% Population selection
tournament_selection(Pop, Selected) :-
length(Pop, Len),
random(1, Len, Index1),
random(1, Len, Index2),
nth1(Index1, Pop, Ind1*Val1),
nth1(Index2, Pop, Ind2*Val2),
(Val1 =< Val2 -> Selected = Ind1*Val1 ; Selected = Ind2*Val2).
select_population_varied(Pop, SelectedPop) :-
population(PopSize),
findall(Selected,
(between(1, PopSize, _),
tournament_selection(Pop, Selected)),
SelectedPop).
% Current time
current_time(T) :-
get_time(T).