-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.cpp
More file actions
135 lines (124 loc) · 3.72 KB
/
data.cpp
File metadata and controls
135 lines (124 loc) · 3.72 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
//
// Created by 王恺忻 on 2018/11/10.
//
#include <random>
#include "data.h"
using namespace std;
void generateSequence(const char *fileName, int len, int lambda) {
FILE *fp = fopen(fileName, "w");
if (fp == nullptr) {
printf("Cannot open file %s\n.", fileName);
exit(-1);
}
if (len <= 0) {
printf("Invalid length parameter %d\n.", len);
exit(-2);
}
int num = 0, totalLen = 2 * len, taskLen = 0, workerLen = 0;
int taskId = 0, workerId = 0, start = 0, end = 0, duration = 0;
random_device rd;
mt19937 gen(rd());
poisson_distribution<> pd(lambda);
uniform_int_distribution<> uid(LOWER_BOUND_SEQ, UPPER_BOUND_SEQ);
while (num < totalLen) {
int k = pd(gen), task_k, worker_k;
if (num + k > totalLen)
k = totalLen - num;
if (taskLen >= workerLen) {
task_k = (int) (k * ((double) (len - taskLen) / (totalLen - taskLen - workerLen)));
worker_k = k - task_k;
}
else {
worker_k = (int) (k * ((double) (len - workerLen) / (totalLen - taskLen - workerLen)));
task_k = k - worker_k;
}
for (int i = 0; i < task_k; ++i) {
duration = uid(gen);
end = start + duration;
fprintf(fp, "0 %d %d %d\n", taskId++, start, end);
}
for (int j = 0; j < worker_k; ++j) {
duration = uid(gen);
end = start + duration;
fprintf(fp, "1 %d %d %d\n", workerId++, start, end);
}
taskLen += task_k;
workerLen += worker_k;
start++;
num += k;
}
fclose(fp);
}
void readSequence(const char *fileName, VVI &seq) {
FILE *fp = fopen(fileName, "r");
if (fp == nullptr) {
printf("Cannot open file %s\n.", fileName);
exit(-1);
}
VI temp;
int type, id, start, end, matched_time = 0;
while (fscanf(fp, "%d %d %d %d", &type, &id, &start, &end) == 4) {
temp.push_back(type); // 0
temp.push_back(id); // 1
temp.push_back(start); // 2
temp.push_back(end); // 3
temp.push_back(matched_time); // 4
seq.push_back(temp);
temp.clear();
}
fclose(fp);
}
void splitSequence(VVI &seq, VVI &L, VVI &R) {
L.clear();
R.clear();
for (int i = 0; i < seq.size(); ++i) {
if (seq[i][0] == 0)
L.push_back(seq[i]);
else
R.push_back(seq[i]);
}
}
/*
* Modified on 2018.11.22 by Wang.
* TODO: To generate the distance between node_l and node_r.
*/
void generateMatrix(const char *fileName, VVI &L, VVI &R) {
FILE *fp = fopen(fileName, "w");
if (fp == nullptr) {
printf("Cannot open file %s\n.", fileName);
exit(-1);
}
double cost;
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> urd(LOWER_BOUND_DIS, UPPER_BOUND_DIS);
for (int i = 0; i < L.size(); ++i) {
for (int j = 0; j < R.size(); ++j) {
if ((L[i][2] - R[j][3]) * (L[i][3] - R[j][2]) <= 0) cost = urd(gen);
else cost = -1;
fprintf(fp, "%7.3lf ", cost);
}
fprintf(fp, "\n");
}
fclose(fp);
}
/*
* Modified on 2018.11.22 by Wang.
* TODO: To read the distance between node_l and node_r.
*/
void readMatrix(const char *fileName, int len, VVD &mat) {
FILE *fp = fopen(fileName, "r");
if (fp == nullptr) {
printf("Cannot open file %s\n.", fileName);
exit(-1);
}
double value;
mat = VVD((unsigned long) len, VD((unsigned long) len));
for (int i = 0; i < len; ++i) {
for (int j = 0; j < len; ++j) {
fscanf(fp, "%lf ", &value);
mat[i][j] = value;
}
}
fclose(fp);
}