My Project
openingBook.cc
Go to the documentation of this file.
1 #include "osl/book/openingBook.h"
3 #include <algorithm>
4 #include <iostream>
5 #include <stdexcept>
6 
7 int osl::book::readInt(std::istream& is)
8 {
9  int ret=0;
10  CArray<char,4> cs;
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 
18 void osl::book::writeInt(std::ostream& os, int n)
19 {
20  CArray<char,4> buf;
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
30 WinCountBook::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  }
39  nStates=readInt();
40 }
41 
44 {
45 }
46 
49 {
50  int ret=0;
51  CArray<char,4> cs;
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 
60 WinCountBook::seek(int offset)
61 {
62  ifs.seekg(offset,std::ios::beg);
63 }
64 
65 std::vector<osl::book::OBMove> osl::book::
66 WinCountBook::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 
85 WinCountBook::winCount(int stateIndex)
86 {
87  seek(4+16*stateIndex);
88  return readInt();
89 }
90 
92 WinCountBook::loseCount(int stateIndex)
93 {
94  seek(4+16*stateIndex+4);
95  return readInt();
96 }
97 
98 std::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 
107 std::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 
116 WeightedBook::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);
130  n_states = readInt(ifs);
131  n_moves = readInt(ifs);
133 }
134 
137 {
138 }
139 
141 WeightedBook::seek(int offset)
142 {
143  ifs.seekg(offset,std::ios::beg);
144 }
145 
147 WeightedBook::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 
167 WeightedBook::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 
177 WeightedBook::board(int stateIndex)
178 {
179  const CompactBoard board = compactBoard(stateIndex);
180  return board.state();
181 }
182 
184 WeightedBook::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 
194 WeightedBook::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 
242 WeightedBook::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 
266  WMoveContainer v;
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 
296 int osl::book::
297 WeightedBook::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 
317 std::vector<int> osl::book::
318 WeightedBook::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の表現 .
Definition: basic_type.h:1052
static const Move makeDirect(int value)
Definition: basic_type.h:1093
利きを持つ局面
void makeMove(Move move)
SimpleStateよりcompactな局面の表現
Definition: compactBoard.h:60
Player turn() const
Definition: compactBoard.h:66
SimpleState state() const
Definition: compactBoard.cc:58
int blackWinCount(int stateIndex)
Definition: openingBook.cc:194
SimpleState board(int stateIndex)
Definition: openingBook.cc:177
WMoveContainer moves(int stateIndex, const bool zero_include=true)
Return moves from the state of the stateIndex.
Definition: openingBook.cc:147
std::vector< WMove > WMoveContainer
Definition: openingBook.h:175
CompactBoard compactBoard(int stateIndex)
Definition: openingBook.cc:167
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.
Definition: openingBook.cc:242
int whiteWinCount(int stateIndex)
Definition: openingBook.cc:184
void seek(int offset)
Definition: openingBook.cc:141
std::vector< int > parents(const int stateIndex)
As traversing the 'tree', return all state indices of the state's parents.
Definition: openingBook.cc:318
WeightedBook(const char *filename)
Definition: openingBook.cc:116
void seek(int offset)
Definition: openingBook.cc:60
int winCount(int stateIndex)
Definition: openingBook.cc:85
std::vector< OBMove > moves(int stateIndex)
Definition: openingBook.cc:66
int loseCount(int stateIndex)
Definition: openingBook.cc:92
WinCountBook(const char *filename)
Definition: openingBook.cc:30
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
Definition: compactBoard.cc:79
std::istream & operator>>(std::istream &os, CompactBoard &c)
Definition: compactBoard.cc:91
void writeInt(std::ostream &os, int n)
Definition: openingBook.cc:18
int readInt(std::istream &is)
Definition: openingBook.cc:7
Player
Definition: basic_type.h:8
@ HIRATE
Definition: simpleState.h:21
int stateIndex() const
Definition: openingBook.h:103