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 ++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 21 deletions(-) (limited to 'src/main/solver/solver.cpp') 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 { -- cgit v1.2.1