-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarketEquilibrium.cpp
99 lines (88 loc) · 2.42 KB
/
MarketEquilibrium.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
//
// Created by Allen Chen on 8/29/16.
//
//
// Created by Allen Chen on 8/29/16.
//
//
// Created by Allen Chen on 8/3/16.
//
#include "TestPlat.h"
class MarketEquilibrium : public SlnBase {
vector<vector<double>> TransferMat;
vector<double> init_share;
vector<double> eq_share;
void PrintDesc() {
cout << "get the stable market share " << endl;
}
void InputLocal() {
double a[] = {0.4, 0.6};
double t[2][2] = { {0.8,0.2}, {0.1,0.9} };
int n = 2;
init_share.insert(init_share.begin(),a,a+ sizeof(a)/ sizeof(double));
for(int i = 0;i < n; i++){
vector<double> tmp;
for(int j = 0;j < n; j++){
tmp.push_back(t[i][j]);
}
TransferMat.push_back(tmp);
}
}
void InputFromFile(ifstream &fh) {
int n;
fh >> n;
for(int i = 0;i < n; i ++){
int tmp;
fh >> tmp;
init_share.push_back(tmp);
}
for(int i = 0;i < n; i++){
vector<double> tmpv;
for(int j = 0; j < n; j++){
int tmp;
fh >> tmp;
tmpv.push_back(tmp);
}
TransferMat.push_back(tmpv);
}
}
void PrintInput() {
PrintAll(init_share);
PrintAll(TransferMat);
}
void PrintResult() {
PrintAll(eq_share);
}
void Algo(){
eq_share = test(init_share,TransferMat);
}
vector<double> test(vector<double> init,vector<vector<double>> mat) {
vector<double> preshare = init, curshare = init;
int n = (int) init.size();
int i = 0, j = 0;
bool isStable = false;
double dif;
//cout << n << endl;
while(!isStable){
dif = 0;
for(i = 0; i < n; i ++){
double cur = 0;
for(j = 0; j < n; j ++){
double tmp = preshare[j];
double TransProb = mat[j][i];
cur += tmp*TransProb;
}
cur = floor(cur*1000)/1000;
curshare[i] = cur;
dif += abs(cur-preshare[i]);
}
preshare = curshare;
//PrintAll(curshare);
if(dif == 0){
isStable = true;
}
}
return curshare;
};
};
const bool reg1 = TestPlat::reg<MarketEquilibrium>("MarketEquilibrium ");