diff options
| -rw-r--r-- | src/main/game/move.h | 3 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 78 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 16 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 16 |
4 files changed, 54 insertions, 59 deletions
diff --git a/src/main/game/move.h b/src/main/game/move.h index b852811..1e3a9d1 100644 --- a/src/main/game/move.h +++ b/src/main/game/move.h @@ -8,7 +8,7 @@ #include "pile.h" struct move { - enum class mtype {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null}; + enum class mtype : uint8_t {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null}; explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1); friend bool operator==(const move&, const move&); @@ -19,5 +19,4 @@ struct move { pile::size_type count; }; - #endif //SOLVITAIRE_MOVE_H 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<lru_cache::item_list::iterator> 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<atomic<bool> &> terminate_solver) { @@ -68,12 +64,14 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) // and repeats. Doesn't cache the state. optional<move> 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<lru_cache::item_list::iterator, bool> 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<move> next_moves = state.get_legal_moves(current_node->mv); @@ -82,7 +80,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> 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<atomic<bool> &> 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<atomic<bool> &> terminate_solver) } } -void solver::add_children(std::vector<move>& moves, optional<lru_cache::item_list::iterator> i) { - current_node->children.reserve(moves.size()); - for (auto move : moves) { - add_child(move, i); - } -} - -void solver::add_child(move m, optional<lru_cache::item_list::iterator> 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<lru_cache::item_list::iterator> 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<lru_cache::item_list::iterator> 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<lru_cache::item_list::it // 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<lru_cache::item_list::iterator> p_state = current_node->parent_state_iter; + optional<lru_cache::item_list::iterator> 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::node> 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<lru_cache::item_list::iterator> = boost::none); - node* parent; + node(move); const move mv; - std::vector<node> children; - boost::optional<lru_cache::item_list::iterator> parent_state_iter; // Optional, as dominance moves aren't cached + std::vector<move> child_moves; + boost::optional<lru_cache::item_list::iterator> cache_state; // Optional, as dominance moves aren't cached }; explicit solver(const game_state&, uint64_t); - ~solver(); sol_state run(boost::optional<std::atomic<bool> &> = 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<node> get_frontier() const; private: bool revert_to_last_node_with_children(boost::optional<lru_cache::item_list::iterator> = boost::none); - void add_children(std::vector<move>&, boost::optional<lru_cache::item_list::iterator> = boost::none); - void add_child(move, boost::optional<lru_cache::item_list::iterator> = boost::none); + void set_to_child(); const game_state init_state; game_state state; + std::vector<node> frontier; int states_searched; int unique_states_searched; @@ -54,7 +52,7 @@ private: int max_depth; node root; - node* current_node; + std::vector<node>::iterator current_node; }; diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index b58c410..eaa0aba 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -49,15 +49,17 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, solver sol(gs, 1000000); sol.run(); - solver::node const *n = &sol.get_search_tree(); + auto frontier = sol.get_frontier(); + auto i = std::begin(frontier); + for (card c : cards) { - n = &n->children.back(); - ASSERT_TRUE(n->mv.type == move::mtype::dominance); - ASSERT_TRUE(gs.get_data()[n->mv.from].top_card() == c); - ASSERT_TRUE(n->mv.to >= 0 && n->mv.to <= 4); - gs.make_move(n->mv); + i++; + ASSERT_TRUE(i->mv.type == move::mtype::dominance); + ASSERT_TRUE(gs.get_data()[i->mv.from].top_card() == c); + ASSERT_TRUE(i->mv.to >= 0 && i->mv.to <= 4); + gs.make_move(i->mv); } - ASSERT_TRUE(n->children.empty()); + ASSERT_TRUE(i->child_moves.empty()); } void test_helper::run_built_group_test(sol_rules sr, |
