-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_1.0.cpp
More file actions
142 lines (113 loc) · 3.65 KB
/
Copy pathtest_1.0.cpp
File metadata and controls
142 lines (113 loc) · 3.65 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
#include "CN_CreditNet.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <thread>
#include <algorithm>
#include <thread>
#include <mutex>
using namespace std;
int defaultList[100];
ofstream fout_trans;
ofstream fout_int;
mutex lock_rates;
mutex lock_cout;
////////////////////////////////////////////////////////////////////
void singleSimulation(
int finNum, int conNum, int proNum,
double threshold, int numIR, int mechanismGenMode,
int window_size,
double* resultRate)
{
// config the network
CreditNet creditNet(finNum, conNum, proNum);
creditNet.genTest0Graph(threshold, numIR);
creditNet.setRoutePreference(mechanismGenMode);
// main loop
// first window_size runs
int failRate1 = 0;
int failRate2 = 0;
int failRateTotal = 0;
vector<int> array;
for (int i = 0; i < window_size; ++i){
int temp;
temp = creditNet.genInterBankTrans();
array.push_back(temp);
failRate1 += temp;
failRateTotal += temp;
}
for (int i = 0; i < window_size; ++i){
int temp;
temp = creditNet.genInterBankTrans();
array.push_back(temp);
failRate2 += temp;
failRateTotal += temp;
}
int cnt = 0;
while (1){
if (abs(failRate1 - failRate2) <= window_size * 2.0 / 1000.0){
break;
}
// move on
int temp;
temp = creditNet.genInterBankTrans();
failRate1 = failRate1 - array[0] + array[window_size];
failRate2 = failRate2 - array[window_size] + temp;
failRateTotal += temp;
array.erase(array.begin());
array.push_back(temp);
cnt++;
}
lock_rates.lock();
*resultRate = failRateTotal / (cnt + 2.0 * window_size + 1.0);
lock_rates.unlock();
lock_cout.lock();
cout << "new thread: threshold " << threshold << " mechanism " << mechanismGenMode << *resultRate << endl;
lock_cout.unlock();
}
// argv[1]: initialize mode
// argv[2]: number of interest rates
int main(int argc, char* argv[]){
int finNum = 200;
int conNum = 0;
int proNum = 0;
double threshold = 0.18;
int numIR = 1;
int mechanismGenMode = 1;
int window_size = 4500;
int iter = 10;
double degrees [10] = {0.01,0.02,0.04,0.06,0.09,0.12,0.15,0.20,0.25,0.35};
// config the network
cout << "prepare to generate new credit network" << endl;
CreditNet creditNet(finNum, conNum, proNum);
creditNet.genTest0Graph(threshold, numIR);
creditNet.setRoutePreference(mechanismGenMode);
// // 10 rounds
// for (int i = 0; i < 10; ++i){
// threshold = degrees[i];
// double* rates = new double [iter];
// vector<std::thread*> threadPool;
// double rateFinal = 0;
// // smooth the result
// for (int j = 0; j < iter; ++j){
// std::thread* singleRoundThread
// = new std::thread(singleSimulation,
// finNum, conNum, proNum,
// threshold, numIR, mechanismGenMode,
// window_size, rates + j);
// threadPool.push_back(singleRoundThread);
// }
// // wait for all threads to finish
// for (int j = 0; j < iter; ++j){
// threadPool[j]->join();
// delete threadPool[j];
// }
// for (int j = 0; j < iter; j++) {
// cout << rates[j] << " ";
// rateFinal += rates[j];
// }
// delete [] rates;
// rateFinal /= iter;
// cout << endl << (double)(threshold*199) << " " << 1 - rateFinal << endl;
// }
}