-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
257 lines (236 loc) · 6.45 KB
/
run.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
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
"use strict"; "use strong"
let FRESH = 0;
let STARTED = 1;
let STATE = FRESH;
// GUI variables
let neighborhoodSize;
let neighborhoodSizeText;
let neighborhoodProp;
let neighborhoodPropText;
let numRounds;
let numRoundsText;
let gamesPerRound;
let gamesPerRoundText;
let matcher;
let matcherInst;
let learner;
let learnerInst;
let matchers = [ "Neighborhood", "Random" ];
let learners = [ "Neighborhood", "Overall", "Random" ];
let strategies = [ "Tit-for-Tat", "Always Defect", "Always Cooperate" ];
let strategy1;
let strategy2;
let strategy3;
let strategy1Text;
let strategy2Text;
let strategy3Text;
// globals
let players;
let tournament;
let width = 40;
let height = 40;
let squareSize = 15;
let sliderWidth = "150px";
function matchersSelect() {
matcher = createSelect();
matcher.position(10, 200);
let matcherText = createDiv("Matcher Type:");
matcherText.position(10, 180);
matchers.forEach(function(name) {
matcher.option(name);
});
matcher.value(matchers[0]);
}
function getMatcher() {
let matcherInst;
switch (matcher.value()) {
case "Neighborhood":
matcherInst = new NeighborhoodMatcher(neighborhoodSize.value(),
players,
neighborhoodProp.value() / 100);
break;
case "Random":
matcherInst = new RandomMatcher(neighborhoodSize.value(),
players,
neighborhoodProp.value() / 100);
break;
}
return matcherInst;
}
function learnersSelect() {
learner = createSelect();
learner.position(10, 250);
let learnerText = createDiv("Learner Type:");
learnerText.position(10, 230);
learners.forEach(function(name) {
learner.option(name);
});
learner.value(learners[0]);
}
function getLearner() {
let learnerInst;
switch (learner.value()) {
case "Neighborhood":
learnerInst = new NeighborhoodLearner(neighborhoodSize.value(),
players);
break;
case "Random":
learnerInst = new RandomLearner();
break;
case "Overall":
learnerInst = new OverallLearner();
break;
}
return learnerInst;
}
function neighborhoodSizeSlider() {
neighborhoodSize = createSlider(1, 10, 1);
neighborhoodSize.position(10, 300);
neighborhoodSize.style('width', sliderWidth);
neighborhoodSizeText = createDiv("Neighborhood Size: " + neighborhoodSize.value());
neighborhoodSizeText.position(10, 280);
neighborhoodSize.changed(function(x) {
neighborhoodSizeText.html("Neighborhood Size: " + neighborhoodSize.value());
initialize();
});
}
function neighborhoodPropSlider() {
neighborhoodProp = createSlider(0, 100, 50);
neighborhoodProp.position(10, 340);
neighborhoodProp.style('width', sliderWidth);
neighborhoodPropText = createDiv("Neighborhood Bias: " + neighborhoodProp.value() / 100);
neighborhoodPropText.position(10, 320);
neighborhoodProp.changed(function(x) {
neighborhoodPropText.html("Neighborhood Bias: " + neighborhoodProp.value() / 100);
initialize();
});
}
function numRoundsSlider() {
numRounds = createSlider(1, 10, 1);
numRounds.position(10, 380);
numRounds.style('width', sliderWidth);
numRoundsText = createDiv("Number of Rounds: " + numRounds.value());
numRoundsText.position(10, 360);
numRounds.changed(function(x) {
numRoundsText.html("Number of Rounds: " + numRounds.value());
initialize();
});
}
function gamesPerRoundSlider() {
gamesPerRound = createSlider(1, 30, 15);
gamesPerRound.position(10, 420);
gamesPerRound.style('width', sliderWidth);
gamesPerRoundText = createDiv("Number of Games per Round: " + gamesPerRound.value());
gamesPerRoundText.position(10, 400);
gamesPerRound.changed(function(x) {
gamesPerRoundText.html("Number of Games per round: " + gamesPerRound.value());
initialize();
});
}
function strategy1Select() {
strategy1 = createSelect();
strategy1.position(10, 460);
let strategy1Text = createDiv("Strategy 1:");
strategy1Text.position(10, 440);
strategies.forEach(function(name) {
strategy1.option(name);
});
strategy1.changed(function(x) {
initialize();
});
strategy1.value(strategies[0]);
}
function strategy2Select() {
strategy2 = createSelect();
strategy2.position(10, 510);
let strategy2Text = createDiv("Strategy 2:");
strategy2Text.position(10, 490);
strategies.forEach(function(name) {
strategy2.option(name);
});
strategy2.changed(function(x) {
initialize();
});
strategy2.value(strategies[1]);
}
function strategy3Select() {
strategy3 = createSelect();
strategy3.position(10, 560);
let strategy3Text = createDiv("Strategy 3:");
strategy3Text.position(10, 540);
strategies.forEach(function(name) {
strategy3.option(name);
});
strategy3.changed(function(x) {
initialize();
});
strategy3.value(strategies[2]);
}
function getStrategy(select) {
switch (select.value()) {
case "Always Cooperate":
return new AllCooperate();
break;
case "Always Defect":
return new AllDefect();
break;
case "Tit-for-Tat":
return new TitForTat();
break;
}
}
function setup() {
strategy1Select();
strategy2Select();
strategy3Select();
matchersSelect();
learnersSelect();
neighborhoodSizeSlider();
neighborhoodPropSlider();
numRoundsSlider();
gamesPerRoundSlider();
noLoop();
initialize();
//frameRate(3);
let c = createCanvas(800, 800);
c.parent("sketch");
stroke(255);
}
function initialize() {
players = [];
let pd = new PrisonersDilemma();
for (let i = 0; i < height; i++) {
let row = [];
for (let j = 0; j < width; j++) {
let r = Math.round(random(1, 3));
let strategy;
if (r == 1) {
strategy = getStrategy(strategy1);
} else if (r == 2) {
strategy = getStrategy(strategy2);
} else {
strategy = getStrategy(strategy3);
}
row.push(new Player(i, j, strategy));
}
players.push(row);
}
tournament = new Tournament(pd, getMatcher(), players, getLearner(), numRounds.value(), gamesPerRound.value());
redraw();
}
function keyPressed() {
if (keyCode == ENTER) {
tournament.play();
redraw();
STATE == STARTED;
} else if (keyCode == BACKSPACE) {
STATE = FRESH;
initialize();
}
}
function draw() {
Player.flatPlayers(players).forEach(function(player) {
fill(player.getStrategy().color());
rect(player.x * squareSize, player.y * squareSize, squareSize, squareSize);
});
}