diff options
| author | Charlie Blake | 2018-06-11 17:23:05 +0100 |
|---|---|---|
| committer | Charlie Blake | 2018-06-11 17:23:05 +0100 |
| commit | 0684148d54e8839ced859e63906449368188d46e (patch) | |
| tree | a7ff103581a13822d0f7d31115197454fffa0c7b /src/main | |
| parent | Merge pull request #59 from thecharlesblake/paper-tidy-up (diff) | |
| download | metasol-0684148d54e8839ced859e63906449368188d46e.tar.gz metasol-0684148d54e8839ced859e63906449368188d46e.zip | |
Speedup build and add lru cache
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/game/global_cache.cpp | 78 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 47 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 4 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.legal_moves.cpp | 2 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 6 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 2 |
6 files changed, 120 insertions, 19 deletions
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 3620ddb..3732789 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -14,6 +14,17 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; +typedef boost::multi_index::multi_index_container< + cached_game_state, + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::hashed_unique< + boost::multi_index::identity<cached_game_state>, + hasher + > + > +> item_list; + /////////////////////// // CACHED GAME STATE // @@ -163,18 +174,69 @@ size_t hasher::hash_value(card const& c) const { // GLOBAL CACHE // ////////////////// -bool global_cache::insert(const game_state& gs) { - return u_set.emplace(gs).second; +unlimited_cache::unlimited_cache(const game_state& gs) + : cache(0, hasher(gs)) { +} + +bool unlimited_cache::insert(const game_state& gs) { + return cache.emplace(gs).second; +} + +bool unlimited_cache::contains(const game_state& gs) const { + return cache.count(cached_game_state(gs)) > 0; +} + +void unlimited_cache::clear() { + cache.clear(); } -bool global_cache::contains(const game_state& gs) const { - return u_set.count(cached_game_state(gs)) > 0; + +/////////////// +// LRU CACHE // +/////////////// + +// Based on https://www.boost.org/doc/libs/1_67_0/libs/multi_index/example/serialization.cpp +/* Boost.MultiIndex example of serialization of a MRU list. + * + * Copyright 2003-2008 Joaquin M Lopez Munoz. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * See http://www.boost.org/libs/multi_index for library home page. + */ + +item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) { + return boost::make_tuple( + item_list::nth_index<0>::type::ctor_args(), + boost::make_tuple( + size_t(0), + multi_index::identity<cached_game_state>(), + hasher(gs), + equal_to<cached_game_state>() + ) + ); +} + +lru_cache::lru_cache(const game_state& gs, size_t max_num_items_) + : max_num_items(max_num_items_), cache(get_init_tuple(gs)) { +} + +bool lru_cache::insert(const game_state& gs) { + pair<item_list::iterator, bool> 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 */ + cache.pop_back(); + } + return p.second; } -void global_cache::clear() { - u_set.clear(); +bool lru_cache::contains(const game_state& gs) const { + return cache.get<1>().count(cached_game_state(gs)) > 0; } -global_cache::global_cache(const game_state& gs) - : u_set(0, hasher(gs)) { +void lru_cache::clear() { + cache.clear(); } diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 9541100..e420b57 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -9,6 +9,10 @@ #include <unordered_set> #include <boost/pool/pool.hpp> #include <boost/pool/pool_alloc.hpp> +#include <boost/multi_index_container.hpp> +#include <boost/multi_index/sequenced_index.hpp> +#include <boost/multi_index/identity.hpp> +#include <boost/multi_index/hashed_index.hpp> #include "sol_rules.h" #include "search-state/game_state.h" @@ -24,6 +28,7 @@ struct cached_game_state { state_data data; }; + bool operator==(const cached_game_state&, const cached_game_state&); struct hasher { @@ -38,16 +43,48 @@ struct hasher { class global_cache { public: - explicit global_cache(const game_state&); - bool insert(const game_state&); - bool contains(const game_state&) const; - void clear(); + 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 - > u_set; + > cache; +}; + +class lru_cache : public global_cache { + typedef boost::multi_index::multi_index_container< + cached_game_state, + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::hashed_unique< + boost::multi_index::identity<cached_game_state>, + hasher + > + > + > 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; + +private: + static item_list::ctor_args_list get_init_tuple(const game_state&); + + std::size_t max_num_items; + item_list cache; }; #endif //SOLVITAIRE_GLOBAL_CACHE_H diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 3136681..23a02af 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -35,7 +35,9 @@ card& pile::operator[] (size_type i) { } card pile::operator[] (size_type i) const { - return pile_vec[pile_vec.size() - 1 - i]; + vector<card>::size_type s = pile_vec.size() - 1 - i; + assert(s < pile_vec.size()); + return pile_vec[s]; } void pile::place(const card c) { diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index 2acfba6..f9fc941 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -242,7 +242,7 @@ void game_state::add_built_group_moves(vector<move> &moves) const { // Finds the size of the built group at the top of a pile pile::size_type game_state::get_built_group_height(pile::ref ref) const { pile::size_type i = 1; - while (is_next_built_group_card(piles[ref][i], piles[ref][i-1]) && i < piles[ref].size()) + while (i < piles[ref].size() && is_next_built_group_card(piles[ref][i], piles[ref][i-1])) i++; return i; } 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<atomic<bool> &> 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<move> 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<global_cache> cache; struct node { node(node*, move); |
