openingBookConverter.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
openingBookConverter.cc
Go to the documentation of this file.
1 #include "openingBookConverter.h"
2
3 #include "osl/state/simpleState.h"
4 #include "osl/stl/vector.h"
5 #include "osl/record/compactBoard.h"
6 #include "osl/record/record.h"
7 #include <iostream>
8
9 OpeningBookConverter::OpeningBookConverter(const char *filename)
10 {
11 std::ifstream ifs(filename);
12 int nStates = osl::record::readInt(ifs);
13 states.reserve(nStates);
14 for (int i = 0 ; i < nStates; i++)
15 {
16 int blackWin = osl::record::readInt(ifs);
17 int whiteWin = osl::record::readInt(ifs);
18 int nMove = osl::record::readInt(ifs);
19 int index = osl::record::readInt(ifs);
20 states.push_back(OBState(index, nMove, blackWin, whiteWin));
21 }
22 while (true)
23 {
24 osl::Move move=osl::Move::makeDirect(osl::record::readInt(ifs));
25 int stateIndex=osl::record::readInt(ifs);
26 if (!ifs)
27 break;
28 moves.push_back(osl::record::opening::OBMove(move,stateIndex));
29 }
30 }
31
32 void
33 OpeningBookConverter::write(const char* filename)
34 {
35 std::ofstream ofs(filename);
36 osl::record::writeInt(ofs, states.size());
37 for (osl::vector<OBState>::const_iterator it = states.begin();
38 it != states.end(); ++it)
39 {
40 osl::record::writeInt(ofs, it->getOBMoveIndex());
41 osl::record::writeInt(ofs, it->getNOBMove());
42 osl::record::writeInt(ofs, it->getBlackWinCount());
43 osl::record::writeInt(ofs, it->getWhiteWinCount());
44 }
45 for (osl::vector<osl::record::opening::OBMove>::const_iterator it = moves.begin();
46 it != moves.end(); ++it)
47 {
48 osl::record::writeInt(ofs, it->getMove().intValue());
49 osl::record::writeInt(ofs, it->getStateIndex());
50 }
51 }
52
53 void
54 OpeningBookConverter::writeInNewFormat(std::ofstream& ofs)
55 {
56 osl::vector<int> weights(moves.size());
57 osl::record::writeInt(ofs, 1); // version number
58 osl::record::writeInt(ofs, states.size());
59 osl::record::writeInt(ofs, moves.size());
60 osl::record::writeInt(ofs, 0); // Start state index
61 for (osl::vector<OBState>::const_iterator it = states.begin();
62 it != states.end(); ++it)
63 {
64 osl::record::writeInt(ofs, it->getOBMoveIndex());
65 osl::record::writeInt(ofs, it->getNOBMove());
66 int total_wins = 0;
67 osl::vector<int> wins;
68 wins.reserve(it->getNOBMove());
69 for (int i = 0; i < it->getNOBMove(); i++)
70 {
71 const osl::record::opening::OBMove& move
72 = moves.at(i + it->getOBMoveIndex());
73
74 const OBState& state = states.at(move.getStateIndex());
75 if (move.getMove().player() == osl::BLACK)
76 wins.push_back(state.getBlackWinCount());
77 else
78 wins.push_back(state.getWhiteWinCount());
79
80 total_wins += wins.at(i);
81 }
82 for (int i = 0; i < it->getNOBMove(); i++)
83 {
84 if (total_wins != 0)
85 weights.at(i + it->getOBMoveIndex()) = (wins.at(i) * 10000 / total_wins);
86 else
87 weights.at(i + it->getOBMoveIndex()) = 0;
88 }
89 osl::record::writeInt(ofs, it->getBlackWinCount());
90 osl::record::writeInt(ofs, it->getWhiteWinCount());
91 }
92 int i = 0;
93 for (osl::vector<osl::record::opening::OBMove>::const_iterator it = moves.begin();
94 it != moves.end(); ++it, ++i)
95 {
96 osl::record::opening::WMove wmove(it->getMove(),
97 it->getStateIndex(),
98 weights.at(i));
99 ofs << wmove;
100 }
101 }
102
103 void
104 OpeningBookConverter::writeInNewFormat(const char* filename)
105 {
106 std::ofstream ofs(filename);
107 writeInNewFormat(ofs);
108 }
109
110 void
111 OpeningBookConverter::writeInNewEditFormat(const char* filename)
112 {
113 std::ofstream ofs(filename);
114 writeInNewFormat(ofs);
115 osl::vector<osl::SimpleState> simpleStates(states.size());
116 osl::vector<bool> visited(states.size());
117 osl::vector<int> toTraceIndex;
118
119 for (unsigned int i = 0; i < states.size(); i++)
120 visited[i] = false;
121
122 assert(states.size() >= 1);
123
124 // First entry is assmed to be the start state.
125 toTraceIndex.push_back(0);
126 simpleStates[0] = osl::SimpleState(osl::HIRATE);
127 visited[0] = true;
128
129 while (!toTraceIndex.empty())
130 {
131 const int index = toTraceIndex.back();
132 toTraceIndex.pop_back();
133 const OBState& s = states.at(index);
134 const int moveIndex = s.getOBMoveIndex();
135 for (int i = 0; i < s.getNOBMove(); i++)
136 {
137 const osl::record::opening::OBMove& m = moves.at(moveIndex + i);
138 const int nextState = m.getStateIndex();
139 if (!visited[nextState])
140 {
141 toTraceIndex.push_back(nextState);
142 osl::NumEffectState newState(simpleStates[index]);
143 newState.makeMove(m.getMove());
144 simpleStates[nextState] = newState;
145 visited[nextState] = true;
146 }
147 }
148 }
149 for (unsigned int i = 0; i < states.size(); i++)
150 {
151 osl::record::CompactBoard board(simpleStates[i]);
152 ofs << board;
153 }
154 }
155 // ;;; Local Variables:
156 // ;;; mode:c++
157 // ;;; c-basic-offset:2
158 // ;;; End: