File: proofTreeDepthDfpn.cc

package info (click to toggle)
libosl 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 273,976 kB
  • sloc: cpp: 129,625; ansic: 7,145; ruby: 1,290; makefile: 558; perl: 413; sh: 35
file content (223 lines) | stat: -rw-r--r-- 6,655 bytes parent folder | download | duplicates (2)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* proofTreeDepthDfpn.cc
 */
#include "osl/checkmate/proofTreeDepthDfpn.h"
#include "osl/checkmate/dfpn.h"
#include "osl/checkmate/dfpnRecord.h"
#include "osl/checkmate/fixedDepthSearcher.h"
#include "osl/checkmate/fixedDepthSearcher.tcc"
#include "osl/container/moveVector.h"
#include "osl/stl/hash_map.h"
#include "osl/stl/slist.h"
#include <boost/foreach.hpp>
/**
 * 深さを記憶するテーブル.
 * -1 は探索中
 */
struct osl::checkmate::ProofTreeDepthDfpn::Table
{
  boost::scoped_array<NumEffectState> state;
  typedef osl::hash_map<HashKey, std::pair<int, Move> > map_t;
  typedef std::pair<const HashKey, std::pair<int, Move> > entry_t;
  typedef slist<const entry_t*> list_t;
  typedef hash_map<BoardKey, list_t> index_t;
  map_t depth_table;
  index_t depth_index;
  const DfpnTable& table;
  Table(const DfpnTable& t) : state(new NumEffectState[t.maxDepth()]), table(t)
  {
  }
  void store(const HashKey& key, int depth, Move best_move=Move()) 
  {
    depth_table[key] = std::make_pair(depth, best_move);
    const entry_t& e = *depth_table.find(key);
    depth_index[key.boardKey()].push_front(&e);
  }
  bool find(const HashKey& key, int& depth, Move& best_move) const
  {
    map_t::const_iterator p=depth_table.find(key);
    if (p == depth_table.end())
      return false;
    depth = p->second.first;
    best_move = p->second.second;
    return true;
  }
  bool expectMoreDepth(Player attack, const HashKey& key, int depth) const
  {
    index_t::const_iterator p=depth_index.find(key.boardKey());
    if (p == depth_index.end())
      return true;
    BOOST_FOREACH(const entry_t *q, p->second) {
      assert(q->first.boardKey() == key.boardKey());
      if (attack == BLACK) {
	if (q->first.blackStand().isSuperiorOrEqualTo(key.blackStand())) {
	  if (q->second.first >= depth)
	    return true;
	} else if (key.blackStand().isSuperiorOrEqualTo(q->first.blackStand())) {
	  if (q->second.first < depth)
	    return false;
	}
      }
      else {
	if (q->first.blackStand().isSuperiorOrEqualTo(key.blackStand())) {
	  if (q->second.first < depth)
	    return false;
	} else if (key.blackStand().isSuperiorOrEqualTo(q->first.blackStand())) {
	  if (q->second.first >= depth)
	    return true;
	}
      }
    }
    return true;
  }
  int maxDepth() const { return table.maxDepth(); }
};

osl::checkmate::
ProofTreeDepthDfpn::ProofTreeDepthDfpn(const DfpnTable& dfpn_table)
  : table(new Table(dfpn_table))
{
}

osl::checkmate::
ProofTreeDepthDfpn::~ProofTreeDepthDfpn()
{
}

int osl::checkmate::
ProofTreeDepthDfpn::depth(const HashKey& key, const NumEffectState& state, bool is_or_node) const
{
  Move dummy;
  table->state[0] = state;
  return (is_or_node ? orNode(key, dummy) : andNode(key, dummy));
}

void osl::checkmate::
ProofTreeDepthDfpn::retrievePV
(const state::NumEffectState& src, bool is_or_node,
 vector<Move>& pv) const
{
  table->state[0] = src;
  HashKey key(table->state[0]);
  pv.clear();
  for (int i=0; i<table->maxDepth(); ++i) {
    Move next;
    if (is_or_node ^ (i%2))
      orNode(key, next);
    else
      andNode(key, next);
    if (! next.isNormal())
      return;
    pv.push_back(next);
    table->state[0].makeMove(next);
    key = key.newMakeMove(next);
  }
}

int osl::checkmate::
ProofTreeDepthDfpn::orNode(const HashKey& key, Move& best_move, int height) const
{
  assert(key == HashKey(table->state[height]));  
  best_move = Move();
  if (height >= table->maxDepth())
    return -1;

  // always test ImmediateCheckmate since users do not want to see redundant solutions
  FixedDepthSearcher fixed_searcher(table->state[height]);
  ProofDisproof pdp = fixed_searcher.hasCheckmateMoveOfTurn(0, best_move);
  if (pdp.isCheckmateSuccess()) {
    table->store(key, 1, best_move);
    return 1;
  }
  pdp = fixed_searcher.hasCheckmateMoveOfTurn(2, best_move);
  if (pdp.isCheckmateSuccess()) {
    table->store(key, 3, best_move);
    return 3;
  }

  const PieceStand white_stand = PieceStand(WHITE, table->state[height]);
  DfpnRecord record = table->table.probe(key, white_stand);
  if (! record.proof_disproof.isCheckmateSuccess()) {
    table->store(key, 5, Move()); // XXX
    return 5;
  }
  {
    int recorded;
    if (table->find(key, recorded, best_move)) 
      return recorded;
  }
  table->store(key, -1, Move());

  if (! record.best_move.isNormal())
  {
    // XXX // ImmediateCheckmate
    table->store(key, 1, Move());
  }

  const HashKey new_key = key.newHashWithMove(record.best_move);
  const PieceStand next_white_stand = (table->state[height].turn() == WHITE) 
    ? white_stand.nextStand(WHITE, record.best_move) : white_stand;
  DfpnRecord new_record = table->table.probe(new_key, next_white_stand);
  if (! new_record.proof_disproof.isCheckmateSuccess())
    new_record = table->table.findProofOracle(new_key, next_white_stand, record.best_move);
  if (new_record.proof_disproof.isCheckmateSuccess()) {
    table->state[height+1] = table->state[height];
    table->state[height+1].makeMove(record.best_move);
    Move dummy;
    const int depth = andNode(new_key, dummy, height+1);
    if (depth >= 0)
    {
      best_move = record.best_move;
      table->store(key, depth+1, best_move);
      return depth+1;
    }
  }
  return 0;
}

int osl::checkmate::
ProofTreeDepthDfpn::andNode(const HashKey& key, Move& best_move, int height) const
{
  best_move = Move();
  if (height >= table->maxDepth())
    return -1;
  {
    int recorded;
    if (table->find(key, recorded, best_move)) 
      return recorded;
  }
  table->store(key, -1, Move());

  int result = 0;	// and node で指手がなくて詰 => 逃げられない
  boost::scoped_ptr<Dfpn::DfpnMoveVector> moves(new Dfpn::DfpnMoveVector);
  if (table->state[height].turn() == BLACK)
    Dfpn::generateEscape<WHITE>(table->state[height], true, Square(), *moves);
  else
    Dfpn::generateEscape<BLACK>(table->state[height], true, Square(), *moves);

  for (size_t i=0; i<moves->size(); ++i)
  {
    const HashKey new_key = key.newHashWithMove((*moves)[i]);
    if (i > 0 && ! table->expectMoreDepth(alt((*moves)[i].player()), new_key, result))
      continue;
    table->state[height+1] = table->state[height];
    table->state[height+1].makeMove((*moves)[i]);
    Move dummy;
    const int depth = orNode(new_key, dummy, height+1);
    if (depth < 0) {
      return depth;		// loop found
    }
    if (result < depth+1) {
      result = depth+1;
      best_move = (*moves)[i];
    }
  }
  
  table->store(key, result, best_move);
  return result;
}

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