File: analyzer.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 (141 lines) | stat: -rw-r--r-- 3,644 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
/* analyzer.cc
 */
#include "analyzer.h"
#include "eval/eval.h"
#include "quiesce.h"
#include "osl/progress/effect5x3.h"
#include "osl/eval/pieceEval.h"
#include <iostream>
#include <cstdio>

// #define DEBUG_ALL

constexpr double gpsshogi::SigmoidUtil::eps;

/* ------------------------------------------------------------------------- */

std::ostream& gpsshogi::operator<<(std::ostream& os, const gpsshogi::MoveData& md)
{
  os << "value " << md.value << " data";
  for (size_t i=0; i<md.diffs.size(); ++i) {
    os << "  " << md.diffs[i].first << " " << md.diffs[i].second;
  }
  return os << "\n";
}

/* ------------------------------------------------------------------------- */

void gpsshogi::
Analyzer::makeLeaf(NumEffectState& state, const PVVector& pv)
{
  for (Move m: pv)
    state.makeMove(m);
}

void gpsshogi::
Analyzer::analyzeLeaf(const NumEffectState& state_org, 
		      const PVVector& pv, Eval& eval,
		      MoveData& data, bool bonanza_compatible)
{
  NumEffectState state(state_org);
  makeLeaf(state, pv);

  if (! bonanza_compatible) {
    assert(! state.inCheck(BLACK));
    assert(! state.inCheck(WHITE));
  }
#ifdef DEBUG_ALL
  std::cerr << state;
  std::cerr << value << "\n";
#endif  
  eval.features(state, data);
#ifdef DEBUG_ALL
  std::cerr << "\n";
#endif
}

int gpsshogi::
Analyzer::leafValue(const NumEffectState& state_org, 
		    const PVVector& pv, Eval& eval)
{
  NumEffectState state(state_org);
  makeLeaf(state, pv);

  return eval.eval(state);
}

void gpsshogi::
Analyzer::makeInstanceSorted
(double turn_coef, 
 const sparse_vector_t& selected, const sparse_vector_t& sibling,
 const std::vector<size_t>& frequency, int min_frequency, InstanceData& instance)
{
  assert(std::is_sorted(selected.begin(), selected.end()));
  assert(std::is_sorted(sibling.begin(), sibling.end()));

  instance.index.reserve(selected.size()+sibling.size()+1);
  instance.value.reserve(selected.size()+sibling.size()+1);
  // NOTE: black = 0, white = 1 for backward compatibility
  instance.y = 1-std::max(0.0, turn_coef); 

  // NOTE: sibling - selected
  sparse_vector_t::const_iterator p = sibling.begin();
  sparse_vector_t::const_iterator q = selected.begin();

  while (p != sibling.end() && q != selected.end()) {
    if (min_frequency) {
      if (frequency[p->first] < min_frequency) {
	++p;
	continue;
      }
      if (frequency[q->first] < min_frequency) {
	++q;
	continue;
      }
    }
    const int index = std::min(p->first, q->first);
    double diff;		// sibling[i] - selected[i];
    if (p->first == q->first) {
      diff = p->second - q->second;
      ++p, ++q;
    }
    else if (p->first < q->first) {
      diff = p->second;		// seleceted[index] == 0
      ++p;
    }
    else {
      assert(p->first > q->first);
      diff = - (q->second);	// sibling[index] == 0
      ++q;
    }
    if (fabs(diff) > SigmoidUtil::eps) {
      instance.index.push_back(index);
      instance.value.push_back(diff);
    }
  }
  while (p != sibling.end()) {
    const int index = p->first;
    const double diff = p->second; // seleceted[index] == 0
    ++p;
    if (fabs(diff) > SigmoidUtil::eps) {
      instance.index.push_back(index);
      instance.value.push_back(diff);
    }
  }
  while (q != selected.end()) {
    const int index = q->first;
    const double diff = - (q->second); // sibling[index] == 0
    ++q;
    if (fabs(diff) > SigmoidUtil::eps) {
      instance.index.push_back(index);
      instance.value.push_back(diff);
    }
  }
}


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