diff options
| author | Charlie Blake | 2018-02-15 12:48:35 +0000 |
|---|---|---|
| committer | GitHub | 2018-02-15 12:48:35 +0000 |
| commit | 9e865ef56ff2b90695343e2e5b7be6e05b2c56f4 (patch) | |
| tree | 7cd620ee74084d74d849b44fae2db3897763cadb /src/main/solver | |
| parent | Merge pull request #37 from thecharlesblake/docs (diff) | |
| parent | Add simple auto-found + works w/ removable founds (diff) | |
| download | metasol-9e865ef56ff2b90695343e2e5b7be6e05b2c56f4.tar.gz metasol-9e865ef56ff2b90695343e2e5b7be6e05b2c56f4.zip | |
Merge pull request #38 from thecharlesblake/foundation-dominance
Foundation dominance
Diffstat (limited to 'src/main/solver')
| -rw-r--r-- | src/main/solver/solver.cpp | 173 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 19 |
2 files changed, 126 insertions, 66 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 6dddfb4..e55a4e3 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -6,8 +6,7 @@ #include <ostream> #include <iostream> #include <algorithm> - -#include <gperftools/profiler.h> +#include <list> #include "solver.h" #include "../input-output/output/log_helper.h" @@ -16,86 +15,136 @@ using namespace std; using namespace boost; solver::solver(const game_state& gs) - : cache(gs), initial_state(gs), states_searched(0) {} + : cache(gs) + , init_state(gs) + , state(gs) + , states_searched(1) + , root(nullptr, game_state::move(0, 0)) + , current_node(&root) { +} -solver::node::node(const game_state::move m, - vector<game_state::move> uc) - : move(m), unsearched_children(std::move(uc)) { +solver::node::node(node* p, const game_state::move m) + : parent(p), move(m), children() { } bool solver::run() { - //ProfilerStart("solvitaire"); - game_state state = initial_state; - - states_searched++; - LOG_DEBUG (state); - frontier.emplace_back(game_state::move(0,0), state.get_legal_moves()); - cache.insert(state); - - while (!frontier.empty()) { - node& current = frontier.back(); - - // If we have a solution, returns true - if (state.is_solved()) { - ProfilerStop(); - return true; + bool states_exhausted = false; + while(!state.is_solved() && !states_exhausted) { +#ifndef NDEBUG + if (current_node->move.is_dominance()) { + LOG_DEBUG("(dominance move)"); } - - // If the current node has no unsearched children - if (current.unsearched_children.empty()) { - // Undoes the move that led to this state - // (unless it's the null first move) - if (frontier.size() > 1) { - state.undo_move(current.move); - assert(cache.contains(state)); - LOG_DEBUG (state); - } - // Returns to the previous state - frontier.pop_back(); + LOG_DEBUG(state); +#endif + + // If there is a dominance move available, adds it to the search tree + // and repeats. Doesn't cache the state. + optional<game_state::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); } else { - game_state::move next_move = current.unsearched_children.back(); - current.unsearched_children.pop_back(); - - // Applies the first possible move in this state - state.make_move(next_move); - - // Insert the state into the global cache + // Caches the current state bool is_new_state = cache.insert(state); if (is_new_state) { - states_searched++; - LOG_DEBUG (state); - // If the state is new, creates a new node in the frontier to - // represent the move just made, and those that are now possible - frontier.emplace_back(next_move, state.get_legal_moves()); - } else { - // If we've seen the state before, undoes the move - state.undo_move(next_move); - assert(cache.contains(state)); + // Gets the legal moves in the current state + vector<game_state::move> next_moves = state.get_legal_moves(); + + // If there are none, reverts to the last node with children + if (state.get_legal_moves().empty()) { + states_exhausted = revert_to_last_node_with_children(); + } else { + add_children(next_moves); + } + } + // If the state is not a new one, reverts to the last node with children + else { + states_exhausted = revert_to_last_node_with_children(); } } + + // Sets the current node to one of its children + assert(states_exhausted == current_node->children.empty()); + if (!states_exhausted) { + current_node = ¤t_node->children.back(); + state.make_move(current_node->move); + } + + states_searched++; + } + + return state.is_solved(); +} + +void solver::add_children(std::vector<game_state::move>& moves) { + for (auto move : moves) { + add_child(move); } +} - ProfilerStop(); - return false; +void solver::add_child(game_state::move move) { + current_node->children.emplace_back(current_node, move); +} + +// 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 (!current_node->parent) + return true; + + state.undo_move(current_node->move); + +#ifndef NDEBUG + // Checks that the state after the undo is in the cache + // (as long as the move wasn't a dominance move) + + if (!current_node->move.is_dominance()) { + assert(cache.contains(state)); + LOG_DEBUG("(undo move)"); + } else { + LOG_DEBUG("(undo dominance move)"); + } + LOG_DEBUG(state); +#endif + + // 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(); + } else { + return false; + } } void solver::print_solution() const { std::flush(clog); std::flush(cout); - game_state state = initial_state; + + const node* n = &root.children.back(); + game_state state_copy = init_state; cout << "Solution:\n"; - cout << state << "\n"; - // Ignores first move which is - bool first = true; - for (auto f_node : frontier) { - if (first) { - first = false; - continue; + cout << state_copy << "\n"; + + if (states_searched > 1) { + while (!n->children.empty()) { + // TODO print move as well + state_copy.make_move(n->move); + cout << state_copy << "\n"; + n = &n->children.back(); } - // TODO print move as well - state.make_move(f_node.move); - cout << state << "\n"; } cout << "States Searched: " << states_searched << "\n"; } + +int solver::get_states_searched() const { + return states_searched; +} + +const solver::node& solver::get_search_tree() const { + return root; +} diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 526bd53..9488dda 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -6,6 +6,7 @@ #define SOLVITAIRE_SOLVER_H #include <string> +#include <list> #include <vector> #include "../game/global_cache.h" @@ -16,20 +17,30 @@ public: global_cache cache; struct node { - explicit node(game_state::move, std::vector<game_state::move>); + node(node*, game_state::move); + node* parent; const game_state::move move; - std::vector<game_state::move> unsearched_children; + std::list<node> children; }; explicit solver(const game_state&); bool run(); void print_solution() const; + int get_states_searched() const; + const node& get_search_tree() const; private: - const game_state initial_state; - std::vector<node> frontier; + bool revert_to_last_node_with_children(); + void add_children(std::vector<game_state::move>&); + void add_child(game_state::move); + + const game_state init_state; + game_state state; int states_searched; + + node root; + node* current_node; }; |
