-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
uci.cpp
executable file
·253 lines (232 loc) · 7.66 KB
/
uci.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
#include <iostream>
#include <string>
#include "uci.h"
#include "search.h"
#include "fenstring.h"
#include "board.h"
#include "stopwatch.h"
#include "benchmark.h"
#include "evaluation.h"
#include "moveselector.h"
#include "movegenerator.h"
#include "searchinfo.h"
//#include "tuner.h"
#include <fstream>
namespace Napoleon
{
using namespace std;
Board Uci::board;
thread Uci::search;
void Uci::Start()
{
SendCommand<Command::Generic>("--------Napoleon Engine--------");
cout.setf(ios::unitbuf);// Make sure that the outputs are sent straight away to the GUI
string line;
string cmd;
Search::Table.SetSize(512);
Search::InitializeThreads();
bool exit = false;
while(!exit && getline(cin, line))
{
istringstream stream(line);
stream >> cmd;
if (cmd == "uci")
{
SendCommand<Command::Generic>("id name Napoleon");
SendCommand<Command::Generic>("id author Marco Pampaloni");
SendCommand<Command::Generic>("option name Hash type spin default 1 min 1 max 131072"); // max 128 GB
SendCommand<Command::Generic>("option name Threads type spin default 1 min 1 max 8");
for (auto i=0; i<Search::Parameters::MAX; i++)
{
SendCommand<Command::Generic>("option name " + Search::param_name[i] + " type spin default 1 min 1 max 1024");
}
SendCommand<Command::Generic>("uciok");
}
else if (cmd == "setoption")
{
string token;
stream >> token; // "name"
stream >> token;
if(token == "Hash")
{
stream >> token; // "value"
stream >> token;
Search::Table.SetSize(std::stoi(token));
}
else if (token == "Threads")
{
int parallel_threads;
stream >> token; // "value"
stream >> parallel_threads;
Search::InitializeThreads(parallel_threads);
}
else if (token == "PstPawnMg") // evaluation parameters
{
stream >> token; // "value"
int i=0;
int val;
while (stream >> val)
PieceSquareTable[PieceType::Pawn][Opening][i++] = val;
}
else if (token == "Record")
{
Search::positions_dataset = new ofstream();
Search::positions_dataset->open("positions_dataset.csv", ios::out | ios::app);
Search::record_positions = true;
}
else
{
for (auto i=0; i<Search::Parameters::MAX; i++)
{
if (token == Search::param_name[i])
{
stream >> token; // "value"
stream >> Search::param[i];
break;
}
}
}
}
else if (cmd == "quit")
{
SendCommand<Command::Generic>("Bye Bye");
Search::KillThreads();
exit = true;
if (Search::record_positions) {
Search::positions_dataset->close();
}
}
else if (cmd == "isready")
{
SendCommand<Command::Generic>("readyok");
}
else if (cmd == "ucinewgame")
{
Search::Table.Clear();
}
else if (cmd == "stop")
{
Search::StopThinking();
}
else if (cmd == "perft")
{
Benchmark bench(board);
int depth;
stream >> depth;
StopWatch watch = StopWatch::StartNew();
cout << "Perft(" << depth << "): ";
cout << "Total Nodes: " << bench.Perft(depth) << endl;
cout << "Time (ms): " << watch.ElapsedMilliseconds() << endl;
}
else if (cmd == "position")
{
//Search::StopThinking();
Move move;
string token;
stream >> token;
if (token == "startpos")
{
board.LoadGame();
stream >> token;
}
else if (token == "fen")
{
string fen;
while (stream >> token && token != "moves")
fen += token + " ";
board.LoadGame(fen);
}
while (stream >> token && !(move = board.ParseMove(token)).IsNull())
{
board.MakeMove(move);
}
}
else if (cmd == "ECM")
{
int depth;
stream >> depth;
Benchmark bench(board);
bench.Start(depth);
}
else if (cmd == "disp")
{
board.Display();
}
else if (cmd == "eval")
{
Evaluation::PrintEval(board);
std::cout << Evaluation::Evaluate(board) << std::endl;
}
else if (cmd == "go")
{
//if (Search::StopSignal)
go(stream);
}
else if (cmd == "ponderhit")
{
Search::PonderHit = true;
}
else if (cmd == "csv")
{
std::cout << board.ToCsv() << std::endl;
}
else if (cmd == "moves")
{
SearchInfo searchInfo;
searchInfo.NewSearch();
MoveSelector moves(board, searchInfo);
MoveGenerator::GetLegalMoves(moves.moves, moves.count, board);
for (auto move = moves.First(); !move.IsNull(); move = moves.Next()) {
std::cout << move.ToAlgebraic() << " ";
}
std::cout << std::endl;
}
/*
else if (cmd == "tune")
{
Tuner tuner;
tuner.Tune();
}
*/
}
}
void Uci::go(istringstream& stream)
{
string token;
SearchType type = SearchType::TimePerGame;
bool san = false;
while(stream >> token)
{
if (token == "depth")
{
stream >> Search::depth_limit;
type = SearchType::Infinite;
}
else if (token == "movetime")
{
stream >> Search::MoveTime;
type = SearchType::TimePerMove;
}
else if (token == "wtime")
{
stream >> Search::GameTime[PieceColor::White];
//type = SearchType::TimePerGame;
}
else if (token == "btime")
{
stream >> Search::GameTime[PieceColor::Black];
//type = SearchType::TimePerGame;
}
else if (token == "infinite")
{
type = SearchType::Infinite;
}
else if (token == "ponder")
{
type = SearchType::Ponder;
}
}
search = thread(Search::StartThinking, type, ref(board), true, san);
search.detach();
}
}