| 1 |
/* treeWriter.cc |
| 2 |
*/ |
| 3 |
#include "analyzer/treeWriter.h" |
| 4 |
#include "analyzer/recordSet.h" |
| 5 |
#include "checkHashRecord.h" |
| 6 |
#include "checkMove.h" |
| 7 |
#include "sameBoardList.h" |
| 8 |
#include "osl/container/moveVector.h" |
| 9 |
#include "osl/record/csa.h" |
| 10 |
|
| 11 |
// TreeWriter |
| 12 |
|
| 13 |
osl::checkmate::analyzer:: |
| 14 |
TreeWriter::TreeWriter() |
| 15 |
: depth(1) |
| 16 |
{ |
| 17 |
} |
| 18 |
osl::checkmate::analyzer:: |
| 19 |
TreeWriter::~TreeWriter() |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
// DotWriter |
| 24 |
osl::checkmate::analyzer:: |
| 25 |
DotWriter::DotWriter(std::ostream& o, size_t m, const char *name) |
| 26 |
: os(o), visited(new RecordSet()), minimumPdp(m) |
| 27 |
{ |
| 28 |
os << "// minimumPdp " << minimumPdp << "\n"; |
| 29 |
os << "digraph " << (name ? name : "OSL_DotWriter") << " {\n"; |
| 30 |
} |
| 31 |
|
| 32 |
osl::checkmate::analyzer:: |
| 33 |
DotWriter::~DotWriter() |
| 34 |
{ |
| 35 |
os << "}\n"; |
| 36 |
} |
| 37 |
|
| 38 |
const std::string osl::checkmate::analyzer:: |
| 39 |
DotWriter::header() const |
| 40 |
{ |
| 41 |
return " "; |
| 42 |
} |
| 43 |
|
| 44 |
namespace osl |
| 45 |
{ |
| 46 |
namespace |
| 47 |
{ |
| 48 |
bool focus(const CheckHashRecord& record, size_t threshold) |
| 49 |
{ |
| 50 |
const ProofDisproof& pdp = record.proofDisproof(); |
| 51 |
return (! record.twins.empty()) |
| 52 |
|| pdp.isFinal() |
| 53 |
|| ((pdp.proof() > threshold) || (pdp.disproof() > threshold)); |
| 54 |
} |
| 55 |
bool primaryFocus(const CheckHashRecord& record) |
| 56 |
{ |
| 57 |
const size_t threshold = 300; |
| 58 |
return focus(record, threshold); |
| 59 |
} |
| 60 |
|
| 61 |
void writeEdge(std::ostream& os, const std::string& header, |
| 62 |
const CheckHashRecord *from, |
| 63 |
const CheckHashRecord *to, |
| 64 |
const char *color, const char *style) |
| 65 |
{ |
| 66 |
os << header << 'N' << from << " -> " << 'N' << to |
| 67 |
<< " [weight=0, color=" << color << ", style=" << style << "]\n"; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void osl::checkmate::analyzer:: |
| 73 |
DotWriter::showRecord(const CheckHashRecord *record) |
| 74 |
{ |
| 75 |
assert(record); |
| 76 |
if (! visited->insert(record).second) |
| 77 |
return; |
| 78 |
|
| 79 |
os << header() << 'N' << record << ' '; |
| 80 |
os << "[label=\"" << record->proofDisproof() |
| 81 |
<< "\\nd=" << record->distance |
| 82 |
<< (record->useMaxInsteadOfSum ? "(max)" : "") |
| 83 |
<< "\""; |
| 84 |
if (record->proofDisproof().isFinal()) |
| 85 |
os << ",fontcolor=blue"; |
| 86 |
else if (primaryFocus(*record)) |
| 87 |
os << ",fontcolor=red"; |
| 88 |
if (getDepth() % 2) |
| 89 |
os << ",shape=box"; |
| 90 |
os << "]\n"; |
| 91 |
// TODO: bestMove, twins |
| 92 |
|
| 93 |
// dominance |
| 94 |
if (record->finalByDominance()) |
| 95 |
{ |
| 96 |
writeEdge(os, header(), record->finalByDominance(), record, |
| 97 |
"green", "dashed"); |
| 98 |
} |
| 99 |
if (record->sameBoards) |
| 100 |
{ |
| 101 |
for (SameBoardList::const_iterator p=record->sameBoards->begin(); |
| 102 |
p!=record->sameBoards->end(); ++p) |
| 103 |
{ |
| 104 |
if (p->stand(BLACK) == record->stand(BLACK)) |
| 105 |
continue; |
| 106 |
if (p->stand(BLACK).hasMoreThan<BLACK>(record->stand(BLACK))) |
| 107 |
{ |
| 108 |
writeEdge(os, header(), record, &*p, "blue", "dotted"); |
| 109 |
showRecord(&*p); |
| 110 |
} |
| 111 |
#if 0 |
| 112 |
// 念の為 ShowAllTree::isTerminal との連携がないと,ちゅうぶらりんの |
| 113 |
// ノードができる. |
| 114 |
if (record->blackStand.hasMoreThan<BLACK>(p->blackStand)) |
| 115 |
{ |
| 116 |
writeEdge(os, header(), &*p, record, "blue", "dotted"); |
| 117 |
showRecord(&*p); |
| 118 |
} |
| 119 |
#endif |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void osl::checkmate::analyzer:: |
| 125 |
DotWriter::showMove(const CheckHashRecord *from, const CheckMove& move) |
| 126 |
{ |
| 127 |
if (! focus(*from, minimumPdp)) |
| 128 |
{ |
| 129 |
// os << "skip " << from << "\n"; |
| 130 |
return; |
| 131 |
} |
| 132 |
const CheckHashRecord *record = move.record; |
| 133 |
if (! record) |
| 134 |
{ |
| 135 |
assert(move.flags.isSet(MoveFlags::ImmediateCheckmate)); |
| 136 |
return; |
| 137 |
} |
| 138 |
showRecord(record); |
| 139 |
int weight = 1; |
| 140 |
if (move.flags.isSet(MoveFlags::Upward)) |
| 141 |
weight = 0; |
| 142 |
if (from->bestMove && (from->bestMove->move == move.move)) |
| 143 |
weight = 2; |
| 144 |
if (primaryFocus(*record)) |
| 145 |
weight += 1; |
| 146 |
const Move m = move.move; |
| 147 |
os << header() << 'N' << from << " -> " << 'N' << record |
| 148 |
<< " [label=\""; |
| 149 |
csaShow(os, m); |
| 150 |
os << "\", weight=" << weight; |
| 151 |
if (move.flags.isSet(MoveFlags::Upward)) |
| 152 |
os << ", color=magenta"; |
| 153 |
else if (primaryFocus(*record)) |
| 154 |
os << ", color=red"; |
| 155 |
if (from->bestMove |
| 156 |
&& (from->bestMove->move == move.move)) |
| 157 |
os << ", style=bold"; |
| 158 |
os << "]\n"; |
| 159 |
} |
| 160 |
|
| 161 |
void osl::checkmate::analyzer:: |
| 162 |
DotWriter::showMoves(const MoveVector& moves) |
| 163 |
{ |
| 164 |
if (moves.size() == 0) |
| 165 |
return; |
| 166 |
os << "// T"; // terminal |
| 167 |
for (MoveVector::const_iterator p=moves.begin(); p!=moves.end(); ++p) |
| 168 |
{ |
| 169 |
os << ' '; |
| 170 |
csaShow(os, *p); |
| 171 |
} |
| 172 |
os << "\n"; |
| 173 |
} |
| 174 |
|
| 175 |
void osl::checkmate::analyzer:: |
| 176 |
DotWriter::writeln(const char *msg) |
| 177 |
{ |
| 178 |
os << "// " << msg << "\n"; |
| 179 |
} |
| 180 |
|
| 181 |
// OutlineWriter |
| 182 |
|
| 183 |
osl::checkmate::analyzer:: |
| 184 |
OutlineWriter::OutlineWriter() |
| 185 |
{ |
| 186 |
} |
| 187 |
|
| 188 |
osl::checkmate::analyzer:: |
| 189 |
OutlineWriter::~OutlineWriter() |
| 190 |
{ |
| 191 |
} |
| 192 |
|
| 193 |
const std::string osl::checkmate::analyzer:: |
| 194 |
OutlineWriter::header() const |
| 195 |
{ |
| 196 |
return std::string(getDepth(), '*'); |
| 197 |
} |
| 198 |
|
| 199 |
// TreeStreamWriter |
| 200 |
|
| 201 |
osl::checkmate::analyzer:: |
| 202 |
TreeStreamWriter::TreeStreamWriter(std::ostream *o, bool s) |
| 203 |
: os(o), simpleMove(s) |
| 204 |
{ |
| 205 |
} |
| 206 |
osl::checkmate::analyzer:: |
| 207 |
TreeStreamWriter::~TreeStreamWriter() |
| 208 |
{ |
| 209 |
} |
| 210 |
|
| 211 |
void osl::checkmate::analyzer:: |
| 212 |
TreeStreamWriter::showRecord(const CheckHashRecord *record) |
| 213 |
{ |
| 214 |
assert(record); |
| 215 |
(*os) << header() << ' ' << record->proofDisproof() << ' ' << record->filter; |
| 216 |
if (record->hasBestMove()) |
| 217 |
{ |
| 218 |
(*os) << " best move "; |
| 219 |
csaShow(*os, record->bestMove->move); |
| 220 |
(*os) << "(" << record->bestMove->record << ")"; |
| 221 |
} |
| 222 |
(*os) << "\n"; |
| 223 |
#if 0 |
| 224 |
(*os) << "bestResultInSolved " << record->bestResultInSolved << "\n"; |
| 225 |
#endif |
| 226 |
if (! record->twins.empty()) |
| 227 |
{ |
| 228 |
for (TwinList::const_iterator p=record->twins.begin(); |
| 229 |
p!=record->twins.end(); ++p) |
| 230 |
{ |
| 231 |
(*os) << " [" << p->path << " "; |
| 232 |
csaShow(*os, p->move.move); |
| 233 |
(*os) << " " << p->move.record << "]"; |
| 234 |
} |
| 235 |
(*os) << "\n"; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void osl::checkmate::analyzer:: |
| 240 |
TreeStreamWriter::showMove(const CheckMove& m) |
| 241 |
{ |
| 242 |
(*os) << header() << ' '; |
| 243 |
csaShow(*os, m.move); |
| 244 |
(*os) << " " << m.flags; |
| 245 |
if (m.record) |
| 246 |
{ |
| 247 |
(*os) << m.record->proofDisproof(); |
| 248 |
if (! m.record->twins.empty()) |
| 249 |
{ |
| 250 |
(*os) << " twins " << m.record->twins.size(); |
| 251 |
for (TwinList::const_iterator p=m.record->twins.begin(); |
| 252 |
p!=m.record->twins.end(); ++p) |
| 253 |
{ |
| 254 |
(*os) << " " << p->path << " "; |
| 255 |
csaShow(*os, p->move.move); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
(*os) << "\n"; |
| 260 |
} |
| 261 |
|
| 262 |
void osl::checkmate::analyzer:: |
| 263 |
TreeStreamWriter::showMove(const CheckHashRecord */*from*/, |
| 264 |
const CheckMove& move) |
| 265 |
{ |
| 266 |
if (simpleMove) |
| 267 |
{ |
| 268 |
showMove(move); |
| 269 |
return; |
| 270 |
} |
| 271 |
const CheckHashRecord *record = move.record; |
| 272 |
|
| 273 |
const Move m = move.move; |
| 274 |
(*os) << header() << ' '; |
| 275 |
csaShow(*os, m); |
| 276 |
(*os) << ' ' << record->distance << ' ' << move.flags << ' ' |
| 277 |
<< record->proofDisproof() << ' ' << record->filter; |
| 278 |
if (record->isVisited) |
| 279 |
(*os) << " (v)"; |
| 280 |
if (record->isConfluence) |
| 281 |
(*os) << " (c)"; |
| 282 |
if (record->useMaxInsteadOfSum) |
| 283 |
(*os) << " (sum/max)"; |
| 284 |
(*os) << ' ' << record << " up " << record->parent << " "; |
| 285 |
if (! record->twins.empty()) |
| 286 |
{ |
| 287 |
(*os) << " twins " << record->twins.size(); |
| 288 |
for (TwinList::const_iterator p=record->twins.begin(); |
| 289 |
p!=record->twins.end(); ++p) |
| 290 |
{ |
| 291 |
(*os) << " " << p->path << " "; |
| 292 |
csaShow(*os, p->move.move); |
| 293 |
} |
| 294 |
} |
| 295 |
if (record->hasBestMove() && (! record->bestMove->move.isPass())) |
| 296 |
{ |
| 297 |
(*os) << " best move "; |
| 298 |
csaShow(*os, record->bestMove->move); |
| 299 |
(*os) << "(" << record->bestMove->record << ")"; |
| 300 |
} |
| 301 |
(*os) << "\n"; |
| 302 |
#if 0 |
| 303 |
(*os) << "bestResultInSolved " << record->bestResultInSolved << "\n"; |
| 304 |
#endif |
| 305 |
if (! record->twins.empty()) |
| 306 |
{ |
| 307 |
for (TwinList::const_iterator p=record->twins.begin(); |
| 308 |
p!=record->twins.end(); ++p) |
| 309 |
{ |
| 310 |
(*os) << " [" << p->path << " "; |
| 311 |
csaShow(*os, p->move.move); |
| 312 |
(*os) << " " << p->move.record << "]"; |
| 313 |
} |
| 314 |
(*os) << "\n"; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
void osl::checkmate::analyzer:: |
| 319 |
TreeStreamWriter::showMoves(const MoveVector& moves) |
| 320 |
{ |
| 321 |
if (moves.size() == 0) |
| 322 |
return; |
| 323 |
(*os) << 'T'; // terminal |
| 324 |
for (MoveVector::const_iterator p=moves.begin(); p!=moves.end(); ++p) |
| 325 |
{ |
| 326 |
(*os) << ' '; |
| 327 |
csaShow(*os, *p); |
| 328 |
} |
| 329 |
(*os) << "\n"; |
| 330 |
} |
| 331 |
|
| 332 |
void osl::checkmate::analyzer:: |
| 333 |
TreeStreamWriter::writeln(const char *msg) |
| 334 |
{ |
| 335 |
(*os) << msg << "\n"; |
| 336 |
} |
| 337 |
|
| 338 |
/* ------------------------------------------------------------------------- */ |
| 339 |
// ;;; Local Variables: |
| 340 |
// ;;; mode:c++ |
| 341 |
// ;;; c-basic-offset:2 |
| 342 |
// ;;; End: |