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/game/global_cache.cpp | 78 +++++++++++++++++++--- src/main/game/global_cache.h | 47 +++++++++++-- src/main/game/pile.cpp | 4 +- .../game/search-state/game_state.legal_moves.cpp | 2 +- src/main/solver/solver.cpp | 6 +- src/main/solver/solver.h | 2 +- src/test/test_helper.cpp | 2 +- src/test/unit_tests/global_cache_test.cpp | 6 +- 8 files changed, 124 insertions(+), 23 deletions(-) (limited to 'src') 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, + 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(), + hasher(gs), + equal_to() + ) + ); +} + +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 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 #include #include +#include +#include +#include +#include #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, + 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::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 &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 &> 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); diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index dbc6f0d..e594c38 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -46,7 +46,7 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, {"4D","3D","2D","AD"} }); - solver sol = solver(game_state(gs)); + solver sol(gs); sol.run(); solver::node const *n = &sol.get_search_tree(); diff --git a/src/test/unit_tests/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp index 6b0accd..c1a4b44 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{{},{},{}}); - global_cache cache(gs); + unlimited_cache cache(gs); 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{{},{},{}}); - global_cache cache(gs); + unlimited_cache cache(gs); 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{{},{},{}}); - global_cache cache(gs); + unlimited_cache cache(gs); 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 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 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/evaluation/benchmark.cpp | 4 ++-- src/main/evaluation/benchmark.h | 2 +- src/main/evaluation/solvability_calc.cpp | 7 +++--- src/main/evaluation/solvability_calc.h | 3 ++- src/main/game/global_cache.cpp | 21 +++++++++++++---- src/main/game/global_cache.h | 10 ++++---- .../input-output/input/command_line_helper.cpp | 14 ++++++++++- src/main/input-output/input/command_line_helper.h | 2 ++ src/main/main.cpp | 27 +++++++++++----------- src/main/solver/solver.cpp | 17 +++++++------- src/main/solver/solver.h | 3 +-- 11 files changed, 70 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp index 42fff9d..367caf5 100644 --- a/src/main/evaluation/benchmark.cpp +++ b/src/main/evaluation/benchmark.cpp @@ -13,7 +13,7 @@ using namespace std; typedef chrono::microseconds microsec; -void benchmark::run(const sol_rules &rules) { +void benchmark::run(const sol_rules &rules, uint64_t cache_capacity) { cout << "Seed " "| Median/Mean Solution Time(μs)" "| Median/Mean States Searched " @@ -28,7 +28,7 @@ void benchmark::run(const sol_rules &rules) { for(int seed = 1; seed <= 1000; seed++) { game_state gs(rules, seed); - solver sol(gs); + solver sol(gs, cache_capacity); auto start = chrono::steady_clock::now(); solver::sol_state result = sol.run(); diff --git a/src/main/evaluation/benchmark.h b/src/main/evaluation/benchmark.h index f934bba..f8737ce 100644 --- a/src/main/evaluation/benchmark.h +++ b/src/main/evaluation/benchmark.h @@ -10,7 +10,7 @@ class benchmark { public: - static void run(const sol_rules &rules); + static void run(const sol_rules &rules, uint64_t); }; diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index 702bec3..1e650eb 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -17,7 +17,8 @@ using namespace std; const double solvability_calc::coeff_ewma_weight = 0.7; -solvability_calc::solvability_calc(const sol_rules& r) : rules(r) { +solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) : + rules(r), cache_capacity(cache_capacity_) { } ////////////////////// @@ -127,7 +128,7 @@ void solvability_calc::calculate_solvability_percentage() { void solvability_calc::solve_seed(int seed, millisec timeout, seed_results& seed_res) { game_state gs(rules, seed); - solver sol(gs); + solver sol(gs, cache_capacity); atomic terminate_solver(false); future future = std::async( @@ -139,8 +140,8 @@ void solvability_calc::solve_seed(int seed, millisec timeout, future_status status; do { - status = future.wait_for(timeout); auto start = chrono::steady_clock::now(); + status = future.wait_for(timeout); if (status == future_status::timeout) { terminate_solver = true; diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index ec9cda2..0a75e36 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -11,7 +11,7 @@ class solvability_calc { public: - explicit solvability_calc(const sol_rules&); + explicit solvability_calc(const sol_rules&, uint64_t); void calculate_solvability_percentage(); @@ -63,6 +63,7 @@ private: const sol_rules& rules; const static double coeff_ewma_weight; + const uint64_t cache_capacity; }; diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 2e271cc..db0c6de 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -219,7 +219,7 @@ 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_) +lru_cache::lru_cache(const game_state& gs, uint64_t max_num_items_) : max_num_items(max_num_items_), cache(get_init_tuple(gs)), states_removed_from_cache(0) { } @@ -232,7 +232,7 @@ pair lru_cache::insert(const game_state& gs) { // 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++) { + for (uint64_t i = 0; prev(cache.end())->live; i++) { cache.relocate(cache.begin(), prev(cache.end())); if (i == max_num_items) { @@ -254,11 +254,24 @@ void lru_cache::clear() { cache.clear(); } +item_list::size_type lru_cache::size() const { + return cache.size(); +} + +item_list::size_type lru_cache::bucket_count() const { + return cache.get<1>().bucket_count(); +} + void lru_cache::set_non_live(item_list::iterator state_iter) { +#ifndef NDEBUG + bool succ = +#endif cache.modify(state_iter, [](auto& v){ v.live = false; }); - // TODO assert +#ifndef NDEBUG + assert(succ); +#endif } -int lru_cache::get_states_removed_from_cache() const { +uint64_t 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 7798f78..dd4670e 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -55,19 +55,21 @@ public: > > item_list; - explicit lru_cache(const game_state&, std::size_t); + explicit lru_cache(const game_state&, uint64_t); std::pair insert(const game_state&); bool contains(const game_state&) const; void clear(); + item_list::size_type size() const; + item_list::size_type bucket_count() const; void set_non_live(item_list::iterator); - int get_states_removed_from_cache() const; + uint64_t 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; + uint64_t max_num_items; item_list cache; - int states_removed_from_cache; + uint64_t states_removed_from_cache; }; #endif //SOLVITAIRE_GLOBAL_CACHE_H diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 4b0942a..1013f50 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -37,7 +37,9 @@ command_line_helper::command_line_helper() "deal based on a seed. Must supply either 'random'," "'solvability', 'benchmark' or list of deals to be solved.") ("classify", "outputs a simple 'solvable/not solvable' " - "classification") + "classification") + ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " + "the cache") ("solvability", "calculates the solvability " "percentage of the supplied solitaire game. Must supply " "either 'random', 'benchmark', 'solvability' or list of deals to be " @@ -102,6 +104,12 @@ bool command_line_helper::parse(int argc, const char* argv[]) { random_deal = -1; } + if (vm.count("cache-capacity")) { + cache_capacity = vm["cache-capacity"].as(); + } else { + cache_capacity = 1000000000; // One billion + } + solvability = (vm.count("solvability") != 0); benchmark = (vm.count("benchmark") != 0); @@ -199,6 +207,10 @@ bool command_line_helper::get_classify() { return classify; } +uint64_t command_line_helper::get_cache_capacity() { + return cache_capacity; +} + bool command_line_helper::get_solvability() { return solvability; } diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index 911cd5a..ca58d5f 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -20,6 +20,7 @@ public: bool get_solvability(); bool get_available_game_types(); bool get_benchmark(); + uint64_t get_cache_capacity(); std::string get_describe_game_rules(); private: @@ -44,6 +45,7 @@ private: bool solvability; bool available_game_types; bool benchmark; + uint64_t cache_capacity; }; #endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index d71db6c..8c2956d 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -19,9 +19,9 @@ using namespace boost; namespace po = boost::program_options; const optional gen_rules(command_line_helper&); -void solve_random_game(int, const sol_rules&, bool); -void solve_input_files(vector, const sol_rules&, bool); -void solve_game(const game_state&, bool); +void solve_random_game(int, const sol_rules&, bool, uint64_t); +void solve_input_files(vector, const sol_rules&, bool, uint64_t); +void solve_game(const game_state&, bool, uint64_t); // Decides what to do given supplied command-line options int main(int argc, const char* argv[]) { @@ -50,16 +50,16 @@ int main(int argc, const char* argv[]) { // If the user has asked for a solvability percentage, calculates it if (clh.get_solvability()) { - solvability_calc solv_c(*rules); + solvability_calc solv_c(*rules, clh.get_cache_capacity()); solv_c.calculate_solvability_percentage(); } // If a random deal seed has been supplied, solves it else if (clh.get_random_deal() != -1) { - solve_random_game(clh.get_random_deal(), *rules, clh.get_classify()); + solve_random_game(clh.get_random_deal(), *rules, clh.get_classify(), clh.get_cache_capacity()); } // If the benchmark option has been supplied, generates it else if (clh.get_benchmark()) { - benchmark::run(*rules); + benchmark::run(*rules, clh.get_cache_capacity()); } // Otherwise there are supplied input files which should be solved else { @@ -68,7 +68,7 @@ int main(int argc, const char* argv[]) { // If there are no input files, solve a random deal based on the // supplied seed assert (!input_files.empty()); - solve_input_files(input_files, *rules, clh.get_classify()); + solve_input_files(input_files, *rules, clh.get_classify(), clh.get_cache_capacity()); } return EXIT_SUCCESS; @@ -90,13 +90,14 @@ const optional gen_rules(command_line_helper& clh) { } } -void solve_random_game(int seed, const sol_rules& rules, bool classify) { +void solve_random_game(int seed, const sol_rules& rules, bool classify, uint64_t cache_capacity) { LOG_INFO ("Attempting to solve with seed: " << seed << "..."); game_state gs(rules, seed); - solve_game(gs, classify); + solve_game(gs, classify, cache_capacity); } -void solve_input_files(const vector input_files, const sol_rules& rules, bool classify) { +void solve_input_files(const vector input_files, const sol_rules& rules, + bool classify, uint64_t cache_capacity) { for (const string& input_file : input_files) { try { // Reads in the input file to a json doc @@ -106,7 +107,7 @@ void solve_input_files(const vector input_files, const sol_rules& rules, game_state gs(rules, in_doc); LOG_INFO ("Attempting to solve " << input_file << "..."); - solve_game(gs, classify); + solve_game(gs, classify, cache_capacity); } catch (const runtime_error& error) { string errmsg = "Error parsing deal file: "; @@ -116,8 +117,8 @@ void solve_input_files(const vector input_files, const sol_rules& rules, } } -void solve_game(const game_state& gs, bool classify) { - solver solv(gs); +void solve_game(const game_state& gs, bool classify, uint64_t cache_capacity) { + solver solv(gs, cache_capacity); bool solution = solv.run() == solver::sol_state::solved; 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 From 5fb3c77b586d7c0e663b178170599a9500efe394 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 13 Jun 2018 15:37:19 +0100 Subject: Add default cache capacity of 100,000,000 --- src/main/input-output/input/command_line_helper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 1013f50..e88f434 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -107,7 +107,7 @@ bool command_line_helper::parse(int argc, const char* argv[]) { if (vm.count("cache-capacity")) { cache_capacity = vm["cache-capacity"].as(); } else { - cache_capacity = 1000000000; // One billion + cache_capacity = 100000000; // One hundred-million } solvability = (vm.count("solvability") != 0); -- cgit v1.2.1