-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_game.cpp
More file actions
128 lines (93 loc) · 3.19 KB
/
Copy pathanalyze_game.cpp
File metadata and controls
128 lines (93 loc) · 3.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
/*
Analyze a specific gametrace from the server logs and apply the learning
mechanism to this file. */
#include <iostream>
#include "assert.h"
#include "Pente.hpp"
#include "wlm.hpp"
using namespace std;
int main(int argc, char **argv) {
Pente p1, p2;
Weight weights(WEIGHTS_FILE); /* Vhat() loads the weight file automatically, but
we want to keep it up to date between calculations
so I'll use this has a pointer. */
p1.players[0] = p2.players[1] = "COMPUTER";
p1.players[1] = p2.players[0] = "OTHER";
p1.fillCell(9,9, WHITE);
p2.fillCell(9,9, WHITE);
// for init, don't read in the file but init to 0's.
if (argc > 1 && string(argv[1]) == "init") {
weights.w.resize(p1.toState().size() + 1);
for (int i = 1; i < weights.size(); i++)
weights[i] = 2;
weights.save();
cout << "reinitializing weights..." << endl;
}
else
weights.load();
// Read in our game
if (argc != 2) {
cout << "SYNTAX: " << argv[0] << " <gamefile>" << endl;
exit(0);
}
ifstream game_file(argv[1]);
if (!game_file) {
cout << "Unable to open file at '" << argv[1] << "'" << endl;
exit(1);
}
p1.deserialize(game_file);
game_file.close();
// computers only.
if (p1.playerNumber("COMPUTER") == -1) {
cout << "A computer did not play in this game." << endl;
exit(0);
}
// Print the summary...
cout << endl;
cout << "p1.gameOutcome(COMPUTER) -> "
<< p1.gameOutcome(p1.playerColor("COMPUTER")) << endl
<< "whtCaps -> " << p1.whtCaps << endl
<< "blkCaps -> " << p1.blkCaps << endl
<< p1.toString() << endl << endl;
// Analyze the game and adjust weights accordingly.
/* Vtrain(b) <- V(Successor(b)) */
/* Adjust weights with wi <- wi + n(Vtrain(b) - V(b))*xi */
Pente *games[] = {&p1, &p2};
char colors[] = {WHITE, BLACK};
char computer_color;
for (int i = 0; i < 1; i++) {
cout << "Analyzing P" << i << endl;
cout << "eta: " << weights.eta << endl;
Pente *game = games[i];
computer_color = game->playerColor("COMPUTER");
if (game->gameOutcome(WHITE) == 0) {
cout << "Game is undecided." << endl;
break;
}
// Our end-game state
State b = game->toState();
State successor = game->toState();
double error =
game->gameOutcome(computer_color)*100 - weights.Vhat(b);
weights.adjust(b, error);
cout << "Initial error: (" <<
game->gameOutcome(computer_color)*100 << " - " <<
weights.Vhat(b) << ") = " << error << endl;
cout << "Errors => ";
while (game->gametrace.size()) {
successor = game->toState();
// remember + remove the last move.
Pente::cell* tCell = game->gametrace.back();
game->gametrace.pop_back();
game->clearCell(tCell->r, tCell->c);
b = game->toState();
error = weights.Vhat(successor) - weights.Vhat(b);
weights.adjust(game->toState(), error);
cout << error << " ";
}
cout << endl;
}
cout << "Weights: " << weights.toString() << endl;
weights.save();
return 0;
}