10 #include <boost/utility.hpp>
16 const int weight_coef_for_the_initial_move,
17 const int weight_coef)
18 :
book(b), state_index(b.getStartState()), start_index(b.getStartState()),
20 weight_coef_for_the_initial_move(weight_coef_for_the_initial_move),
21 weight_coef(weight_coef)
29 book(copy.
book), state_index(copy.state_index),
30 start_index(copy.start_index), turn(copy.turn),
31 state_stack(copy.state_stack),
32 weight_coef_for_the_initial_move(copy.weight_coef_for_the_initial_move),
33 weight_coef(copy.weight_coef)
47 std::cerr <<
"WeightTracer "
49 const time_t now = time(0);
51 std::cerr <<
ctime_r(&now, ctime_buf)
55 state_stack.push(state_index);
56 assert(move.
player() == turn);
62 for (
size_t i=0; i<moves.size(); i++)
64 if(moves[i].getMove() == move)
66 state_index = moves[i].getStateIndex();
69 << state_stack.top() <<
"->" << state_index <<
"\n";
74 std::cerr <<
"book: end" <<
"\n";
82 state_index = state_stack.top();
86 std::cerr <<
"WeightTracer " <<
this <<
" pop: " << turn << std::endl;
92 return state_index < 0;
99 std::cerr <<
"book " << moves.size() <<
" candidates\n" << std::endl;
101 int max_weight_index = 0;
104 for (
size_t i=0; i<moves.size(); ++i) {
105 sum += moves[i].getWeight();
106 if (max_weight < moves[i].getWeight()) {
107 max_weight = moves[i].getWeight();
108 max_weight_index = i;
114 std::cerr <<
"random number: " << random_value << std::endl;
118 for (
size_t index = 0; index < moves.size(); index++)
120 weight += moves[index].getWeight();
121 if (random_value <= weight)
130 const int weight = moves[maxIndex].getWeight();
132 << 100.0*weight/sum <<
'%';
133 if (weight != max_weight)
134 std::cerr <<
" (c.f. " << 100.0*max_weight/sum
137 std::cerr << std::endl;
139 return moves[maxIndex].getMove();
147 const vector<record::opening::WMove> raw_moves =
book.
getMoves(state_index);
148 vector<record::opening::WMove>
moves;
151 for (
size_t i=0; i<raw_moves.size(); ++i) {
152 if (max_weight < raw_moves[i].getWeight()) {
153 max_weight = raw_moves[i].getWeight();
157 const int coef = ((state_index == start_index) ?
158 weight_coef_for_the_initial_move : weight_coef);
159 for (
size_t i=0; i<raw_moves.size(); ++i) {
160 if (raw_moves[i].getWeight()*coef < max_weight)
162 moves.push_back(raw_moves[i]);
163 sum += raw_moves[i].getWeight();
169 return selectMoveAtRandom(moves);
183 const int original_size = moves.size();
194 vector<record::opening::WMove>::iterator it = moves.begin();
195 for (; it != moves.end() && top > 0; ++it) {
196 if (it->getWeight() == 0)
199 if (it->getWeight() != boost::next(it)->getWeight())
202 moves.erase(it, moves.end());
205 std::cerr <<
"book: there remain " << moves.size() <<
" candidates of "
206 << original_size <<
" moves\n";
212 return selectMoveAtRandom(moves);