-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
218 lines (187 loc) · 5.92 KB
/
main.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
#include <iostream>
#include <random>
#include <algorithm>
#include "game.h"
#include "gamesolver.h"
#include "cpu.h"
#include "ntuple.h"
using namespace std;
vector<pair<vector<int>,float>> trainingData;
NTupleNetwork network;
void generateTrainingData() {
cout << "generating data from games..." << endl;
GameSolver gameSolver;
gameSolver.solveAllStates();
auto solverData = gameSolver.getData();
trainingData = vector<pair<vector<int>,float>>(solverData.begin(),solverData.end());
cout << "there are " << trainingData.size() << " positions" << endl;
gameSolver.solveAllStates();
}
void trainNetwork() {
cout << "training network..." << endl;
random_device rd;
mt19937 mt = mt19937(rd());
for (int epoch = 0; epoch < 100; epoch++) {
shuffle(trainingData.begin(), trainingData.end(), mt);
for (auto & trainRow : trainingData) {
network.learn(trainRow.first, trainRow.second);
}
if ((epoch+1) % 5 == 0) {
float error = 0;
for (auto & trainRow : trainingData) {
float score = network.predict(trainRow.first);
error += (trainRow.second - score) * (trainRow.second - score);
}
cout << "epoch " << epoch+1 << ". error: " << error/trainingData.size() << endl;
}
}
}
void perfectCpuVsRandom() {
int scores[3] = {};
Cpu cpu1; cpu1.setPlayer(PLAYER_X);
cout << "perfect cpu vs random: " << endl;
cout << "CPU1 CPU2 DRAW: " << endl;
for (int i = 0; i < 1000; i++) {
cpu1.setPlayer(cpu1.getPlayer()^1);
Game game;
cpu1.setGame(&game);
while (!game.isOver()) {
if (game.currentPlayer == cpu1.getPlayer()) {
auto move = cpu1.getBestMove(9);
game.makeMove(move.move);
} else {
auto moves = game.getMoves();
game.makeMove(moves[rand()%moves.size()]);
}
}
int winner = game.getWinner();
if (winner == cpu1.getPlayer()) {
scores[0]++;
} else if (winner != NONE) {
scores[1]++;
} else {
scores[2]++;
}
if ((i+1) % 100 == 0) {
cout << scores[0] << " " << scores[1] << " " << scores[2] << endl;
}
}
}
void networkCpuVsRandom() {
int scores[3] = {};
Cpu cpu1; cpu1.setPlayer(PLAYER_X);
cpu1.setNetwork(&network);
cout << "network cpu vs random: " << endl;
cout << "CPU1 CPU2 DRAW: " << endl;
for (int i = 0; i < 1000; i++) {
cpu1.setPlayer(cpu1.getPlayer()^1);
Game game;
cpu1.setGame(&game);
while (!game.isOver()) {
if (game.currentPlayer == cpu1.getPlayer()) {
auto move = cpu1.getBestMove(1);
game.makeMove(move.move);
} else {
auto moves = game.getMoves();
game.makeMove(moves[rand()%moves.size()]);
}
}
int winner = game.getWinner();
if (winner == cpu1.getPlayer()) {
scores[0]++;
} else if (winner != NONE) {
scores[1]++;
} else {
scores[2]++;
}
if ((i+1) % 100 == 0) {
cout << scores[0] << " " << scores[1] << " " << scores[2] << endl;
}
}
}
void networkCpuVsCpuPly3() {
int scores[3] = {};
Cpu cpu1; cpu1.setPlayer(PLAYER_X);
cpu1.setNetwork(&network);
Cpu cpu2; cpu2.setPlayer(PLAYER_O);
cout << "network cpu vs cpu ply 3: " << endl;
cout << "CPU1 CPU2 DRAW: " << endl;
for (int i = 0; i < 1000; i++) {
cpu1.setPlayer(cpu1.getPlayer()^1);
cpu2.setPlayer(cpu2.getPlayer()^1);
Game game;
cpu1.setGame(&game);
cpu2.setGame(&game);
while (!game.isOver()) {
if (game.currentPlayer == cpu1.getPlayer()) {
auto move = cpu1.getBestMove(1);
game.makeMove(move.move);
} else {
auto move = cpu2.getBestMove(3);
game.makeMove(move.move);
}
}
int winner = game.getWinner();
if (winner == cpu1.getPlayer()) {
scores[0]++;
} else if (winner == cpu2.getPlayer()) {
scores[1]++;
} else {
scores[2]++;
}
if ((i+1) % 100 == 0) {
cout << scores[0] << " " << scores[1] << " " << scores[2] << endl;
}
}
}
void networkCpuVsPerfectCpu() {
int scores[3] = {};
Cpu cpu1; cpu1.setPlayer(PLAYER_X);
cpu1.setNetwork(&network);
Cpu cpu2; cpu2.setPlayer(PLAYER_O);
cout << "network cpu vs perfect cpu: " << endl;
cout << "CPU1 CPU2 DRAW: " << endl;
for (int i = 0; i < 1000; i++) {
cpu1.setPlayer(cpu1.getPlayer()^1);
cpu2.setPlayer(cpu2.getPlayer()^1);
Game game;
cpu1.setGame(&game);
cpu2.setGame(&game);
while (!game.isOver()) {
if (game.currentPlayer == cpu1.getPlayer()) {
auto move = cpu1.getBestMove(1);
game.makeMove(move.move);
} else {
auto move = cpu2.getBestMove(9);
game.makeMove(move.move);
}
}
int winner = game.getWinner();
if (winner == cpu1.getPlayer()) {
scores[0]++;
} else if (winner == cpu2.getPlayer()) {
scores[1]++;
} else {
scores[2]++;
}
if ((i+1) % 100 == 0) {
cout << scores[0] << " " << scores[1] << " " << scores[2] << endl;
}
}
}
int main() {
srand(time(NULL) ^ (uint64_t)(&main));
generateTrainingData();
trainNetwork();
perfectCpuVsRandom();
networkCpuVsRandom();
networkCpuVsCpuPly3();
networkCpuVsPerfectCpu();
// Game game;
// for (auto & m : game.getMoves()) {
// game.makeMove(m);
// cout << network.predict(game.getTuples()) << endl;
// game.undoMove();
// }
return 0;
}