-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmA.cpp
More file actions
355 lines (315 loc) · 13.8 KB
/
AlgorithmA.cpp
File metadata and controls
355 lines (315 loc) · 13.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/************************************************************************************************
* File: AlgorithmA.cpp
* Author: Abbey DuBois, Stephen Thomson
* Date: 3/14/2024
* Description: File for Algorithm A where functions are defined
*************************************************************************************************/
#include "AlgorithmA.h"
#include "TOR.h"
//Constructor takes the initial task set
//Will Sort the set, find lcm and create the TOR list
AlgorithmA::AlgorithmA() {
m_processors = 0;
//This is to add in all of tasks needed to hit the lcm time unit into m_set
m_set = vector<Task>();
m_gap = 0;
}
//Constructor takes the initial task set
//Will Sort the set, find lcm and create the TOR list
AlgorithmA::AlgorithmA(TaskSet taskSet) {
//This is to add in all of tasks needed to hit the lcm time unit into m_set
m_set = addInAdditionalTasks(taskSet.getTasks());
//Sorts m_set
sortTaskSetChronologically();
//Creates TOR list and assigns it to m_torList
createTORList();
}
//Basic psuedo code Implementation of Algorithm A
//1: Check if feasible through USI
//2: Need to sort tasks in task set chronologically d-c primary d as secondary
//3: Set the begining of the chains (T1..T2...T(USI)) and remove them from task set as they are placed
//4: In while loop where it's feasible and non empty
/*
Choose task where Computation of Task + Computation of Chain is less than Deadline of T
If chain exists
add task to chain
Remove from Task Set
Else
Choose chain where last in chain can swap with current task (Will be from list given from TOR)
If chain exists
Take out last in chain
Add in Task
Add in last in chain again
remove task from set
Else
Set is infeasible
*/
//5: Return Chains to main
string AlgorithmA::algorithmASchedule() {
bool feasible = true;
bool added = false;
m_gap = 0;
vector<int>chainComputation(m_processors);
vector<int>sortedProcessors(m_processors);
vector<vector<Task>> schedule(m_processors);
int computation;
int deadline;
int start;
int index;
//Set the beginning of the chains
//Erase the set tasks from m_set
for (int i = 0; i < m_processors; i++) {
schedule[i].push_back(m_set[0]);
chainComputation[i] = m_set[0].getComputationTime();
m_set.erase(m_set.begin());
}
//feasible and non empty
while (m_set.size() != 0 && feasible) {
computation = m_set[0].getComputationTime();
deadline = m_set[0].getHardDeadline();
start = m_set[0].getStartTime();
sortedProcessors = sortProcessors(chainComputation, start);
added = false;
for (int i = 0; i < m_processors && !added; i++) {
index = sortedProcessors[i];
//See if you can add the current task to the current chain without swapping
m_gap = 0;
//If the start time is after totalComputationTime calculate the gap needed
if (start > chainComputation[index])
{
m_gap = start - chainComputation[index];
}
//Can the task fit before it's deadline?
if (m_gap + chainComputation[index] + computation <= deadline) {
//If there is a gap add that into computation time and add gap to schedule
if (m_gap > 0) {
//Add gap to the scedule
schedule[index].push_back(Task("G", chainComputation[index], m_gap, 0, chainComputation[index] + m_gap, 0));
chainComputation[index] += m_gap;
}
//Add it's computation Time
chainComputation[index] += computation;
//Add Task to Chain
schedule[index].push_back(m_set[0]);
//Erase Task from set
m_set.erase(m_set.begin());
added = true;
}
//See if you can add the current Task to current chain with swapping
else if (inTORList(m_set[0], schedule[index].back(), chainComputation[index])) {
Task potentialGap = Task("", 0, 0, 0, 0, 0);
int size = schedule[index].size();
if (size - 2 >= 0) {
//Get the second to last task in the chain
potentialGap = schedule[index][schedule[index].size() - 2];
}
//Save last task
Task swappedTask = schedule[index].back();
//Delete task from chain
schedule[index].pop_back();
//Was the second to last a gap?
if (potentialGap.getName() == "G") {
//Is there a possibility of getting rid or shortening gap?
if (start < potentialGap.getHardDeadline()) {
//Can you get rid of gap completely?
if (start <= potentialGap.getStartTime()) {
//Delete gaps computation from the chain
chainComputation[index] -= schedule[index].back().getComputationTime();
//Delete Gap from chain
schedule[index].pop_back();
}
//Getting rid of gap partially
else {
//Getting the new gap between the start times of the tasks
m_gap = start - potentialGap.getStartTime();
//Delete old gaps computation from the chain
chainComputation[index] -= schedule[index].back().getComputationTime();
//Delete old gap from chain
schedule[index].pop_back();
//Add new gap to the scedule
schedule[index].push_back(Task("G", chainComputation[index], m_gap, 0, chainComputation[index] + m_gap, 0));
}
}
}
//Add current Task then saved task back into chain
schedule[index].push_back(m_set[0]);
schedule[index].push_back(swappedTask);
//Add current Tasks computation to chain
chainComputation[index] += computation;
//Erase Task from set
m_set.erase(m_set.begin());
added = true;
}
}
if (!added) {
feasible = false;
}
//Double checking that all's good and tasks meet deadline
for (int i = 0; i < m_processors; i++) {
if (chainComputation[i] > schedule[i].back().getHardDeadline()) {
feasible = false;
}
}
}
return configureSetToOutput(schedule, feasible);
}
//Checks if there is an opportunity to swap between the two tasks,
//If there is an opportunity, adds it to a list of swappable Tasks
void AlgorithmA::createTORList() {
//For TOR
//1: TOR is Empty Set
//2: For each task in the Task set (Passed in)
/*
If the Deadline of task - computation of task > 0
For each other task in the set (Comparing all other tasks to the one your looking at)
If the deadline of new task is less than the current time unit
If the computation time of original task + computation time of new task is less than or equal to deadline of original task
Add the available Swap to the TOR List
*/
int lowerEnd;
int higherEnd;
int C1;
int C2;
int D1;
int D2;
//Look through each task in the task set
for (int i = 0; i < m_set.size(); i++) {
C1 = m_set[i].getComputationTime();
D1 = m_set[i].getHardDeadline();
//Check if there is wiggle room between the computation and deadline of the set
if (D1 - C1 > 0) {
//Start comparing current task to all other tasks in the set
for (int j = i + 1; j < m_set.size() - 1; j++) {
C2 = m_set[j].getComputationTime();
D2 = m_set[j].getHardDeadline();
//Does the new task end faster than current? Does the current task not start after the new task?
if (D2 < D1 && m_set[i].getStartTime() < D2) {
//Making it one above the lower bounds so it is not equal to D2
lowerEnd = D2 - (C1 + C2) + 1;
higherEnd = D1 - (C1 + C2);
//Are the time + computations greater than deadline 2 and lessthan or equal to deadline 2?
if (lowerEnd + C1 + C2 > D2 && lowerEnd + C1 + C2 <= D1) {
if (higherEnd + C1 + C2 > D2 && higherEnd + C1 + C2 <= D1) {
m_torList.push_back(TOR(m_set[i], m_set[j], lowerEnd, higherEnd));
}
}
}
}
}
}
};
//Sorts the Tasks chronologically with a flagged bubble sort
void AlgorithmA::sortTaskSetChronologically() {
bool swapped = true;
int size = m_set.size() - 1;
for (int i = 0; i < size && swapped == true; i++) {
swapped = false;
for (int j = 0; j < size - i; j++) {
if (canSwap(m_set[j], m_set[j + 1])) {
swap(m_set[j], m_set[j + 1]);
swapped = true;
}
}
}
}
//Sorts the Lowest computation processors with a flagged bubble sort
vector<int> AlgorithmA::sortProcessors(vector<int> chainComputation, int start) {
vector<int>indexes;
vector<int>tempComputation = chainComputation;
int index = 0;
vector<int>::iterator iter;
while (tempComputation.size() > 0) {
index = 0;
//Find lowest element in array
for (int i = 1; i < tempComputation.size(); i++) {
index = tempComputation[index] < tempComputation[i] ? index : i;
}
//Find where element is in chaincompuation
iter = find(chainComputation.begin(), chainComputation.end(), tempComputation[index]);
//Push that index into indexes
indexes.push_back(iter - chainComputation.begin());
//Take out element in tempComputation
tempComputation.erase(tempComputation.begin() + index);
}
////Setting the initial indexes
//for (int i = 0; i < chainComputation.size(); i++) {
// indexes[i] = i;
//}
//for (int i = 0; i < chainComputation.size(); i++) {
// swapped = false;
// for (int j = i; j < chainComputation.size() - 1 && swapped != true; j++) {
// //How long is it until the task can start?
// diff1 = start - tempComputation[j] < 0 ? 0 : start - tempComputation[j];
// diff2 = start - tempComputation[j+1] < 0 ? 0 : start - tempComputation[j+1];
// //Swap if it takes longer for task to start on this processor
// //Else take the one with the lowest computation
// if (diff1 > diff2) {
// swap(indexes[j], indexes[j + 1]);
// swap(tempComputation[j], tempComputation[j + 1]);
// swapped = true;
// }
// else if (diff1 == diff2 && tempComputation[j] > tempComputation[j + 1]) {
// swap(indexes[j], indexes[j + 1]);
// swap(tempComputation[j], tempComputation[j + 1]);
// swapped = true;
// }
// }
//}
return indexes;
}
//Checks if two tasks can swap
//First checks between the tasks the deadline - computation for both (lowest goes first)
//Then checks between the tasks dedline
bool AlgorithmA::canSwap(Task task1, Task task2) {
bool swap = false;
int firstLaxity = task1.getHardDeadline() - task1.getComputationTime();
int secondLaxity = task2.getHardDeadline() - task2.getComputationTime();
if (task1.getStartTime() > task2.getStartTime()) {
swap = true;
}
else if (task1.getStartTime() == task2.getStartTime()) {
if (firstLaxity > secondLaxity) {
swap = true;
}
else if (firstLaxity == secondLaxity && task1.getHardDeadline() > task2.getHardDeadline()) {
swap = true;
}
}
return swap;
}
//Checks if the two tasks are in the TOR list and if they have availability to swap
//m_torList <current Task, new Task, time that they can swap>
bool AlgorithmA::inTORList(Task currentTask, Task lastTask, int chainComputation) {
//Potential start of the task (Does not take into consideration of gap)
int start = chainComputation - lastTask.getComputationTime();
bool canSwap = false;
string firstTask;
string secondTask;
int lowerEnd;
int higherEnd;
//For each element in the list check if both tasks are inside of the tuple and if they could swap due to the time unit
for (int i = 0; i < m_torList.size(); i++) {
firstTask = m_torList[i].getT1().getName();
secondTask = m_torList[i].getT2().getName();
lowerEnd = m_torList[i].getLowerEnd();
higherEnd = m_torList[i].getHigherEnd();
//Does the start time fall between the swapping range of the tasks?
if (start >= lowerEnd && start <= higherEnd) {
//Do the names line up with the tasks?
if (firstTask == currentTask.getName() && secondTask == lastTask.getName()) {
canSwap = true;
}
else if (secondTask == currentTask.getName() && firstTask == lastTask.getName()) {
canSwap = true;
}
}
}
return canSwap;
}
void AlgorithmA::setTaskSet(int index, TaskSet taskSet) {
m_set = addInAdditionalTasks(taskSet.getTasks());
sortTaskSetChronologically();
m_torList.clear();
//Creates TOR list and assigns it to m_torList
createTORList();
}