From 8cc7517e2c345d6afcb99715788a01e6d5d1b423 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 15 Jun 2018 14:14:13 +0100 Subject: Log cache clearances in solvability output --- src/main/solver/solver.cpp | 4 ++++ src/main/solver/solver.h | 1 + 2 files changed, 5 insertions(+) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index e44188d..09e95f5 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -218,3 +218,7 @@ int solver::get_unique_states_searched() const { int solver::get_cache_size() const { return cache.size(); } + +int solver::get_states_rem_from_cache() const { + return cache.get_states_removed_from_cache(); +} diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 961072f..ef0709a 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -35,6 +35,7 @@ public: int get_states_searched() const; int get_unique_states_searched() const; int get_cache_size() const; + int get_states_rem_from_cache() const; const node& get_search_tree() const; private: -- cgit v1.2.1 From 2bad89dfd8d100145526637c5ef33b8f410014df Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 19 Jun 2018 16:48:56 +0100 Subject: Reduce memory taken up by dfs datastructures --- src/main/solver/solver.cpp | 78 ++++++++++++++++++++++------------------------ src/main/solver/solver.h | 16 +++++----- 2 files changed, 44 insertions(+), 50 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 09e95f5..53c7d1b 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -18,34 +18,30 @@ using std::vector; using std::cout; using std::clog; using std::pair; -using boost::optional; using std::max; +using std::begin; +using std::end; +using boost::optional; solver::solver(const game_state& gs, uint64_t cache_capacity) : cache(lru_cache(gs, cache_capacity)) , init_state(gs) , state(gs) + , frontier() , states_searched(0) , unique_states_searched(0) , backtracks(0) , dominance_moves(0) , depth(0) , max_depth(0) - , root(nullptr, move(move::mtype::null)) - , current_node(&root) { -} - -solver::~solver() { - // This code looks to be unnecessary, but on alpha-star 6212 without it, - // for reasons that I cannot fathom, it segfaults. - while (current_node != nullptr) { - current_node->children.clear(); - current_node = current_node->parent; - } + , root(move(move::mtype::null)) + , current_node() { + frontier.push_back(root); + current_node = begin(frontier); } -solver::node::node(node* p, const move m, optional c) - : parent(p), mv(m), children(), parent_state_iter(c) { +solver::node::node(const move m) + : mv(m), child_moves(), cache_state() { } solver::sol_state solver::run(boost::optional &> terminate_solver) { @@ -68,12 +64,14 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) // and repeats. Doesn't cache the state. 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); + // Adds the dominance move as a child of the current search node; + current_node->child_moves.emplace_back(*dominance_move); } else { // Caches the current state pair insert_res = cache.insert(state); + current_node->cache_state = insert_res.first; bool is_new_state = insert_res.second; + if (is_new_state) { // Gets the legal moves in the current state vector next_moves = state.get_legal_moves(current_node->mv); @@ -82,7 +80,7 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) if (next_moves.empty()) { states_exhausted = revert_to_last_node_with_children(insert_res.first); } else { - add_children(next_moves, insert_res.first); + current_node->child_moves = std::move(next_moves); } } // If the state is not a new one, reverts to the last node with children @@ -93,9 +91,9 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) } // Sets the current node to one of its children - assert(states_exhausted == current_node->children.empty()); + assert(states_exhausted == current_node->child_moves.empty()); if (!states_exhausted) { - current_node = ¤t_node->children.back(); + set_to_child(); state.make_move(current_node->mv); depth++; max_depth = max(depth, max_depth); @@ -114,17 +112,6 @@ solver::sol_state solver::run(boost::optional &> terminate_solver) } } -void solver::add_children(std::vector& moves, optional i) { - current_node->children.reserve(moves.size()); - for (auto move : moves) { - add_child(move, i); - } -} - -void solver::add_child(move m, optional i) { - current_node->children.emplace_back(current_node, m, i); -} - // 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. @@ -132,7 +119,7 @@ void solver::add_child(move m, optional i) { // 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) + if (current_node == begin(frontier)) return true; // Turns the 'live' bit false on the state we are backtracking out of @@ -157,35 +144,44 @@ bool solver::revert_to_last_node_with_children(optional p_state = current_node->parent_state_iter; + optional p_state = prev(current_node)->cache_state; // Reverts the current node to its parent and removes it - current_node = current_node->parent; - current_node->children.pop_back(); + frontier.pop_back(); + current_node = prev(end(frontier)); // If the current node now has no children, repeat - if (current_node->children.empty()) { + if (current_node->child_moves.empty()) { return revert_to_last_node_with_children(p_state); } else { return false; } } +void solver::set_to_child() { + assert(!current_node->child_moves.empty()); + + move b = current_node->child_moves.back(); + current_node->child_moves.pop_back(); + frontier.emplace_back(b); + + current_node = prev(end(frontier)); +} + void solver::print_solution() const { std::flush(clog); std::flush(cout); - const node* n = &root.children.back(); + auto i = begin(frontier); game_state state_copy = init_state; cout << "Solution:\n"; cout << state_copy << "\n"; if (states_searched > 1) { - while (!n->children.empty()) { - state_copy.make_move(n->mv); + while (++i != end(frontier)) { + state_copy.make_move(i->mv); cout << state_copy << "\n"; - n = &n->children.back(); } } cout << "\n"; @@ -203,8 +199,8 @@ void solver::print_solution_info() const { << "Final Search Depth: " << depth << "\n"; } -const solver::node& solver::get_search_tree() const { - return root; +const vector solver::get_frontier() const { + return frontier; } int solver::get_states_searched() const { diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index ef0709a..14dedc5 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -18,15 +18,13 @@ public: lru_cache cache; struct node { - node(node*, move, boost::optional = boost::none); - node* parent; + node(move); const move mv; - std::vector children; - boost::optional parent_state_iter; // Optional, as dominance moves aren't cached + std::vector child_moves; + boost::optional cache_state; // Optional, as dominance moves aren't cached }; explicit solver(const game_state&, uint64_t); - ~solver(); sol_state run(boost::optional &> = boost::none); @@ -36,15 +34,15 @@ public: int get_unique_states_searched() const; int get_cache_size() const; int get_states_rem_from_cache() const; - const node& get_search_tree() const; + const std::vector get_frontier() const; 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); + void set_to_child(); const game_state init_state; game_state state; + std::vector frontier; int states_searched; int unique_states_searched; @@ -54,7 +52,7 @@ private: int max_depth; node root; - node* current_node; + std::vector::iterator current_node; }; -- cgit v1.2.1