This repository was archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTask.h
More file actions
319 lines (249 loc) · 8.41 KB
/
Copy pathTask.h
File metadata and controls
319 lines (249 loc) · 8.41 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
/****************************************************************************
* *
* File: Task.h *
* *
* Author: Branch Vincent *
* *
* Date: Jun 6, 2016 *
* *
* Purpose: This file defines the Task class. *
* *
****************************************************************************/
#ifndef TASK_H
#define TASK_H
#include <iostream>
#include <string>
#include <random>
#include <cmath>
#include <algorithm>
#include "Parameters/Parameters.h"
using namespace std;
using namespace params;
/****************************************************************************
* *
* Definition of Task class *
* *
****************************************************************************/
class Task
{
// Public member functions
public:
// Constructor
Task(int tp, float prevArrTime, int phase);
// Inspectors
// int getType() const {return type;}
const int& getType() {return type;}
const int& getPriority() {return priority;}
const float& getArrTime() {return arrTime;}
const float& getSerTime() {return serTime;}
const float& getDepTime() {return depTime;}
const float& getExpTime() {return expTime;}
const float& getBegTime() {return begTime;}
const float& getQueTime() {return queTime;}
const float& getSerLeft() {return serLeft;}
// const float getPercLeft() {return serLeft/serTime;}
const vector<int> getOpNums() {return opNums;}
// Mutators
void setArrTime(float t) {arrTime = t;}
void setSerTime(float t) {serTime = t;}
void setDepTime(float t) {depTime = t;}
void setSerLeft(float t) {serLeft = t;}
void setBegTime(float t) {begTime = t;}
void setQueTime(float t) {queTime = t;}
// Other member functions
void output(ostream& out) const
{cout << "(Pr " << priority << ", Tp " << type << ", Arr " << arrTime;
cout << ", Ser " << serTime << ", Dep " << depTime << ", Exp " << expTime << ")";}
// Private member functions
private:
// Used by constructor
float genArrTime(float prevArrTime, int phase);
float genSerTime();
float genExpTime(int phase);
float genRandNum(char distType, int seed, float arg1, float arg2 = 0);
// Data members
private:
int type; // type
int priority; // priority level
float arrTime; // arrival time (min)
float serTime; // service time (min)
float depTime; // depature time (min)
float expTime; // expiration time (min)
float begTime; // begin service time (min)
float queTime; // enter queue time (min)
float serLeft; // service time left
vector<int> opNums; // operator id number
};
// Operators
ostream& operator<<(ostream& out, const Task& t) {t.output(out); return out;}
/****************************************************************************
* *
* Function: Task *
* *
* Purpose: To construct a task of the specified type using the *
* specified previous arrival time and distribution seed *
* *
****************************************************************************/
Task::Task(int tp, float prevArrTime, int phase) :
type(tp),
priority(PRTY[type][phase]),
arrTime(genArrTime(prevArrTime, phase)),
serTime(genSerTime()),
depTime(INFINITY),
expTime(genExpTime(phase)),
begTime(0),
queTime(arrTime),
serLeft(serTime),
opNums(OP_NUMS[type])
{
// Check type
if (tp < 0 || tp >= NUM_TASK_TYPES)
{
cerr << "Error: Incompatible task type . Exiting..." << endl;
exit(1);
}
// Check phase
else if (phase < 0 || phase >= NUM_PHASES)
{
cerr << "Error: Incompatible phase. Exiting..." << endl;
exit(1);
}
}
/****************************************************************************
* *
* Function: genArrTime *
* *
* Purpose: To generate an arrival time based on the specified task *
* type, previous arrival time, and distribution seed *
* *
****************************************************************************/
float Task::genArrTime(float prevArrTime, int phase)
{
// Generate random interarrival time
float interArrTime = genRandNum(ARR_DISTS[type], rand(), ARR_DIST_PARAMS[type][phase]);
// Adjust for arrival time for traffic, if applicable
float newArrTime = prevArrTime + interArrTime;
if (isinf(newArrTime)) return INFINITY;
if (AFF_BY_TRAFF[type][phase] && TRAFFIC_ON)
{
float budget = interArrTime;
float currTime = prevArrTime;
int currHour = currTime/60;
float traffLevel = TRAFFIC[currHour];
float timeToAdj = (currHour + 1) * 60 - currTime;
float adjTime = timeToAdj * traffLevel;
// If time is left in budget, proceed
while (budget > adjTime)
{
// Decrement budget
budget -= adjTime;
// Calculate new values
currTime += timeToAdj;
currHour++;
if (currHour >= TRAFFIC.size())
{
if (DEBUG_ON) cout << "OVERFLOW" << endl;
return INFINITY;
}
traffLevel = TRAFFIC[currHour];
timeToAdj = (currHour + 1) * 60 - currTime;
adjTime = timeToAdj * traffLevel;
}
newArrTime = currTime + budget/traffLevel;
}
return newArrTime;
}
/****************************************************************************
* *
* Function: genSerTime *
* *
* Purpose: To generate a service time based on the specified task type *
* and distribution seed *
* *
****************************************************************************/
float Task::genSerTime()
{
// Get service time parameters
char distType = SER_DISTS[type];
float param1 = SER_DIST_PARAMS[type][0];
float param2 = SER_DIST_PARAMS[type][1];
// Return random number
float num = genRandNum(distType, rand(), param1, param2);
while (isinf(num))
{
if (DEBUG_ON) cout << "ERROR: " << *this << endl;
if (DEBUG_ON) cout << distType << " " << param1 << " " << param2 << endl;
num = genRandNum(distType, rand(), param1, param2);
}
return num;
}
/****************************************************************************
* *
* Function: genExpTime *
* *
* Purpose: To generate an expiration time based on the specified task *
* type *
* *
****************************************************************************/
float Task::genExpTime(int phase)
{
// Initialize variables
float param;
float expiration = 0;
int hour = arrTime/60;
// Set lambda
if (hour >= TRAFFIC.size())
return arrTime;
else if (TRAFFIC[hour] == 2)
param = EXP_DIST_PARAMS_HIGH[type][phase];
else
param = EXP_DIST_PARAMS_LOW[type][phase];
// Get random number
expiration = 2*serTime;
expiration = 1000000;
// while (expiration <= serTime)
// expiration = genRandNum(EXP_DISTS[type], rand(), param);
return arrTime + expiration;
}
/****************************************************************************
* *
* Function: genRandNum *
* *
* Purpose: To generate a random number based on the specified *
* distribution type and seed *
* *
****************************************************************************/
float Task::genRandNum(char distType, int seed, float arg1, float arg2)
{
// Initialize generator
default_random_engine gen(seed);
// Return random number, based on distribution type
switch (distType)
{
// Exponential
case 'E':
{
exponential_distribution<float> dist(arg1); // args = lambda
return dist(gen);
}
// Lognormal
case 'L':
{
lognormal_distribution<double> dist(arg1, arg2); // args = mean, stddev
return dist(gen);
}
// Uniform
case 'U':
{
uniform_real_distribution<float> dist(arg1, arg2); // args = min, max
return dist(gen);
}
// Error message
default:
{
cerr << "Error: Incompatible distribution type. Exiting..." << endl;
exit(1);
}
}
}
#endif