From 0684148d54e8839ced859e63906449368188d46e Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 11 Jun 2018 17:23:05 +0100 Subject: Speedup build and add lru cache --- src/main/solver/solver.cpp | 6 +++--- src/main/solver/solver.h | 2 +- 2 files 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 7d490be..b227598 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -18,7 +18,7 @@ using std::cout; using std::clog; solver::solver(const game_state& gs) - : cache(gs) + : cache(new unlimited_cache(gs)) , init_state(gs) , state(gs) , states_searched(1) @@ -67,7 +67,7 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) add_child(*dominance_move); } else { // Caches the current state - bool is_new_state = cache.insert(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(); @@ -138,7 +138,7 @@ bool solver::revert_to_last_node_with_children() { // (as long as the move wasn't a dominance move) if (current_node->mv.type != move::mtype::dominance) { - assert(cache.contains(state)); + assert(cache->contains(state)); LOG_DEBUG("(undo move)"); } else { LOG_DEBUG("(undo dominance move)"); diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 21b8d6e..dedce39 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -15,7 +15,7 @@ class solver { public: enum class sol_state {solved, unsolvable, timed_out}; - global_cache cache; + std::unique_ptr cache; struct node { node(node*, move); -- cgit v1.2.1 From 43a11ee2ee7827290d631698ea25b6724c8a1c29 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 12 Jun 2018 16:57:32 +0100 Subject: Add lru cache with elems limit --- src/main/solver/solver.cpp | 60 ++++++++++++++++++++++++++++++---------------- src/main/solver/solver.h | 12 ++++++---- 2 files changed, 46 insertions(+), 26 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index b227598..d7c5465 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -16,9 +16,12 @@ using std::atomic; using std::vector; using std::cout; using std::clog; +using std::pair; +using boost::optional; +using std::max; solver::solver(const game_state& gs) - : cache(new unlimited_cache(gs)) + : cache(lru_cache(gs, 1000000)) , init_state(gs) , state(gs) , states_searched(1) @@ -26,6 +29,7 @@ solver::solver(const game_state& gs) , backtracks(0) , dominance_moves(0) , depth(0) + , max_depth(0) , root(nullptr, move(move::mtype::null)) , current_node(&root) { } @@ -39,8 +43,8 @@ solver::~solver() { } } -solver::node::node(node* p, const move m) - : parent(p), mv(m), children() { +solver::node::node(node* p, const move m, optional c) + : parent(p), mv(m), children(), parent_state_iter(c) { } solver::sol_state solver::run(boost::optional &> terminate_solver) { @@ -61,22 +65,23 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) // If there is a dominance move available, adds it to the search tree // and repeats. Doesn't cache the state. - boost::optional dominance_move = state.get_dominance_move(); + 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); + pair insert_res = cache.insert(state); + bool is_new_state = insert_res.second; 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(); + states_exhausted = revert_to_last_node_with_children(insert_res.first); } else { - add_children(next_moves); + add_children(next_moves, insert_res.first); } } // If the state is not a new one, reverts to the last node with children @@ -92,6 +97,7 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) current_node = ¤t_node->children.back(); state.make_move(current_node->mv); depth++; + max_depth = max(depth, max_depth); if (current_node->mv.type == move::mtype::dominance) dominance_moves++; } @@ -107,28 +113,34 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) } } -void solver::add_children(std::vector& moves) { +void solver::add_children(std::vector& moves, optional i) { current_node->children.reserve(moves.size()); for (auto move : moves) { - add_child(move); + add_child(move, i); } } -vector solver::get_next_moves() { - return state.get_legal_moves(current_node->mv); +void solver::add_child(move m, optional i) { + current_node->children.emplace_back(current_node, m, i); } -void solver::add_child(move m) { - current_node->children.emplace_back(current_node, m); +vector solver::get_next_moves() { + return state.get_legal_moves(current_node->mv); } // Called when the current node's children have been exhausted. Travels back up // the search tree until it finds a node which still has children. Returns true // unless all children have been exhausted. -bool solver::revert_to_last_node_with_children() { + +// If an iterator to the current state is supplied to the function, will also +// make sure to turn the 'live' bit off upon backtracking +bool solver::revert_to_last_node_with_children(optional cur_state) { if (!current_node->parent) return true; + // Turns the 'live' bit false on the state we are backtracking out of + if (cur_state) cache.set_non_live(*cur_state); + state.undo_move(current_node->mv); depth--; backtracks++; @@ -138,7 +150,7 @@ bool solver::revert_to_last_node_with_children() { // (as long as the move wasn't a dominance move) if (current_node->mv.type != move::mtype::dominance) { - assert(cache->contains(state)); + if (cache.get_states_removed_from_cache() == 0) assert(cache.contains(state)); LOG_DEBUG("(undo move)"); } else { LOG_DEBUG("(undo dominance move)"); @@ -146,13 +158,17 @@ bool solver::revert_to_last_node_with_children() { LOG_DEBUG(state); #endif + // Gets a reference to the parent state which can be supplied if this function is + // called recursively. This ensures that the cached state's 'live' bit is set as appropriate + optional p_state = current_node->parent_state_iter; + // Reverts the current node to its parent and removes it current_node = current_node->parent; current_node->children.pop_back(); // If the current node now has no children, repeat if (current_node->children.empty()) { - return revert_to_last_node_with_children(); + return revert_to_last_node_with_children(p_state); } else { return false; } @@ -179,11 +195,13 @@ void solver::print_solution() const { } 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"; + cout << "States Searched: " << states_searched << "\n" + << "Unique States Searched: " << unique_states_searched << "\n" + << "Backtracks: " << backtracks << "\n" + << "Dominance Moves: " << dominance_moves << "\n" + << "States Removed From Cache: " << cache.get_states_removed_from_cache() << "\n" + << "Maximum Search Depth: " << max_depth << "\n" + << "Final Search Depth: " << depth << "\n"; } const solver::node& solver::get_search_tree() const { diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index dedce39..73d5e41 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -15,13 +15,14 @@ class solver { public: enum class sol_state {solved, unsolvable, timed_out}; - std::unique_ptr cache; + lru_cache cache; struct node { - node(node*, move); + node(node*, move, boost::optional = boost::none); node* parent; const move mv; std::vector children; + boost::optional parent_state_iter; // Optional, as dominance moves aren't cached }; explicit solver(const game_state&); @@ -35,9 +36,9 @@ public: const node& get_search_tree() const; private: - bool revert_to_last_node_with_children(); - void add_children(std::vector&); - void add_child(move); + bool revert_to_last_node_with_children(boost::optional = boost::none); + void add_children(std::vector&, boost::optional = boost::none); + void add_child(move, boost::optional = boost::none); std::vector get_next_moves(); const game_state init_state; @@ -48,6 +49,7 @@ private: int backtracks; int dominance_moves; int depth; + int max_depth; node root; node* current_node; -- cgit v1.2.1 From fb3e96a333cc9b27529f5d2c7866a2f7c4d43ec2 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 13 Jun 2018 15:08:11 +0100 Subject: Add cache capacity command line option --- src/main/solver/solver.cpp | 17 ++++++++--------- src/main/solver/solver.h | 3 +-- 2 files changed, 9 insertions(+), 11 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index d7c5465..fd80556 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "solver.h" #include "../game/move.h" @@ -20,12 +21,12 @@ using std::pair; using boost::optional; using std::max; -solver::solver(const game_state& gs) - : cache(lru_cache(gs, 1000000)) +solver::solver(const game_state& gs, uint64_t cache_capacity) + : cache(lru_cache(gs, cache_capacity)) , init_state(gs) , state(gs) - , states_searched(1) - , unique_states_searched(1) + , states_searched(0) + , unique_states_searched(0) , backtracks(0) , dominance_moves(0) , depth(0) @@ -75,7 +76,7 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) bool is_new_state = insert_res.second; if (is_new_state) { // Gets the legal moves in the current state - vector next_moves = get_next_moves(); + vector next_moves = state.get_legal_moves(current_node->mv); // If there are none, reverts to the last node with children if (next_moves.empty()) { @@ -124,10 +125,6 @@ void solver::add_child(move m, optional i) { current_node->children.emplace_back(current_node, m, i); } -vector solver::get_next_moves() { - return state.get_legal_moves(current_node->mv); -} - // Called when the current node's children have been exhausted. Travels back up // the search tree until it finds a node which still has children. Returns true // unless all children have been exhausted. @@ -200,6 +197,8 @@ void solver::print_solution_info() const { << "Backtracks: " << backtracks << "\n" << "Dominance Moves: " << dominance_moves << "\n" << "States Removed From Cache: " << cache.get_states_removed_from_cache() << "\n" + << "Final States In Cache: " << cache.size() << "\n" + << "Final Buckets In Cache: " << cache.bucket_count() << "\n" << "Maximum Search Depth: " << max_depth << "\n" << "Final Search Depth: " << depth << "\n"; } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 73d5e41..bc3af0a 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -25,7 +25,7 @@ public: boost::optional parent_state_iter; // Optional, as dominance moves aren't cached }; - explicit solver(const game_state&); + explicit solver(const game_state&, uint64_t); ~solver(); sol_state run(boost::optional &> = boost::none); @@ -39,7 +39,6 @@ private: bool revert_to_last_node_with_children(boost::optional = boost::none); void add_children(std::vector&, boost::optional = boost::none); void add_child(move, boost::optional = boost::none); - std::vector get_next_moves(); const game_state init_state; game_state state; -- cgit v1.2.1