aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver
diff options
context:
space:
mode:
authorCharlie Blake2018-03-08 22:45:52 +0000
committerGitHub2018-03-08 22:45:52 +0000
commit0dc0066a33ff3febaab438934c4dc8bb36aed87e (patch)
tree616323e505d846aa827394883e6a2d1e0cdcdd54 /src/main/solver
parentMerge pull request #42 from thecharlesblake/solvability-percentages (diff)
parentAdd ID search to solver for short sols (diff)
downloadmetasol-0dc0066a33ff3febaab438934c4dc8bb36aed87e.tar.gz
metasol-0dc0066a33ff3febaab438934c4dc8bb36aed87e.zip

Merge pull request #43 from thecharlesblake/shortest-solution

Add ID search to solver for short sols

Diffstat (limited to 'src/main/solver')
-rw-r--r--src/main/solver/solver.cpp76
-rw-r--r--src/main/solver/solver.h8
2 files changed, 57 insertions, 27 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 463d059..474800c 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -19,6 +19,7 @@ solver::solver(const game_state& gs)
, init_state(gs)
, state(gs)
, states_searched(1)
+ , depth(0)
, root(nullptr, game_state::move(255, 255, 255))
, current_node(&root) {
}
@@ -36,12 +37,19 @@ solver::node::node(node* p, const game_state::move m)
: parent(p), move(m), children() {
}
-bool solver::run(optional<atomic<bool> &> terminate_solver) {
+solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
+ return run_with_cutoff(terminate_solver, none);
+}
+
+solver::sol_state solver::run_with_cutoff(
+ boost::optional<std::atomic<bool> &> terminate_solver,
+ boost::optional<uint> cutoff_depth) {
+ bool cutoff_triggered = false;
bool states_exhausted = false;
- while(!state.is_solved() && !states_exhausted) {
+ while(!(state.is_solved() || states_exhausted)) {
// If the terminate flag was supplied and has been set to true, return
if (terminate_solver && *terminate_solver) {
- return false;
+ return sol_state::cutoff;
}
#ifndef NDEBUG
@@ -51,29 +59,34 @@ bool solver::run(optional<atomic<bool> &> terminate_solver) {
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);
+ if (cutoff_depth && depth >= *cutoff_depth) {
+ cutoff_triggered = true;
+ states_exhausted = revert_to_last_node_with_children();//TODO-
} else {
- // Caches the current state
- bool is_new_state = cache.insert(state);
- if (is_new_state) {
- // Gets the legal moves in the current state
- vector<game_state::move> next_moves = get_next_moves();
-
- // If there are none, reverts to the last node with children
- if (next_moves.empty()) {
- states_exhausted = revert_to_last_node_with_children();
- } else {
- add_children(next_moves);
+ // 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);//TODO+
+ } else {
+ // Caches the current state
+ bool is_new_state = cache.insert(state);
+ if (is_new_state) {
+ // Gets the legal moves in the current state
+ vector<game_state::move> next_moves = get_next_moves();
+
+ // If there are none, reverts to the last node with children
+ if (next_moves.empty()) {
+ states_exhausted = revert_to_last_node_with_children();//TODO-
+ } else {
+ add_children(next_moves);//TODO+
+ }
+ }
+ // If the state is not a new one, reverts to the last node with children
+ else {
+ states_exhausted = revert_to_last_node_with_children();//TODO-
}
- }
- // If the state is not a new one, reverts to the last node with children
- else {
- states_exhausted = revert_to_last_node_with_children();
}
}
@@ -82,12 +95,18 @@ bool solver::run(optional<atomic<bool> &> terminate_solver) {
if (!states_exhausted) {
current_node = &current_node->children.back();
state.make_move(current_node->move);
+ depth++;
}
states_searched++;
}
- return state.is_solved();
+ if (state.is_solved()) {
+ return sol_state::solved;
+ } else {
+ assert(states_exhausted);
+ return cutoff_triggered ? sol_state::cutoff : sol_state::unsolvable;
+ }
}
void solver::add_children(std::vector<game_state::move>& moves) {
@@ -117,6 +136,7 @@ bool solver::revert_to_last_node_with_children() {
return true;
state.undo_move(current_node->move);
+ depth--;
#ifndef NDEBUG
// Checks that the state after the undo is in the cache
@@ -161,13 +181,17 @@ void solver::print_solution() const {
n = &n->children.back();
}
}
- cout << "States Searched: " << states_searched << "\n";
+ cout << "\n";
}
int solver::get_states_searched() const {
return states_searched;
}
+int solver::get_final_depth() const {
+ return depth;
+}
+
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 2a024ac..b1455e5 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -14,6 +14,7 @@
class solver {
public:
+ enum class sol_state {solved, unsolvable, cutoff};
global_cache cache;
struct node {
@@ -26,9 +27,13 @@ public:
explicit solver(const game_state&);
~solver();
- bool run(boost::optional<std::atomic<bool> &> = boost::none);
+ sol_state run(boost::optional<std::atomic<bool> &> = boost::none);
+ sol_state run_with_cutoff(boost::optional<std::atomic<bool> &>,
+ boost::optional<uint> = boost::none);
+
void print_solution() const;
int get_states_searched() const;
+ int get_final_depth() const;
const node& get_search_tree() const;
private:
@@ -40,6 +45,7 @@ private:
const game_state init_state;
game_state state;
int states_searched;
+ uint depth;
node root;
node* current_node;