Debian Codegraph @

old-sample/legacy-checkmate/test/checkmateSearcherTest.h

raw
1 /* checkmateSearcherTest.h
2 */
3 #ifndef _CHECKMATESEARCHERTEST_H
4 #define _CHECKMATESEARCHERTEST_H
5
6 #ifndef CHECKMATE_DEBUG
7 # define CHECKMATE_DEBUG
8 #endif
9
10 #include "osl/checkmate/checkmateSearcher.h"
11 #include "osl/checkmate/checkmateSearcher.tcc"
12 #include "osl/checkmate/oracleProver.tcc"
13 #include "osl/checkmate/oracleDisprover.tcc"
14 #include "osl/checkmate/analyzer/checkTableAnalyzer.h"
15
16 #include "osl/record/csaRecord.h"
17 #include "osl/record/csaString.h"
18
19 #include "osl/state/hashEffectState.h"
20 #include "osl/oslConfig.h"
21
22 #include <cppunit/TestCase.h>
23 #include <cppunit/extensions/HelperMacros.h>
24
25 #include <string>
26 #include <fstream>
27 #include <iostream>
28
29 using namespace osl;
30 using namespace osl::checkmate;
31 extern bool isShortTest;
32
33 /**
34 * 様々な CheckmateSearcher をテストするためのテンプレート
35 */
36 template <class Searcher>
37 class CheckmateSearcherTest
38 {
39 public:
40 typedef HashEffectState state_t;
41 typedef typename Searcher::table_t table_t;
42 void testNoCheck();
43 void testPawnCheckmate();
44 void test30c();
45 void testFilesCheck();
46 void testFilesNoCheck();
47 void testLimit();
48 private:
49 static CArray<int,2> numSolved;
50 template<bool isCheck,bool isDefense>
51 void testFile(const std::string& filename);
52 };
53
54 static const int total_node_limit = CHECKMATE_DEFAULT_TOTAL_NODE_LIMIT;
55
56 template <class Searcher>
57 osl::CArray<int,2> CheckmateSearcherTest<Searcher>::numSolved = {{ 0 }};
58
59 template <class Searcher>
60 void CheckmateSearcherTest<Searcher>::testPawnCheckmate()
61 {
62 {
63 SimpleState sstate=CsaString(
64 "P1 * * * * * * * -KE-OU\n"
65 "P2 * * * * * * * * +FU\n"
66 "P3 * * * * * * * +TO-FU\n"
67 "P4 * * * * * * +RY * * \n"
68 "P5 * * * * * * * * * \n"
69 "P6 * * * * * * * * * \n"
70 "P7 * * * * * * * * * \n"
71 "P8 * * * * * * * * * \n"
72 "P9 * * +OU * * * * * * \n"
73 "P+00FU\n"
74 "P-00AL\n"
75 "-\n").getInitialState();
76 state_t state(sstate);
77 table_t table(BLACK);
78 Searcher searcher(BLACK,table,total_node_limit);
79 searcher.setVerbose(!isShortTest);
80 const PathEncoding path(WHITE);
81 const Move lastMove = Move(Square(1,2),PAWN,BLACK);;
82 ProofDisproof proofDisproof
83 =searcher.hasEscapeMove(state,state.getHash(),path,1500,lastMove);
84 CPPUNIT_ASSERT_EQUAL(ProofDisproof::PawnCheckmate(),proofDisproof); // bug id 1
85 }
86 {
87 SimpleState sstate=CsaString(
88 "P1 * * * * * * * -KE-OU\n"
89 "P2 * * * * * * * * * \n"
90 "P3 * * * * * * * +TO-FU\n"
91 "P4 * * * * * * +RY * * \n"
92 "P5 * * * * * * * * * \n"
93 "P6 * * * * * * * * * \n"
94 "P7 * * * * * * * * * \n"
95 "P8 * * * * * * * * * \n"
96 "P9 * * +OU * * * * * * \n"
97 "P+00FU\n"
98 "P-00AL\n"
99 "+\n").getInitialState();
100 state_t state(sstate);
101 table_t table(BLACK);
102 const PathEncoding path(BLACK);
103 Searcher searcher(BLACK,table,total_node_limit);
104 searcher.setVerbose(!isShortTest);
105 Move bestMove;
106 ProofDisproof pdp
107 =searcher.hasCheckmateMove(state,state.getHash(),path,1500,bestMove);
108 const HashKey& key = state.getHash();
109 const CheckHashRecord *record = table.find(key);
110 CheckTableAnalyzer analyzer(table.getTwinTable());
111 CPPUNIT_ASSERT(analyzer.proofOrDisproofTreeSize(record,key,path,false));
112 CPPUNIT_ASSERT(pdp.isCheckmateFail());
113 CPPUNIT_ASSERT_EQUAL(ProofDisproof::PawnCheckmate(),
114 record->bestResultInSolved);
115 }
116 {
117 SimpleState sstate=CsaString(
118 "P1 * * * * * * * -KE * \n"
119 "P2 * * * * * * * -OU * \n"
120 "P3 * * * * * * * -FU-FU\n"
121 "P4 * * * * * * +RY+FU * \n"
122 "P5 * * * * * * * * * \n"
123 "P6 * * * * * * * * * \n"
124 "P7 * * * * * * * * * \n"
125 "P8 * * * * * * * * * \n"
126 "P9 * * +OU * * * * * * \n"
127 "P-00AL\n"
128 "+\n").getInitialState();
129 state_t state(sstate);
130 table_t table(BLACK);
131 CheckTableAnalyzer analyzer(table.getTwinTable());
132 const PathEncoding path(BLACK);
133 Searcher searcher(BLACK,table,total_node_limit);
134 searcher.setVerbose(!isShortTest);
135 Move checkMove=Move::INVALID();
136 ProofDisproof proofDisproof
137 =searcher.hasCheckmateMove(state,state.getHash(),path,3000,checkMove);
138 if (! proofDisproof.isCheckmateSuccess())
139 {
140 std::cerr << proofDisproof << "\n";
141 CheckTableAnalyzer analyzer(table.getTwinTable());
142 const CheckHashRecord *record = table.find(state.getHash());
143 std::ofstream os("CheckmateSearcherTest.log");
144 analyzer.showTree(record, os, 100, true, true);
145 }
146 CPPUNIT_ASSERT(proofDisproof.isCheckmateSuccess());
147 const Move bestMove=Move(Square(2,4),Square(2,3),PAWN,PAWN,false,BLACK);
148 CPPUNIT_ASSERT_EQUAL(bestMove, checkMove);
149 }
150 {
151 SimpleState sstate=CsaString(
152 "P1 * * * * * * * -KE * \n"
153 "P2 * * * * * * * -OU * \n"
154 "P3 * * * * * * * +TO-FU\n"
155 "P4 * * * * * * +RY * * \n"
156 "P5 * * * * * * * * * \n"
157 "P6 * * * * * * * * * \n"
158 "P7 * * * * * * * * * \n"
159 "P8 * * * * * * * * * \n"
160 "P9 * * +OU * * * * * * \n"
161 "P+00FU\n"
162 "P-00AL\n"
163 "-\n").getInitialState();
164 state_t state(sstate);
165 table_t table(BLACK);
166 CheckTableAnalyzer analyzer(table.getTwinTable());
167 const PathEncoding path(WHITE);
168 Searcher searcher(BLACK,table,total_node_limit);
169 searcher.setVerbose(!isShortTest);
170 Move lastMove=Move(Square(2,4),Square(2,3),PPAWN,PAWN,true,BLACK);
171 ProofDisproof pdp
172 =searcher.hasEscapeMove(state,state.getHash(),path,1500,lastMove);
173 CPPUNIT_ASSERT(pdp.isCheckmateFail());
174 if (pdp != ProofDisproof::PawnCheckmate())
175 {
176 const HashKey& key = state.getHash();
177 const CheckHashRecord *record = table.find(key);
178 CPPUNIT_ASSERT(analyzer.proofOrDisproofTreeSize(record,key,path,true));
179 const TwinEntry *loop = record->findLoop(path, table.getTwinTable());
180 CPPUNIT_ASSERT(loop && loop->move.record);
181 CPPUNIT_ASSERT_EQUAL(ProofDisproof::PawnCheckmate(),
182 loop->move.record->bestResultInSolved);
183 }
184 }
185 {
186 SimpleState sstate=CsaString(
187 "P1 * * * * * * * -KE-OU\n"
188 "P2 * * * * * * * * * \n"
189 "P3 * * * * * * * +TO-FU\n"
190 "P4 * * * * * * +RY * * \n"
191 "P5 * * * * * * * * * \n"
192 "P6 * * * * * * * * * \n"
193 "P7 * * * * * * * * * \n"
194 "P8 * * * * * * * * * \n"
195 "P9 * * +OU * * * * * * \n"
196 "P+00FU\n"
197 "P-00AL\n"
198 "+\n").getInitialState();
199 state_t state(sstate);
200 const PathEncoding path(BLACK);
201 table_t table(BLACK);
202 CheckTableAnalyzer analyzer(table.getTwinTable());
203 Searcher searcher(BLACK,table,total_node_limit);
204 searcher.setVerbose(!isShortTest);
205 Move checkMove=Move::INVALID();
206 ProofDisproof pdp
207 =searcher.hasCheckmateMove(state,state.getHash(),path,1000,checkMove);
208 const HashKey& key = state.getHash();
209 const CheckHashRecord *record = table.find(key);
210 CPPUNIT_ASSERT(analyzer.proofOrDisproofTreeSize(record,key,path,false));
211 CPPUNIT_ASSERT_EQUAL(ProofDisproof::PawnCheckmate(),
212 record->bestResultInSolved);
213 CPPUNIT_ASSERT(pdp.isCheckmateFail());
214 }
215 }
216
217 template <class Searcher>
218 void CheckmateSearcherTest<Searcher>::testNoCheck()
219 {
220 {
221 SimpleState sstate=CsaString(
222 "P1 * * * * -KI * * -KE-KY\n"
223 "P2 * -OU * * * * * -HI * \n"
224 "P3 * -FU * -KI-FU-FU-GI * -FU\n"
225 "P4+FU * +FU-FU * * * * * \n"
226 "P5-FU * * * * * * -FU * \n"
227 "P6 * -UM * * +UM * * * +FU\n"
228 "P7 * * * +FU+FU+FU+FU+FU * \n"
229 "P8 * * * +KI * * * +HI * \n"
230 "P9+KY * * * +OU * +GI+KE+KY\n"
231 "P+00FU\n"
232 "P-00FU\n"
233 "P-00FU\n"
234 "P+00KE\n"
235 "P-00KE\n"
236 "P-00GI\n"
237 "P-00GI\n"
238 "P-00KI\n"
239 "P+00KY\n"
240 "-\n").getInitialState();
241 state_t state(sstate);
242 table_t table(WHITE);
243 CheckTableAnalyzer analyzer(table.getTwinTable());
244 const PathEncoding path(WHITE);
245 Searcher searcher(WHITE,table,total_node_limit);
246 searcher.setVerbose(!isShortTest);
247 Move checkMove=Move::INVALID();
248 ProofDisproof proofDisproof
249 =searcher.hasCheckmateMove(state,state.getHash(),path,1000,checkMove);
250 CPPUNIT_ASSERT(proofDisproof.isUnknown());
251 proofDisproof
252 =searcher.hasCheckmateMove(state,state.getHash(),path,100000,checkMove);
253 CPPUNIT_ASSERT(! proofDisproof.isCheckmateSuccess());
254 }
255 {
256 SimpleState sstate=CsaString(
257 "P1-KY-KE * -KI * * * -KE-KY\n"
258 "P2 * -OU-GI * -KI * -HI * * \n"
259 "P3 * -FU * * -FU-GI-KA-FU-FU\n"
260 "P4-FU * -FU-FU * -FU-FU * * \n"
261 "P5 * * * * * * * +FU * \n"
262 "P6+FU * +FU * +FU+GI+FU * +FU\n"
263 "P7 * +FU * +FU * +FU * * * \n"
264 "P8 * +KA+OU * +KI+GI * +HI * \n"
265 "P9+KY+KE * +KI * * * +KE+KY\n"
266 "+\n").getInitialState();
267 state_t state(sstate);
268 const PathEncoding path(BLACK);
269 table_t table(BLACK);
270 CheckTableAnalyzer analyzer(table.getTwinTable());
271 Searcher searcher(BLACK,table,total_node_limit);
272 searcher.setVerbose(!isShortTest);
273 Move checkMove=Move::INVALID();
274 const ProofDisproof result
275 = searcher.hasCheckmateMove(state,state.getHash(),path,1000,checkMove);
276 CPPUNIT_ASSERT(result.isCheckmateFail());
277 }
278 {
279 state_t state(CsaString(
280 "P1 * * * -FU * * * * *\n"
281 "P2-OU+FU * * * * * * *\n"
282 "P3 * +KE+OU * * * * * *\n"
283 "P4 * * * * * * * * *\n"
284 "P5 * * * * * * * * *\n"
285 "P6-FU * * * * * * * *\n"
286 "P7 * * +GI * * * * * *\n"
287 "P8 * * * * * * * * *\n"
288 "P9 * * * * * * * * *\n"
289 "P+00KY\n"
290 "P-00HI00HI00KA00KA00KI00KI00KI00KI00GI00GI00GI00KE00KE00KE00KY00KY00KY00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU\n"
291 "+\n").getInitialState());
292 const PathEncoding path(BLACK);
293 table_t table(BLACK);
294 CheckTableAnalyzer analyzer(table.getTwinTable());
295 Searcher searcher(BLACK,table,total_node_limit);
296 searcher.setVerbose(!isShortTest);
297 Move checkMove=Move::INVALID();
298 const ProofDisproof result
299 = searcher.hasCheckmateMove(state,state.getHash(),path,8000,checkMove);
300 if (! result.isCheckmateFail())
301 std::cerr << "false checkmate " << typeid(*this).name() << " " << checkMove << "\n";
302 CPPUNIT_ASSERT(result.isCheckmateFail());
303 }
304 }
305
306 /**
307 *
308 */
309 template <class Searcher>
310 void CheckmateSearcherTest<Searcher>::testLimit()
311 {
312 SimpleState sstate0=CsaString(
313 "P1-KY-KE * -KI * * * -KE-KY\n"
314 "P2 * -OU-GI * -KI * -HI * * \n"
315 "P3 * -FU * * -FU-GI-KA-FU-FU\n"
316 "P4-FU * -FU-FU * -FU-FU * * \n"
317 "P5 * * * * * * * +FU * \n"
318 "P6+FU * +FU * +FU+GI+FU * +FU\n"
319 "P7 * +FU * +FU * +FU * * * \n"
320 "P8 * +KA+OU * +KI+GI * +HI * \n"
321 "P9+KY+KE * +KI * * * +KE+KY\n"
322 "+\n").getInitialState();
323 state_t state0(sstate0);
324
325
326 SimpleState sstate1=CsaString(
327 "P1+NY+TO * * * * -OU-KE-KY\n"
328 "P2 * * * * * -GI-KI * *\n"
329 "P3 * +RY * * +UM * -KI-FU-FU\n"
330 "P4 * * +FU-FU * * * * *\n"
331 "P5 * * -KE * +FU * * +FU *\n"
332 "P6-KE * * +FU+GI-FU * * +FU\n"
333 "P7 * * -UM * * * * * *\n"
334 "P8 * * * * * * * * * \n"
335 "P9 * +OU * -GI * * * * -NG\n"
336 "P+00HI00KI00KE00KY00FU00FU00FU00FU00FU00FU\n"
337 "P-00KI00KY00FU00FU\n"
338 "P-00AL\n"
339 "+\n").getInitialState();
340 state_t state1(sstate1);
341 const PathEncoding path(BLACK);
342
343 table_t table(BLACK);
344 CheckTableAnalyzer analyzer(table.getTwinTable());
345 Searcher searcher(BLACK,table,10);
346 searcher.setVerbose(!isShortTest);
347 /* すぐに詰まないことがわかる局面を読む */
348 {
349 Move checkMove=Move::INVALID();
350 ProofDisproof proofDisproof
351 =searcher.hasCheckmateMove(state0,state0.getHash(),path,1000,checkMove);
352 CPPUNIT_ASSERT(proofDisproof.isCheckmateFail());
353 }
354 /* 詰みのチェックに時間がかかる局面を読む */
355 {
356 Move checkMove=Move::INVALID();
357 ProofDisproof proofDisproof
358 =searcher.hasCheckmateMove(state1,state1.getHash(),path,1000,checkMove);
359 CPPUNIT_ASSERT(proofDisproof.isUnknown());
360 }
361 /* すぐに詰まないことがわかる局面を読む */
362 {
363 Move checkMove=Move::INVALID();
364 ProofDisproof proofDisproof
365 =searcher.hasCheckmateMove(state0,state0.getHash(),path,1000,checkMove);
366 CPPUNIT_ASSERT(proofDisproof.isCheckmateFail());
367 }
368 /* 詰みのチェックに時間がかかる局面を読む */
369 {
370 Move checkMove=Move::INVALID();
371 ProofDisproof proofDisproof
372 =searcher.hasCheckmateMove(state1,state1.getHash(),path,1000,checkMove);
373 CPPUNIT_ASSERT(proofDisproof.isUnknown());
374 }
375 }
376
377
378 template <class Searcher>
379 template<bool isCheck,bool isDefense>
380 void CheckmateSearcherTest<Searcher>::testFile(const std::string& filename)
381 {
382 static table_t tableBlack(BLACK);
383 static table_t tableWhite(WHITE);
384
385 Record rec=CsaFile(filename).getRecord();
386 vector<osl::Move> moves=rec.getMoves();
387 SimpleState sstate=rec.getInitialState();
388
389 state_t state(sstate);
390 ProofDisproof proofDisproof;
391 Move checkMove=Move::INVALID();
392 const Player attacker = ((state.turn()==BLACK) ^ isDefense) ? BLACK:WHITE;
393 table_t& table = (attacker == BLACK) ? tableBlack : tableWhite;
394 const PathEncoding path(state.turn());
395 table.clear();
396 table.clearTwins();
397 Searcher searcher(attacker,table,total_node_limit);
398 if (isDefense)
399 proofDisproof
400 =searcher.hasEscapeMove(state,state.getHash(),path,100000,checkMove);
401 else
402 proofDisproof
403 =searcher.hasCheckmateMove(state,state.getHash(),path,100000,checkMove);
404
405 if (! isShortTest)
406 {
407 if (proofDisproof.isFinal())
408 std::cerr << " | ";
409 else
410 std::cerr << "proof=" << proofDisproof.proof()
411 << ", disproof=" << proofDisproof.disproof() << " ";
412 }
413 if (proofDisproof.isFinal())
414 {
415 CheckTableAnalyzer analyzer(table.getTwinTable());
416 ++numSolved[isCheck];
417 if (isCheck)
418 {
419 CPPUNIT_ASSERT(proofDisproof.isCheckmateSuccess());
420 if ((! moves.empty()) && (checkMove!=moves[0]))
421 {
422 if (! isShortTest)
423 std::cerr << "different solution in " << filename << "\n";
424 #if 0
425 std::cerr << "checkMove=" << checkMove << ",correct answer=" << moves[0] << std::endl;
426 std::cerr << sstate;
427 #endif
428 }
429 const HashKey& key = state.getHash();
430 const CheckHashRecord *record = table.find(key);
431 const size_t proofSize = analyzer.proofTreeSize(record, key, path, ! isDefense);
432 if (!isShortTest)
433 std::cerr << proofSize << "\n";
434 }
435 else
436 {
437 CPPUNIT_ASSERT(proofDisproof.isCheckmateFail());
438 CheckTableAnalyzer analyzer(table.getTwinTable());
439 const HashKey& key = state.getHash();
440 const CheckHashRecord *record = table.find(key);
441 const size_t proofSize = analyzer.proofOrDisproofTreeSize(record, key, path, isDefense);
442 if (!isShortTest)
443 std::cerr << proofSize << "\n";
444 }
445 }
446 }
447
448 template <class Searcher>
449 void CheckmateSearcherTest<Searcher>::test30c()
450 {
451 if (! isShortTest)
452 std::cerr << "testFilesCheck: 30c.csa" << std::endl;
453 testFile<true,true>(OslConfig::testFile("ProblemProgress2/checkmate/30c.csa"));
454 }
455
456 template <class Searcher>
457 void CheckmateSearcherTest<Searcher>::testFilesCheck()
458 {
459 std::ifstream ifs(OslConfig::testFile("kisenCheckProblems/FILES"));
460 CPPUNIT_ASSERT(ifs);
461 int i=0;
462 int count=400;
463 if (isShortTest)
464 count=40;
465 std::string filename;
466 while((ifs >> filename) && (++i<count)) {
467 if(filename == "")
468 break;
469 if (! isShortTest)
470 std::cerr << "testFilesCheck: " << filename << " ";
471 testFile<true,false>(OslConfig::testFile("kisenCheckProblems/" + filename));
472 }
473 const double ratio = (double)numSolved[true]/i;
474 if (! isShortTest)
475 std::cerr << numSolved[true] << " / " << i << " = "
476 << ratio << "\n";
477 CPPUNIT_ASSERT(ratio >= 0.9);
478 }
479
480 template <class Searcher>
481 void CheckmateSearcherTest<Searcher>::testFilesNoCheck()
482 {
483 // CheckmateRecorder::DepthTracer::maxVerboseLogDepth=5;
484 std::ifstream ifs(OslConfig::testFile("kisenNoCheckProblems/FILES"));
485 CPPUNIT_ASSERT(ifs);
486 int i=0;
487 int count=400;
488 if (isShortTest)
489 count=40;
490 std::string filename;
491 while((ifs >> filename) && (++i<count)) {
492 if(filename == "")
493 break;
494 if (! isShortTest)
495 std::cerr << "testFilesNoCheck: " << filename << " ";
496 testFile<false,false>(OslConfig::testFile("kisenNoCheckProblems/" + filename));
497 }
498 const double ratio = (double)numSolved[false]/i;
499 std::cerr << numSolved[false] << " / " << i << " = "
500 << ratio << "\n";
501 CPPUNIT_ASSERT(ratio >= 0.9);
502 }
503
504
505
506 #endif /* _CHECKMATESEARCHERTEST_H */
507 // ;;; Local Variables:
508 // ;;; mode:c++
509 // ;;; c-basic-offset:2
510 // ;;; End: