-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategyPercentAll.java
More file actions
266 lines (184 loc) · 4.19 KB
/
StrategyPercentAll.java
File metadata and controls
266 lines (184 loc) · 4.19 KB
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
import java.util.ArrayList;
public class StrategyPercentAll extends Strategy
{
/**
* Encoding for tit-for-tat strategy.
*/
// 0 = defect, 1 = cooperate
int numDefects;
int moveNum;
int ourScore;
int theirScore;
// int theirMovesDefect_Index = 0; // Getting first defect index (= 0)
int co_OpCount = 0; // The cooperation count (maximum value 2) after a defect is detected
int numCoop = 0;
int t_NumDef = 0;
int defCount = 0;
ArrayList<Integer> ourMoves = new ArrayList<>();
ArrayList<Integer> theirMoves = new ArrayList<>();
ArrayList<Integer> ourScores = new ArrayList<>();
ArrayList<Integer> theirScores = new ArrayList<>();
//ArrayList<Integer> temp_theirMoves = new ArrayList<>(); //Copying a temporary ArrayList for evaluation and manipulation
public StrategyPercentAll(){
name = "Percent All";
moveNum = 0;
numDefects = 0;
ourScore = 0;
theirScore = 0;
}
public int nextMove(){
int move;
double selection = Math.random();
if(moveNum != 0)
{
ourMoves.add(myLastMove);
theirMoves.add(opponentLastMove);
ourScores.add(calcScore(myLastMove,opponentLastMove));
theirScores.add(calcScore(opponentLastMove,myLastMove));
ourScore += ourScores.get(moveNum-1);
theirScore += theirScores.get(moveNum-1);
}
if (selection < .1)
move = Pavlov();
else if(selection < .2)
move = Adaptive();
else if (selection < .3)
move = SoftGurder();
else if(selection < .4)
move = Gradual();
else if (selection < .5)
move = Friedmen();
else if(selection < .6)
move = AlwaysCooperate();
else
move = TitForTat();
moveNum++;
return move;
}
public int TitForTat()
{
//name = "Tit for Tat";
if(moveNum == 0)
opponentLastMove = 1;
return opponentLastMove;
} /* StrategyTitForTat */
public int Adaptive(){
double averageCooperate = 0;
double averageDefect = 0;
int numCoop = 0;
int numDef = 0;
if(moveNum < 5)
return 1;
else if(moveNum < 10)
return 0;
for(int i=0; i < ourMoves.size(); i++){
if(ourMoves.get(i)==1){
averageCooperate += ourScores.get(i);
numCoop++;
}
else{
averageDefect += ourScores.get(i);
numDef++;
}
}
averageCooperate /= numCoop;
averageDefect /= numDef;
return averageCooperate >= averageDefect ? 1 : 0;
}
public int Pavlov(){
if(moveNum == 0)
return 1;
if(ourScores.get(moveNum-1) == 3 || ourScores.get(moveNum-1) == 5)
return myLastMove;
else
return (myLastMove + 1) % 2;
}
// 0 = defect, 1 = cooperate
public int TitForTwoTats()
{
//name = "Tit for Two Tats";
if(moveNum == 0)
{
opponentLastMove = 1;
numDefects = 0;
}
if (opponentLastMove == 0) numDefects++;
if (opponentLastMove == 1)
{
numDefects = 0;
return 1;
}
else
{
if (opponentLastMove == 0 && numDefects < 2)
return 1;
else
{
return 0;
}
}
}
public int AlwaysCooperate()
{
//name = "Always cooperate";
return 1;
} /* StrategyAlwaysCooperate */
public int Random()
{
//name = "Random";
if (Math.random() < 0.5) return 1;
return 0;
} /* StrategyRandom */
public int calcScore(int me, int op){
if(me == 0 && op == 0)
return 3;
if(me == 0 && op == 1)
return 7;
if(me == 1 && op == 1)
return 5;
return 1;
}
public int Gradual() {
for(int i=0; i < theirMoves.size(); i++){
if(theirMoves.get(i)==1){
numCoop++;
}
else{
t_NumDef++;
}
}
if(t_NumDef > 0) {
co_OpCount=2;
t_NumDef--;
return 0;
}
if (co_OpCount > 0){
co_OpCount--;
return 1;
}
return 1 ;
}
public int SoftGurder(){
for(int i=0; i < theirMoves.size(); i++){
if(theirMoves.get(i)==0)
t_NumDef = 4;
if(t_NumDef > 0) {
co_OpCount=2;
t_NumDef--;
return 0;
}
if (co_OpCount > 0){
co_OpCount--;
return 1;
}
}
return 1;
}
public int Friedmen(){
for(int i=0; i < theirMoves.size(); i++){
if(theirMoves.get(i)==0)
return 0;
}
return 1 ;
}
}