-
Notifications
You must be signed in to change notification settings - Fork 0
/
initdata.cc
277 lines (244 loc) · 7.69 KB
/
initdata.cc
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
#include "initdata.h"
#include "initpop.h"
#include "gene.h"
#include <istream>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int ite_num = 1;
// Generation information
int gen_num = 100;
int pop_num = 128;
int test_num = DEFAULT_VALUE;
int code_num = DEFAULT_VALUE;
double scan_propo = 0.1;
double cross_propo = 0.1;
double mutation_propo = 0.001;
double impro_bound = 0.05;
// The input file.
string cov_file;
string time_file;
int cur_ite_num;
int cur_gen_num;
int code_num_set = 0;
int test_num_set = 0;
double best_eet = -1;
double best_apsc = -1;
int stable_count = 0;
// The output directory.
// The final output file will be the format
// $out_dir + $cur_gen_dir + file_name
string out_dir = "default_data//";
string cur_gen_dir;
string initialize_data_file = "initialize_data_log";
string initialize_fit_file = "initialize_fit_log";
string first_com_fit_file = "first_com_fit_log";
string initialize_pop_file = "initialize_pop_log";
string fatherselect_file = "fatherselect_log";
string crossover_file = "crossover_log";
string mutaupdatefitness_file = "mutaupdatefitness_log";
/*
string scancroupdatefitness_file = "scancroupdatefitness_log";
string rscancroupdatefitness_file = "rscancroupdatefitness_log";
string singlecroupdatefitness_file = "singlecroupdatefitness_log";
string reverse_singlecroupdatefitness_file = "reverse_singlecroupdatefitness_log";
string ordercroupdatefitness_file = "ordercroupdatefitness_log";
string cuda_scroupdatefitness_file = "cuda_scroupdatefitness_log";
string cuda_smutaupdatefitness_file = "cuda_smutaupdatefitness_log";
*/
string cuda_init_fitness_file = "cuda_initfitness_log";;
string cuda_crossover_file = "cuda_crossover_log";
string cuda_computefitness_file = "cuda_comp_fitness_log";
string computefitness_file = "computefitness_log";
string mutation_file = "mutation_log";
string eliteselect_file = "eliteselect_log";
string generation_file = "generation_log";
string front0_file = "front0";
string crossover_tc_file = "crossover_tc";
string mutation_tc_file = "mutation_tc";
vector<int> father_sel_vec;
vector<int> swap_vec_a;
vector<int> swap_vec_b;
vector<int> single_rand_vec;
vector<int> pop_id;
vector<int> first_exe_vec;
vector<int> muta_vec;
list<list<int> > front;
vector<pair<double, double> > front0_value;
// @description: get the integer value of $sec_name.$key_name from file $conf_file
// @return
// 0 success
// -1 configuration tag lost.
int ReadConfToInt(string conf_file, string sec_name, string key_name, int& value) {
string t_str = CIceIniFile::GetValue(key_name, sec_name, conf_file);
if (t_str.length() == 0) {
cerr << "Failed to get integer value of " << sec_name << "." << key_name << " from configuratoin file " << conf_file << endl;
return -1;
}
value = atoi(t_str.c_str());
return 0;
}
// @description: get the double value of $sec_name.$key_name from file $conf_file
// @return
// 0 success
// -1 configuration tag lost.
int ReadConfToDouble(string conf_file, string sec_name, string key_name, double& value) {
string t_str = CIceIniFile::GetValue(key_name, sec_name, conf_file);
if (t_str.length() == 0) {
cerr << "Failed to get double value of " << sec_name << "." << key_name << " from configuratoin file " << conf_file << endl;
return -1;
}
value = atof(t_str.c_str());
return 0;
}
int ReadConfToString(string conf_file, string sec_name, string key_name, string& str) {
str = CIceIniFile::GetValue(key_name, sec_name, conf_file);
if (str.length() == 0) {
cerr << "Failed to get string value of " << sec_name << "." << key_name << " from configuratoin file " << conf_file << endl;
return -1;
}
return 0;
}
// @description: get the configuration of the algorithm
// @return
// 0 success
// -1 configuration tag lost.
int GetConf(string conf_file) {
if (ReadConfToInt(conf_file, "nsga2", "iteration_num" ,ite_num) == -1) {
return -1;
}
if (ReadConfToInt(conf_file, "nsga2", "generation_num" ,gen_num) == -1) {
return -1;
}
if (ReadConfToInt(conf_file, "nsga2", "population_num" ,pop_num) == -1) {
return -1;
}
// not set
test_num_set = 0;
if (ReadConfToInt(conf_file, "nsga2", "test_num" , test_num) == -1) {
// return -1;
} else {
test_num_set = 1;
}
// not set
code_num_set = 0;
if (ReadConfToInt(conf_file, "nsga2", "code_num" , code_num) == -1) {
// return -1;
} else {
code_num_set = 1;
}
if (ReadConfToDouble(conf_file, "nsga2", "cross_propo" ,cross_propo) == -1) {
}
if (ReadConfToDouble(conf_file, "nsga2", "scan_propo" ,scan_propo) == -1) {
}
if (ReadConfToDouble(conf_file, "nsga2", "mutation_propo", mutation_propo) == -1) {
}
if (ReadConfToDouble(conf_file, "nsga2", "impro_bound", impro_bound) == -1) {
return -1;
}
if (ReadConfToString(conf_file, "data", "time", time_file) == -1) {
return -1;
}
if ( ReadConfToString(conf_file, "data", "coverage", cov_file) == -1) {
return -1;
}
return 0;
}
// @description: read data from file $cov_file & $time_file
// @return
// 0 success
// -1 failed to open file
// -2 coverage file is empty
// -3 time file's data doesn't coverage file
int ReadData() {
ifstream cov_fp(cov_file.c_str());
ifstream time_fp(time_file.c_str());
struct stat info;
string t_str;
int t_test_num = 0;
// if ((cov_fp = fopen(cov_file.c_str(), "r")) == NULL) {
cout << "access: " << access(cov_file.c_str(), F_OK) << endl;
stat(cov_file.c_str() ,&info);
if (S_ISDIR(info.st_mode) || !cov_fp.is_open()) {
cerr << "Open " << cov_file << " error!" << endl;
return -1;
} else {
// get sample line to set $code_num
// if (fgets(buf, CODE_BUFFER_LEN, cov_fp) != NULL) {
gen_vec.resize(VEC_SIZE);
cout << cov_file << endl;
getchar();
t_test_num = 0;
while (!std::getline(cov_fp, gen_vec[t_test_num].coverage).eof()) {
++t_test_num;
if (t_test_num >= gen_vec.size()) {
gen_vec.resize(gen_vec.size() + VEC_SIZE);
}
if (test_num_set == 1 && t_test_num == test_num) {
// if (test_num != DEFAULT_VALUE && t_test_num == test_num) {
break;
}
}
}
if (t_test_num > 0) {
// if (test_num == DEFAULT_VALUE) {
if (test_num_set == 0) {
test_num = t_test_num;
}
// if (code_num == DEFAULT_VALUE) {
if (code_num_set == 0) {
code_num = gen_vec[0].coverage.length();
} else if (code_num_set == 1 && code_num > gen_vec[0].coverage.length()) {
cout << "code_NUM: " << code_num << endl;
return -2;
}
father_sel_vec.resize(pop_num);
swap_vec_a.resize(pop_num << 1);
swap_vec_b.resize(pop_num << 1);
single_rand_vec.resize(pop_num);
} else {
cout << "t_test_num: " << t_test_num << endl;
return -3;
}
gen_vec.resize(t_test_num);
cout << "code_num is: " << code_num << endl;
cout << "test_num is: " << test_num << endl;
cout << "GetData 2" << endl;
stat(cov_file.c_str() ,&info);
// if (!time_fp.is_open()) {
if (S_ISDIR(info.st_mode) || !time_fp.is_open()) {
cerr << "OPEN " << time_file << " error!" << endl;
return -1;
} else {
for (int i = 0; i < test_num; ++i) {
if (std::getline(time_fp, t_str).eof()) {
return -4;
}
gen_vec[i].time = atof(t_str.c_str());
// cout << gen_vec[i].time << " " << t_str << endl;
}
// getchar();
}
cov_fp.close();
time_fp.close();
return 0;
}
// @description: initialize the data nsga2 need
// @return
// 0 success
// -1 failed to read data
int InitData() {
int ret = 0;
if ((ret = ReadData()) != 0) {
cout << "ReadData Error " << ret << endl;
return -1;
}
// out_dir += "//";
system(("mkdir " + out_dir).c_str());
srand(time(NULL));
return 0;
}