-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShale.cpp
375 lines (330 loc) · 11.8 KB
/
Shale.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
375
//
// Created by linpingta.
//
#include "Shale.h"
// compare key
bool compareByKey(const std::pair<std::string, double>& a, const std::pair<std::string, double>& b) {
// Compare by value in descending order
if (a.second != b.second) {
return a.second > b.second;
}
// If values are equal, compare by key in ascending order
return a.first < b.first;
}
bool existsInVector(const std::string& str, const std::vector<std::string>& vec) {
return std::find(vec.begin(), vec.end(), str) != vec.end();
}
// max value
// max(coef.1 - coef.2 * x) = y
std::vector<double> mathMax(std::vector<std::tuple<double, double, double>>& coef, double y) {
std::vector<double> solutions;
double sum_cons = 0.0;
double sum_coef = 0.0;
for (const auto& t : coef) {
sum_cons += std::get<1>(t);
sum_coef += std::get<2>(t);
}
if (sum_coef == 0.0) {
return solutions;
}
std::sort(coef.begin(), coef.end(), [](const auto& a, const auto& b) {
return std::get<0>(a) < std::get<0>(b);
});
double res = (sum_cons - y) / sum_coef;
if (res <= std::get<0>(coef[0])) {
solutions.push_back(res);
}
for (size_t i = 1; i < coef.size(); ++i) {
sum_cons -= std::get<1>(coef[i - 1]);
sum_coef -= std::get<2>(coef[i - 1]);
res = (sum_cons - y) / sum_coef;
if (res <= std::get<0>(coef[i])) {
solutions.push_back(res);
break;
}
}
return solutions;
}
// Shale Offline
void ShaleOffline::init() {
for (const auto& j : this->demand.getDemandKeys()) {
this->alphaJ[j] = 0.0;
double sum = 0.0;
for (const auto& i : this->demand.getTargetSupply(j)) {
double supply = this->supply.getSupply(i);
if (supply < 0) {
continue;
}
sum += supply;
}
if (sum <= 0.0) { // no mapping supply, just set thetaIJ as 1.0
this->thetaIJ[j] = 1.0;
} else {
this->thetaIJ[j] = this->demand.getDemandBudget(j) / sum;
}
}
}
std::vector<std::string> ShaleOffline::getDemandAllocationOrder() {
std::vector<std::pair<std::string, double>> keyValuePairs(this->thetaIJ.begin(), this->thetaIJ.end());
std::sort(keyValuePairs.begin(), keyValuePairs.end(), compareByKey);
std::vector<std::string> result;
for (const auto& pair : keyValuePairs) {
result.push_back(pair.first);
}
return result;
}
void ShaleOffline::stageOne(int iters) {
while (iters > 0) {
std::cout << "remaining iters round: " << iters << std::endl;
// control alpha, update beta
for (const auto& i : this->supply.getSupplyKeys()) {
this->updateBeta(i);
}
// control beta, update alpha
for (const auto& j : this->demand.getDemandKeys()) {
this->updateAlpha(j);
}
iters--;
}
}
void ShaleOffline::stageTwo() {
for (const auto& i : this->supply.getSupplyKeys()) {
this->sI[i] = this->supply.getSupply(i);
this->updateBeta(i);
}
for (const auto& pair : this->betaI) {
std::cout << "betaI key: " << pair.first << ", value: " << pair.second << std::endl;
}
std::unordered_map<std::string, double> sigma;
for (const auto& j : getDemandAllocationOrder()) {
// for (const auto& j : this->demand.getDemandKeys()) {
// update logic here
this->findSigma(j);
for (const auto& i : this->demand.getTargetSupply(j)) {
double g = std::max(0.0, this->thetaIJ[j] * (1.0 + (sigma[j] - this->betaI[i]) / this->demand.getV(j)));
this->sI[i] -= std::min(this->sI[i], this->supply.getSupply(i) * g);
}
}
}
void ShaleOffline::print() {
std::cout << "<--- alpha --->" << std::endl;
for (const auto& kv : this->alphaJ) {
std::cout << "demand alpha: " << kv.first << " : " << kv.second << std::endl;
}
std::cout << "<--- beta --->" << std::endl;
for (const auto& kv : this->betaI) {
std::cout << "supply beta: " << kv.first << " : " << kv.second << std::endl;
}
std::cout << "<--- sigma --->" << std::endl;
for (const auto& kv : this->sigmaJ) {
std::cout << "demand sigma: " << kv.first << " : " << kv.second << std::endl;
}
std::cout << "<--- sI --->" << std::endl;
for (const auto& kv : this->sI) {
std::cout << "supply sI: " << kv.first << " : " << kv.second << std::endl;
}
std::cout << "<--- thetaIJ --->" << std::endl;
for (const auto& kv : this->thetaIJ) {
std::cout << "demand thetaIJ: " << kv.first << " : " << kv.second << std::endl;
}
}
std::vector<double> ShaleOffline::updateSigma(const std::string& j) {
std::vector<double> solutions;
std::vector<std::tuple<double, double, double, double, double>> coef;
// Assuming you have these member functions and variables defined in your class
auto targetSupply = this->demand.getTargetSupply(j);
double demand_v = this->demand.getV(j);
for (auto i : targetSupply) {
double s = this->supply.getSupply(i);
double a = s * this->thetaIJ[j] * (1.0 - this->betaI[i] / demand_v);
double b = s * this->thetaIJ[j] / demand_v;
coef.push_back(std::make_tuple((this->sI[i] - a) / b, a / b, a, b, this->sI[i]));
}
std::sort(coef.begin(), coef.end(), [](const auto &t1, const auto &t2) {
return std::get<1>(t1) < std::get<1>(t2);
});
for (size_t k = 0; k < coef.size(); ++k) {
std::vector<std::tuple<double, double, double, double, double>> temp(coef.begin() + k, coef.end());
std::sort(temp.begin(), temp.end(), [](const auto &t1, const auto &t2) {
return std::get<0>(t1) < std::get<0>(t2);
});
double sum_remained = 0.0;
double sum_cons = 0.0;
double sum_coef = 0.0;
for (const auto &t : temp) {
sum_cons += std::get<2>(t);
sum_coef += std::get<3>(t);
sum_remained += std::get<4>(t);
}
double res = (sum_cons - this->demand.getDemandBudget(j)) / sum_coef;
if (sum_remained < this->demand.getDemandBudget(j)) {
continue;
}
if (k == 0) {
if (res <= std::get<0>(temp[0]) && res <= std::get<1>(coef[k])) {
solutions.push_back(res);
}
} else {
if (res <= std::get<0>(temp[0]) && res <= std::get<1>(coef[k]) && res >= std::get<1>(coef[k - 1])) {
solutions.push_back(res);
}
}
sum_remained = 0.0;
for (size_t t = 1; t < temp.size(); ++t) {
sum_cons -= std::get<2>(temp[t - 1]);
sum_coef -= std::get<3>(temp[t - 1]);
sum_remained += std::get<4>(temp[t - 1]);
res = (sum_cons + sum_remained - this->demand.getDemandBudget(j)) / sum_coef;
if (k == 0) {
if (res <= std::get<0>(temp[t]) && res >= std::get<0>(temp[t - 1]) && res <= std::get<1>(coef[k])) {
solutions.push_back(res);
}
} else {
if (res <= std::get<0>(temp[t]) && res >= std::get<0>(temp[t - 1]) &&
res <= std::get<1>(coef[k]) && res >= std::get<1>(coef[k - 1])) {
solutions.push_back(res);
}
}
}
}
return solutions;
}
void ShaleOffline::findSigma(const std::string& j) {
std::vector<double> result = this->updateSigma(j);
if (result.empty()) {
this->sigmaJ[j] = std::numeric_limits<double>::infinity();
} else {
this->sigmaJ[j] = -1.0 * result[0];
}
}
void ShaleOffline::updateBeta(const std::string& i) {
std::vector<std::tuple<double, double, double>> coef;
// iterate through j, sum j belongs to i
auto satisfyDemandList = this->supply.getSatisfyDemandList();
for (const auto& j : satisfyDemandList[i]) {
// max(sum(thetaJ * (1+ (alphaJ - betaI) / V)) = 1
// a = sum(thetaJ * (1+ alphaJ / V))
// b = sum(thetaJ * 1/V) * betaI
// max(a - b * betaI) = 1
double a = this->thetaIJ[j] * (1.0 + this->alphaJ[j] / this->demand.getV(j));
double b = this->thetaIJ[j] / this->demand.getV(j);
coef.emplace_back(std::make_tuple(a / b, a, b));
}
std::vector<double> result = mathMax(coef, 1.0);
if (result.empty() || result[0] < 0.0) {
this->betaI[i] = 0.0;
}
else {
this->betaI[i] = result[0];
}
std::cout << "update supply beta: " << i << " with betaI: " << this->betaI[i] << std::endl;
}
void ShaleOffline::updateAlpha(const std::string& j) {
std::vector<std::tuple<double, double, double>> coef;
// iterate through i, sum i belong to j
for (const auto& i : this->demand.getTargetSupply(j)) {
double s = this->supply.getSupply(i);
double a = -1.0 * s * this->thetaIJ[j] * (1.0 - this->betaI[i] / this->demand.getV(j));
double b = -1.0 * s * this->thetaIJ[j] / this->demand.getV(j);
coef.emplace_back(std::make_tuple(a / b, a, b));
}
std::vector<double> result = mathMax(coef, -1.0 * this->demand.getDemandBudget(j));
if (result.empty() || -1.0 * result[0] > this->demand.getPenalty(j)) {
this->alphaJ[j] = this->demand.getPenalty(j); // aj = pj
}
else {
this->alphaJ[j] = -1.0 * result[0];
}
std::cout << "update demand alpha: " << j << " with alphaJ: " << this->alphaJ[j] << std::endl;
}
// Shale Online
void ShaleOnline::init() {
for (const auto& i : this->supply.getSupplyKeys()) {
this->remainedI[i] = this->supply.getSupply(i);
}
for (const auto& j : this->demand.getDemandKeys()) {
double sum = 0.0;
for (const auto& i : this->demand.getTargetSupply(j)) {
sum += this->supply.getSupply(i);
}
thetaIJ[j] = this->demand.getDemandBudget(j) / sum;
this->allocationJ[j] = 0;
}
}
std::vector<std::string> ShaleOnline::getDemandAllocationOrderV2(const std::string& i) {
auto demandListMap = this->supply.getSatisfyDemandList();
auto demandListI = demandListMap[i];
std::vector<std::pair<std::string, double>> keyValuePairs(this->thetaIJ.begin(), this->thetaIJ.end());
std::sort(keyValuePairs.begin(), keyValuePairs.end(), compareByKey);
std::vector<std::string> result;
for (const auto& pair : keyValuePairs) {
if (existsInVector(pair.first, demandListI)) {
result.push_back(pair.first);
}
}
return result;
}
void ShaleOnline::allocation(const std::string& i) {
double s = 1.0;
std::unordered_map<std::string, double> xIJ;
if (this->betaI.find(i) == this->betaI.end()) {
this->updateBeta(i);
}
// auto demandList = this->supply.getSatisfyDemandList();
// for (const auto& j : demandList[i]) {
auto orderedDemandList = getDemandAllocationOrderV2(i);
for (const auto& j : orderedDemandList) {
// std::cout << "thetaIJ: " << this->thetaIJ[j] << std::endl;
double g = std::max(0.0, this->thetaIJ[j] * (1.0 + (this->sigmaJ[j] - this->betaI[i]) / this->demand.getV(j)));
xIJ[j] = std::min(s, g);
s -= xIJ[j];
}
// std::cout << "s value: " << s << std::endl;
double sum = 0.0;
for (const auto& kv : xIJ) {
sum += kv.second;
}
// if (sum < 1.0) {
// std::cout << "In supply: " << i << ", there is " << 1.0 - sum << " probability with no contract" << std::endl;
// }
double r = (double)rand() / RAND_MAX;
sum = 0.0;
for (const auto& kv : xIJ) {
sum += kv.second;
if (r < sum) {
this->allocationJ[kv.first]++;
this->remainedI[i]--;
break;
}
}
}
void ShaleOnline::updateBeta(const std::string& i) {
std::vector<std::tuple<double, double, double>> coef;
auto satisfyDemandList = this->supply.getSatisfyDemandList();
for (const auto& j : satisfyDemandList[i]) {
double a = this->thetaIJ[j] * (1.0 + this->alphaJ[j] / this->demand.getV(j));
double b = this->thetaIJ[j] / this->demand.getV(j);
coef.push_back(std::make_tuple(a / b, a, b));
}
std::vector<double> result = mathMax(coef, 1.0);
if (result.empty() || result[0] < 0.0) {
this->betaI[i] = 0.0;
}
else {
this->betaI[i] = result[0];
}
}
void ShaleOnline::print() {
// demand allocation
std::cout << "\nAllocation:" << std::endl;
std::cout << "demand_node\tdemand\t\tallocation" << std::endl;
for (const auto& kv : this->allocationJ) {
std::cout << kv.first << "\t" << this->demand.getDemandBudget(kv.first) << "\t" << kv.second << std::endl;
}
// supply remaining
std::cout << "\nRemained:" << std::endl;
std::cout << "supply_node\tinventory\tremained" << std::endl;
for (const auto& kv : this->remainedI) {
std::cout << kv.first << "\t" << this->supply.getSupply(kv.first) << "\t" << kv.second << std::endl;
}
}