diff options
| author | Charlie Blake | 2018-06-10 00:30:55 +0100 |
|---|---|---|
| committer | GitHub | 2018-06-10 00:30:55 +0100 |
| commit | d82642305dbcf53262ff41705a41c6e7c1591161 (patch) | |
| tree | b2bbe8c0aadaec16280f020e55eb55955be43d3f /src/main/solver | |
| parent | Merge pull request #58 from thecharlesblake/fin (diff) | |
| parent | Finish refactoring game state class (diff) | |
| download | metasol-d82642305dbcf53262ff41705a41c6e7c1591161.tar.gz metasol-d82642305dbcf53262ff41705a41c6e7c1591161.zip | |
Merge pull request #59 from thecharlesblake/paper-tidy-up
Paper tidy up
Diffstat (limited to 'src/main/solver')
| -rw-r--r-- | src/main/solver/solver.cpp | 113 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 22 |
2 files changed, 66 insertions, 69 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 884b2e5..7d490be 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -9,19 +9,24 @@ #include <list> #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) , 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)) + , root(nullptr, move(move::mtype::null)) , current_node(&root) { } @@ -34,95 +39,87 @@ 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<atomic<bool> &> terminate_solver) { - return run_with_cutoff(terminate_solver, none); -} - -solver::sol_state solver::run_with_cutoff( - boost::optional<std::atomic<bool> &> terminate_solver, - boost::optional<uint> cutoff_depth) { - bool cutoff_triggered = false; +solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) { 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) { - return sol_state::cutoff; + return sol_state::timed_out; } #ifndef NDEBUG - if (current_node->move.is_dominance()) { + if (current_node->mv.type == move::mtype::dominance) { LOG_DEBUG("(dominance move)"); } 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. + boost::optional<move> 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<game_state::move> 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<game_state::move> 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<move> 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 { + unique_states_searched--; + states_exhausted = revert_to_last_node_with_children(); + } } // Sets the current node to one of its children 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->mv.type == move::mtype::dominance) dominance_moves++; } states_searched++; + unique_states_searched++; } if (state.is_solved()) { return sol_state::solved; } else { assert(states_exhausted); - return cutoff_triggered ? sol_state::cutoff : sol_state::unsolvable; + return sol_state::unsolvable; } } -void solver::add_children(std::vector<game_state::move>& moves) { +void solver::add_children(std::vector<move>& moves) { current_node->children.reserve(moves.size()); for (auto move : moves) { add_child(move); } } -vector<game_state::move> solver::get_next_moves() { - return state.get_legal_moves(current_node->move); +vector<move> 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 @@ -132,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++; @@ -140,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.type != move::mtype::dominance) { assert(cache.contains(state)); LOG_DEBUG("(undo move)"); } else { @@ -173,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(); } @@ -181,18 +178,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 5eda894..21b8d6e 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -14,13 +14,13 @@ class solver { public: - enum class sol_state {solved, unsolvable, cutoff}; + enum class sol_state {solved, unsolvable, timed_out}; 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<node> children; }; @@ -28,26 +28,26 @@ public: ~solver(); sol_state run(boost::optional<std::atomic<bool> &> = boost::none); - sol_state run_with_cutoff(boost::optional<std::atomic<bool> &>, - boost::optional<uint> = 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(); - void add_children(std::vector<game_state::move>&); - void add_child(game_state::move); - std::vector<game_state::move> get_next_moves(); + void add_children(std::vector<move>&); + void add_child(move); + std::vector<move> get_next_moves(); 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; |
