Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
313 lines (267 loc) · 9.27 KB
/
Copy pathmain.cpp
File metadata and controls
313 lines (267 loc) · 9.27 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
// Library includes
#include <array>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <limits>
#include <memory>
#include <sstream>
#include <random>
#include <unistd.h>
// User includes
#include "job.hh"
#include "node.hh"
#include "tree.hh"
// Global constants
int const DEFAULT_JOBS = 0;
int const DEFAULT_MIN_ARRIVAL_TIME = 0;
int const DEFAULT_MAX_ARRIVAL_TIME = std::numeric_limits<int>::max();
int const DEFAULT_MIN_COMPUTATION_TIME = 0;
int const DEFAULT_MAX_COMPUTATION_TIME = std::numeric_limits<int>::max();
int const DEFAULT_MIN_DEADLINE = 0;
int const DEFAULT_MAX_DEADLINE = std::numeric_limits<int>::max();
int const DEFAULT_SEED = std::time(nullptr);
// Global variables
std::string prog_name;
std::vector<std::shared_ptr<Job>> global_jobs_list;
// Arguments struct
typedef struct arguments {
int jobs = DEFAULT_JOBS;
int min_arrival_time = DEFAULT_MIN_ARRIVAL_TIME;
int max_arrival_time = DEFAULT_MAX_ARRIVAL_TIME;
int min_computation_time = DEFAULT_MIN_COMPUTATION_TIME;
int max_computation_time = DEFAULT_MAX_COMPUTATION_TIME;
int min_deadline = DEFAULT_MIN_DEADLINE;
int max_deadline = DEFAULT_MAX_DEADLINE;
int seed = DEFAULT_SEED;
} arguments_t;
// Function prototypes
int check_arg(std::string argument);
int parse_arg(int argc, char * argv[], arguments_t * arguments);
int import_list_from_file(char * fn);
int main(int argc, char * argv[]);
// Check if integer passed is a valid argument
int check_arg(std::string argument) {
int ret;
// Try to convert argument to an int
try {
ret = std::stoi(argument);
}
// Catch any invalid arguments
catch (std::invalid_argument) {
std::cout << prog_name << ": invalid option -- \'";
std::cout << argument << "\'" << std::endl;
return -1;
}
// Catch out of range integers
catch (std::out_of_range) {
std::cout << prog_name << ": integer out of range -- \'";
std::cout << argument << "\'" << std::endl;
return -1;
}
if (ret < 0) {
std::cout << prog_name << ": integer must not be negative -- \'";
std::cout << argument << "\'" << std::endl;
return -1;
}
return ret;
}
// Abstract parsing of command line arguments away from main()
int parse_arg(int argc, char * argv[], arguments_t * arguments) {
int opt, temp;
int input_flag = 0;
// Set global prog_name to program name
prog_name = std::string(argv[0]);
// Parse options
while ((opt = getopt(argc, argv, "a:c:d:A:C:D:i:s:")) != -1) {
switch (opt) {
// Minimum arrival time
case 'a':
temp = check_arg(optarg);
if (temp >= 0) {
arguments->min_arrival_time = temp;
}
break;
// Maximum arrival time
case 'A':
temp = check_arg(optarg);
if (temp >= 0) {
arguments->max_arrival_time = temp;
}
break;
// Minimum computation time
case 'c':
temp = check_arg(optarg);
if (temp >= 0) {
arguments->min_computation_time = temp;
}
break;
// Maximum computation time
case 'C':
temp = check_arg(optarg);
if (temp >= 0) {
arguments->max_computation_time = temp;
}
break;
// Minimum deadline
case 'd':
temp = check_arg(optarg);
if (temp >= 0) {
arguments->min_deadline = temp;
}
break;
// Maximum deadline
case 'D':
temp = check_arg(optarg);
if (temp >= 0) {
arguments->max_deadline = temp;
}
break;
// Input file
case 'i':
input_flag = 1;
temp = import_list_from_file(optarg);
if (temp >= 0) {
arguments->jobs = temp;
}
break;
// Seed current UNIX time, else take user input
case 's':
temp = check_arg(optarg);
if (temp >= 0) {
arguments->seed = temp;
}
break;
}
}
// Too few arguments passed
if (argc + input_flag == optind) {
std::cout << prog_name << ": too few arguments" << std::endl;
return -1;
}
// Too many arguments passed
else if (argc - 1 + input_flag > optind) {
std::cout << prog_name << ": too many arguments" << std::endl;
return -1;
}
// Just enough arguments passed, set jobs
else if (argc - 1 + input_flag == optind) {
if (input_flag == 0) {
temp = check_arg(argv[optind++]);
if (temp >= 0) {
arguments->jobs = temp;
}
}
}
// Check for "correctness"
if ((arguments->min_arrival_time > arguments->max_arrival_time) ||
(arguments->min_computation_time > arguments->max_computation_time) ||
(arguments->min_deadline > arguments->max_deadline))
{
std::cout << prog_name << ": incorrect arguments" << std::endl;
return -1;
}
return 0;
}
// Check if valid filename, if file exists, and if it does then import the list
// of jobs from the file specified
// Returns number of jobs imported if true, -1 if false
int import_list_from_file(char * fn) {
// Check if argument passed
if (fn == nullptr) {
return -1;
}
// Try to open file for reading
std::fstream in_file(fn, std::ios_base::in);
// Check if successful
if (!in_file.is_open()) {
std::cout << "ERROR - !is_open" << std::endl;
return -1;
}
if (!in_file.good()) {
std::cout << "ERROR - !good" << std::endl;
return -1;
}
int i = 0;
std::string line = "";
while (getline(in_file, line)) {
std::stringstream ss(line);
std::string arrival_s, computation_s, deadline_s;
int arrival, computation, deadline;
getline(ss, arrival_s, ',');
getline(ss, computation_s, ',');
getline(ss, deadline_s, ',');
arrival = check_arg(arrival_s);
if (arrival < 0) {
return -1;
}
computation = check_arg(computation_s);
if (computation < 0) {
return -1;
}
deadline = check_arg(deadline_s);
if (deadline < 0) {
return -1;
}
global_jobs_list.push_back(std::make_shared<Job>(i, arrival,
computation, deadline));
i++;
}
in_file.close();
return 0;
}
// Main
int main(int argc, char * argv[]) {
auto timer_start = std::chrono::steady_clock::now();
arguments_t arguments;
// Parse command line arguments
if (parse_arg(argc, argv, &arguments) < 0) {
return EXIT_FAILURE;
}
// Initialize RNG with command line arguments
std::mt19937 rng(arguments.seed);
std::uniform_int_distribution<std::mt19937::result_type> arrival_time(
arguments.min_arrival_time, arguments.max_arrival_time
);
std::uniform_int_distribution<std::mt19937::result_type> computation_time(
arguments.min_computation_time, arguments.max_computation_time
);
std::uniform_int_distribution<std::mt19937::result_type> deadline(
arguments.min_deadline, arguments.max_deadline
);
// Create list of jobs with random parameters
std::vector<std::shared_ptr<Job>> jobs_list;
if (global_jobs_list.empty()) {
for (int i = 0; i < arguments.jobs; i++) {
jobs_list.push_back(std::make_shared<Job>(i, arrival_time(rng),
computation_time(rng), deadline(rng)));
}
} else {
jobs_list = global_jobs_list;
}
// Print jobs
std::cout << "------------------- Jobs to schedule: -------------------" << std::endl;
for (std::shared_ptr<Job> jo : jobs_list) {
std::cout << *jo << std::endl;
}
std::cout << "--------------------- End jobs list ---------------------" << std::endl;
std::cout << std::endl;
// Run the Bratley's algorithm scheduler
Tree my_tr;
if (my_tr.runScheduler(jobs_list)) {
std::cout << "------------------- Feasible schedule: ------------------" << std::endl;
for (std::shared_ptr<Node> n : my_tr.getSchedule()) {
std::cout << *n << std::endl;
}
std::cout << "---------------------- End schedule ---------------------" << std::endl;
std::cout << "Overall finishing time: " << my_tr.getFinishingTime() << std::endl;
} else {
std::cout << "No feasible schedule found." << std::endl;
}
// Print execution time
std::chrono::duration<double> elapsed = std::chrono::steady_clock::now() - timer_start;
std::cout << "Execution time: " << elapsed.count() << "s" << std::endl;
return EXIT_SUCCESS;
}