File: book.cc

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (194 lines) | stat: -rw-r--r-- 5,629 bytes parent folder | download | duplicates (3)
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
#include "book.h"
#include "osl/record/kanjiPrint.h"
#include "osl/csa.h"
#ifndef MINIMAL_GPSSHELL
#  include <gsl_cdf.h>
#endif
#include <boost/format.hpp>
#include <cmath>
#include <iostream>

int gpsshell::
Book::getStateIndex(const osl::SimpleState& state, 
                    const std::vector<osl::Move>& moves) const
{
  int state_index = the_book->stateIndex(moves);
  if (state_index < 0)
    state_index = the_book->stateIndex(state); // it could take a long time
  return state_index;
}

void gpsshell::
Book::showState(const osl::SimpleState& state, 
                const std::vector<osl::Move>& moves,
                int max_count) const
{
  osl::record::KanjiPrint printer(std::cerr, 
                                  std::shared_ptr<osl::record::Characters>(
                                            new osl::record::KIFCharacters())
                                  );

  const int state_index = getStateIndex(state, moves);
  if (state_index < 0)
  {
    std::cout << "The current position not found in the book" << std::endl;
    return;
  }

  const osl::Player turn   = state.turn();
  const int black_win = the_book->blackWinCount(state_index);
  const int white_win = the_book->whiteWinCount(state_index);
  const int trial     = black_win + white_win;
  const int nwins     = (turn == osl::BLACK ? black_win : white_win); 
  double win_rate     = 0.0;
  double deviation    = 0.0;
  double cd           = 0.0; // cumulative distribution
  if (trial > 0)
  {
    win_rate = 1.0 * nwins / trial;
#ifndef MINIMAL_GPSSHELL
    cd = gsl_cdf_binomial_P(nwins, 0.5, trial);
#else
    cd = 0;
#endif
    deviation = sqrt( win_rate * (1-win_rate) / trial);
  }

  std::cout << boost::format("State_index: % 7d\n")   % state_index
            << boost::format("Black win:   % 7d\n")   % black_win
            << boost::format("White win:   % 7d\n")   % white_win
            <<               "Binomial:\n" 
            << boost::format("  E(X) [win rate] : % 7.4f\n") % win_rate
            << boost::format("  D(X):             % 7.4f\n") % deviation
            << boost::format("  cumulative:       % 7.4f\n") % cd;

  std::cout << "\n";
  printer.print(state);
  std::cout << "\n";

  gpsshell::WMoveStatsContainer wstats = getWMoveStats(state, moves);
  std::cout << boost::format("found %d moves\n") % wstats.size();

  for (const gpsshell::WMoveStats& st: wstats)
  {
    if (--max_count < 0) break;

    std::cout << boost::format("[% 6d] %s  win:% 7d loss:% 7d  rate:% 7.4f - % 7.4f = % 7.4f\n") 
                                % st.getWmove().weight 
                                % osl::csa::show(st.getWmove().move)
                                % st.nwins()
                                % st.nlosses()
                                % st.winRate()
                                % st.deviation()
                                % (st.winRate() - st.deviation());
  }
}

gpsshell::WMoveContainer gpsshell::
Book::getMoves(const osl::SimpleState& state, 
               const std::vector<osl::Move>& moves) const
{
  const gpsshell::WMoveStatsContainer wstats
    = getWMoveStats(state, moves);
  
  gpsshell::WMoveContainer wmoves;

  if (wstats.empty())
    return wmoves; // empty
  
  for (const gpsshell::WMoveStats& ws: wstats)
  {
    wmoves.push_back(ws.getWmove());
  }

  return wmoves;
}

gpsshell::WMoveStatsContainer gpsshell::
Book::getWMoveStats(const osl::SimpleState& state, 
                    const std::vector<osl::Move>& moves) const
{
  const int state_index = getStateIndex(state, moves);
  gpsshell::WMoveStatsContainer wstats;

  if (state_index < 0)
    return wstats; // empty

  {
    WMoveContainer wmoves = the_book->moves(state_index);
    wstats.reserve(wmoves.size());
    for (const auto& wmove: wmoves)
    {
      const int next_state_index = wmove.stateIndex();
      const int black_win        = the_book->blackWinCount(next_state_index);
      const int white_win        = the_book->whiteWinCount(next_state_index);
      gpsshell::WMoveStats st(wmove, black_win, white_win);
      wstats.push_back(st);
    }
  }

  /*
   * Sort:
   *   weight such as wegiht > 0
   *   #wins such as weight == 0
   */
  std::sort(wstats.begin(), wstats.end(), gpsshell::WMoveStatsSortByWeight());
  gpsshell::WMoveStatsContainer::iterator it = wstats.begin();
  for (/* none */; it != wstats.end(); ++it)
  {
    if (it->getWmove().weight < 1)
      break;
  }
  std::sort(it, wstats.end(), gpsshell::WMoveStatsSortByWins());
  return wstats;
}

// ==============================================
// Utility functions
// ==============================================

int gpsshell::getMaxWeight(const gpsshell::WMoveContainer& moves)
{
  int max_weight = 0;
  for (const osl::book::WMove& wmove: moves) {
    if (max_weight < wmove.weight) {
      max_weight = wmove.weight;
    }
  }
  return max_weight;
}

int gpsshell::getSumOfWeights(const gpsshell::WMoveContainer& moves)
{
  int sum = 0;
  for (const osl::book::WMove& wmove: moves) {
    sum += wmove.weight;
  }
  return sum;
}

struct WeightLessThan
{
  int criteria;

  explicit WeightLessThan(const int criteria)
    : criteria(criteria)
  {}

  bool operator()(const osl::book::WMove& wmove) const
  {
    return (wmove.weight < criteria ? true : false);
  }
};

void gpsshell::deleteLessWeightedMoves(gpsshell::WMoveContainer& wmoves,
                                            const int criteria)
{
  wmoves.erase(std::remove_if(wmoves.begin(), wmoves.end(), WeightLessThan(criteria)), 
               wmoves.end());
}

// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: