From bc1c882983ecb8fda3d8d2ed92a39e8ce8a54013 Mon Sep 17 00:00:00 2001 From: Johannes Czech Date: Wed, 16 Feb 2022 13:44:09 +0100 Subject: [PATCH] Fix terminal solver for MCTS_SINGLE_PLAYER (#168) Add template bool only_child_nodes_of_one_kind() const --- engine/src/node.cpp | 22 ++++++---------------- engine/src/node.h | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/engine/src/node.cpp b/engine/src/node.cpp index 24c7ebd1..c011108c 100644 --- a/engine/src/node.cpp +++ b/engine/src/node.cpp @@ -144,23 +144,18 @@ bool Node::at_least_one_drawn_child() const bool Node::only_won_child_nodes() const { - for (auto it = d->childNodes.begin(); it != d->childNodes.end(); ++it) { - const Node* childNode = it->get(); - if (childNode->d->nodeType != WIN) { - return false; - } - } - return true; + return only_child_nodes_of_one_kind(); } bool Node::solved_loss(const Node* childNode) const { #ifndef MCTS_SINGLE_PLAYER if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == WIN) { + return only_won_child_nodes(); #else if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == LOSS) { + return only_child_nodes_of_one_kind(); #endif - return only_won_child_nodes(); } return false; } @@ -210,23 +205,18 @@ bool Node::solved_tb_loss(const Node* childNode) const { #ifndef MCTS_SINGLE_PLAYER if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == TB_WIN) { + return only_won_tb_child_nodes(); #else if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == TB_LOSS) { + return only_child_nodes_of_one_kind(); #endif - return only_won_tb_child_nodes(); } return false; } bool Node::only_won_tb_child_nodes() const { - for (auto it = d->childNodes.begin(); it != d->childNodes.end(); ++it) { - const Node* childNode = it->get(); - if (childNode->d->nodeType != TB_WIN) { - return false; - } - } - return true; + return only_child_nodes_of_one_kind(); } void Node::mark_as_tb_loss() diff --git a/engine/src/node.h b/engine/src/node.h index a9cc2326..a3eb9366 100644 --- a/engine/src/node.h +++ b/engine/src/node.h @@ -583,6 +583,22 @@ class Node */ bool only_won_child_nodes() const; + /** + * @brief only_child_nodes_of_one_kind Check if all expanded child nodes are of the same kind. + * @return true if only child nodes of type exist else false + */ + template + bool only_child_nodes_of_one_kind() const + { + for (auto it = d->childNodes.begin(); it != d->childNodes.end(); ++it) { + const Node* childNode = it->get(); + if (childNode->d->nodeType != nodeType) { + return false; + } + } + return true; + } + /** * @brief solved_loss Checks if the current node is a solved loss based on the given child node * @param childNode Child nodes which backpropagates the value