-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.cpp
275 lines (253 loc) · 9.7 KB
/
Simulator.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
#include "Simulator.h"
Simulator::Simulator(const char* param_filename) {
read_xml(param_filename, this->pt);
try {
this->num_producer = this->pt.get<int>("parameter_list.global.num_producer");
this->num_consumer = this->pt.get<int>("parameter_list.global.num_consumer");
} catch (std::exception &e) {
// error
}
this->authority = new Authority(this);
try {
authority->setFeeRate(this->pt.get<double>("parameter_list.global.fee_rate"));
} catch (std::exception &e) {
// no fee
}
for (int i = 0; i < this->num_producer; i++) {
Producer *p = new Producer(this, i);
this->producers.push_back(p);
}
for (int j = 0; j < this->num_consumer; j++) {
//Consumer *c = new Consumer(this, j);
double claim_prob = 1.0 * (j+1) / this->num_consumer;
Consumer *c = new Consumer(this, j, claim_prob);
this->consumers.push_back(c);
}
setParameters();
}
Simulator::~Simulator() {
delete(authority);
for (int i = 0; i < num_producer; i++) {
producers[i]->deleteCosts();
}
for (int j = 0; j < num_consumer; j++) {
consumers[j]->deleteValues();
}
for (int i = 0; i < num_producer; i++) {
delete(producers[i]);
}
for (int j = 0; j < num_consumer; j++) {
delete(consumers[j]);
}
}
int Simulator::getNumberOfProducers() {
return num_producer;
}
int Simulator::getNumberOfConsumers() {
return num_consumer;
}
void Simulator::setParameters() {
// Alpha Profile
Profile *alpha_profile_authority, *alpha_profile_consumer;
try {
alpha_profile_authority = new ProfileDouble(this->pt.get<double>("parameter_list.alpha.authority"));
} catch (std::exception &e) {
try {
alpha_profile_authority = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.alpha.authority_min"), this->pt.get<double>("parameter_list.alpha.authority_max")));
} catch (std::exception &e) {
// error
}
}
try {
alpha_profile_consumer = new ProfileDouble(this->pt.get<double>("parameter_list.alpha.consumer"));
} catch (std::exception &e) {
try {
alpha_profile_consumer = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.alpha.consumer_min"), this->pt.get<double>("parameter_list.alpha.consumer_max")));
} catch (std::exception &e) {
// error
}
}
// Capital Profile
Profile *capital_profile_authority, *capital_profile_producer, *capital_profile_consumer;
try {
capital_profile_authority = new ProfileDouble(this->pt.get<double>("parameter_list.capital.authority"));
} catch (std::exception &e) {
try {
capital_profile_authority = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.capital.authority_min"), this->pt.get<double>("parameter_list.capital.authority_max")));
} catch (std::exception &e) {
// error
}
}
try {
capital_profile_producer = new ProfileDouble(this->pt.get<double>("parameter_list.capital.producer"));
} catch (std::exception &e) {
try {
capital_profile_producer = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.capital.producer_min"), this->pt.get<double>("parameter_list.capital.producer_max")));
} catch (std::exception &e) {
// error
}
}
try {
capital_profile_consumer = new ProfileDouble(this->pt.get<double>("parameter_list.capital.consumer"));
} catch (std::exception &e) {
try {
capital_profile_consumer = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.capital.consumer_min"), this->pt.get<double>("parameter_list.capital.consumer_max")));
} catch (std::exception &e) {
// error
}
}
// Cost Profile
Profile *cost_location, *cost_scale, *cost_shape, *cost_lambda, *cost_claimprob;
std::string cost_profile;
try {
cost_profile = this->pt.get<std::string>("parameter_list.cost.<xmlattr>.profile");
} catch (std::exception &e) {
// error
}
if (cost_profile == "Normal") {
try {
cost_location = new ProfileDouble(this->pt.get<double>("parameter_list.cost.location"));
} catch (std::exception &e) {
try {
cost_location = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.cost.location_min"), this->pt.get<double>("parameter_list.cost.location_max")));
} catch (std::exception &e) {
// error
}
}
try {
cost_scale = new ProfileDouble(this->pt.get<double>("parameter_list.cost.scale"));
} catch (std::exception &e) {
try {
cost_scale = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.cost.scale_min"), this->pt.get<double>("parameter_list.cost.scale_max")));
} catch (std::exception &e) {
// error
}
}
} else if (cost_profile == "Exponential") {
try {
cost_lambda = new ProfileDouble(this->pt.get<double>("parameter_list.cost.lamda"));
} catch (std::exception &e) {
try {
cost_lambda = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.cost.lambda_min"), this->pt.get<double>("parameter_list.cost.lambda_max")));
} catch (std::exception &e) {
// error
}
}
} else if (cost_profile == "ZAGA") {
try {
cost_claimprob = new ProfileDouble(this->pt.get<double>("parameter_list.cost.claimprob"));
} catch (std::exception &e) {
try {
cost_claimprob = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.cost.claimprob_min"), this->pt.get<double>("parameter_list.cost.claimprob_max")));
} catch (std::exception &e) {
// error
}
}
try {
cost_shape = new ProfileDouble(this->pt.get<double>("parameter_list.cost.shape"));
} catch (std::exception &e) {
try {
cost_shape = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.cost.shape_min"), this->pt.get<double>("parameter_list.cost.shape_max")));
} catch (std::exception &e) {
// error
}
}
try {
cost_scale = new ProfileDouble(this->pt.get<double>("parameter_list.cost.scale"));
} catch (std::exception &e) {
try {
cost_scale = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.cost.scale_min"), this->pt.get<double>("parameter_list.cost.scale_max")));
} catch (std::exception &e) {
// error
}
}
} else {
// error (unsupported cost profile)
}
// Value Profile
Profile *value_location, *value_scale;
std::string value_profile;
try {
value_profile = this->pt.get<std::string>("parameter_list.value.<xmlattr>.profile");
} catch (std::exception &e) {
// error
}
if (value_profile == "Normal") {
try {
value_location = new ProfileDouble(this->pt.get<double>("parameter_list.value.location"));
} catch (std::exception &e) {
try {
value_location = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.value.location_min"), this->pt.get<double>("parameter_list.value.location_max")));
} catch (std::exception &e) {
// error
}
}
try {
value_scale = new ProfileDouble(this->pt.get<double>("parameter_list.value.scale"));
} catch (std::exception &e) {
try {
value_scale = new ProfileUniform(boost::math::uniform(this->pt.get<double>("parameter_list.value.scale_min"), this->pt.get<double>("parameter_list.value.scale_max")));
} catch (std::exception &e) {
// error
}
}
} else {
// error (unsupported value profile)
}
// Set Parameters
authority->setUtilityExponential(alpha_profile_authority->getSample());
authority->setCapital(capital_profile_authority->getSample());
authority->setLowestCapital(authority->getCapital());
for (int j = 0; j < num_consumer; j++) {
consumers[j]->setUtilityExponential(alpha_profile_consumer->getSample());
consumers[j]->setCapital(capital_profile_consumer->getSample());
}
for (int i = 0; i < num_producer; i++) {
producers[i]->setCapital(capital_profile_producer->getSample());
for (int j = 0; j < num_consumer; j++) {
Profile *cost;
if (cost_profile == "Normal") {
cost = new ProfileNormal(boost::math::normal(cost_location->getSample(), cost_scale->getSample()));
} else if (cost_profile == "Exponential") {
cost = new ProfileExponential(boost::math::exponential(cost_lambda->getSample()));
} else if (cost_profile == "ZAGA") {
cost = new ProfileZAGA(boost::math::gamma_distribution<double>(cost_shape->getSample(), cost_scale->getSample()), cost_claimprob->getSample());
//cost = new ProfileZAGA(boost::math::gamma_distribution<double>(cost_shape->getSample(), cost_scale->getSample()), consumers[j]->getClaimProbability());
} else {
// error (unsupported cost profile)
}
producers[i]->setCost(consumers[j], cost);
Profile *value;
if (value_profile == "Normal") {
value = new ProfileNormal(boost::math::normal(value_location->getSample(), value_scale->getSample()));
} else {
// error (unsupported value profile)
}
consumers[j]->setValue(producers[i], value);
}
}
// delete
delete(alpha_profile_authority);
delete(alpha_profile_consumer);
delete(capital_profile_authority);
delete(capital_profile_producer);
delete(capital_profile_consumer);
if (cost_profile == "Normal") {
delete(cost_location);
delete(cost_scale);
} else if (cost_profile == "Exponential") {
delete(cost_lambda);
} else if (cost_profile == "ZAGA") {
delete(cost_claimprob);
delete(cost_shape);
delete(cost_scale);
} else {
// error (unsupported cost profile)
}
if (value_profile == "Normal") {
delete(value_location);
delete(value_scale);
} else {
// error (unsupported value profile)
}
}