-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathbot.cpp
345 lines (316 loc) · 8.25 KB
/
mathbot.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
#include <cstdio>
#include <iterator>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>
using namespace std;
ofstream debug ("mathbot.debug");
const int NUM_PREDICTORS = 5;
const bool DEBUG = true;
bool CONTEST = false;
int stat[3] = {0, 0, 0},
total = 0,
h,
turns[NUM_PREDICTORS * 2],
shift[NUM_PREDICTORS * 2];
long double expectation = 0.0,
expectations[NUM_PREDICTORS * 2],
rates [NUM_PREDICTORS * 2][3],
predictions [NUM_PREDICTORS * 2][3],
counts [NUM_PREDICTORS * 2][3];
string meaning[3] = {"Tie ", "Win ", "Lose"};
stringstream human, robot, result;
class Predictor{
public:
Predictor(){};
int total;
virtual long double* predict() = 0;
virtual void getInput() = 0;
};
class UnigramPredictor : public Predictor{
public:
UnigramPredictor(istream* is){
in = is;
total = 0;
}
long double* predict(){
getInput();
return count;
}
void getInput(){
char token;
token = in->get();
in->putback(token);
token -= '0';
++total;
for(int i = 0; i < 3; i++)
count[i] *= 0.87358;
count[token] += 1.0;
}
private:
istream* in;
long double count[3] = {0.0, 0.0, 0.0};
};
class BigramPredictor : public Predictor{
public:
BigramPredictor(istream* is1, istream* is2){
in1 = is1;
in2 = is2;
total = 0;
}
long double* predict(){
getInput();
return count[last];
}
void getInput(){
char token1, token2;
token1 = in1->get();
in1->putback(token1);
token1 -= '0';
token2 = in2->get();
in2->putback(token2);
token2 -= '0';
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
count[i][j] *= 0.9;
if(total > 0)
count[last][token2] += 1.0;
last = token1;
++total;
}
private:
int last;
long double count[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
istream* in1;
istream* in2;
};
class LongPatternMatcher : public Predictor{
public:
LongPatternMatcher(istream* is){
in = is;
total = 0;
}
long double* predict(){
getInput();
int last = history.back();
long double* ret = new long double[3];
ret[0] = 0; ret[1] = 0; ret[2] = 0;
toDelete = ret;
if(total < 5)
return ret;
for(int i = total - 2; i >= 0; i--){
for(int x = 0; history[i - x] == history[total - 1 - x] && x < 25; x++)
ret[last] += x;
last = history[i];
}
return ret;
}
void getInput(){
if(toDelete != 0){
delete[] toDelete;
}
char token;
token = in->get();
in->putback(token);
token -= '0';
history.push_back((unsigned char) token);
++total;
}
private:
istream* in;
long double* toDelete = 0x000000;
vector<unsigned char> history;
};
Predictor* predictors[NUM_PREDICTORS] = {new UnigramPredictor(&human),
new BigramPredictor(&human, &human),
new BigramPredictor(&result, &human),
new BigramPredictor(&robot, &human),
new LongPatternMatcher(&human)};
Predictor* reflexive[NUM_PREDICTORS] = {new UnigramPredictor(&robot),
new BigramPredictor(&robot, &robot),
new BigramPredictor(&result, &robot),
new BigramPredictor(&human, &robot),
new LongPatternMatcher(&robot)};
int getInput(){
start:
char c = getchar();
if(c != EOF)
switch(c){
case 'R':
return 0;
case 'r':
return 0;
case 'P':
return 1;
case 'p':
return 1;
case 'S':
return 2;
case 's':
return 2;
default:
goto start;
}
else
exit(0);
}
bool predictOrRandom(){
return *max_element(expectations, expectations + 2 * NUM_PREDICTORS) > 0.0 && total > 2;
}
long double* predict(){
for(int i = 0; i < NUM_PREDICTORS; i++){
long double* t = predictors[i]->predict();
for(int j = 0; j < 3; j++)
predictions[i][(j + shift[i]) % 3] = t[j];
debug << "Predictor " << i << " predicts ";
copy(predictions[i], predictions[i] + 3, ostream_iterator<long double>(debug, " "));
debug << endl;
}
for(int i = 0; i < NUM_PREDICTORS; i++){
long double* t = reflexive[i]->predict();
for(int j = 0; j < 3; j++)
predictions[i + NUM_PREDICTORS][(j + shift[i + NUM_PREDICTORS]) % 3] = t[j];
if(DEBUG)
debug << "Predictor " << i + NUM_PREDICTORS << " predicts ";
copy(predictions[i + NUM_PREDICTORS], predictions[i + NUM_PREDICTORS] + 3, ostream_iterator<long double>(debug, " "));
debug << endl;
}
int maxidx;
for(int i = 0; i < NUM_PREDICTORS * 2; i++){
int rank = 0;
for(int j = 1; j < NUM_PREDICTORS * 2; j++)
rank += (expectations[i] >= expectations[(i + j) % (2 * NUM_PREDICTORS)]);
if(rank == 2 * NUM_PREDICTORS - 1){
maxidx = i;
break;
}
}
debug << "Adopting predictor " << maxidx << endl;
return predictions[maxidx];
}
int getRandom(){
return rand() % 3;
}
void verify(int h){
for(int i = 0; i < NUM_PREDICTORS; i++){
counts[i][(distance(predictions[i], max_element(predictions[i], predictions[i] + 3)) - h + 4) % 3] *= 0.99;
counts[i][(distance(predictions[i], max_element(predictions[i], predictions[i] + 3)) - h + 4) % 3] += 1.0;
}
for(int i = 0; i < NUM_PREDICTORS; i++){
counts[i + NUM_PREDICTORS][(distance(predictions[i + NUM_PREDICTORS],
max_element(predictions[i + NUM_PREDICTORS],
predictions[i + NUM_PREDICTORS] + 3))
- h + 4) % 3] *= 0.99;
counts[i + NUM_PREDICTORS][(distance(predictions[i + NUM_PREDICTORS],
max_element(predictions[i + NUM_PREDICTORS],
predictions[i + NUM_PREDICTORS] + 3))
- h + 4) % 3] += 1.0;
}
for(int i = 0; i < NUM_PREDICTORS * 2; i++){
long double sum = 0.0;
for(int j = 0; j < 3; j++)
sum += counts[i][j];
for(int j = 0; j < 3; j++)
rates[i][j] = 1.0 * counts[i][j] / sum;
}
for(int i = 0; i < NUM_PREDICTORS * 2; i++){
expectations[i] = rates[i][1] - rates[i][2];
if(expectations[i] < 0.0)
++turns[i];
if(turns[i] > 3){
shift[i] += 2;
turns[i] = 0;
}
}
debug << "Shift ";
copy(shift, shift + 2 * NUM_PREDICTORS, ostream_iterator<int>(debug, " "));
debug << endl;
debug << "Expectations : ";
copy(expectations, expectations + NUM_PREDICTORS * 2, ostream_iterator<long double>(debug, " "));
debug << endl;
}
int processPrediction(long double* m){
long double rock_expectation = m[2] - m[1];
long double paper_expectation = m[0] - m[2];
long double scissor_expectation = m[1] - m[0];
if (rock_expectation > scissor_expectation && rock_expectation > paper_expectation)
return 0;
else if(paper_expectation > rock_expectation && paper_expectation > scissor_expectation)
return 1;
else
return 2;
}
void postPredict(int b){
if(!CONTEST)
h = getInput();
switch(b){
case 0:
putchar('R');
break;
case 1:
putchar('P');
break;
case 2:
putchar('S');
break;
}
if(CONTEST){
fflush(stdout);
h = getInput();
debug << "Opponent plays " << h << ", and I play " << b << endl;
}
if(!CONTEST){
putchar('\n');
}
human << h;
robot << b;
result << (h - b + 3) % 3;
++stat[(h - b + 3) % 3];
++total;
expectation = 1.0 * stat[2] / total - 1.0 * stat[1] / total;
debug << "Overall expectation : " << expectation << endl;
if(!CONTEST)
for(int i = 0; i < 3; i++)
printf("%s %d %.2f\n", meaning[i].c_str(), stat[i], 1.0 * stat[i] / total);
}
int main(int argc, char* argv[]){
CONTEST = atoi(argv[1]);
char token;
for(int i = 0; i < NUM_PREDICTORS; i++)
for(int j = 0; j < 3; j++){
counts[i][j] = 0;
rates[i][j] = 0;
}
for(int i = 0; i < NUM_PREDICTORS; i++)
shift[i + NUM_PREDICTORS] = 1;
while(true){
debug << "Round " << total + 1 << ' ';
if(predictOrRandom()){
debug << endl;
long double* p = predict();
postPredict(processPrediction(p));
verify(h);
}
else{
debug << ": Random fallback is in effect." << endl;
if(total >= 2){
predict();
}
postPredict(getRandom());
verify(h);
if(total == 0)
for(int i = 0; i < NUM_PREDICTORS; i++){
predictors[i]->getInput();
reflexive[i]->getInput();
}
}
if(total != 1){
human.ignore();
result.ignore();
robot.ignore();
}
}
}