My Project
openingBook.cc
Go to the documentation of this file.
3#include <algorithm>
4#include <iostream>
5#include <stdexcept>
6
7int osl::book::readInt(std::istream& is)
8{
9 int ret=0;
11 is.read(&cs[0],4);
12 for (int i=0;i<4;i++) {
13 ret = (ret<<8)|(cs[i]&255);
14 }
15 return ret;
16}
17
18void osl::book::writeInt(std::ostream& os, int n)
19{
21 for (int i = 0; i < 4; i++)
22 {
23 buf[i] = (n >> (8 * (4 - i - 1))) & 255;
24 }
25 os.write(&buf[0], 4);
26}
27
28#ifndef MINIMAL
30WinCountBook::WinCountBook(const char *filename)
31 : ifs(filename, std::ios_base::binary)
32{
33 if (! ifs)
34 {
35 const char *message = "WinCountBook: open failed ";
36 std::cerr << message << filename << std::endl;
37 throw std::runtime_error(std::string(message) + filename);
38 }
40}
41
46
49{
50 int ret=0;
52 ifs.read(&cs[0],4);
53 for (int i=0;i<4;i++) {
54 ret = (ret<<8)|(cs[i]&255);
55 }
56 return ret;
57}
58
60WinCountBook::seek(int offset)
61{
62 ifs.seekg(offset,std::ios::beg);
63}
64
65std::vector<osl::book::OBMove> osl::book::
66WinCountBook::moves(int stateIndex)
67{
68 assert(stateIndex >= 0);
69 seek(4+16*stateIndex+8);
70 int nMoves=readInt();
71 int moveIndex=readInt();
72 seek(4+16*nStates+8*moveIndex);
73 std::vector<OBMove> moves;
74 moves.reserve(nMoves);
75 for(int i=0;i<nMoves;i++)
76 {
78 int stateIndex=readInt();
79 moves.push_back({move,stateIndex});
80 }
81 return moves;
82}
83
85WinCountBook::winCount(int stateIndex)
86{
87 seek(4+16*stateIndex);
88 return readInt();
89}
90
92WinCountBook::loseCount(int stateIndex)
93{
94 seek(4+16*stateIndex+4);
95 return readInt();
96}
97
98std::ostream& osl::book::operator<<(std::ostream& os, const WMove& w)
99{
100 writeInt(os, OMove(w.move));
101 writeInt(os, w.stateIndex());
102 writeInt(os, w.weight);
103 return os;
104}
105#endif
106
107std::istream& osl::book::operator>>(std::istream& is, WMove& w)
108{
109 w.move = OMove(readInt(is)).operator Move();
110 w.state_index = readInt(is);
111 w.weight = readInt(is);
112 return is;
113}
114
116WeightedBook::WeightedBook(const char *filename)
117 : ifs(filename, std::ios_base::binary)
118{
119 if (! ifs)
120 {
121 const char *message = "WeightedBook: open failed ";
122 std::cerr << message << filename << std::endl;
123 throw std::runtime_error(std::string(message) + filename);
124 }
125#ifndef NDEBUG
126 int version =
127#endif
128 readInt(ifs);
129 assert(version == 1);
133}
134
139
141WeightedBook::seek(int offset)
142{
143 ifs.seekg(offset,std::ios::beg);
144}
145
147WeightedBook::moves(int stateIndex, const bool visit_zero)
148{
149 assert(stateIndex >= 0);
150 seek(HEADER_SIZE + STATE_SIZE * stateIndex);
151 int moveIndex=readInt(ifs);
152 int nWMoves=readInt(ifs);
153 seek(HEADER_SIZE + STATE_SIZE * n_states + MOVE_SIZE * moveIndex);
154 std::vector<WMove> moves;
155 moves.reserve(nWMoves);
156 for(int i=0;i<nWMoves;i++)
157 {
158 WMove wm;
159 ifs >> wm;
160 if (!visit_zero && wm.weight == 0) continue;
161 moves.push_back(wm);
162 }
163 return moves;
164}
165
167WeightedBook::compactBoard(int stateIndex)
168{
169 seek(HEADER_SIZE + STATE_SIZE * n_states + MOVE_SIZE * n_moves
170 + BOARD_SIZE * stateIndex);
171 CompactBoard board;
172 ifs >> board;
173 return board;
174}
175
177WeightedBook::board(int stateIndex)
178{
179 const CompactBoard board = compactBoard(stateIndex);
180 return board.state();
181}
182
184WeightedBook::whiteWinCount(int stateIndex)
185{
186 seek(HEADER_SIZE + STATE_SIZE * stateIndex);
187 readInt(ifs);
188 readInt(ifs);
189 readInt(ifs);
190 return readInt(ifs);
191}
192
194WeightedBook::blackWinCount(int stateIndex)
195{
196 seek(HEADER_SIZE + STATE_SIZE * stateIndex);
197 readInt(ifs);
198 readInt(ifs);
199 return readInt(ifs);
200}
201
204{
205#ifndef NDEBUG
206 {
207 SimpleState state(HIRATE);
208 SimpleState start = board(start_state);
209 assert(state == start);
210 }
211#endif
212 std::vector<char> visited(n_states);
213 std::fill(visited.begin(), visited.end(), false);
214
215 std::vector<int> stateToCheck;
216 stateToCheck.push_back(start_state);
217 visited[start_state] = true;
218
219 while (!stateToCheck.empty())
220 {
221 const int index = stateToCheck.back();
222 stateToCheck.pop_back();
223 SimpleState state = board(index);
224 for (WMove move: moves(index))
225 {
226 NumEffectState newState(state);
227 newState.makeMove(move.move);
228 const int nextIndex = move.stateIndex();
229
230 SimpleState stateInFile = board(nextIndex);
231 assert(newState == stateInFile);
232 if (!visited[nextIndex])
233 {
234 stateToCheck.push_back(nextIndex);
235 visited[nextIndex] = true;
236 }
237 }
238 }
239}
240
242WeightedBook::stateIndex(const SimpleState& state_to_look_for,
243 const bool visit_zero,
244 const Player player)
245{
246 int ret = -1;
247 const CompactBoard board_to_look_for(state_to_look_for);
248
249 const CompactBoard start_state = compactBoard(startState());
250 if (start_state == board_to_look_for)
251 {
252 ret = startState();
253 return ret;
254 }
255
256 std::vector<char> states(totalState(), false); // mark states that have been visited.
257 std::vector<int> stateToVisit;
258 stateToVisit.push_back(startState());
259
260 while (!stateToVisit.empty())
261 {
262 const int stateIndex = stateToVisit.back();
263 stateToVisit.pop_back();
264 states[stateIndex] = true;
265
267 if (visit_zero)
268 v = moves(stateIndex);
269 else
270 {
271 const CompactBoard stateIndexCB = compactBoard(stateIndex);
272 const Player turn = stateIndexCB.turn();
273 const bool zero_include = turn == player ? false : true;
274 v = moves(stateIndex, zero_include);
275 }
276 for (WMove move: v)
277 {
278 const int nextIndex = move.stateIndex();
279 if (! states[nextIndex])
280 {
281 const CompactBoard state = compactBoard(nextIndex);
282 if (state == board_to_look_for)
283 {
284 ret = nextIndex;
285 return ret;
286 }
287
288 stateToVisit.push_back(nextIndex);
289 }
290 } // each wmove
291 } // while loop
292
293 return ret;
294}
295
296int osl::book::
297WeightedBook::stateIndex(const std::vector<osl::Move>& moves)
298{
299 int state_index = startState();
300 for (Move move: moves)
301 {
302 const WMoveContainer wmoves = this->moves(state_index);
303 WMoveContainer::const_iterator it = wmoves.begin();
304 for (; it != wmoves.end(); ++it)
305 if (it->move == move) break;
306 if (it != wmoves.end())
307 {
308 state_index = it->stateIndex(); // next state to visit
309 continue;
310 }
311 return -1; // not found
312 }
313 return state_index;
314}
315
316
317std::vector<int> osl::book::
318WeightedBook::parents(const int target_state_index)
319{
320 std::vector<int> ret;
321
322 if (startState() == target_state_index)
323 return ret;
324
325 std::vector<char> states(totalState(), false); // mark states that have been visited.
326 std::vector<int> stateToVisit;
327 stateToVisit.push_back(startState());
328
329 while (!stateToVisit.empty())
330 {
331 const int stateIndex = stateToVisit.back();
332 stateToVisit.pop_back();
333 states[stateIndex] = true;
334
335 const WMoveContainer moves = this->moves(stateIndex);
336 for (WMove move: moves)
337 {
338 const int nextIndex = move.stateIndex();
339
340 if (nextIndex == target_state_index)
341 ret.push_back(stateIndex);
342
343 if (! states[nextIndex])
344 stateToVisit.push_back(nextIndex);
345 } // each wmove
346 } // while loop
347
348 return ret;
349}
350
351// ;;; Local Variables:
352// ;;; mode:c++
353// ;;; c-basic-offset:2
354// ;;; End:
圧縮していない moveの表現 .
static const Move makeDirect(int value)
利きを持つ局面
void makeMove(Move move)
SimpleStateよりcompactな局面の表現
SimpleState state() const
int blackWinCount(int stateIndex)
SimpleState board(int stateIndex)
WMoveContainer moves(int stateIndex, const bool zero_include=true)
Return moves from the state of the stateIndex.
std::vector< WMove > WMoveContainer
CompactBoard compactBoard(int stateIndex)
int stateIndex(const SimpleState &state, const bool visit_zero=true, const Player player=BLACK)
As traversing the 'tree', find a state index of the state.
int whiteWinCount(int stateIndex)
void seek(int offset)
std::vector< int > parents(const int stateIndex)
As traversing the 'tree', return all state indices of the state's parents.
WeightedBook(const char *filename)
void seek(int offset)
int winCount(int stateIndex)
std::vector< OBMove > moves(int stateIndex)
int loseCount(int stateIndex)
WinCountBook(const char *filename)
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
std::istream & operator>>(std::istream &os, CompactBoard &c)
void writeInt(std::ostream &os, int n)
int readInt(std::istream &is)
Definition openingBook.cc:7
Player
Definition basic_type.h:8
@ HIRATE
Definition simpleState.h:21
int stateIndex() const