-
Notifications
You must be signed in to change notification settings - Fork 0
/
hd.js
126 lines (119 loc) · 4.51 KB
/
hd.js
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
class HawkDove {
constructor() {
//0 = NEUMANN NEIGHBOURHOOD, 1 = MOORE NEIGHBOURHOOD, 2 = ?
this.stratArray = [
'hawk',
'dove',
'empty'
]
this.distributions = [0.5, 0.5];
this.bioSettings = [0.1, 0.5, 0.4];
this.sliceNeighbours = function(rect) {
let neighbours = undefined;
if (neighbourhoodType === 0) {
neighbours = rect.neighbours.slice(0, 4);
}
else if (neighbourhoodType === 1) {
neighbours = rect.neighbours;
}
return neighbours;
}
this.playGame = function(rect) {
let neighbours = this.sliceNeighbours(rect);
let score = 0;
if (selfInteractions) {
if (rect.strategy == 'hawk' ) {
score += (u - c)/2;
}
if (rect.strategy == 'dove' ) {
score += u/2;
}
}
neighbours.forEach(neighbour => {
//mutual cooperation
if (neighbour.strategy == 'hawk' && rect.strategy == 'hawk') {
score += (u - c)/2;
}
//suckers payoff cooperation
if (neighbour.strategy == 'dove' && rect.strategy == 'dove') {
score += u/2;
}
if (neighbour.strategy == 'dove' && rect.strategy == 'hawk') {
score += u;
}
});
rect.score = score;
}
//updates double buffer strategies (***stratNew)
this.updateRandomly = function(rect) {
if (generations % 1 === 0) {
let neighbours = this.sliceNeighbours(rect);
let neighbour = neighbours[Math.floor(Math.random() * neighbours.length)];
if (rect.score < neighbour.score) {
rect.strategyNew = neighbour.strategy;
}
else {
rect.strategyNew = rect.strategy;
}
}
}
this.updateDeterministic = function(rect) {
if (generations % 1 === 0) {
let bestStrat = undefined;
let highScore = -999;
const neighbours = this.sliceNeighbours(rect);
neighbours.forEach(neighbour => {
if (neighbour.score > highScore) {
highScore = neighbour.score;
bestStrat = neighbour.strategy;
}
});
if (rect.score < highScore) {
rect.strategyNew = bestStrat;
}
}
}
this.updateBiological = function(rect) {
let probabilityArray = [0.1, 0.5, 0.4];
const distribution = createDistribution([0, 1, 2],
probabilityArray,
10);
const decision = randomIndex(distribution);
let player = rectsArray[Math.floor(Math.random() * rectsArray.length)]
let neighbours = this.sliceNeighbours(player);
let neighbour = neighbours[Math.floor(Math.random() * neighbours.length)];
if (decision === 0) {
//selection!
if (player.score > neighbour.score && neighbour.strategy != 'empty' && player.strategy != 'empty') {
neighbour.strategy = 'empty';
neighbour.strategyNew = 'empty';
}
}
if (decision === 1) {
//reproduction!
if (player.strategy != 'empty' && neighbour.strategy === 'empty') {
neighbour.strategyNew = player.strategy;
}
}
if (decision === 2) {
//exchange!
if (player.strategy != 'empty' && neighbour.strategy != 'empty') {
const neighbourStrat = neighbour.strategy;
neighbour.strategy = player.strategy;
player.strategy = neighbourStrat;
}
}
}
this.updateStrategies = function(rect, updateRule) {
if (updateRule === 0) {
this.updateDeterministic(rect);
}
if (updateRule === 1) {
this.updateRandomly(rect);
}
if (updateRule === 2) {
this.updateBiological(rect);
}
}
}
}