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/game/global_cache.cpp | 34 ++++++++++++++---- src/main/game/global_cache.h | 35 +++++------------- src/main/solver/solver.cpp | 60 ++++++++++++++++++++----------- src/main/solver/solver.h | 12 ++++--- src/test/unit_tests/global_cache_test.cpp | 6 ++-- 5 files changed, 86 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 3732789..2e271cc 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -7,6 +7,7 @@ #include #include "global_cache.h" +#include "../input-output/output/log_helper.h" using namespace std; using namespace boost; @@ -30,7 +31,7 @@ typedef boost::multi_index::multi_index_container< // CACHED GAME STATE // /////////////////////// -cached_game_state::cached_game_state(const game_state& gs) { +cached_game_state::cached_game_state(const game_state& gs) : live(true) { data.reserve(52+18); // Enough for each card and up to 18 piles if (gs.rules.hole) { @@ -170,7 +171,7 @@ size_t hasher::hash_value(card const& c) const { -////////////////// +/*////////////////// // GLOBAL CACHE // ////////////////// @@ -188,7 +189,7 @@ bool unlimited_cache::contains(const game_state& gs) const { void unlimited_cache::clear() { cache.clear(); -} +}*/ /////////////// @@ -219,18 +220,30 @@ item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) { } lru_cache::lru_cache(const game_state& gs, size_t max_num_items_) - : max_num_items(max_num_items_), cache(get_init_tuple(gs)) { + : max_num_items(max_num_items_), cache(get_init_tuple(gs)), states_removed_from_cache(0) { } -bool lru_cache::insert(const game_state& gs) { +pair lru_cache::insert(const game_state& gs) { pair p = cache.push_front(cached_game_state(gs)); if(!p.second){ /* duplicate item */ cache.relocate(cache.begin(), p.first); /* put in front */ } else if(cache.size() > max_num_items){ /* keep the length <= max_num_items */ + + // If the least recently used node is 'live' (i.e. a parent), relocates + // it to the head of the list until this is no longer the case + for (size_t i = 0; prev(cache.end())->live; i++) { + cache.relocate(cache.begin(), prev(cache.end())); + + if (i == max_num_items) { + LOG_ERROR("All items in cache are live and cache is full"); + throw runtime_error("All items in cache are live and cache is full"); + } + } cache.pop_back(); + states_removed_from_cache++; } - return p.second; + return p; } bool lru_cache::contains(const game_state& gs) const { @@ -240,3 +253,12 @@ bool lru_cache::contains(const game_state& gs) const { void lru_cache::clear() { cache.clear(); } + +void lru_cache::set_non_live(item_list::iterator state_iter) { + cache.modify(state_iter, [](auto& v){ v.live = false; }); + // TODO assert +} + +int lru_cache::get_states_removed_from_cache() const { + return states_removed_from_cache; +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index e420b57..7798f78 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -27,6 +27,7 @@ struct cached_game_state { void add_card_divider(); state_data data; + bool live; // Is a parent in the current search tree }; bool operator==(const cached_game_state&, const cached_game_state&); @@ -41,29 +42,8 @@ struct hasher { const game_state& init_gs; }; -class global_cache { +class lru_cache { public: - virtual ~global_cache() = default; - virtual bool insert(const game_state&) = 0; - virtual bool contains(const game_state&) const = 0; - virtual void clear() = 0; -}; - -class unlimited_cache : public global_cache { -public: - explicit unlimited_cache(const game_state&); - bool insert(const game_state&) override; - bool contains(const game_state&) const override; - void clear() override; - -private: - std::unordered_set< - cached_game_state, - hasher - > cache; -}; - -class lru_cache : public global_cache { typedef boost::multi_index::multi_index_container< cached_game_state, boost::multi_index::indexed_by< @@ -74,17 +54,20 @@ class lru_cache : public global_cache { > > > item_list; -public: + explicit lru_cache(const game_state&, std::size_t); - bool insert(const game_state&) override; - bool contains(const game_state&) const override; - void clear() override; + std::pair insert(const game_state&); + bool contains(const game_state&) const; + void clear(); + void set_non_live(item_list::iterator); + int get_states_removed_from_cache() const; private: static item_list::ctor_args_list get_init_tuple(const game_state&); std::size_t max_num_items; item_list cache; + int states_removed_from_cache; }; #endif //SOLVITAIRE_GLOBAL_CACHE_H 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; diff --git a/src/test/unit_tests/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp index c1a4b44..cd3c686 100644 --- a/src/test/unit_tests/global_cache_test.cpp +++ b/src/test/unit_tests/global_cache_test.cpp @@ -15,7 +15,7 @@ TEST(GlobalCache, CommutativeTableauPiles) { rules.tableau_pile_count = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; game_state gs(rules, std::initializer_list{{},{},{}}); - unlimited_cache cache(gs); + lru_cache cache(gs, 1000); cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); @@ -39,7 +39,7 @@ TEST(GlobalCache, CommutativeReserve) { rules.reserve_size = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; game_state gs(rules, std::initializer_list{{},{},{}}); - unlimited_cache cache(gs); + lru_cache cache(gs, 1000); cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); @@ -63,7 +63,7 @@ TEST(GlobalCache, CommutativeCells) { rules.cells = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; game_state gs(rules, std::initializer_list{{},{},{}}); - unlimited_cache cache(gs); + lru_cache cache(gs, 1000); cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); -- cgit v1.2.1