aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver
diff options
context:
space:
mode:
authorCharlie Blake2017-12-31 11:53:50 +0000
committerGitHub2017-12-31 11:53:50 +0000
commit22e8b69a830d3e61be83762b9f1e6ce97bd3ec1e (patch)
treefaa726d97fcde3ea236f46e0c2b2d77a68f4cd9f /src/main/solver
parentMerge pull request #26 from thecharlesblake/deal-description-and-test (diff)
parentRemove need for default switch in compile flags (diff)
downloadmetasol-22e8b69a830d3e61be83762b9f1e6ce97bd3ec1e.tar.gz
metasol-22e8b69a830d3e61be83762b9f1e6ce97bd3ec1e.zip

Merge pull request #27 from thecharlesblake/efficient-implementation

Efficient implementation

Diffstat (limited to 'src/main/solver')
-rw-r--r--src/main/solver/solver.cpp115
-rw-r--r--src/main/solver/solver.h41
2 files changed, 74 insertions, 82 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index c487c29..23ed693 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -6,6 +6,7 @@
#include <ostream>
#include <iostream>
#include <algorithm>
+#include <utility>
#include "solver.h"
#include "../input-output/log_helper.h"
@@ -13,71 +14,77 @@
using namespace std;
using namespace boost;
-solver::solver(const game_state gs, const sol_rules& sr)
- : root(NULL, gs), rules(sr), solution(root), states_searched(0) {}
+solver::solver(const game_state& gs, const sol_rules& sr)
+ : initial_state(gs), rules(sr), states_searched(0) {}
-solver::node::node(const node* parent, const game_state& gs) :
- history(solver::node::gen_history(parent)), state(gs) {}
-
-const solver::node_history solver::node::gen_history(const node* parent) {
- node_history hist;
- if (parent != NULL) {
- copy (begin(parent->history.get<0>()),
- end(parent->history.get<0>()),
- back_inserter(hist.get<0>()));
- hist.get<0>().push_back(parent->state);
- }
- return hist;
+solver::node::node(const game_state::move m,
+ vector<game_state::move> &&uc)
+ : move(m), unsearched_children(std::move(uc)) {
}
-const optional<solver::node> solver::run() {
- // Iteratively run a depth-first search. Nodes in the frontier are yet to
- // be explored
- vector<node> frontier;
- frontier.push_back(root);
-
- while (!frontier.empty()) {
- // Pop the element at the top of the stack
- const node current = frontier.back();
- frontier.pop_back();
+bool solver::run() {
+ game_state state = initial_state;
- LOG_DEBUG (current.state);
+ frontier.emplace_back(NULL_MOVE, state.get_legal_moves());
+ states_searched++;
- // Create new nodes for each of the children
- vector<game_state> new_children = current.state.get_next_legal_states();
- states_searched++;
-
- for (auto it = new_children.rbegin(); it != new_children.rend(); it++) {
- // If we have seen the state before, ignore it (loop detection))
- if (current.history.get<1>().find(*it)
- != current.history.get<1>().end()) {
- continue;
+ while (!frontier.empty()) {
+ node& current = frontier.back();
+ LOG_DEBUG (state);
+
+ // If we have a solution, returns true
+ if (state.is_solved()) return true;
+
+ // 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);
}
-
- const node n(&current, *it);
- if (n.state.is_solved()) {
- return n;
+ // Returns to the previous state
+ frontier.pop_back();
+ } else {
+ // Applies the first possible move in this state
+ state.make_move(current.unsearched_children.back());
+
+ // Insert the state into the global cache
+ bool is_new_state = global_cache.insert(state.get_data()).second;
+ if (is_new_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(current.unsearched_children.back(),
+ state.get_legal_moves());
+ states_searched++;
} else {
- frontier.push_back(n);
+ // If we've seen the state before, undo the move
+ state.undo_move(current.unsearched_children.back());
}
+ // Removes the move just made from the list of possible moves
+ current.unsearched_children.pop_back();
}
}
- return none;
+ return false;
}
-const solver::node solver::get_root() const {
- return root;
-}
-
-int solver::get_states_searched() const {
- return states_searched;
-}
-
-std::ostream& operator<< (std::ostream& stream, const solver::node& solution) {
- for (auto state : solution.history.get<0>()) {
- stream << state << "\n";
+void solver::print_solution() const {
+ std::flush(clog);
+ std::flush(cout);
+ game_state state = initial_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;
+ }
+ // TODO print move as well
+ state.make_move(f_node.move);
+ cout << state << "\n";
}
- stream << solution.state << "\n";
- return stream;
-} \ No newline at end of file
+ cout << "States Searched: " << states_searched << "\n";
+}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index bb5f8cf..1ef841d 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -7,48 +7,33 @@
#include <string>
#include <vector>
-
-#include <boost/optional.hpp>
-#include <boost/multi_index/hashed_index.hpp>
-#include <boost/multi_index_container.hpp>
-#include <boost/multi_index/identity.hpp>
-#include <boost/multi_index/sequenced_index.hpp>
+#include <unordered_set>
#include "../game/game_state.h"
#include "../game/sol_rules.h"
class solver {
public:
- typedef boost::multi_index::multi_index_container<
- game_state,
- boost::multi_index::indexed_by<
- boost::multi_index::sequenced<>,
- boost::multi_index::hashed_unique<boost::multi_index::identity<game_state>>
- >
- > node_history;
+ std::unordered_set<
+ std::vector<pile>,
+ boost::hash<std::vector<pile>>
+ > global_cache;
struct node {
- node(const node*, const game_state&);
- node_history history;
- game_state state;
-
- static const node_history gen_history(const node* parent);
+ explicit node(const game_state::move, std::vector<game_state::move>&&);
+ const game_state::move move;
+ std::vector<game_state::move> unsearched_children;
};
- solver(const game_state, const sol_rules&);
-
- const node get_root() const;
- int get_states_searched() const;
+ solver(const game_state&, const sol_rules&);
- const boost::optional<node> run();
-
- friend std::ostream& operator<< (std::ostream&, const solver::node&);
+ bool run();
+ void print_solution() const;
private:
-
- const node root;
+ const game_state initial_state;
const sol_rules rules;
- node solution;
+ std::vector<node> frontier;
int states_searched;
};