aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/solver')
-rw-r--r--src/main/solver/solver.cpp60
-rw-r--r--src/main/solver/solver.h12
2 files changed, 46 insertions, 26 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index b227598..d7c5465 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -16,9 +16,12 @@ using std::atomic;
using std::vector;
using std::cout;
using std::clog;
+using std::pair;
+using boost::optional;
+using std::max;
solver::solver(const game_state& gs)
- : cache(new unlimited_cache(gs))
+ : cache(lru_cache(gs, 1000000))
, init_state(gs)
, state(gs)
, states_searched(1)
@@ -26,6 +29,7 @@ solver::solver(const game_state& gs)
, backtracks(0)
, dominance_moves(0)
, depth(0)
+ , max_depth(0)
, root(nullptr, move(move::mtype::null))
, current_node(&root) {
}
@@ -39,8 +43,8 @@ solver::~solver() {
}
}
-solver::node::node(node* p, const move m)
- : parent(p), mv(m), children() {
+solver::node::node(node* p, const move m, optional<lru_cache::item_list::iterator> c)
+ : parent(p), mv(m), children(), parent_state_iter(c) {
}
solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) {
@@ -61,22 +65,23 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver)
// 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();
+ 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 {
// Caches the current state
- bool is_new_state = cache->insert(state);
+ pair<lru_cache::item_list::iterator, bool> insert_res = cache.insert(state);
+ bool is_new_state = insert_res.second;
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();
+ states_exhausted = revert_to_last_node_with_children(insert_res.first);
} else {
- add_children(next_moves);
+ add_children(next_moves, insert_res.first);
}
}
// If the state is not a new one, reverts to the last node with children
@@ -92,6 +97,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver)
current_node = &current_node->children.back();
state.make_move(current_node->mv);
depth++;
+ max_depth = max(depth, max_depth);
if (current_node->mv.type == move::mtype::dominance) dominance_moves++;
}
@@ -107,28 +113,34 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver)
}
}
-void solver::add_children(std::vector<move>& moves) {
+void solver::add_children(std::vector<move>& moves, optional<lru_cache::item_list::iterator> i) {
current_node->children.reserve(moves.size());
for (auto move : moves) {
- add_child(move);
+ add_child(move, i);
}
}
-vector<move> solver::get_next_moves() {
- return state.get_legal_moves(current_node->mv);
+void solver::add_child(move m, optional<lru_cache::item_list::iterator> i) {
+ current_node->children.emplace_back(current_node, m, i);
}
-void solver::add_child(move m) {
- current_node->children.emplace_back(current_node, m);
+vector<move> solver::get_next_moves() {
+ return state.get_legal_moves(current_node->mv);
}
// 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 an iterator to the current state is supplied to the function, will also
+// make sure to turn the 'live' bit off upon backtracking
+bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::iterator> cur_state) {
if (!current_node->parent)
return true;
+ // Turns the 'live' bit false on the state we are backtracking out of
+ if (cur_state) cache.set_non_live(*cur_state);
+
state.undo_move(current_node->mv);
depth--;
backtracks++;
@@ -138,7 +150,7 @@ bool solver::revert_to_last_node_with_children() {
// (as long as the move wasn't a dominance move)
if (current_node->mv.type != move::mtype::dominance) {
- assert(cache->contains(state));
+ if (cache.get_states_removed_from_cache() == 0) assert(cache.contains(state));
LOG_DEBUG("(undo move)");
} else {
LOG_DEBUG("(undo dominance move)");
@@ -146,13 +158,17 @@ bool solver::revert_to_last_node_with_children() {
LOG_DEBUG(state);
#endif
+ // Gets a reference to the parent state which can be supplied if this function is
+ // called recursively. This ensures that the cached state's 'live' bit is set as appropriate
+ optional<lru_cache::item_list::iterator> p_state = current_node->parent_state_iter;
+
// 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();
+ return revert_to_last_node_with_children(p_state);
} else {
return false;
}
@@ -179,11 +195,13 @@ void solver::print_solution() const {
}
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";
+ cout << "States Searched: " << states_searched << "\n"
+ << "Unique States Searched: " << unique_states_searched << "\n"
+ << "Backtracks: " << backtracks << "\n"
+ << "Dominance Moves: " << dominance_moves << "\n"
+ << "States Removed From Cache: " << cache.get_states_removed_from_cache() << "\n"
+ << "Maximum Search Depth: " << max_depth << "\n"
+ << "Final Search Depth: " << depth << "\n";
}
const solver::node& solver::get_search_tree() const {
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index dedce39..73d5e41 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -15,13 +15,14 @@
class solver {
public:
enum class sol_state {solved, unsolvable, timed_out};
- std::unique_ptr<global_cache> cache;
+ lru_cache cache;
struct node {
- node(node*, move);
+ node(node*, move, boost::optional<lru_cache::item_list::iterator> = boost::none);
node* parent;
const move mv;
std::vector<node> children;
+ boost::optional<lru_cache::item_list::iterator> parent_state_iter; // Optional, as dominance moves aren't cached
};
explicit solver(const game_state&);
@@ -35,9 +36,9 @@ public:
const node& get_search_tree() const;
private:
- bool revert_to_last_node_with_children();
- void add_children(std::vector<move>&);
- void add_child(move);
+ bool revert_to_last_node_with_children(boost::optional<lru_cache::item_list::iterator> = boost::none);
+ void add_children(std::vector<move>&, boost::optional<lru_cache::item_list::iterator> = boost::none);
+ void add_child(move, boost::optional<lru_cache::item_list::iterator> = boost::none);
std::vector<move> get_next_moves();
const game_state init_state;
@@ -48,6 +49,7 @@ private:
int backtracks;
int dominance_moves;
int depth;
+ int max_depth;
node root;
node* current_node;