-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatagen.cpp
More file actions
194 lines (155 loc) · 6.45 KB
/
Copy pathdatagen.cpp
File metadata and controls
194 lines (155 loc) · 6.45 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
#include "search.h"
#include <fstream>
#include <iomanip>
#include <thread>
int openingLength = 8;
int infoPrintInterval = 10;
int numberOfThreads = 8;
float softmaxTemp = 0.2;
int main(){
#if DATAGEN == 0
std::cout << "Preprocessor Variable DATAGEN must be set to 1 or 2 to Generate Data";
getchar();
return 0;
#else
std::string version = VERSION_NUM;
version += "-datagen";
search::init();
//tb_init("C:\\Users\\kjlji\\OneDrive\\Documents\\VSCode\\C++\\AuroraChessEngine-main\\3-4-5");
std::cout << "Aurora " << version << ", a chess engine by kjljixx\n";
std::vector<std::thread> threads;
threads.reserve(numberOfThreads);
for(int threadId=1; threadId<=numberOfThreads; threadId++) {
threads.emplace_back([threadId, version] {
std::random_device rd;
std::mt19937 eng(rd());
std::vector<std::string> gameData;
search::timeManagement tm(search::NODES, 450);
chess::Board board;
chess::Board rootBoard; //Only exists to make the search::makeMove function happy
search::Tree tree;
bool validOpening = false;
while(validOpening == false){
board.setToFen(chess::startPosFen);
for(int i=0; i<openingLength; i++){
chess::MoveList moves(board);
if(moves.size()==0){break;}
std::uniform_int_distribution<> distr(0, moves.size() - 1);
int moveIndex = distr(eng);
chess::makeMove(board, moves[moveIndex]);
}
if(chess::getGameStatus(board, chess::isLegalMoves(board)) == chess::ONGOING){
validOpening = true;
}
}
rootBoard = board;
int gameIter = 0;
int fenIter = 0;
int previousFenIter = 0;
int previousElapsed = 0;
int totalSearches = 0;
int differChoices = 0;
float differValue = 0;
auto start = std::chrono::steady_clock::now();
search::Node* root = nullptr;
while(true){
if(chess::getGameStatus(board, chess::isLegalMoves(board)) != chess::ONGOING){
for(std::string currData : gameData){
std::cout << "\n" << currData;
}
std::cout << "\n";
board.printBoard();
std::cout << "\n" << chess::getGameStatus(board, chess::isLegalMoves(board)) << " " << root->isTerminal;
assert(0);
}
search::search(board, tm, tree);
root = tree.root();
rootBoard = board;
search::Edge bestEdge = search::findBestEdge(root);
search::Edge chosenEdge = bestEdge;
// float softmaxTotal = 0;
// for(int i=0; i<root->children.size(); i++){
// softmaxTotal += exp(fminf(fmaxf(round(tan(-fminf(fmaxf(root->children[i].value, -0.9999), 0.9999)*1.57079633)*100), -100000), 100000)*softmaxTemp);
// }
// std::vector<float> probDistr;
// for(int i=0; i<root->children.size(); i++){
// probDistr.push_back(exp(fminf(fmaxf(round(tan(-fminf(fmaxf(root->children[i].value, -0.9999), 0.9999)*1.57079633)*100), -100000), 100000)*softmaxTemp)/softmaxTotal);
// }
// float cumProb = 0;
// for(int i=0; i<probDistr.size(); i++){
// probDistr[i] = cumProb + probDistr[i];
// cumProb = probDistr[i];
// }
// std::uniform_real_distribution<> distr(0, 1);
// float r = distr(eng);
// for(int i=0; i<probDistr.size(); i++){
// if(r<probDistr[i]){
// chosenEdge = root->children[i];
// break;
// }
// }
if(chosenEdge.child != bestEdge.child){
differChoices += 1;
differValue += -(bestEdge.value - chosenEdge.value);
}
totalSearches += 1;
if(board.squareUnderAttack(bitscanForward(board.getOurPieces(chess::KING)))==64 && board.mailbox[0][chosenEdge.edge.getEndSquare()]==0 && std::abs(bestEdge.value)<0.9999){
gameData.push_back(board.getFen() + " | " + std::to_string(int(round(tan((board.sideToMove ? search::findBestValue(root) : -search::findBestValue(root))*1.56375)*100))));
fenIter++;
}
float rootVal = chosenEdge.value;
search::makeMove(board, chosenEdge.edge, rootBoard, tree);
if((chess::getGameStatus(board, chess::isLegalMoves(board)) != chess::ONGOING) || std::abs(rootVal)>0.9999){
gameIter++;
if(gameIter % infoPrintInterval == 0){
std::cout << "Thread: " << threadId << "\n";
std::cout << "Games: " << gameIter << "\n";
std::cout << "FENs: " << fenIter << "\n";
std::chrono::duration<float> elapsed = std::chrono::steady_clock::now() - start;
std::cout << "Overall FENs/second: " << fenIter / elapsed.count() << "\n";
std::cout << "Recent FENs/second: " << (fenIter - previousFenIter) / (elapsed.count() - previousElapsed) << "\n";
std::cout << "Total searches: " << totalSearches << "\n";
std::cout << "Differ choices: " << differChoices << "\n";
std::cout << "Differ value: " << differValue / differChoices << "\n";
previousElapsed = elapsed.count();
previousFenIter = fenIter;
}
std::stringstream stream;
stream << std::fixed << std::setprecision(1) << ((board.sideToMove ? -rootVal : rootVal) + 1) / 2.0;
std::string gameResultStr = stream.str();
std::ofstream dataFile;
dataFile.open(dataFolderPath+"/"+version+"-thread"+std::to_string(threadId)+".txt", std::ios_base::app);
for(std::string currData : gameData){
currData += " | " + gameResultStr + "\n";
dataFile << currData;
}
gameData.clear();
dataFile.close();
search::destroyTree(tree);
root = nullptr;
validOpening = false;
while(validOpening == false){
board.setToFen(chess::startPosFen);
for(int i=0; i<openingLength; i++){
chess::MoveList moves(board);
if(moves.size()==0){break;}
std::uniform_int_distribution<> distr(0, moves.size() - 1);
int moveIndex = distr(eng);
chess::makeMove(board, moves[moveIndex]);
}
if(chess::getGameStatus(board, chess::isLegalMoves(board)) == chess::ONGOING){
validOpening = true;
}
else{}
}
rootBoard = board;
}
}
});
}
for(auto& thread : threads){
thread.join();
}
#endif
return 1;
}