Scheduler.py
11.8 KB
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
from operator import attrgetter
from .UserManager import UserManager
from .Interval import *
SIMULATION = False
class Scheduler(IntervalManagement):
REJECTED_ROOM = "Insufficient room for this sequence"
def __init__(self):
super().__init__("Scheduler", "Scheduler")
self.schedule = Schedule.objects.create()
self.sequences = []
def log(self, message: str):
self.logger.info(message)
def getNightLimits(self) -> tuple:
start = getNightStart()
end = getNightEnd()
return (start, end)
def setNightLimits(self, plan_start: float, plan_end: float) -> int:
self.schedule.plan_start = Decimal(plan_start)
self.schedule.plan_end = Decimal(plan_end)
self.log("Schedule plan start -> " + str(plan_start))
self.log("Schedule plan end -> " + str(plan_end))
return 0
def isFirstSchedule(self) -> bool:
return False
def copyFromPrevious(self) -> int:
if not Schedule.objects.exists():
self.schedule.plan_night_start = self.schedule.plan_start
return 1
try:
previous_sched = Schedule.objects.order_by('-created')[1]
previous_exc_seq = previous_sched.sequences.filter(status=Sequence.EXECUTED)
except:
self.schedule.plan_night_start = self.schedule.plan_start
self.debug("Scheduler could not get informations from previous schedule")
return 1
for seq in previous_exc_seq:
shs = seq.shs
shs.pk = None
shs.schedule = self.schedule
shs.save()
self.schedule.plan_night_start = previous_sched.plan_night_start
self.schedule.plan_end = previous_sched.plan_end
self.schedule.plan_start = Decimal(secondsToJulianDate(getCurrentTime())) + self.max_overhead
return 0
def simulateSchedule(self, sequences) -> tuple:
global SIMULATION
SIMULATION = True
self.schedule.plan_night_start = self.schedule.plan_start
self.sequences = list(sequences)
shs_list = []
for sequence in self.sequences:
shs_list.append(ScheduleHasSequences(sequence=sequence, schedule=self.schedule))
self.sequences = [(sequence, shs_list[index]) for index, sequence in enumerate(self.sequences)]
self.computeSchedule()
return (self.schedule, self.sequences)
def computeSchedule(self) -> int:
interval = Interval(self.schedule.plan_start, self.schedule.plan_end)
self.intervals.append(interval)
self.log("Interval created : " + str(interval.__dict__))
self.removeInvalidSequences()
self.determinePriorities()
self.removeNonEligible()
self.sortSequences()
self.placeSequences()
return 0
'''
JB: Using : list(self.sequences) makes a copy.
'''
def removeNonEligible(self) -> int:
for sequence, shs in list(self.sequences):
overlap = Decimal(min(self.schedule.plan_end, sequence.jd2)) - Decimal(max(self.schedule.plan_start, sequence.jd1)) - self.max_overhead
if overlap < sequence.duration:
if sequence.jd1 < self.schedule.plan_start:
sequence.status = Sequence.UNPLANNABLE
if not SIMULATION:
sequence.save()
self.sequences.remove((sequence, shs))
self.log("Removing non eligible sequence")
return 0
def removeInvalidSequences(self):
for sequence,shs in list(self.sequences):
if (sequence.jd1 < 0 or sequence.jd2 < 0 or
is_nearby_less_or_equal(sequence.duration, Decimal(0)) or
sequence.jd2 - sequence.jd1 < sequence.duration):
self.log("Removing sequence in removeInvalidSequences")
self.sequences.remove((sequence, shs))
sequence.status = Sequence.INVALID
if not SIMULATION:
sequence.save()
return 0
def updateOtherDeltas(self, sequence: Sequence, shs: ScheduleHasSequences, interval: Interval) -> int:
seq_before, shs_b_i = self.findSequenceBefore(interval)
seq_after, shs_a_i = self.findSequenceAfter(interval)
if seq_before and shs_b_i:
shs_b_i.deltaTR = min(shs.tsp - self.max_overhead, seq_before.jd2) - shs_b_i.tep
if seq_after and shs_a_i:
shs_a_i.deltaTL = shs_a_i.tsp - self.max_overhead - max(shs.tep, seq_after.jd1)
return 0
def placeSequences(self) -> int:
for sequence, shs in list(self.sequences):
quota = UserManager.determineQuota(sequence)
if not UserManager.isSufficient(quota, sequence):
shs.status = Sequence.REJECTED
shs.desc = UserManager.REJECTED
continue
matching_intervals = self.getMatchingIntervals(sequence)
if len(matching_intervals) > 0:
inter = self.placeSequence(sequence, shs, matching_intervals)
if inter:
self.updateOtherDeltas(sequence, shs, inter)
sequence_placed = True
else:
sequence_placed = self.tryShiftingSequences(sequence, shs)
if sequence_placed:
shs.status = Sequence.PENDING
self.decreaseQuota(sequence, sequence.duration)
else:
self.log("Removing sequence in place_sequences")
shs.status = Sequence.REJECTED
shs.desc = self.REJECTED_ROOM
return 0
def findSequenceBefore(self, interval: Interval):
for seq, s in self.sequences:
if s.status == Sequence.PENDING:
if is_nearby_equal(s.tep, interval.start):
return (seq, s)
return (None, None)
def findSequenceAfter(self, interval: Interval):
for seq, s in self.sequences:
if s.status == Sequence.PENDING:
if is_nearby_equal(s.tsp - self.max_overhead, interval.end):
return (seq, s)
return (None, None)
'''
pm(l/r) = Possible Move (Left / Right)
shs_(b/a)_i = shs (before/after) interval
'''
def tryShiftingSequences(self, sequence: Sequence, shs: ScheduleHasSequences) -> bool:
potential = self.getPotentialIntervals(sequence)
potential.sort(key=attrgetter("duration"), reverse=True)
pml = Decimal(0)
pmr = Decimal(0)
for interval in potential:
seq_before, shs_b_i = self.findSequenceBefore(interval)
seq_after, shs_a_i = self.findSequenceAfter(interval)
available = min(interval.end, sequence.jd2) - max(interval.start, sequence.jd1)
missing = sequence.duration - available + self.max_overhead
if seq_before and shs_b_i:
pml = min(shs_b_i.deltaTL, interval.start - sequence.jd1)
if seq_after and shs_a_i:
pmr = min(shs_a_i.deltaTR, sequence.jd2 - interval.end)
if is_nearby_sup_or_equal(pml, missing):
self.moveSequence(seq_before, shs_b_i, missing, "LEFT")
elif is_nearby_sup_or_equal(pmr, missing):
self.moveSequence(seq_after, shs_a_i, missing, "RIGHT")
elif is_nearby_sup_or_equal(pml + pmr, missing):
self.moveSequence(seq_before, shs_b_i, pml, "LEFT")
self.moveSequence(seq_after, shs_a_i, missing - pml, "RIGHT")
else:
continue
matching = self.getMatchingIntervals(sequence)
if len(matching) != 1:
raise ValueError("There should be one and only one matching interval after shifting")
inter = self.placeSequence(sequence, shs, matching)
if inter:
self.updateOtherDeltas(sequence, shs, inter)
return True
return False
def moveSequence(self, sequence: Sequence, shs: ScheduleHasSequences, time_shift: Decimal, direction: str) -> int:
interval_before = None
interval_after = None
if direction not in ["LEFT", "RIGHT"]:
return 1
if time_shift > (shs.deltaTL if direction == "LEFT" else shs.deltaTR):
return 1
for interval in self.intervals:
if is_nearby_equal(interval.end, shs.tsp - self.max_overhead):
interval_before = interval
elif is_nearby_equal(interval.start, shs.tep):
interval_after = interval
if direction == "LEFT":
interval_before.end -= time_shift
if interval_after:
interval_after.start -= time_shift
shs.tsp -= time_shift
shs.tep -= time_shift
shs.deltaTL -= time_shift
shs.deltaTR += time_shift
else:
if interval_before:
interval_before.end += time_shift
interval_after.start += time_shift
shs.tsp += time_shift
shs.tep += time_shift
shs.deltaTL += time_shift
shs.deltaTR -= time_shift
if interval_after:
if is_nearby_less_or_equal(interval_after.duration, self.max_overhead):
self.intervals.remove(interval_after)
if interval_before:
if is_nearby_less_or_equal(interval_before.duration, self.max_overhead):
self.intervals.remove(interval_before)
return 0
'''
Sort by jd2 and priority -> (main sorting value)
'''
def sortSequences(self) -> int:
self.sequences.sort(key=lambda x: x[0].jd2)
self.sequences.sort(key=lambda x: x[0].priority)
return 0
def determinePriorities(self) -> int:
return 0
def decreaseQuota(self, sequence: Sequence, quota: float) -> int:
user = UserManager(sequence.request.pyros_user)
if SIMULATION:
return 0
return user.decreaseQuota(Decimal(quota))
def makeSchedule(self) -> Schedule:
global SIMULATION
SIMULATION = False
if self.isFirstSchedule():
self.schedule.plan_night_start = self.schedule.plan_start
else:
if self.copyFromPrevious():
self.schedule.plan_night_start = self.schedule.plan_start
self.sequences = list(Sequence.objects.filter(status=Sequence.OBSERVABLE))
self.sequences = [(sequence, ScheduleHasSequences(sequence=sequence, schedule=self.schedule))
for sequence in self.sequences]
self.log("There is : " + str(len(self.sequences)) + " sequences")
self.computeSchedule()
self.saveSchedule()
self.log("Saving schedule with " + str(len(self.schedule.sequences.all())) + " sequences")
return self.schedule
def saveSchedule(self) -> int:
if self.isEmptySchedule():
self.log("Schedule is empty")
return 1
self.schedule.save()
for sequence, shs in self.sequences:
shs.schedule = self.schedule
shs.save()
self.logSchedule()
return 0
def isEmptySchedule(self) -> bool:
if len(self.sequences) == 0:
return True
return False
def logSchedule(self) -> int:
sequences = Sequence.objects.filter(shs__status=Sequence.PENDING).order_by('shs__tsp')
self.log("There are %d sequence(s) planned" % len(sequences))
for sequence in sequences:
self.log("--> name: %r, start: %d, end: %d, duration: %d, deltaTL: %d, deltaTR: %d"
% (sequence.name, sequence.shs.get().tsp, sequence.shs.get().tep, sequence.duration, sequence.shs.get().deltaTL, sequence.shs.get().deltaTR))
self.log("There are %d free intervals" % len(self.intervals))
for interval in self.intervals:
self.log("--> start: %d, end: %d" % (interval.start, interval.end))
return 0