aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver/solver.cpp
diff options
context:
space:
mode:
authorCharlie Blake2018-06-13 15:38:20 +0100
committerGitHub2018-06-13 15:38:20 +0100
commitccb1232a48deef855d0e0f3ea0f69382494975cc (patch)
treebcc8fe29ea84abbd6682c1a880064a90dd65f959 /src/main/solver/solver.cpp
parentMerge pull request #59 from thecharlesblake/paper-tidy-up (diff)
parentAdd default cache capacity of 100,000,000 (diff)
downloadmetasol-ccb1232a48deef855d0e0f3ea0f69382494975cc.tar.gz
metasol-ccb1232a48deef855d0e0f3ea0f69382494975cc.zip

Merge pull request #60 from thecharlesblake/cache-mem-limit

Cache mem limit

Diffstat (limited to 'src/main/solver/solver.cpp')
-rw-r--r--src/main/solver/solver.cpp71
1 files changed, 44 insertions, 27 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 7d490be..fd80556 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -7,6 +7,7 @@
#include <iostream>
#include <algorithm>
#include <list>
+#include <malloc.h>
#include "solver.h"
#include "../game/move.h"
@@ -16,16 +17,20 @@ 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(gs)
+solver::solver(const game_state& gs, uint64_t cache_capacity)
+ : cache(lru_cache(gs, cache_capacity))
, init_state(gs)
, state(gs)
- , states_searched(1)
- , unique_states_searched(1)
+ , states_searched(0)
+ , unique_states_searched(0)
, backtracks(0)
, dominance_moves(0)
, depth(0)
+ , max_depth(0)
, root(nullptr, move(move::mtype::null))
, current_node(&root) {
}
@@ -39,8 +44,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 +66,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();
+ vector<move> next_moves = state.get_legal_moves(current_node->mv);
// 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 +98,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 +114,30 @@ 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) {
- current_node->children.emplace_back(current_node, m);
+void solver::add_child(move m, optional<lru_cache::item_list::iterator> i) {
+ current_node->children.emplace_back(current_node, m, i);
}
// 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 +147,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 +155,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 +192,15 @@ 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"
+ << "Final States In Cache: " << cache.size() << "\n"
+ << "Final Buckets In Cache: " << cache.bucket_count() << "\n"
+ << "Maximum Search Depth: " << max_depth << "\n"
+ << "Final Search Depth: " << depth << "\n";
}
const solver::node& solver::get_search_tree() const {