-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_function.cpp
407 lines (361 loc) · 11.2 KB
/
game_function.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "game_function.h"
//extern vector<vector<int> > vLadjmx;
int GAgame::ruleupdate(double dg, double dr)//策略更新接口
{
if (nSyncType == SYNC) //SYNC=40 同步更新
{
switch (nImitType)
{
//case 30: imitmaxs(); break; //无条件学习unconditional imitation
//case 31: imitrands(); break; // 随机学习
//case 32: imitdyns(dg, dr); break; // replicator dynamic 动态学习
//case 33: imitgas(); break; //遗传算法
case 34: imitsecs(); break; //学习次大者
default: exit(331);
}
}
else //SYNC=41 异步更新
{
switch (nImitType)
{
//case 30: imitmaxas(); break; //无条件学习unconditional imitation
//case 31: imitrandas(); break; // 随机学习
//case 32: imitdynas(dg); break; // replicator dynamic 动态学习
//case 33: imitgaas(); break; //遗传算法
case 34: imitsecas(); break; //学习次大者
default: exit(332);
}
}
return 0;
}
/*
同步更新 学习次大者
by 张松磊 201226010427
2016/04/10
*/
int GAgame::imitsecs()
{
int i, j, k, max, sec;
//printf(" imitmaxs ");
//Gsize=1000
for (i = 0; i < Gsize; i++)//For each NODE,we need to do SELECTION,CROSSOVER,AND MUTATION
{
max = sec = i;
if (vLadj[i].size() == 0)
{
for (j = 0; j < MEMLEN; j++)//memlength
MidStrategy[i][j] = Strategy[i][j];
continue;
}
// if(vLadj[i].size()==0) continue;
for (j = 0; j < (int)vLadj[i].size(); j++)
{
k = vLadj[i][j];
if (Fitness[max] < Fitness[k])
{
sec = max;
max = k;
}
else if (Fitness[sec] < Fitness[k] && Fitness[max] != Fitness[k])
sec = k;
}
for (j = 0; j < MEMLEN; j++)//memlength
MidStrategy[i][j] = Strategy[sec][j];
}//END OF for(i<Gsize)
//printf("\nstrategt ga \n");
for (i = 0; i < Gsize; i++)
{
for (j = 0; j < MEMLEN; j++)
{
Strategy[i][j] = MidStrategy[i][j];
if (rand() % 100<1)
Strategy[i][j] = 1 - Strategy[i][j];
if (Strategy[i][j] > 1)
{
printf("strategy wrong at geneticalgrithm %d %d\n", i, j);
exit(0);
}
}
// printf("%d%d%d%d\t",Strategy[i][0],Strategy[i][1],Strategy[i][2],Strategy[i][3] );
}
return 0;
}
/*
异步更新 学习次大者
by 张松磊 201226010427
2016/04/12
*/
int GAgame::imitsecas()// now not ga 2011 2 11
{
int i, j, k, max, sec;
i = Randnode;
sec = max = i;
// if(vLadj[i].size()==0) continue;
for (j = 0; j < (int)vLadj[i].size(); j++)
{
k = vLadj[i][j];
if (Fitness[max] < Fitness[k])
{
sec = max;
max = k;
}
else if (Fitness[sec] < Fitness[k] && Fitness[max] != Fitness[k])
sec = k;
}
for (j = 0; j < MEMLEN; j++)//memlength
{
Strategy[i][j] = Strategy[sec][j];
if (rand() % 100 < 1)
Strategy[i][j] = 1 - Strategy[i][j];
}
// printf(" imitmaxs over ");
return 0;
}
//////////////////////////////////////////////////////////////
double GAgame::getpayoff()// 收益 计算,取各种平均,博弈次数,群体个数
{
int i;
double payoff = 0;
if (Gsize>100)
{
for (i = 0; i<Gsize; i++)
{
if (vLadj[i].size()>0)
payoff = payoff + Fitness[i];
}
payoff = payoff / (Gsize * 200 * JLOOP);
}
else
{
for (i = 0; i<Gsize; i++)
{
if (vLadj[i].size()>0)
payoff = payoff + Fitness[i] / vLadj[i].size();
}
payoff = payoff / Gsize;
}
return payoff;
}
int GAgame::Strtinit()//个体策略初始化
{
int i, k;
for (i = 0; i<Gsize; i++)
for (k = 0; k<MEMLEN; k++)////////////////////////////////////////memlength///////////////
{
Strategy[i][k] = (unsigned short)(rand() % 2);
// cout<<Strategy[i][k]<<"\t";
// if(Strategy[i][k]>1) {printf("strategy wrong at Strategy init i %d k %d\n",i,k); exit (0);}
}
return 0;
}
int GAgame::Infoprint()//打印一些信息
{
int i, j, k;
for (i = 0; i<Gsize; i++)
{
printf("\t node %d ", i);
for (k = 0; k<MEMLEN; k++)////////////////////////////////////////memlength///////////////
{
printf(" %d ", Strategy[i][k]);
// if(Strategy[i][k]>1) {printf("strategy wrong at Strategy init i %d k %d\n",i,k); exit (0);}
}
}
printf("\n");
for (i = 0; i<Gsize; i++)
{
printf("\t %d %lf ", i, Fitness[i]);
}
/* for(i=0;i<Gsize;i++)
{
printf("\n %d ",i);
for(j=0;j<vLadj[i].size();j++)
printf("%lf ",Pfmx[i][vLadj[i][j]]);
}
*/
printf("history\n");
for (i = 0; i<Gsize; i++)
for (j = 0; j<vLadj[i].size(); j++)
printf("%d %d %d\t", i, vLadj[i][j], (GameHistory[i][vLadj[i][j]]) % 2);
return 0;
}
int GAgame::Hstinit(double rt)//博弈历史初始化,主要对有记忆情况
{
int i, j, k, ans1, ans2;
for (i = 0; i<Gsize; i++)
for (k = i + 1; k<Gsize; k++)//Some values are meaningless
{
for (j = 0; j<MEM; j++)//memlength
{
if (rand() % 100 / 100.00<rt)
ans1 = 1;
else
ans1 = 0;
if (rand() % 100 / 100.00<rt)
ans2 = 1;
else
ans2 = 0;
GameHistory[i][k] = (GameHistory[i][k] << 2); //左移操作符
GameHistory[i][k] = GameHistory[i][k] + ans1 * 2 + ans2;
GameHistory[k][i] = (GameHistory[k][i] << 2);
GameHistory[k][i] = GameHistory[k][i] + ans2 * 2 + ans1;
}
}
return 0;
}
int GAgame::Fitinit()//个体适应度水平初始化
{
Fitness.assign(Gsize, 0.0);
Pfmx.assign(Gsize, vector<double>(Gsize, 0.0));
GameCountmx.assign(Gsize, vector<int>(Gsize, 0));
return 0;
}
GAgame::GAgame(int gamet, int imitt, int synct, int mem, const vector<vector<int> > &vTemp) :vLadj(vTemp)//构造函数,将网络结构通过vTemp传过来
{
// vLadj=vTemp;
Gsize = (int)vLadj.size();
GAMETYPE = gamet;
nImitType = imitt;
nSyncType = synct;
MEM = mem; // printf("ga ok0");
switch (mem)
{
case 0: MEMLEN = 1, GLOOP = 1; break;
case 1: MEMLEN = 4, GLOOP = 20; break;//if(synctype==41)GLOOP=8;
case 2: MEMLEN = 16, GLOOP = 40; break;//if(synctype==41)GLOOP=32;
case 3: MEMLEN = 64, GLOOP = 80; break;//if(synctype==41)GLOOP=32;
case 4: MEMLEN = 256, GLOOP = 100; break;//if(synctype==41)GLOOP=32;
case 5: MEMLEN = 1024, GLOOP = 200; break;//if(synctype==41)GLOOP=32;
case 6: MEMLEN = 4096, GLOOP = 200; break;//if(synctype==41)GLOOP=32;
default:exit(24);
}
// printf("ga ok");
Strategy.resize(Gsize, vector<int>(MEMLEN));
MidStrategy.resize(Gsize, vector<int>(MEMLEN, 0));
GameHistory.resize(Gsize, vector<unsigned>(Gsize));
Pfmx.resize(Gsize, vector<double>(Gsize, 0.0));
Fitness.resize(Gsize, 0);
// cout<<Strategy[1][1]<<" ";
GameCountmx.resize(Gsize, vector<int>(Gsize, 0));
Strtinit();
Hstinit(0.5);//初始化合作比0.5
}
GAgame::~GAgame()
{
}
double GAgame::Game(double dg, double dr)//Determine the fitness of each strategy具体博弈 静止模式
{
// printf(" Game dg ");
double incomeMatrix[2][2], C_Ratio;//收益矩阵,合作水平
int m, n, i, j, node1, node2;
int strategy1, strategy2;
int StrategyCount[2] = { 0, 0 };//StrategyCount[0]背叛者的数量 StrategyCount[1]合作者的数量
switch (GAMETYPE)
{
case 20:
//for 2x2 incomeMatrix, the subscript variable have the opposite meaning of game stratege,in game 0 means betray,1 means cooperate.
incomeMatrix[0][0] = 1; //R
incomeMatrix[0][1] = dr - 1; //S -1~1
incomeMatrix[1][0] = dg; //T 0~2 dg gr 0-2
incomeMatrix[1][1] = 0; //P
break;
case 21: // pd game
incomeMatrix[0][0] = 1.0; //R dg 0-1
incomeMatrix[0][1] = 0.0; //S
incomeMatrix[1][0] = dg + 1.0; //T
incomeMatrix[1][1] = 0.0; //P
break;
case 22: // SD
incomeMatrix[0][0] = 1.0; //R
incomeMatrix[0][1] = 1.0 - dg; //S
incomeMatrix[1][0] = 1.0 + dg; //T
incomeMatrix[1][1] = 0.0; //P
break;
case 23: //SH
incomeMatrix[0][0] = 1.0; //R
incomeMatrix[0][1] = -dg; //S
incomeMatrix[1][0] = dg; //T
incomeMatrix[1][1] = 0.0; //P
break;
case 24: //great2x2 dg dr 0-1.5
incomeMatrix[0][0] = 1.0; //R
incomeMatrix[0][1] = (dr * 10 - 4)*1.0 / 2; //S -2-5 14 samples
incomeMatrix[1][0] = (dg * 10 - 2)*1.0 / 2; //T -1-6 14 samples
incomeMatrix[1][1] = 0.0; //P
break;
case 25: //general
incomeMatrix[0][0] = 1.0;
incomeMatrix[0][1] = dg; //S
incomeMatrix[1][0] = dr; //T
incomeMatrix[1][1] = 0.0; //P
break;
default: exit(29);
}
// Fitinit();
// Hstinit();
if (Gsize>100)// 群体大小一般是1000,小于100 单独处理 ,估计是提高计算速度
{
int ti;
for (m = 0; m<Gsize; m++)
for (n = 0; n<100; n++)
{
node1 = m;
ti = rand() % (int)vLadj[m].size(); //??????????????
node2 = vLadj[m][ti];
for (i = 0; i<GLOOP; i++)
{
strategy1 = Strategy[node1][GameHistory[node1][node2] % MEMLEN];//memlenghth
strategy2 = Strategy[node2][GameHistory[node2][node1] % MEMLEN];//memlenghth
Pfmx[node1][node2] = Pfmx[node1][node2] + incomeMatrix[1 - strategy1][1 - strategy2];
Pfmx[node2][node1] = Pfmx[node2][node1] + incomeMatrix[1 - strategy2][1 - strategy1];
StrategyCount[strategy1]++;
StrategyCount[strategy2]++;
GameHistory[node1][node2] = (GameHistory[node1][node2] << 2);
GameHistory[node1][node2] = GameHistory[node1][node2] + strategy1 * 2 + strategy2;
GameHistory[node2][node1] = (GameHistory[node2][node1] << 2);
GameHistory[node2][node1] = GameHistory[node2][node1] + strategy2 * 2 + strategy1;
}
}
for (i = 0; i<Gsize; i++)
{
Fitness[i] = 0;
for (j = 0; j<Gsize; j++)
Fitness[i] = Fitness[i] + Pfmx[i][j];
}
}
else
{
for (i = 0; i<GLOOP; i++)
{
for (m = 0; m<(int)vLadj.size(); m++)
for (n = 0; n<(int)vLadj[m].size(); n++)
{
node1 = m;
node2 = vLadj[m][n];
strategy1 = Strategy[node1][GameHistory[node1][node2] % MEMLEN];//memlenghth
strategy2 = Strategy[node2][GameHistory[node2][node1] % MEMLEN];//memlenghth
Pfmx[node1][node2] = Pfmx[node1][node2] + incomeMatrix[1 - strategy1][1 - strategy2];
Pfmx[node2][node1] = Pfmx[node2][node1] + incomeMatrix[1 - strategy2][1 - strategy1];
StrategyCount[strategy1]++;
StrategyCount[strategy2]++;
GameHistory[node1][node2] = (GameHistory[node1][node2] << 2);
GameHistory[node1][node2] = GameHistory[node1][node2] + strategy1 * 2 + strategy2;
GameHistory[node2][node1] = (GameHistory[node2][node1] << 2);
GameHistory[node2][node1] = GameHistory[node2][node1] + strategy2 * 2 + strategy1;
}
}
for (i = 0; i<Gsize; i++)
for (j = 0; j<vLadj[i].size(); j++)
Pfmx[i][vLadj[i][j]] /= (2 * GLOOP);//previous' /2' //conside 2 times count for one game in vladjmx
for (i = 0; i<Gsize; i++)
{
Fitness[i] = 0;
for (j = 0; j<vLadj[i].size(); j++)
Fitness[i] = Fitness[i] + Pfmx[i][vLadj[i][j]];
// if (vLadj[i].size()>0) Fitness[i]=Fitness[i]/vLadj[i].size(); //(GLOOP*2); 130614
}
// for(i=0;i<Gsize;i++)
// if (vLadj[i].size()>0) Fitness[i]=Fitness[i]/(GLOOP*2);////conside the degree effection
}
C_Ratio = (double)StrategyCount[1] / (StrategyCount[0] + StrategyCount[1]);
return C_Ratio;
}