-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtst_basic.cpp
More file actions
307 lines (270 loc) · 10.6 KB
/
tst_basic.cpp
File metadata and controls
307 lines (270 loc) · 10.6 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
#include <QtTest>
// add necessary includes here
#include "algorithm.h"
#include "../rcpsp_qt/algorithm/loader.h"
class basic : public QObject
{
Q_OBJECT
public:
basic();
~basic();
private slots:
void workerTests();
//void algorithmCase1();
void algorithmCase2();
void algorithmCase3();
void algorithmCase4();
void algorithmCase5();
void algorithmCase6();
void algorithmCase7();
//void algorithmCase8();
//friend class PendingFronts;
void weights_1()
{
int current_time = 13;
CompletedJobs c;
AssignedJobs j(¤t_time, &c);
AlgorithmWeights w;
for (const auto& n : Weights::WeightsNames)
{
Weights::set(w, n, 0.2);
}
PendingFronts p(¤t_time, &j, Preference::NONE, 0, w);
Job job = Job(0, 0, {{4, 0.3}, {5, 0.5}});
job.set_start_after(13);
job.set_end_before(30);
job.set_critical_time(21);
// job.set_ancestors in the future?
WorkerGroup g;
g.set_clock(¤t_time);
Plan plan = Plan({{9, 1}});
auto* worker = new Worker(plan);
worker->set_clock(¤t_time);
g.add_worker(worker);
JobPair jp =
{
.start_after = 13,
.end_before = 30,
.job = &job,
.worker_groups = {&g},
.id = 0
};
p.add(13, jp);
AlgorithmDataForWeights dw
{
.job_count_not_assigned = 0,
.job_count_overall = 1,
.max_critical_time = 21
};
//"ancestors_per_left" = 0, // кол-во последователей / кол-во оставшихся требований
//"ancestors_per_job" = 0, // кол-во последователей / кол-во требований всего
//"critical_time_per_max_critical_time" = 1, // критическое время требования / максимальное критическое время всех требований
//"avg_occupancy" = (4 * 0.3 + 5 * 0.5) / 9, // средняя занятость станка во время выполнения
//"time_after_begin_per_overall_time" = 0 // время от начала выполнения до текущего момента / время всего на выполнение этого требования
p.tick(dw);
//qDebug() << p._data.size() << p._data[0].job_pairs[0].current_preference << p._data[0].time;
double preference = ((4.0 * 0.3 + 5.0 * 0.5) / 9.0 + 1.0) / 5;
double current = p._data[0].job_pairs[0].current_preference;
QVERIFY(std::abs(preference - current) < 0.0001);
//QCOMPARE(p._data[0].job_pairs[0].current_preference, ((4.0 * 0.3 + 5.0 * 0.5) / 9.0 + 1.0) / 5);
}
void weights_2()
{
int current_time = 13;
CompletedJobs c;
AssignedJobs j(¤t_time, &c);
AlgorithmWeights w;
for (const auto& n : Weights::WeightsNames)
{
Weights::set(w, n, 0.2);
}
PendingFronts p(¤t_time, &j, Preference::NONE, 0, w);
WorkerGroup g;
g.set_clock(¤t_time);
Plan plan = Plan({{9, 1}});
auto* worker = new Worker(plan);
worker->set_clock(¤t_time);
g.add_worker(worker);
Job job = Job(0, 0, {{4, 0.3}, {5, 0.5}});
job.set_start_after(13);
job.set_end_before(30);
job.set_critical_time(21);
JobPair jp =
{
.start_after = 13,
.end_before = 30,
.job = &job,
.worker_groups = {&g},
.id = 0
};
p.add(13, jp);
Job job2 = Job(0, 0, {{5, 0.6}, {4, 0.9}});
job2.set_start_after(18);
job2.set_end_before(40);
job2.set_critical_time(31);
JobPair jp2 =
{
.start_after = 18,
.end_before = 40,
.job = &job2,
.worker_groups = {&g},
.id = 0
};
job.set_ancestors({&job2});
AlgorithmDataForWeights dw
{
.job_count_not_assigned = 0,
.job_count_overall = 2,
.max_critical_time = 31
};
p.tick(dw);
//"ancestors_per_left" = 1 / 1, // кол-во последователей / кол-во оставшихся требований
//"ancestors_per_job" = 1 / 2, // кол-во последователей / кол-во требований всего
//"critical_time_per_max_critical_time" = 21 / 31, // критическое время требования / максимальное критическое время всех требований
//"avg_occupancy" = (4 * 0.3 + 5 * 0.5) / 9, // средняя занятость станка во время выполнения
//"time_after_begin_per_overall_time" = 0 // время от начала выполнения до текущего момента / время всего на выполнение этого требования
double preference = (1 + 0.5 + 21.0 / 31.0 + (4.0 * 0.3 + 5.0 * 0.5) / 9.0) / 5;
double current = p._data[0].job_pairs[0].current_preference;
QVERIFY(std::abs(preference - current) < 0.0001);
p.tick(dw);
j.tick();
p.add(30, jp2);
p.tick(dw);
p.tick(dw);
// now check second
//"ancestors_per_left" = 0, // кол-во последователей / кол-во оставшихся требований
//"ancestors_per_job" = 0, // кол-во последователей / кол-во требований всего
//"critical_time_per_max_critical_time" = 1, // критическое время требования / максимальное критическое время всех требований
//"avg_occupancy" = (5.0 * 0.6 + 4.0 * 0.9) / 9.0, // средняя занятость станка во время выполнения
//"time_after_begin_per_overall_time" = 12 / 22 // время от начала выполнения до текущего момента / время всего на выполнение этого требования
preference = (1.0 + (5.0 * 0.6 + 4.0 * 0.9) / 9.0 + 12.0 / 22.0) / 5;
//current = p._data[0].job_pairs[0].current_preference;
//QVERIFY(std::abs(preference - current) < 0.0001);
//FIXME: i know that this is right but i need to actually pass the value somehow
}
void stats()
{
Job job = Job(0, 0, {{5, 0.6}, {4, 0.9}});
job.set_start_after(10);
job.set_end_before(35);
ResultPair r;
r.job = &job;
r.job_id = 0;
r.start = 20;
Job job2 = Job(0, 0, {{3, 0.6}, {4, 0.9}});
job2.set_start_after(10);
job2.set_end_before(37);
ResultPair r2;
r2.job = &job2;
r2.job_id = 1;
r2.start = 25;
Stats stats({r, r2}, 0.03);
QVERIFY(stats.wait_coeff.at(0.39) == 1);
QVERIFY(stats.work_coeff.at(0.36) == 1);
QVERIFY(stats.wait_coeff.at(0.57) == 1);
QVERIFY(stats.work_coeff.at(0.27) == 1);
//qDebug() << "STATS (wait_coeff): \n";
//for (auto point : stats.wait_coeff)
//{
// qDebug() << "X = " << point.first << ", Y = " << point.second << "\n";
//}
//qDebug() << "STATS (work_coeff): \n";
//for (auto point : stats.work_coeff)
//{
// qDebug() << "X = " << point.first << ", Y = " << point.second << "\n";
//}
}
private:
void check_loaded(QString file_name, QString preference_file_name, std::vector<int> expected);
void compare_result(const std::vector<ResultPair> &completed, std::vector<int> expected);
};
basic::basic() {}
basic::~basic() {}
void basic::workerTests()
{
Job firstJob(0, 0, {{2, 0.3}, {3, 0.7}, {4, 0.2}});
Job secondJob(0, 0, {{2, 0.3}, {3, 0.7}, {4, 0.2}});
Job thirdJob(0, 0, {{2, 0.3}, {3, 0.2}});
{
Worker worker(Plan({{11, 2}}));
int clock = 0;
worker.set_clock(&clock);
QVERIFY(worker.is_free({{10, 1.0f}}));
QCOMPARE(worker.current_occupancy(), 0);
clock = 1;
worker.assign(&firstJob);
QVERIFY(worker.is_free({{1, 1.0f}}, 10));
QCOMPARE(worker.is_free({{2, 1.0f}}, 10), false);
QCOMPARE(worker.is_free({{2, 1.0f}}, 9), false);
QVERIFY(worker.is_free({{2, 0.8f}}, 9));
clock = 12;
worker.update();
}
{
Worker worker(Plan({{13, 1}}));
int clock = 1;
worker.set_clock(&clock);
worker.assign(&firstJob);
QCOMPARE(worker.can_be_placed_after(secondJob.get_occupancy()), 3);
}
}
void basic::compare_result(const std::vector<ResultPair>& completed, std::vector<int> expected)
{
for (int i = 0; i < expected.size(); i++)
{
QCOMPARE(completed[i].job_id, expected.size() - i - 1);
QCOMPARE(completed[i].start, expected[expected.size() - i - 1]);
}
}
void basic::check_loaded(QString file_name, QString preference_file_name, std::vector<int> expected)
{
Algorithm algorithm;
std::vector<Job*> all_jobs;
std::vector<Worker*> all_workers;
Loader::Load("../rcpsp_test/" + file_name, algorithm, all_workers, all_jobs);
Loader::LoadPreferences("../rcpsp_test/" + preference_file_name, algorithm);
Loader::LoadWeights("../rcpsp_test/weights.csv", algorithm);
algorithm.run();
auto completed = algorithm.get_completed();
compare_result(completed, expected);
}
//void basic::algorithmCase1()
//{
// check_loaded("case1.csv", "preferences1.csv", {0, 0, 0, 9});
//}
void basic::algorithmCase2()
{
check_loaded("case2.csv", "preferences2.csv", {1, 0, 0, 1});
}
void basic::algorithmCase3()
{
check_loaded("case3.csv", "preferences3.csv", {10, 1, 20, 22});
}
void basic::algorithmCase4()
{
check_loaded("case4.csv", "preferences4.csv", {1, 14, 10, 21, 9});
}
void basic::algorithmCase5()
{
check_loaded("case5.csv", "preferences5.csv", {1, 9, 16, 18});
}
void basic::algorithmCase6()
{
check_loaded("case6.csv", "preferences6.csv", {0, 11, 22, 33});
}
void basic::algorithmCase7()
{
check_loaded("case7.csv", "preferences7.csv", {5, 3});
}
//void basic::algorithmCase8()
//{
// Algorithm algorithm;
// std::vector<Job*> all_jobs;
// std::vector<Worker*> all_workers;
// Loader::Load("../rcpsp_test/case8_large.csv", algorithm, all_workers, all_jobs);
// algorithm.run();
// auto completed = algorithm.get_completed();
// QCOMPARE(completed.size(), 4000);
//}
QTEST_APPLESS_MAIN(basic)
#include "tst_basic.moc"