-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
374 lines (325 loc) · 13.8 KB
/
main.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <cmath>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "apm.h"
#include "data2hdf5.h"
#include "general.h"
#include "graph.h"
#include "parser.h"
#include "power.h"
#include "rng.h"
#include "testing.h"
using namespace apm;
// ./apm test
// ./apm single -g 100 3 -s 1. -t 10000 -f "data"
// ./apm averaging -g 100 3 -s 1. -t 10000 -r 10 -f "data"
// ./apm ratefunc -g 100 3 -s 1. -t 1000 -e 10 -f "data"
// ./apm learningrate -g 100 3 -s 1. -t 10000 -f "data" -r 10
// ./apm transfer -g 100 3 -s 1. -t 10000 -f "data" -e 4
// ./apm transavg -g 100 3 -s 1. -t 10000 -f "data" -e 4 -r 10
// ./apm power -g 100 3 -s 1. -t 10000 -f "data" -a 0.3
// ./apm learns -g 100 3 -c 3 -t 2000 -f "data" -a 0.1 -e 10
int main(int argc, char **argv) {
herr_t status = 0; // hdf5 status
Rng rng; // random number generator
apmMode_t mode = NONE;
vInt times; // list of t values
vDouble sVals; // list of s values
vDouble alphaVals; // list of alpha values
apmParam_t params; // simulation parameters
vInt danglingChains; // list of dangling chain endpoints
bool modeLooping = false; // flag for looping everyting again
size_t chainThreshold = 1; // threshold for regenerating the graph
size_t tEnd = 0;
std::string learningrate = "";
vString arg_mode_list;
bool pathStatus = false;
// check if we got a valid mode
if (argc <= 1) {
std::cout << "[Error]\t[main]\t no arguments where given.\n";
return 1;
}
#ifdef LOG_DEBUG
std::cout << "[Debug]\t[main]\tParsing mode argument\n";
#endif
arg_mode_list = split(argv[1], ',');
// parse and check the command line arguments
if (parseArgs(params, times, sVals, alphaVals, learningrate, argc, argv)) {
return 1;
}
// check initially if the data folder exists to prevent an error at the end
pathStatus = std::filesystem::exists(params.data_folder);
if (!pathStatus) {
std::cout << "The folder: " << params.data_folder
<< " does not exist. Aborting!\n";
return 1;
}
// setting up the graph
Graph graph(params.n, params.kmean, rng);
// generate the graph as long as the the number of dangling chains
// is below the threshold
do {
graph.generateGiantComponent();
// graph.printDegreeSeqence();
danglingChains = graph.findDanglingChains();
} while (danglingChains.size() < chainThreshold);
vvInt edgeList = graph.getEdgeList();
matrixSparse pi = graph.transitionMatrix();
vInt k = graph.getDegreeSequence();
// loop over different modes given
for (std::string arg_mode : arg_mode_list) {
if (arg_mode == "test") {
mode = TEST;
} else if (arg_mode == "single") {
mode = SINGLE;
} else if (arg_mode == "ratefunc") {
mode = RATE_FUNC;
} else if (arg_mode == "power") {
mode = POWER;
} else if (arg_mode == "transfer") {
mode = TRANSFER;
} else {
mode = NONE;
}
if (mode == NONE) {
std::cout << "[Error]\t[main] Could not determine a mode.\n";
return 1;
}
params.arg_mode = arg_mode;
// loop again if modeLoop is set
do {
// adapt the arg_mode if the learning rate needs to be in it
if (learningrate == "both") {
if (params.lr) {
params.arg_mode = arg_mode + "-" + "lr";
} else {
params.arg_mode = arg_mode + "-" + "nolr";
}
}
// loop over the list of parameter values
for (size_t t : times) {
for (double s : sVals) {
for (double alpha : alphaVals) {
// result variables
apmResults_t results;
apmResults_t resultsAveraged;
powerResults_t resultsPower;
apmEstimators_t resLeft;
apmEstimators_t resRight;
apmEstimators_t resLeftAvg;
apmEstimators_t resRightAvg;
// modify initial condition
if (params.initialCondition) {
params.r0 = 1. / pi.size();
std::cout << "[Info]\t[main]\t Initial condition changed to 1/n.\n";
}
// save loop variables into struct
params.t = t;
params.s = s;
params.alpha = alpha;
constructOutName(params);
// performing the desired action
switch (mode) {
case TEST:
testVectorSparse();
testRng();
testGraph();
testRngChoice();
testRngInteger();
testApmSingle();
testApmAveraged();
testApmRateFunction();
status = 0;
break;
case SINGLE:
// Run the APM bare
std::cout << "[Info]\t[main]\tSINGLE"
<< ", n=" << params.n
<< ", kmean=" << params.kmean
<< ", s=" << params.s
<< ", t=" << params.t
<< ", repeats=" << params.repeats
<< ", alpha=" << params.alpha
<< ", lr=" << params.lr
<< ", r0=" << params.r0
<< ", data_folder=" << params.data_folder
<< '\n';
for (int i = 0; i < params.repeats; i++) {
#ifdef LOG_VERBOSE
std::cout << "[Verbose]\t[main]\tRepetition " << i << "\t";
#endif
results = adaptivePowerMethod(rng,
pi,
params.s,
params.t,
k,
params.alpha,
params.lr,
params.r0);
apmResultsAdd(resultsAveraged, results);
}
apmResultsDivide(resultsAveraged, params.repeats);
status = saveTrajectoryAveraged(params.file_path,
resultsAveraged,
k,
edgeList);
tEnd = params.t - 1;
std::cout << "[Info]\t[main]\tFinal values"
<< ", zeta=" << resultsAveraged.zeta.at(tEnd)
<< ", psi=" << resultsAveraged.psi.at(tEnd)
<< ", cns=" << resultsAveraged.cns.at(tEnd)
<< ", kns=" << resultsAveraged.kns.at(tEnd)
<< ", s=" << resultsAveraged.s.at(tEnd)
<< ", cns*s-kns=" << resultsAveraged.psiEst.at(tEnd)
<< std::endl;
break;
case TRANSFER:
// Run the APM with transfer learning
std::cout << "[Info]\t[main]\tTRANSFER"
<< ", n=" << params.n
<< ", kmean=" << params.kmean
<< ", s=" << params.s
<< ", t=" << params.t
<< ", epochs=" << params.epochs
<< ", repeats=" << params.repeats
<< ", alpha=" << params.alpha
<< ", lr=" << params.lr
<< ", r0=" << params.r0
<< ", data_folder=" << params.data_folder
<< '\n';
for (int i = 0; i < params.repeats; i++) {
#ifdef LOG_VERBOSE
std::cout << "[Verbose]\t[main]\tRepetition " << i << "\t";
#endif
results = apmTransferLearning(rng,
pi,
params.s,
params.epochs,
params.t,
k,
params.alpha,
params.lr,
params.r0);
apmResultsAdd(resultsAveraged, results);
}
apmResultsDivide(resultsAveraged, params.repeats);
status = saveTrajectoryAveraged(params.file_path,
resultsAveraged,
k,
edgeList);
tEnd = params.epochs * params.t - 1;
std::cout << "[Info]\t[main]\tFinal values"
<< ", zeta=" << resultsAveraged.zeta.at(tEnd)
<< ", psi=" << resultsAveraged.psi.at(tEnd)
<< ", cns=" << resultsAveraged.cns.at(tEnd)
<< ", kns=" << resultsAveraged.kns.at(tEnd)
<< ", s=" << resultsAveraged.s.at(tEnd)
<< ", cns*s-kns=" << resultsAveraged.psiEst.at(tEnd)
<< std::endl;
break;
case RATE_FUNC:
// Run the APM with transfer learning and extract the rate function
std::cout << "[Info]\t[main]\tRATE_FUNC"
<< ", n=" << params.n
<< ", kmean=" << params.kmean
<< ", s=" << params.s
<< ", t=" << params.t
<< ", epochs=" << params.epochs
<< ", repeats=" << params.repeats
<< ", alpha=" << params.alpha
<< ", lr=" << params.lr
<< ", data_folder=" << params.data_folder
<< '\n';
// flip the sign of s to make it positive
if (params.s < 0) {
params.s = -params.s;
}
for (int i = 0; i < params.repeats; i++) {
#ifdef LOG_VERBOSE
std::cout << "[Verbose]\t[main]\tRepetition " << i
<< std::endl;
#endif
resLeft = apmRateFunction(rng,
pi,
-params.s,
params.epochs,
params.t,
k,
params.alpha,
params.lr,
params.r0);
resRight = apmRateFunction(rng,
pi,
params.s,
params.epochs,
params.t,
k,
params.alpha,
params.lr,
params.r0);
apmRateFunctionAdd(resLeftAvg, resLeft);
apmRateFunctionAdd(resRightAvg, resRight);
}
apmRateFunctionDivide(resLeftAvg, params.repeats);
apmRateFunctionDivide(resRightAvg, params.repeats);
status = saveRateFunction(params.file_path,
resLeftAvg,
resRightAvg,
k,
edgeList);
break;
case POWER:
// Run the Power Method
std::cout << "[Info]\t[main]\tPOWER"
<< ", n=" << params.n
<< ", kmean=" << params.kmean
<< ", s=" << params.s
<< ", t=" << params.t
<< ", alpha=" << params.alpha
<< ", lr=" << params.lr
<< ", data_folder=" << params.data_folder
<< '\n';
resultsPower = powerMethod(pi,
params.s,
params.t,
k,
params.alpha,
params.lr,
params.r0);
status = savePowerMethod(params.file_path,
resultsPower,
k,
edgeList);
tEnd = params.t - 1;
std::cout << "[Info]\t[main]\tFinal values"
<< ", zeta=" << resultsPower.zeta.at(tEnd)
<< ", psi=" << resultsPower.psi.at(tEnd)
<< ", s=" << resultsPower.s
<< std::endl;
break;
default:
std::cout << "[Error] Unkown mode. Doing nothing!\n";
status = 0;
break;
} // end switch mode
} // end for over different alpha values
} // end for over different s values
} // end for over different t values
// loop again if we want both learning rate options and
// we are just finished with the first one
if (learningrate == "both") {
if (params.lr) {
params.lr = false;
modeLooping = true;
} else {
modeLooping = false;
}
}
} while (modeLooping); // end while different modes for learning rate
} // end for over arg_mode_list
return status;
}