From bba8775ce7c63783303240470d4a4b8404c9498b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 11:36:27 +0000 Subject: Add src file directory structure --- src/main/solver/solver.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++ src/main/solver/solver.h | 39 ++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/main/solver/solver.cpp create mode 100644 src/main/solver/solver.h (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp new file mode 100644 index 0000000..1c03d02 --- /dev/null +++ b/src/main/solver/solver.cpp @@ -0,0 +1,82 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#include +#include +#include +#include + +#include "solver.h" +#include "input-output/log_helper.h" +#include "game/game_state.h" +#include +using namespace std; + +solver::solver(const game_state gs, const sol_rules& sr) + : root(gs), rules(sr), solution(gs), solution_found(false), + states_searched(0) {} + +solver::node::node(const game_state gs) : state(gs) {} + +bool solver::run() { + been_run = true; + + // Iteratively run a depth-first search. Nodes in the frontier are yet to + // be explored + vector frontier; + frontier.push_back(root); + while (!frontier.empty()) { + // Pop the element at the top of the stack + node current = frontier.back(); + frontier.pop_back(); + + LOG_DEBUG(current.state); + + // Create new nodes for each of the children + vector new_children = current.state.get_next_legal_states(); + states_searched++; + + // NOTE: get_next_legal_states() makes the is_solved() function work + if (current.state.is_solved()) { + solution = current; + solution_found = true; + break; + } + + for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { + // If we have seen the state before, ignore it (loop detection)) + if (find(begin(current.history), end(current.history), *it) + != end(current.history)) { + continue; + } + + node n = current; + n.history.push_back(n.state); + n.state = *it; + + frontier.push_back(n); + } + } + + return solution_found; +} + +std::ostream& operator<< (std::ostream& stream, const solver& sol) { + + if (sol.solution_found) { + stream << "Solution:\n"; + for (auto state : sol.solution.history) { + stream << state << "\n"; + } + stream << sol.solution.state << "\nSolved!\n"; + + } else if (sol.been_run) { + stream << "Deal:\n" << sol.root.state << "\n" + << "No Possible Solution\n"; + } else { + stream << "Solver has not been run yet\n"; + } + stream << "States Searched: " << sol.states_searched << "\n"; + return stream; +} \ No newline at end of file diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h new file mode 100644 index 0000000..c1a72d3 --- /dev/null +++ b/src/main/solver/solver.h @@ -0,0 +1,39 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#ifndef SOLVITAIRE_SOLVER_H +#define SOLVITAIRE_SOLVER_H + +#include +#include + +#include + +#include "game/game_state.h" +#include "game/sol_rules.h" + +class solver { +public: + solver(const game_state, const sol_rules&); + bool run(); + + friend std::ostream& operator<< (std::ostream&, const solver&); + +private: + struct node { + node(const game_state); + std::vector history; + game_state state; + }; + + const node root; + const sol_rules rules; + bool been_run; + node solution; + bool solution_found; + int states_searched; +}; + + +#endif //SOLVITAIRE_SOLVER_H -- cgit v1.2.1 From 14f534452ac99913931c0513532dfbad2e277493 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 12:20:31 +0000 Subject: Update logging --- src/main/solver/solver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 1c03d02..ca60333 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -31,7 +31,7 @@ bool solver::run() { node current = frontier.back(); frontier.pop_back(); - LOG_DEBUG(current.state); + LOG_DEBUG << current.state; // Create new nodes for each of the children vector new_children = current.state.get_next_legal_states(); -- cgit v1.2.1 From 4b66501286a4e7e87f5a310ff060b5f4ee4c8315 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 09:50:25 +0000 Subject: Improve game state gen + run() returns opt --- src/main/solver/solver.cpp | 58 +++++++++++++++++++++++++--------------------- src/main/solver/solver.h | 18 ++++++++------ 2 files changed, 43 insertions(+), 33 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index ca60333..160af6f 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -11,15 +11,29 @@ #include "input-output/log_helper.h" #include "game/game_state.h" #include + using namespace std; +using namespace boost; solver::solver(const game_state gs, const sol_rules& sr) - : root(gs), rules(sr), solution(gs), solution_found(false), + : root(NULL, gs), rules(sr), solution(root), solution_found(false), states_searched(0) {} -solver::node::node(const game_state gs) : state(gs) {} +solver::node::node(const node* parent, const game_state& gs) : + history(solver::node::gen_history(parent)), state(gs) {} + +const vector solver::node::gen_history(const node* parent) { + vector hist; + if (parent != NULL) { + hist = std::vector(parent->history); + hist.push_back(parent->state); + } else { + hist = vector(); + } + return hist; +} -bool solver::run() { +optional solver::run() { been_run = true; // Iteratively run a depth-first search. Nodes in the frontier are yet to @@ -28,10 +42,10 @@ bool solver::run() { frontier.push_back(root); while (!frontier.empty()) { // Pop the element at the top of the stack - node current = frontier.back(); + const node current = frontier.back(); frontier.pop_back(); - LOG_DEBUG << current.state; + LOG_DEBUG (current.state); // Create new nodes for each of the children vector new_children = current.state.get_next_legal_states(); @@ -39,9 +53,7 @@ bool solver::run() { // NOTE: get_next_legal_states() makes the is_solved() function work if (current.state.is_solved()) { - solution = current; - solution_found = true; - break; + return node(current); } for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { @@ -51,32 +63,26 @@ bool solver::run() { continue; } - node n = current; - n.history.push_back(n.state); - n.state = *it; - + const node n(¤t, *it); frontier.push_back(n); } } - return solution_found; + return none; } -std::ostream& operator<< (std::ostream& stream, const solver& sol) { +const solver::node solver::get_root() const { + return root; +} - if (sol.solution_found) { - stream << "Solution:\n"; - for (auto state : sol.solution.history) { - stream << state << "\n"; - } - stream << sol.solution.state << "\nSolved!\n"; +int solver::get_states_searched() const { + return states_searched; +} - } else if (sol.been_run) { - stream << "Deal:\n" << sol.root.state << "\n" - << "No Possible Solution\n"; - } else { - stream << "Solver has not been run yet\n"; +std::ostream& operator<< (std::ostream& stream, const solver::node& solution) { + for (auto state : solution.history) { + stream << state << "\n"; } - stream << "States Searched: " << sol.states_searched << "\n"; + stream << solution.state << "\n"; return stream; } \ No newline at end of file diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index c1a72d3..6dfccc2 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -15,18 +15,22 @@ class solver { public: - solver(const game_state, const sol_rules&); - bool run(); - - friend std::ostream& operator<< (std::ostream&, const solver&); - -private: struct node { - node(const game_state); + node(const node*, const game_state&); std::vector history; game_state state; + + static const std::vector gen_history(const node* parent); }; + solver(const game_state, const sol_rules&); + boost::optional run(); + const node get_root() const; + int get_states_searched() const; + + friend std::ostream& operator<< (std::ostream&, const solver::node&); + +private: const node root; const sol_rules rules; bool been_run; -- cgit v1.2.1 From 16556a7f36da4fc8093285392f81409b1cfa7aa7 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 22:48:15 +0000 Subject: Refactor solver --- src/main/solver/solver.cpp | 19 ++++++++----------- src/main/solver/solver.h | 6 +++--- 2 files changed, 11 insertions(+), 14 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 160af6f..f412d03 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -16,8 +16,7 @@ using namespace std; using namespace boost; solver::solver(const game_state gs, const sol_rules& sr) - : root(NULL, gs), rules(sr), solution(root), solution_found(false), - states_searched(0) {} + : root(NULL, gs), rules(sr), solution(root), states_searched(0) {} solver::node::node(const node* parent, const game_state& gs) : history(solver::node::gen_history(parent)), state(gs) {} @@ -33,13 +32,12 @@ const vector solver::node::gen_history(const node* parent) { return hist; } -optional solver::run() { - been_run = true; - +const optional solver::run() { // Iteratively run a depth-first search. Nodes in the frontier are yet to // be explored vector frontier; frontier.push_back(root); + while (!frontier.empty()) { // Pop the element at the top of the stack const node current = frontier.back(); @@ -51,11 +49,6 @@ optional solver::run() { vector new_children = current.state.get_next_legal_states(); states_searched++; - // NOTE: get_next_legal_states() makes the is_solved() function work - if (current.state.is_solved()) { - return node(current); - } - for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { // If we have seen the state before, ignore it (loop detection)) if (find(begin(current.history), end(current.history), *it) @@ -64,7 +57,11 @@ optional solver::run() { } const node n(¤t, *it); - frontier.push_back(n); + if (n.state.is_solved()) { + return n; + } else { + frontier.push_back(n); + } } } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 6dfccc2..91c93d6 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -24,18 +24,18 @@ public: }; solver(const game_state, const sol_rules&); - boost::optional run(); + const node get_root() const; int get_states_searched() const; + const boost::optional run(); + friend std::ostream& operator<< (std::ostream&, const solver::node&); private: const node root; const sol_rules rules; - bool been_run; node solution; - bool solution_found; int states_searched; }; -- cgit v1.2.1