aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver/solver.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/solver/solver.cpp')
-rw-r--r--src/main/solver/solver.cpp113
1 files changed, 55 insertions, 58 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 884b2e5..7d490be 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -9,19 +9,24 @@
#include <list>
#include "solver.h"
+#include "../game/move.h"
#include "../input-output/output/log_helper.h"
-using namespace std;
-using namespace boost;
+using std::atomic;
+using std::vector;
+using std::cout;
+using std::clog;
solver::solver(const game_state& gs)
: cache(gs)
, init_state(gs)
, state(gs)
, states_searched(1)
+ , unique_states_searched(1)
, backtracks(0)
+ , dominance_moves(0)
, depth(0)
- , root(nullptr, game_state::move(255, 255, 255))
+ , root(nullptr, move(move::mtype::null))
, current_node(&root) {
}
@@ -34,95 +39,87 @@ solver::~solver() {
}
}
-solver::node::node(node* p, const game_state::move m)
- : parent(p), move(m), children() {
+solver::node::node(node* p, const move m)
+ : parent(p), mv(m), children() {
}
-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;
+solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) {
bool states_exhausted = false;
+
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 sol_state::cutoff;
+ return sol_state::timed_out;
}
#ifndef NDEBUG
- if (current_node->move.is_dominance()) {
+ if (current_node->mv.type == move::mtype::dominance) {
LOG_DEBUG("(dominance move)");
}
LOG_DEBUG(state);
#endif
- if (cutoff_depth && depth >= *cutoff_depth) {
- cutoff_triggered = true;
- states_exhausted = revert_to_last_node_with_children();
+ // If there is a dominance move available, adds it to the search tree
+ // and repeats. Doesn't cache the state.
+ boost::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);
} else {
- // 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 {
- // 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 the state is not a new one, reverts to the last node with children
- 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<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 the state is not a new one, reverts to the last node with children
+ else {
+ unique_states_searched--;
+ 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 = &current_node->children.back();
- state.make_move(current_node->move);
+ state.make_move(current_node->mv);
depth++;
+ if (current_node->mv.type == move::mtype::dominance) dominance_moves++;
}
states_searched++;
+ unique_states_searched++;
}
if (state.is_solved()) {
return sol_state::solved;
} else {
assert(states_exhausted);
- return cutoff_triggered ? sol_state::cutoff : sol_state::unsolvable;
+ return sol_state::unsolvable;
}
}
-void solver::add_children(std::vector<game_state::move>& moves) {
+void solver::add_children(std::vector<move>& moves) {
current_node->children.reserve(moves.size());
for (auto move : moves) {
add_child(move);
}
}
-vector<game_state::move> solver::get_next_moves() {
- return state.get_legal_moves(current_node->move);
+vector<move> solver::get_next_moves() {
+ return state.get_legal_moves(current_node->mv);
}
-void solver::add_child(game_state::move move) {
- current_node->children.emplace_back(current_node, move);
+void solver::add_child(move m) {
+ current_node->children.emplace_back(current_node, m);
}
// Called when the current node's children have been exhausted. Travels back up
@@ -132,7 +129,7 @@ bool solver::revert_to_last_node_with_children() {
if (!current_node->parent)
return true;
- state.undo_move(current_node->move);
+ state.undo_move(current_node->mv);
depth--;
backtracks++;
@@ -140,7 +137,7 @@ bool solver::revert_to_last_node_with_children() {
// 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()) {
+ if (current_node->mv.type != move::mtype::dominance) {
assert(cache.contains(state));
LOG_DEBUG("(undo move)");
} else {
@@ -173,7 +170,7 @@ void solver::print_solution() const {
if (states_searched > 1) {
while (!n->children.empty()) {
- state_copy.make_move(n->move);
+ state_copy.make_move(n->mv);
cout << state_copy << "\n";
n = &n->children.back();
}
@@ -181,18 +178,18 @@ void solver::print_solution() const {
cout << "\n";
}
-int solver::get_states_searched() const {
- return states_searched;
-}
-
-int solver::get_final_depth() const {
- return depth;
+void solver::print_solution_info() const {
+ cout << "States Searched: " << states_searched << "\n"
+ << "Unique States Searched: " << unique_states_searched << "\n"
+ << "Backtracks: " << backtracks << "\n"
+ << "Dominance Moves: " << dominance_moves << "\n"
+ << "Final Search Depth: " << depth << "\n";
}
const solver::node& solver::get_search_tree() const {
return root;
}
-int solver::get_backtrack_count() {
- return backtracks;
+int solver::get_states_searched() const {
+ return states_searched;
}