| 1 |
#include "dualCheckmateSearcher.h" |
| 2 |
#include "dualCheckmateSearcher.tcc" |
| 3 |
#include "checkmateSearcher.tcc" |
| 4 |
#include "osl/checkmate/fixedDepthSearcher.tcc" |
| 5 |
#include "oracleProver.tcc" |
| 6 |
#include "oracleDisprover.tcc" |
| 7 |
#include "analyzer/checkTableAnalyzer.h" |
| 8 |
#include "analyzer/proofTreeDepth.h" |
| 9 |
#include "simpleCheckHashTable.h" |
| 10 |
#include "dominanceTable.h" |
| 11 |
#include "checkHashRecord.h" |
| 12 |
#include "checkmateRecorder.h" |
| 13 |
#include "osl/checkmate/libertyEstimator.h" |
| 14 |
#include "nullEstimator.h" |
| 15 |
#include "pieceCost2.h" |
| 16 |
#include "nullCost.h" |
| 17 |
#include "osl/record/csaString.h" |
| 18 |
#include "osl/record/csaRecord.h" |
| 19 |
#include "osl/state/numEffectState.h" |
| 20 |
#include "osl/misc/perfmon.h" |
| 21 |
#include "osl/misc/milliSeconds.h" |
| 22 |
|
| 23 |
#include <boost/scoped_ptr.hpp> |
| 24 |
#include <string> |
| 25 |
#include <iostream> |
| 26 |
#include <fstream> |
| 27 |
#include <cstdlib> |
| 28 |
#include <unistd.h> |
| 29 |
|
| 30 |
using namespace osl; |
| 31 |
using namespace osl::checkmate; |
| 32 |
using namespace osl::misc; |
| 33 |
using osl::checkmate::analyzer::ProofTreeDepth; |
| 34 |
void usage(const char *prog) |
| 35 |
{ |
| 36 |
using namespace std; |
| 37 |
cerr << "Usage: " << prog << " [-vpPEOt] [-T tree-size-out] [-F FILES] [-l nodelimit] [-L loglimit] [-o tree-output-filename] [-d logdepth] [-A depth(>0)] [-a depth(>0)] csa-filenames " |
| 38 |
<< endl |
| 39 |
<< "-v show result\n" |
| 40 |
<< "-p show proof tree\n" |
| 41 |
<< "-P (plain) don't use heuristics\n" |
| 42 |
<< "-A depth show all tree (expanding solved branch)\n" |
| 43 |
<< "-a depth show tree\n" |
| 44 |
<< "-E show filnames that are solved and found to be 'escape'\n" |
| 45 |
<< "-t force turn's player to attack even if he is in check\n" |
| 46 |
<< "-O use outline format for dumping tree\n" |
| 47 |
<< endl; |
| 48 |
exit(1); |
| 49 |
} |
| 50 |
|
| 51 |
bool verbose=false; |
| 52 |
unsigned long long total_cycles=0; |
| 53 |
size_t limit = 409600; // 3900; |
| 54 |
bool showProofTree = false; |
| 55 |
int showAllTreeDepth = 0; |
| 56 |
int showTreeDepth = 0; |
| 57 |
bool showEscapeFilename = false; |
| 58 |
bool force_attack = false; |
| 59 |
bool useDominanceRelation = false; |
| 60 |
int num_checkmate=0, num_escape=0, num_unkown=0; |
| 61 |
double total_nodes=0, total_tables=0; |
| 62 |
bool useOutlineFormat = false; |
| 63 |
size_t logThreshold = 0; |
| 64 |
bool useHeuristics = true; |
| 65 |
|
| 66 |
std::ostream *treeOut = 0; |
| 67 |
std::ostream *tree_size_out = 0; |
| 68 |
|
| 69 |
template <class Table, class H, class Cost> |
| 70 |
void search(const char *filename); |
| 71 |
void searchFile(const char *filename); |
| 72 |
|
| 73 |
int main(int argc, char **argv) |
| 74 |
{ |
| 75 |
const char *program_name = argv[0]; |
| 76 |
bool error_flag = false; |
| 77 |
extern char *optarg; |
| 78 |
extern int optind; |
| 79 |
|
| 80 |
const char *FILES = 0; |
| 81 |
treeOut = &std::cout; |
| 82 |
std::unique_ptr<std::ofstream> treefs, treesizefs; |
| 83 |
|
| 84 |
char c; |
| 85 |
while ((c = getopt(argc, argv, "A:a:Dd:F:EL:l:Oo:T:Pptvh")) != EOF) |
| 86 |
{ |
| 87 |
switch(c) |
| 88 |
{ |
| 89 |
case 'A': showAllTreeDepth = atoi(optarg); |
| 90 |
assert(showAllTreeDepth); |
| 91 |
break; |
| 92 |
case 'a': showTreeDepth = atoi(optarg); |
| 93 |
assert(showTreeDepth); |
| 94 |
break; |
| 95 |
case 'D': useDominanceRelation = true; |
| 96 |
break; |
| 97 |
case 'd': checkmate::CheckmateRecorder::DepthTracer::maxVerboseLogDepth |
| 98 |
= atoi(optarg); |
| 99 |
break; |
| 100 |
case 'E': showEscapeFilename = true; |
| 101 |
break; |
| 102 |
case 'F': FILES = optarg; |
| 103 |
break; |
| 104 |
case 'L': logThreshold = atoi(optarg); |
| 105 |
assert(logThreshold); |
| 106 |
break; |
| 107 |
case 'l': limit = atoi(optarg); |
| 108 |
assert(limit); |
| 109 |
break; |
| 110 |
case 'O': useOutlineFormat = true; |
| 111 |
break; |
| 112 |
case 'o': treefs.reset(new std::ofstream(optarg)); |
| 113 |
treeOut = &*treefs; |
| 114 |
break; |
| 115 |
case 'P': useHeuristics = false; |
| 116 |
break; |
| 117 |
case 'p': showProofTree = true; |
| 118 |
break; |
| 119 |
case 't': force_attack = true; |
| 120 |
break; |
| 121 |
case 'T': treesizefs.reset(new std::ofstream(optarg)); |
| 122 |
tree_size_out = &*treesizefs; |
| 123 |
break; |
| 124 |
case 'v': verbose = true; |
| 125 |
break; |
| 126 |
default: error_flag = true; |
| 127 |
} |
| 128 |
} |
| 129 |
argc -= optind; |
| 130 |
argv += optind; |
| 131 |
|
| 132 |
if (error_flag || ((argc < 1) && (! FILES))) |
| 133 |
usage(program_name); |
| 134 |
|
| 135 |
std::cerr << "limit " << limit << "\n"; |
| 136 |
try |
| 137 |
{ |
| 138 |
for (int i=0; i<argc; ++i) |
| 139 |
{ |
| 140 |
searchFile(argv[i]); |
| 141 |
total_cycles = 0; |
| 142 |
} |
| 143 |
if (FILES) |
| 144 |
{ |
| 145 |
std::ifstream is(FILES); |
| 146 |
std::string filename; |
| 147 |
size_t count=0; |
| 148 |
while (is >> filename) |
| 149 |
{ |
| 150 |
if (tree_size_out && (++count % 512) == 0) |
| 151 |
*tree_size_out << std::flush; |
| 152 |
searchFile(filename.c_str()); |
| 153 |
total_cycles = 0; |
| 154 |
} |
| 155 |
} |
| 156 |
std::cerr << "check " << num_checkmate << " escape " << num_escape |
| 157 |
<< " unknown " << num_unkown << "\n"; |
| 158 |
std::cerr << "total nodes " << total_nodes |
| 159 |
<< " tables " << total_tables << "\n"; |
| 160 |
} |
| 161 |
catch (std::exception& e) |
| 162 |
{ |
| 163 |
std::cerr << e.what() << "\n"; |
| 164 |
return 1; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void dumpTree(const CheckTableAnalyzer& analyzer, |
| 169 |
const CheckHashRecord *record) |
| 170 |
{ |
| 171 |
if (! record) |
| 172 |
return; |
| 173 |
Move debug_move; // place the first move for debug here |
| 174 |
for (CheckMoveList::const_iterator p=record->moves.begin(); p!=record->moves.end(); |
| 175 |
++p) |
| 176 |
{ |
| 177 |
if (p->move == debug_move) |
| 178 |
{ |
| 179 |
if (p->record) |
| 180 |
{ |
| 181 |
p->record->dump(); |
| 182 |
record = p->record; |
| 183 |
} |
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (showAllTreeDepth) |
| 189 |
{ |
| 190 |
analyzer.showTree(record,*treeOut,showAllTreeDepth,true,false,logThreshold); |
| 191 |
} |
| 192 |
if (showTreeDepth) |
| 193 |
{ |
| 194 |
analyzer.showTree(record,*treeOut,showTreeDepth,false,false,logThreshold); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void searchFile(const char *filename) |
| 199 |
{ |
| 200 |
if (useDominanceRelation) |
| 201 |
{ |
| 202 |
if (useHeuristics) |
| 203 |
search<DominanceTable,LibertyEstimator,PieceCost2>(filename); |
| 204 |
else |
| 205 |
search<DominanceTable,PureLibertyEstimator,NullCost>(filename); |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
if (useHeuristics) |
| 210 |
search<SimpleCheckHashTable,LibertyEstimator,PieceCost2>(filename); |
| 211 |
else |
| 212 |
search<SimpleCheckHashTable,PureLibertyEstimator,NullCost>(filename); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* CSA 形式のファイルから stream から1局面(だけを)読む |
| 218 |
*/ |
| 219 |
void readCsaState(std::istream& is, SimpleState& state) |
| 220 |
{ |
| 221 |
Record rec; |
| 222 |
record::csa::InputStream irs(is); |
| 223 |
irs.load(&rec); |
| 224 |
state = rec.getInitialState(); |
| 225 |
} |
| 226 |
|
| 227 |
double real_seconds = 0.0; |
| 228 |
|
| 229 |
template <Player P, class Searcher> |
| 230 |
void testWinOrLose(const char *curFilename, |
| 231 |
Searcher& searcher, |
| 232 |
const SimpleState& sstate, int limit) |
| 233 |
{ |
| 234 |
typedef typename Searcher::table_t table_t; |
| 235 |
NumEffectState state(sstate); |
| 236 |
const PathEncoding path(state.turn()); |
| 237 |
const Square my_king = state.template kingSquare<P>(); |
| 238 |
if ((! force_attack) |
| 239 |
&& ! my_king.isPieceStand() && state.hasEffectAt(alt(P), my_king)) |
| 240 |
{ |
| 241 |
// 相手から王手がかかっている |
| 242 |
time_point timer = clock::now(); |
| 243 |
misc::PerfMon clock; |
| 244 |
const bool lose |
| 245 |
= searcher.template isLosingState<P>(limit, state, HashKey(state), path); |
| 246 |
total_cycles += clock.stop(); |
| 247 |
real_seconds = timer.elapsedSeconds(); |
| 248 |
|
| 249 |
const HashKey key(state); |
| 250 |
const table_t& table = searcher.getTable(alt(P)); |
| 251 |
const CheckHashRecord *record = table.find(key); |
| 252 |
if (lose) { |
| 253 |
++num_checkmate; |
| 254 |
} |
| 255 |
else { |
| 256 |
if (record->proofDisproof().isCheckmateFail() |
| 257 |
|| record->findLoop(path, table.getTwinTable())) |
| 258 |
++num_escape; |
| 259 |
else { |
| 260 |
assert(! record->proofDisproof().isFinal()); |
| 261 |
++num_unkown; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (! verbose) |
| 266 |
return; |
| 267 |
|
| 268 |
CheckTableAnalyzer analyzer(table.getTwinTable(), useOutlineFormat); |
| 269 |
dumpTree(analyzer, record); |
| 270 |
if (lose) |
| 271 |
{ |
| 272 |
size_t leaf_size; |
| 273 |
const size_t tree_size |
| 274 |
= analyzer.proofTreeSize(record,key,path,false,leaf_size); |
| 275 |
ProofTreeDepth depth_analyzer; |
| 276 |
const unsigned int depth = depth_analyzer.depth(record, false); |
| 277 |
std::cerr << "lose\n"; |
| 278 |
std::cerr << "proof tree " << tree_size |
| 279 |
<< " leaf " << leaf_size |
| 280 |
<< " depth " << depth << "\n"; |
| 281 |
if (tree_size_out) |
| 282 |
{ |
| 283 |
*tree_size_out << tree_size << " " << table.size() << "\n"; |
| 284 |
} |
| 285 |
if (showProofTree) |
| 286 |
analyzer.showProofTree(record,key,path,false,*treeOut); |
| 287 |
return; |
| 288 |
} |
| 289 |
else |
| 290 |
{ |
| 291 |
assert(record); |
| 292 |
if (showEscapeFilename) |
| 293 |
std::cerr << curFilename << "\n"; |
| 294 |
if (record->proofDisproof().isCheckmateFail() |
| 295 |
|| record->findLoop(path, table.getTwinTable())) |
| 296 |
{ |
| 297 |
size_t leaf_size; |
| 298 |
const size_t tree_size |
| 299 |
= analyzer.disproofTreeSize(record,key,path,true, leaf_size); |
| 300 |
std::cerr << "escape\n"; |
| 301 |
std::cerr << "disproof tree " << tree_size |
| 302 |
<< " leaf " << leaf_size << "\n"; |
| 303 |
if (tree_size_out) |
| 304 |
*tree_size_out << tree_size << " " << table.size() << "\n"; |
| 305 |
|
| 306 |
if (showProofTree) |
| 307 |
analyzer.showProofTree(record,key,path,true,*treeOut); |
| 308 |
return; |
| 309 |
} |
| 310 |
else |
| 311 |
{ |
| 312 |
assert(! record->proofDisproof().isFinal()); |
| 313 |
std::cerr << "unknown " << record->proofDisproof() << "\n"; |
| 314 |
return; |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
Move checkmateMove; |
| 321 |
time_point timer=clock::now(); |
| 322 |
PerfMon clock; |
| 323 |
AttackOracleAges age; |
| 324 |
const bool win = searcher. |
| 325 |
template isWinningState<P>(limit, state, HashKey(state), path, checkmateMove, age); |
| 326 |
total_cycles += clock.stop(); |
| 327 |
real_seconds = timer.elapsedSeconds(); |
| 328 |
#if 0 |
| 329 |
std::cerr << " done\n"; |
| 330 |
#endif |
| 331 |
const HashKey key(state); |
| 332 |
const table_t& table = searcher.getTable(P); |
| 333 |
const CheckHashRecord *record = table.find(HashKey(state)); |
| 334 |
if (win) { |
| 335 |
++num_checkmate; |
| 336 |
} |
| 337 |
else { |
| 338 |
assert(record); |
| 339 |
if (record->proofDisproof().isFinal() |
| 340 |
|| record->findLoop(path, table.getTwinTable())) |
| 341 |
++num_escape; |
| 342 |
else |
| 343 |
++num_unkown; |
| 344 |
} |
| 345 |
if (! verbose) |
| 346 |
return; |
| 347 |
|
| 348 |
CheckTableAnalyzer analyzer(table.getTwinTable(), useOutlineFormat); |
| 349 |
if ((record->proofDisproof() != ProofDisproof::Checkmate()) |
| 350 |
&& (record->proofDisproof() != ProofDisproof::NoCheckmate())) |
| 351 |
std::cerr << record->proofDisproof() << "\n"; |
| 352 |
// std::cerr << record->bestResultInSolved << "\n"; |
| 353 |
dumpTree(analyzer, record); |
| 354 |
if (win) |
| 355 |
{ |
| 356 |
size_t leaf_size; |
| 357 |
const size_t tree_size = analyzer.proofTreeSize(record,key,path,true, |
| 358 |
leaf_size); |
| 359 |
ProofTreeDepth depth_analyzer; |
| 360 |
const unsigned int depth = depth_analyzer.depth(record, true); |
| 361 |
std::cerr << "win by " << checkmateMove << "\n"; |
| 362 |
std::cerr << "proof tree " << tree_size |
| 363 |
<< " leaf " << leaf_size |
| 364 |
<< " depth " << depth << "\n"; |
| 365 |
if (tree_size_out) |
| 366 |
*tree_size_out << tree_size << " " << table.size() << "\n"; |
| 367 |
if (showProofTree) |
| 368 |
analyzer.showProofTree(record,key,path,true,*treeOut); |
| 369 |
return; |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
assert(record); |
| 374 |
if (record->proofDisproof().isFinal() |
| 375 |
|| record->findLoop(path, table.getTwinTable())) |
| 376 |
{ |
| 377 |
size_t leaf_size; |
| 378 |
const size_t tree_size |
| 379 |
= analyzer.disproofTreeSize(record,key,path,false, leaf_size); |
| 380 |
std::cerr << "no checkmate\n"; |
| 381 |
std::cerr << "disproof tree " << tree_size |
| 382 |
<< " leaf " << leaf_size << "\n"; |
| 383 |
if (tree_size_out) |
| 384 |
*tree_size_out << tree_size << " " << table.size() << "\n"; |
| 385 |
if (showProofTree) |
| 386 |
analyzer.showProofTree(record,key,path,false,*treeOut); |
| 387 |
return; |
| 388 |
} |
| 389 |
else |
| 390 |
{ |
| 391 |
std::cerr << "unknown " << record->proofDisproof() << "\n"; |
| 392 |
return; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
template <class Table, class H, class Cost> |
| 399 |
void search(const char *filename) |
| 400 |
{ |
| 401 |
std::ifstream is(filename); |
| 402 |
if (! is) { |
| 403 |
std::cerr << "\nskipping " << filename << "\n"; |
| 404 |
return; |
| 405 |
} |
| 406 |
if (verbose) |
| 407 |
std::cerr << "\nsolving " << filename << "\n"; |
| 408 |
|
| 409 |
typedef DualCheckmateSearcher<Table, H, Cost> searcher_t; |
| 410 |
searcher_t searcher(std::max(limit, (size_t)20000000)); |
| 411 |
searcher.setVerbose(verbose); |
| 412 |
|
| 413 |
SimpleState state; |
| 414 |
readCsaState(is, state); |
| 415 |
|
| 416 |
if (state.turn() == BLACK) |
| 417 |
testWinOrLose<BLACK,searcher_t>(filename, searcher, state, limit); |
| 418 |
else |
| 419 |
testWinOrLose<WHITE,searcher_t>(filename, searcher, state, limit); |
| 420 |
|
| 421 |
const size_t table_used = searcher.getTable(BLACK).size() |
| 422 |
+ searcher.getTable(WHITE).size(); |
| 423 |
|
| 424 |
total_nodes += searcher.totalNodeCount(); |
| 425 |
total_tables += table_used; |
| 426 |
|
| 427 |
if (verbose) { |
| 428 |
PerfMon::message(total_cycles, "total ", |
| 429 |
searcher.totalNodeCount()); |
| 430 |
PerfMon::message(total_cycles, "unique", table_used); |
| 431 |
std::cerr << "real " << real_seconds << " sec.\n"; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
/* ------------------------------------------------------------------------- */ |
| 437 |
// ;;; Local Variables: |
| 438 |
// ;;; mode:c++ |
| 439 |
// ;;; c-basic-offset:2 |
| 440 |
// ;;; End: |