-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetterScheduling.pl
222 lines (193 loc) · 9.83 KB
/
BetterScheduling.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
:- dynamic availability/3.
:- dynamic agenda_staff/3.
:- dynamic agenda_staff1/3.
:- dynamic agenda_operation_room/3.
:- dynamic agenda_operation_room1/3.
:- dynamic better_sol/5.
:- consult('Data.pl').
agenda_operation_room1(Room, Date, Agenda) :- agenda_operation_room(Room, Date, Agenda).
% Free Agenda:
% Calcular slots livres para um staff
% Uso: free_agenda0(TerceiroParametroAgendaStaff, HorariosLivres).
free_agenda0([],[(0,1440)]).
free_agenda0([(0,Tfin,_)|LT],LT1):-!,free_agenda1([(0,Tfin,_)|LT],LT1).
free_agenda0([(Tin,Tfin,_)|LT],[(0,T1)|LT1]):- T1 is Tin-1, free_agenda1([(Tin,Tfin,_)|LT],LT1).
free_agenda1([(_,Tfin,_)],[(T1,1440)]):-Tfin\==1440,!,T1 is Tfin+1.
free_agenda1([(_,_,_)],[]).
free_agenda1([(_,T,_),(T1,Tfin2,_)|LT],LT1):-Tx is T+1,T1==Tx,!, free_agenda1([(T1,Tfin2,_)|LT],LT1).
free_agenda1([(_,Tfin1,_),(Tin2,Tfin2,_)|LT],[(T1,T2)|LT1]):-T1 is Tfin1+1,T2 is Tin2-1, free_agenda1([(Tin2,Tfin2,_)|LT],LT1).
% Adapt Timetable:
% Adaptar o timetable de um staff para um dado dia
% Uso: adapt_timetable(IDStaff, Data, ResultadoFreeAgenda, HorariosLivres).
adapt_timetable(D,Date,LFA,LFA2):-timetable(D,Date,(InTime,FinTime)),treatin(InTime,LFA,LFA1),treatfin(FinTime,LFA1,LFA2).
treatin(InTime,[(In,Fin)|LFA],[(In,Fin)|LFA]):-InTime=<In,!.
treatin(InTime,[(_,Fin)|LFA],LFA1):-InTime>Fin,!,treatin(InTime,LFA,LFA1).
treatin(InTime,[(_,Fin)|LFA],[(InTime,Fin)|LFA]).
treatin(_,[],[]).
treatfin(FinTime,[(In,Fin)|LFA],[(In,Fin)|LFA1]):-FinTime>=Fin,!,treatfin(FinTime,LFA,LFA1).
treatfin(FinTime,[(In,_)|_],[]):-FinTime=<In,!.
treatfin(FinTime,[(In,_)|_],[(In,FinTime)]).
treatfin(_,[],[]).
% availability/3: Calcula e armazena a disponibilidade de um staff em um determinado dia
% StaffID: O ID do staff para o qual a disponibilidade será calculada
% Date: A data para a qual a disponibilidade será verificada
% AvailableSlots: Os intervalos de tempo livres disponíveis após a adaptação do timetable e a remoção dos intervalos ocupados
availability(StaffID, Date, AvailableSlots) :-
agenda_staff(StaffID, Date, BusySlots),
free_agenda0(BusySlots, FreeSlots),
adapt_timetable(StaffID, Date, FreeSlots, AvailableSlots).
% Intersect All Agendas:
% StaffId's, Data Introduzida, Resultado
% Uso: Todos os IDS Staff, Data Escolhida
intersect_all_agendas([Name],Date,LA):-!,availability(Name,Date,LA).
intersect_all_agendas([Name|LNames],Date,LI):-
availability(Name,Date,LA),
intersect_all_agendas(LNames,Date,LI1),
intersect_2_agendas(LA,LI1,LI).
intersect_2_agendas([],_,[]).
intersect_2_agendas([D|LD],LA,LIT):- intersect_availability(D,LA,LI,LA1),
intersect_2_agendas(LD,LA1,LID),
append(LI,LID,LIT).
intersect_availability((_,_),[],[],[]).
intersect_availability((_,Fim),[(Ini1,Fim1)|LD],[],[(Ini1,Fim1)|LD]):- Fim<Ini1,!.
intersect_availability((Ini,Fim),[(_,Fim1)|LD],LI,LA):-
Ini>Fim1,!,
intersect_availability((Ini,Fim),LD,LI,LA).
intersect_availability((Ini,Fim),[(Ini1,Fim1)|LD],[(Imax,Fmin)],[(Fim,Fim1)|LD]):-
Fim1>Fim,!,
min_max(Ini,Ini1,_,Imax),
min_max(Fim,Fim1,Fmin,_).
intersect_availability((Ini,Fim),[(Ini1,Fim1)|LD],[(Imax,Fmin)|LI],LA):-
Fim>=Fim1,!,
min_max(Ini,Ini1,_,Imax),
min_max(Fim,Fim1,Fmin,_),
intersect_availability((Fim1,Fim),LD,LI,LA).
min_max(I,I1,I,I1):- I<I1,!.
min_max(I,I1,I1,I).
% Novo Predicado para calcular tempo total de uma surgery
sum_operation_times(OpType, TResult) :-
surgery(OpType, TAnesthesia, TSurgery, TCleaning),
TResult is TAnesthesia + TSurgery + TCleaning.
% Obter Staff a partir da Specialization
find_staff_by_specialization(OpCode, Specialization, LStaff) :-
surgery_id(OpCode, SurgeryType),
findall(StaffId, ( staff(StaffId, _, Specialization, Specializations), member(SurgeryType, Specializations) ), LStaff).
% Agendamento de surgeries
schedule_all_surgeries(Room,Day):-
retractall(agenda_staff1(_,_,_)),
retractall(agenda_operation_room1(_,_,_)),
retractall(availability(_,_,_)),
findall(_,(agenda_staff(D,Day,Agenda),assertz(agenda_staff1(D,Day,Agenda))),_),
agenda_operation_room(Or,Date,Agenda),assert(agenda_operation_room1(Or,Date,Agenda)),
findall(_,(agenda_staff1(D,Date,L),free_agenda0(L,LFA),adapt_timetable(D,Date,LFA,LFA2),assertz(availability(D,Date,LFA2))),_),
findall(OpCode,surgery_id(OpCode,_),LOpCode),
availability_all_surgeries(LOpCode,Room,Day),!.
% Calcular disponibilidade de todas as surgeries
availability_all_surgeries([],_,_).
availability_all_surgeries([OpCode|LOpCode],Room,Day):-
surgery_id(OpCode,OpType),
surgery(OpType, TAnesthesia, TSurgery, TCleaning),
sum_operation_times(OpType, TResult),
availability_operation(OpCode,Room,Day,LPossibilities,LDoctors),
schedule_first_interval(TResult,LPossibilities,(TinS,TfinS)),
retract(agenda_operation_room1(Room,Day,Agenda)),
insert_agenda((TinS,TfinS,OpCode),Agenda,Agenda1),
assertz(agenda_operation_room1(Room,Day,Agenda1)),
% Anaesthetists Time
find_staff_by_specialization(OpCode, anaesthetist, LAnesthesia),
TinAnesthesia is TinS,
TfinAnesthesia is TinS + TAnesthesia + TSurgery,
insert_agenda_doctors((TinAnesthesia, TfinAnesthesia, OpCode), Day, LAnesthesia),
% Orthopaedists Time
find_staff_by_specialization(OpCode, orthopaedist, LSurgery),
TinSurgery is TinS + TAnesthesia + 1,
TfinSurgery is TinSurgery + TSurgery,
insert_agenda_doctors((TinSurgery, TfinSurgery, OpCode), Day, LSurgery),
% Intrumenting and Circulating Times
find_staff_by_specialization(OpCode, instrumenting, LInstrumenting),
TinSurgery is TinS + TAnesthesia + 1,
TfinSurgery is TinSurgery + TSurgery,
insert_agenda_doctors((TinSurgery, TfinSurgery, OpCode), Day, LInstrumenting),
% Technicians Time
find_staff_by_specialization(OpCode, technician, LCleaning),
TinCleaning is TfinSurgery + 1,
TfinCleaning is TfinSurgery + TCleaning,
insert_agenda_doctors((TinCleaning, TfinCleaning, OpCode), Day, LCleaning),
availability_all_surgeries(LOpCode,Room,Day).
% Calcular disponibilidade para uma operação
availability_operation(OpCode,Room,Day,LPossibilities,LDoctors):-
surgery_id(OpCode,OpType),
findall(Doctor,assignment_surgery(OpCode,Doctor),LDoctors),
intersect_all_agendas(LDoctors,Day,LA),
agenda_operation_room1(Room,Day,LAgenda),
free_agenda0(LAgenda,LFAgRoom),
intersect_2_agendas(LA,LFAgRoom,LIntAgDoctorsRoom),
sum_operation_times(OpType, TResult),
remove_unf_intervals(TResult,LIntAgDoctorsRoom,LPossibilities).
% Remover intervalos não utilizáveis
remove_unf_intervals(_,[],[]).
remove_unf_intervals(TSurgery,[(Tin,Tfin)|LA],[(Tin,Tfin)|LA1]):-DT is Tfin-Tin+1,TSurgery=<DT,!,remove_unf_intervals(TSurgery,LA,LA1).
remove_unf_intervals(TSurgery,[_|LA],LA1):- remove_unf_intervals(TSurgery,LA,LA1).
% Schedule First Interval
schedule_first_interval(TSurgery,[(Tin,_)|_],(Tin,TfinS)):- TfinS is Tin + TSurgery - 1.
% Insert Agenda
insert_agenda((TinS,TfinS,OpCode),[],[(TinS,TfinS,OpCode)]).
insert_agenda((TinS,TfinS,OpCode),[(Tin,Tfin,OpCode1)|LA],[(TinS,TfinS,OpCode),(Tin,Tfin,OpCode1)|LA]):-TfinS<Tin,!.
insert_agenda((TinS,TfinS,OpCode),[(Tin,Tfin,OpCode1)|LA],[(Tin,Tfin,OpCode1)|LA1]):-insert_agenda((TinS,TfinS,OpCode),LA,LA1).
% Insert Agenda Doctors
insert_agenda_doctors(_,_,[]).
insert_agenda_doctors((TinS,TfinS,OpCode),Day,[Doctor|LDoctors]):-
retract(agenda_staff1(Doctor,Day,Agenda)),
insert_agenda((TinS,TfinS,OpCode),Agenda,Agenda1),
assert(agenda_staff1(Doctor,Day,Agenda1)),
insert_agenda_doctors((TinS,TfinS,OpCode),Day,LDoctors).
% RESULTADO, MELHOR SOLUÇÃO, US1
obtain_better_sol(Room,Day,AgOpRoomBetter,LAgDoctorsBetter,TFinOp):-
get_time(Ti),
(obtain_better_sol1(Room,Day);true),
retract(better_sol(Day,Room,AgOpRoomBetter,LAgDoctorsBetter,TFinOp)),
write('Final Result: AgOpRoomBetter='),write(AgOpRoomBetter),nl,
write('LAgDoctorsBetter='),write(LAgDoctorsBetter),nl,
write('TFinOp='),write(TFinOp),nl,
get_time(Tf),
T is Tf-Ti,
write('Tempo de geracao da solucao:'),write(T),nl.
% Procurar melhor solução
obtain_better_sol1(Room,Day):-
asserta(better_sol(Day,Room,_,_,1441)),
findall(OpCode,surgery_id(OpCode,_),LOC),!,
permutation(LOC,LOpCode),
retractall(agenda_staff1(_,_,_)),
retractall(agenda_operation_room1(_,_,_)),
retractall(availability(_,_,_)),
findall(_,(agenda_staff(D,Day,Agenda),assertz(agenda_staff1(D,Day,Agenda))),_),
agenda_operation_room(Room,Day,Agenda),assert(agenda_operation_room1(Room,Day,Agenda)),
findall(_,(agenda_staff1(D,Day,L),free_agenda0(L,LFA),adapt_timetable(D,Day,LFA,LFA2),assertz(availability(D,Day,LFA2))),_),
availability_all_surgeries(LOpCode,Room,Day),
agenda_operation_room1(Room,Day,AgendaR),
update_better_sol(Day,Room,AgendaR,LOpCode),
fail.
% Update for new better solution
update_better_sol(Day,Room,Agenda,LOpCode):-
better_sol(Day,Room,_,_,FinTime),
reverse(Agenda,AgendaR),
evaluate_final_time(AgendaR,LOpCode,FinTime1),
write('Analysing for LOpCode='),write(LOpCode),nl,
write('now: FinTime1='),write(FinTime1),write(' Agenda='),write(Agenda),nl,FinTime1<FinTime,
write('Best Solution Updated'),nl,
retract(better_sol(_,_,_,_,_)),
findall(Doctor,assignment_surgery(_,Doctor),LDoctors1),
remove_equals(LDoctors1,LDoctors),
list_doctors_agenda(Day,LDoctors,LDAgendas),
asserta(better_sol(Day,Room,Agenda,LDAgendas,FinTime1)).
% Verificar se a solução é a melhor
evaluate_final_time([],_,1441).
evaluate_final_time([(_,Tfin,OpCode)|_],LOpCode,Tfin):- member(OpCode,LOpCode),!.
evaluate_final_time([_|AgR],LOpCode,Tfin):- evaluate_final_time(AgR,LOpCode,Tfin).
% Listar médicos com agenda
list_doctors_agenda(_,[],[]).
list_doctors_agenda(Day,[D|LD],[(D,AgD)|LAgD]):-agenda_staff1(D,Day,AgD),list_doctors_agenda(Day,LD,LAgD).
% Remover elementos repetidos de uma lista
remove_equals([],[]).
remove_equals([X|L],L1):-member(X,L),!,remove_equals(L,L1).
remove_equals([X|L],[X|L1]):-remove_equals(L,L1).