From dec5d20add75c12a95aa4e2910b1f59f8cdf9a7c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 7 Jun 2018 15:47:10 +0100 Subject: Remove depth cutoff from solver class --- src/main/solver/solver.cpp | 55 +++++++++++++++++++--------------------------- src/main/solver/solver.h | 2 -- 2 files changed, 22 insertions(+), 35 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 884b2e5..9af10a3 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -39,14 +39,8 @@ solver::node::node(node* p, const game_state::move m) } solver::sol_state solver::run(optional &> terminate_solver) { - return run_with_cutoff(terminate_solver, none); -} - -solver::sol_state solver::run_with_cutoff( - boost::optional &> terminate_solver, - boost::optional cutoff_depth) { - bool cutoff_triggered = false; bool states_exhausted = false; + while(!(state.is_solved() || states_exhausted)) { // If the terminate flag was supplied and has been set to true, return if (terminate_solver && *terminate_solver) { @@ -60,35 +54,30 @@ solver::sol_state solver::run_with_cutoff( LOG_DEBUG(state); #endif - if (cutoff_depth && depth >= *cutoff_depth) { - cutoff_triggered = true; - states_exhausted = revert_to_last_node_with_children(); + // If there is a dominance move available, adds it to the search tree + // and repeats. Doesn't cache the state. + optional dominance_move = state.get_dominance_move(); + if (dominance_move) { + // Adds the dominance move as a child of the current search node + add_child(*dominance_move); } else { - // If there is a dominance move available, adds it to the search tree - // and repeats. Doesn't cache the state. - optional dominance_move = state.get_dominance_move(); - if (dominance_move) { - // Adds the dominance move as a child of the current search node - add_child(*dominance_move); - } else { - // Caches the current state - bool is_new_state = cache.insert(state); - if (is_new_state) { - // Gets the legal moves in the current state - vector next_moves = get_next_moves(); - - // If there are none, reverts to the last node with children - if (next_moves.empty()) { - states_exhausted = revert_to_last_node_with_children(); - } else { - add_children(next_moves); - } - } - // If the state is not a new one, reverts to the last node with children - else { + // Caches the current state + bool is_new_state = cache.insert(state); + if (is_new_state) { + // Gets the legal moves in the current state + vector next_moves = get_next_moves(); + + // If there are none, reverts to the last node with children + if (next_moves.empty()) { states_exhausted = revert_to_last_node_with_children(); + } else { + add_children(next_moves); } } + // If the state is not a new one, reverts to the last node with children + else { + states_exhausted = revert_to_last_node_with_children(); + } } // Sets the current node to one of its children @@ -106,7 +95,7 @@ solver::sol_state solver::run_with_cutoff( return sol_state::solved; } else { assert(states_exhausted); - return cutoff_triggered ? sol_state::cutoff : sol_state::unsolvable; + return sol_state::unsolvable; } } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 5eda894..665d72a 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -28,8 +28,6 @@ public: ~solver(); sol_state run(boost::optional &> = boost::none); - sol_state run_with_cutoff(boost::optional &>, - boost::optional = boost::none); void print_solution() const; int get_states_searched() const; -- cgit v1.2.1 From ca5e97bfeec3b03562b4c4a312b6a83c51b013fc Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 7 Jun 2018 16:31:14 +0100 Subject: Add more detailed logging info about search sols --- src/main/solver/solver.cpp | 23 ++++++++++++++--------- src/main/solver/solver.h | 10 ++++++---- 2 files changed, 20 insertions(+), 13 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 9af10a3..51cb574 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -19,7 +19,9 @@ solver::solver(const game_state& gs) , init_state(gs) , state(gs) , states_searched(1) + , unique_states_searched(1) , backtracks(0) + , dominance_moves(0) , depth(0) , root(nullptr, game_state::move(255, 255, 255)) , current_node(&root) { @@ -44,7 +46,7 @@ solver::sol_state solver::run(optional &> terminate_solver) { while(!(state.is_solved() || states_exhausted)) { // If the terminate flag was supplied and has been set to true, return if (terminate_solver && *terminate_solver) { - return sol_state::cutoff; + return sol_state::timed_out; } #ifndef NDEBUG @@ -76,6 +78,7 @@ solver::sol_state solver::run(optional &> terminate_solver) { } // If the state is not a new one, reverts to the last node with children else { + unique_states_searched--; states_exhausted = revert_to_last_node_with_children(); } } @@ -86,9 +89,11 @@ solver::sol_state solver::run(optional &> terminate_solver) { current_node = ¤t_node->children.back(); state.make_move(current_node->move); depth++; + if (current_node->move.is_dominance()) dominance_moves++; } states_searched++; + unique_states_searched++; } if (state.is_solved()) { @@ -170,18 +175,18 @@ void solver::print_solution() const { cout << "\n"; } -int solver::get_states_searched() const { - return states_searched; -} - -int solver::get_final_depth() const { - return depth; +void solver::print_solution_info() const { + cout << "States Searched: " << states_searched << "\n" + << "Unique States Searched: " << unique_states_searched << "\n" + << "Backtracks: " << backtracks << "\n" + << "Dominance Moves: " << dominance_moves << "\n" + << "Final Search Depth: " << depth << "\n"; } const solver::node& solver::get_search_tree() const { return root; } -int solver::get_backtrack_count() { - return backtracks; +int solver::get_states_searched() const { + return states_searched; } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 665d72a..77e8bde 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -14,7 +14,7 @@ class solver { public: - enum class sol_state {solved, unsolvable, cutoff}; + enum class sol_state {solved, unsolvable, timed_out}; global_cache cache; struct node { @@ -30,10 +30,9 @@ public: sol_state run(boost::optional &> = boost::none); void print_solution() const; + void print_solution_info() const; int get_states_searched() const; - int get_final_depth() const; const node& get_search_tree() const; - int get_backtrack_count(); private: bool revert_to_last_node_with_children(); @@ -43,9 +42,12 @@ private: const game_state init_state; game_state state; + int states_searched; + int unique_states_searched; int backtracks; - uint depth; + int dominance_moves; + int depth; node root; node* current_node; -- cgit v1.2.1 From 819a1064c5142a9b2f04ff8149d34fc915c18322 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 8 Jun 2018 10:31:22 +0100 Subject: Refactor legal moves gen and moves logic --- src/main/solver/solver.cpp | 41 ++++++++++++++++++++++------------------- src/main/solver/solver.h | 10 +++++----- 2 files changed, 27 insertions(+), 24 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 51cb574..b104da3 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -9,10 +9,13 @@ #include #include "solver.h" +#include "../game/move.h" #include "../input-output/output/log_helper.h" -using namespace std; -using namespace boost; +using std::atomic; +using std::vector; +using std::cout; +using std::clog; solver::solver(const game_state& gs) : cache(gs) @@ -23,7 +26,7 @@ solver::solver(const game_state& gs) , backtracks(0) , dominance_moves(0) , depth(0) - , root(nullptr, game_state::move(255, 255, 255)) + , root(nullptr, move(255, 255, 255)) , current_node(&root) { } @@ -36,11 +39,11 @@ solver::~solver() { } } -solver::node::node(node* p, const game_state::move m) - : parent(p), move(m), children() { +solver::node::node(node* p, const move m) + : parent(p), mv(m), children() { } -solver::sol_state solver::run(optional &> terminate_solver) { +solver::sol_state solver::run(boost::optional &> terminate_solver) { bool states_exhausted = false; while(!(state.is_solved() || states_exhausted)) { @@ -50,7 +53,7 @@ solver::sol_state solver::run(optional &> terminate_solver) { } #ifndef NDEBUG - if (current_node->move.is_dominance()) { + if (current_node->mv.is_dominance()) { LOG_DEBUG("(dominance move)"); } LOG_DEBUG(state); @@ -58,7 +61,7 @@ solver::sol_state solver::run(optional &> terminate_solver) { // If there is a dominance move available, adds it to the search tree // and repeats. Doesn't cache the state. - optional dominance_move = state.get_dominance_move(); + boost::optional dominance_move = state.get_dominance_move(); if (dominance_move) { // Adds the dominance move as a child of the current search node add_child(*dominance_move); @@ -67,7 +70,7 @@ solver::sol_state solver::run(optional &> terminate_solver) { bool is_new_state = cache.insert(state); if (is_new_state) { // Gets the legal moves in the current state - vector next_moves = get_next_moves(); + vector next_moves = get_next_moves(); // If there are none, reverts to the last node with children if (next_moves.empty()) { @@ -87,9 +90,9 @@ solver::sol_state solver::run(optional &> terminate_solver) { assert(states_exhausted == current_node->children.empty()); if (!states_exhausted) { current_node = ¤t_node->children.back(); - state.make_move(current_node->move); + state.make_move(current_node->mv); depth++; - if (current_node->move.is_dominance()) dominance_moves++; + if (current_node->mv.is_dominance()) dominance_moves++; } states_searched++; @@ -104,19 +107,19 @@ solver::sol_state solver::run(optional &> terminate_solver) { } } -void solver::add_children(std::vector& moves) { +void solver::add_children(std::vector& moves) { current_node->children.reserve(moves.size()); for (auto move : moves) { add_child(move); } } -vector solver::get_next_moves() { - return state.get_legal_moves(current_node->move); +vector solver::get_next_moves() { + return state.get_legal_moves(current_node->mv); } -void solver::add_child(game_state::move move) { - current_node->children.emplace_back(current_node, move); +void solver::add_child(move m) { + current_node->children.emplace_back(current_node, m); } // Called when the current node's children have been exhausted. Travels back up @@ -126,7 +129,7 @@ bool solver::revert_to_last_node_with_children() { if (!current_node->parent) return true; - state.undo_move(current_node->move); + state.undo_move(current_node->mv); depth--; backtracks++; @@ -134,7 +137,7 @@ bool solver::revert_to_last_node_with_children() { // Checks that the state after the undo is in the cache // (as long as the move wasn't a dominance move) - if (!current_node->move.is_dominance()) { + if (!current_node->mv.is_dominance()) { assert(cache.contains(state)); LOG_DEBUG("(undo move)"); } else { @@ -167,7 +170,7 @@ void solver::print_solution() const { if (states_searched > 1) { while (!n->children.empty()) { - state_copy.make_move(n->move); + state_copy.make_move(n->mv); cout << state_copy << "\n"; n = &n->children.back(); } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 77e8bde..21b8d6e 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -18,9 +18,9 @@ public: global_cache cache; struct node { - node(node*, game_state::move); + node(node*, move); node* parent; - const game_state::move move; + const move mv; std::vector children; }; @@ -36,9 +36,9 @@ public: private: bool revert_to_last_node_with_children(); - void add_children(std::vector&); - void add_child(game_state::move); - std::vector get_next_moves(); + void add_children(std::vector&); + void add_child(move); + std::vector get_next_moves(); const game_state init_state; game_state state; -- cgit v1.2.1 From a7396c5497601855fad019cfd145ca6c93e1c40f Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 8 Jun 2018 21:54:48 +0100 Subject: Refactor moves logic --- src/main/solver/solver.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index b104da3..7d490be 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -26,7 +26,7 @@ solver::solver(const game_state& gs) , backtracks(0) , dominance_moves(0) , depth(0) - , root(nullptr, move(255, 255, 255)) + , root(nullptr, move(move::mtype::null)) , current_node(&root) { } @@ -53,7 +53,7 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) } #ifndef NDEBUG - if (current_node->mv.is_dominance()) { + if (current_node->mv.type == move::mtype::dominance) { LOG_DEBUG("(dominance move)"); } LOG_DEBUG(state); @@ -92,7 +92,7 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) current_node = ¤t_node->children.back(); state.make_move(current_node->mv); depth++; - if (current_node->mv.is_dominance()) dominance_moves++; + if (current_node->mv.type == move::mtype::dominance) dominance_moves++; } states_searched++; @@ -137,7 +137,7 @@ bool solver::revert_to_last_node_with_children() { // Checks that the state after the undo is in the cache // (as long as the move wasn't a dominance move) - if (!current_node->mv.is_dominance()) { + if (current_node->mv.type != move::mtype::dominance) { assert(cache.contains(state)); LOG_DEBUG("(undo move)"); } else { -- cgit v1.2.1