-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeSlot.cpp
246 lines (208 loc) · 7.63 KB
/
TimeSlot.cpp
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
#include "classes.hpp"
using namespace std;
TimeSlot::TimeSlot(
WeatherHandler* w,
LongTermTime* ltt,
ProjectHandler* p,
unsigned int index,
time_t start,
time_t end)
{
this->slotIndex = index;
this->startDate = start;
this->endDate = end;
// Weather association
this->weatherHandler = w;
this->updateWeatherCondition();
// Long Term Time association
this->longTermTime = ltt;
this->longTermTimeSlot = this->longTermTime->getTimeSlot(startDate);
this->availability = this->longTermTimeSlot->isAvailable(); //Initial availability
// Initialize execPriority and execSuccessProbability maps
list<SchedBlock*> sbs = this->longTermTimeSlot->getFeasibleSchedBlocks();
list<SchedBlock*>::iterator it;
for (it=sbs.begin(); it!=sbs.end(); it++) {
// Skip if not visible in this time interval
if (!(*it)->isVisible(this->startDate, this->endDate))
continue;
this->futureFeasibility.insert(make_pair(*it,calculateFutureFeasibility(*it)));
this->execSuccessProbability.insert(make_pair(*it,calculateSuccessProbability(*it)));
this->execPriority.insert(make_pair(*it,calculateExecPriority(*it)));
(*it)->getProject()->registerObserver(this);
}
}
TimeSlot::~TimeSlot()
{
// Unregister from observed Projects
map<SchedBlock*,double>::iterator it;
for (it=futureFeasibility.begin(); it!=futureFeasibility.end(); it++)
it->first->getProject()->unregisterObserver(this);
}
bool TimeSlot::isAvailable()
{
return this->availability;
}
void TimeSlot::setAvailability(bool availability)
{
// This can never be True if longTermTimeSlot->availability is False
this->availability = longTermTimeSlot->isAvailable() && availability;
}
map<SchedBlock*,double> TimeSlot::getPriorityList()
{
return this->execPriority;
}
//TODO: To make this request cheaper (now O(n)), consider a more efficient search algorithm.
// (same for all other linear searches)
SchedBlock* TimeSlot::getTopPriority()
{
// Return NULL if TimeSlot not available or priority list empty
if (!availability || execPriority.empty())
return (SchedBlock*)NULL;
// Search for the highest priority in execPriority map.
// If 2 priorities are equal (conflict), go for the highest SciGrade.
map<SchedBlock*, double>::iterator maxSb = execPriority.begin();
for(map<SchedBlock*, double>::iterator it = execPriority.begin(); it != execPriority.end(); ++it) {
if ( ((*it).second > (*maxSb).second) ||
((*it).second == (*maxSb).second &&
(*it).first->getProject()->getSciGrade() > (*maxSb).first->getProject()->getSciGrade()) )
maxSb = it;
}
// Return NULL if top priority is zero
if ((*maxSb).second == 0.0)
return (SchedBlock*)NULL;
cout << "Current top priority: " << (*maxSb).first->getProject()->getIndex()
<< "," << (*maxSb).first->getIndex()
<< " " << (*maxSb).second
<< " (SciGrade: " << (*maxSb).first->getProject()->getSciGrade() << ")"
<< endl;
return (*maxSb).first;
}
time_t TimeSlot::getStartDate()
{
return this->startDate;
}
time_t TimeSlot::getEndDate()
{
return this->endDate;
}
unsigned int TimeSlot::getIndex()
{
return this->slotIndex;
}
LongTermTimeSlot* TimeSlot::getLongTermTimeSlot()
{
return this->longTermTimeSlot;
}
void TimeSlot::updateWeatherCondition()
{
this->currentWeather = weatherHandler->getWeatherCondition(startDate, endDate);
this->futureWeather = weatherHandler->getWeatherCondition(endDate+1, endDate+(endDate-startDate));
}
double TimeSlot::calculateSuccessProbability(SchedBlock* sb)
{
//Calculate atmospheric data and Tsys
atmosphericData data = weatherHandler->getFrequencyParameters(sb->getObservationFrequency(), currentWeather.pwv);
double tsys = weatherHandler->getSystemTemperature(sb->getRA(), sb->getDec(), data);
//Calculate projected atmospheric data and Tsys
atmosphericData pdata = weatherHandler->getFrequencyParameters(sb->getObservationFrequency(), futureWeather.pwv);
double ptsys = weatherHandler->getSystemTemperature(sb->getRA(), sb->getDec(), pdata);
// Calculate Tsys variation
double tsysVariation = abs((ptsys - tsys) / tsys);
// cout << "** " << data.pwv << " " << data.frequency << " " << data.opacity << " " << data.temperature << endl;
// cout << "** " << pdata.pwv << " " << pdata.frequency << " " << pdata.opacity << " " << pdata.temperature << endl;
// cout << "** DELTA Tsys: " << tsysVariation << " (" << tsys << " / " << ptsys << ")" << endl;
// Calculate execution probability according to Tsys variation (stability)
// if tsysVariation >= 0.15 -> 0.0
// if tsysVariation = 0.0 -> 1.0
// else -> intermediate value
double probability = 0.0;
if (tsysVariation < 0.15)
probability = (0.15 - tsysVariation) / 0.15;
// cout << "** PROBABILITY: " << probability << endl;
return probability;
}
double TimeSlot::calculateFutureFeasibility(SchedBlock* sb)
{
return this->longTermTime->getFutureFeasibilities(sb, this->longTermTimeSlot->getIndex());
}
double TimeSlot::calculateExecPriority(SchedBlock* schedBlock)
{
// Get exec success probability
map<SchedBlock*,double>::iterator it;
double probability(0.0);
it = execSuccessProbability.find(schedBlock);
if (it != execSuccessProbability.end())
probability = it->second;
// Get future feasibility
double feasibility(0.0);
it = futureFeasibility.find(schedBlock);
if (it != futureFeasibility.end())
feasibility = it->second;
// Calculate priority
double priority = double(
schedBlock->getProject()->getSciGrade()
* schedBlock->isAvailable()
* probability
/ schedBlock->getProject()->getRemainingExec()
/ feasibility
);
// cout << ">> Priority: "
// << schedBlock->getProject()->getSciGrade() << " "
// << schedBlock->isAvailable() << " "
// << probability << " "
// << schedBlock->getProject()->getRemainingExec() << " "
// << feasibility << " "
// << endl;
return priority;
}
unsigned int TimeSlot::getSBListSize()
{
return this->execPriority.size();
}
void TimeSlot::notify(Observable* o)
{
// Notification by ShortTermTime
ShortTermTime* timeSlotSubject = dynamic_cast<ShortTermTime*>(o);
if (timeSlotSubject != NULL) {
// cout << "-- TimeSlot received ShortTermTime notification." << endl;
// Update weather condition
weatherCondition oldWeather = currentWeather;
this->updateWeatherCondition();
// Re-calculate success probabilities (only necessary if weather changed)
if (currentWeather.temperature != oldWeather.temperature
&& currentWeather.humidity != oldWeather.humidity
&& currentWeather.pwv != oldWeather.pwv) {
map<SchedBlock*,double>::iterator it;
for (it=execPriority.begin(); it!=execPriority.end(); it++) {
execSuccessProbability[it->first] = calculateSuccessProbability(it->first);
execPriority[it->first] = calculateExecPriority(it->first);
}
}
return;
}
// Notification by Project
Project* projectSubject = dynamic_cast<Project*>(o);
if (projectSubject != NULL) {
// cout << "-- TimeSlot received Project notification." << endl;
// Update SBs belonging to projectSubject in execPriority and execSuccessPriority lists
list<SchedBlock*> updatedSchedBlocks = projectSubject->getSchedBlocks();
list<SchedBlock*>::iterator it;
for (it=updatedSchedBlocks.begin(); it!=updatedSchedBlocks.end(); it++) {
map<SchedBlock*,double>::iterator espIt = execSuccessProbability.find(*it);
map<SchedBlock*,double>::iterator epIt = execPriority.find(*it);
// Skip to next SB if not in maps
if (espIt == execSuccessProbability.end() || epIt == execPriority.end())
continue;
// Remove from maps if executed
if ((*it)->isExecuted()) {
execPriority.erase(epIt);
execSuccessProbability.erase(espIt);
continue;
}
// Update priority
execPriority[*it] = calculateExecPriority(*it);
}
return;
}
cerr << "ERROR: TimeSlot received notification from unknown subject." << endl;
}