aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/evaluation/benchmark.cpp4
-rw-r--r--src/main/evaluation/benchmark.h2
-rw-r--r--src/main/evaluation/solvability_calc.cpp7
-rw-r--r--src/main/evaluation/solvability_calc.h3
-rw-r--r--src/main/game/global_cache.cpp21
-rw-r--r--src/main/game/global_cache.h10
-rw-r--r--src/main/input-output/input/command_line_helper.cpp14
-rw-r--r--src/main/input-output/input/command_line_helper.h2
-rw-r--r--src/main/main.cpp27
-rw-r--r--src/main/solver/solver.cpp17
-rw-r--r--src/main/solver/solver.h3
11 files changed, 70 insertions, 40 deletions
diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp
index 42fff9d..367caf5 100644
--- a/src/main/evaluation/benchmark.cpp
+++ b/src/main/evaluation/benchmark.cpp
@@ -13,7 +13,7 @@
using namespace std;
typedef chrono::microseconds microsec;
-void benchmark::run(const sol_rules &rules) {
+void benchmark::run(const sol_rules &rules, uint64_t cache_capacity) {
cout << "Seed "
"| Median/Mean Solution Time(μs)"
"| Median/Mean States Searched "
@@ -28,7 +28,7 @@ void benchmark::run(const sol_rules &rules) {
for(int seed = 1; seed <= 1000; seed++) {
game_state gs(rules, seed);
- solver sol(gs);
+ solver sol(gs, cache_capacity);
auto start = chrono::steady_clock::now();
solver::sol_state result = sol.run();
diff --git a/src/main/evaluation/benchmark.h b/src/main/evaluation/benchmark.h
index f934bba..f8737ce 100644
--- a/src/main/evaluation/benchmark.h
+++ b/src/main/evaluation/benchmark.h
@@ -10,7 +10,7 @@
class benchmark {
public:
- static void run(const sol_rules &rules);
+ static void run(const sol_rules &rules, uint64_t);
};
diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp
index 702bec3..1e650eb 100644
--- a/src/main/evaluation/solvability_calc.cpp
+++ b/src/main/evaluation/solvability_calc.cpp
@@ -17,7 +17,8 @@ using namespace std;
const double solvability_calc::coeff_ewma_weight = 0.7;
-solvability_calc::solvability_calc(const sol_rules& r) : rules(r) {
+solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) :
+ rules(r), cache_capacity(cache_capacity_) {
}
//////////////////////
@@ -127,7 +128,7 @@ void solvability_calc::calculate_solvability_percentage() {
void solvability_calc::solve_seed(int seed, millisec timeout,
seed_results& seed_res) {
game_state gs(rules, seed);
- solver sol(gs);
+ solver sol(gs, cache_capacity);
atomic<bool> terminate_solver(false);
future<bool> future = std::async(
@@ -139,8 +140,8 @@ void solvability_calc::solve_seed(int seed, millisec timeout,
future_status status;
do {
- status = future.wait_for(timeout);
auto start = chrono::steady_clock::now();
+ status = future.wait_for(timeout);
if (status == future_status::timeout) {
terminate_solver = true;
diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h
index ec9cda2..0a75e36 100644
--- a/src/main/evaluation/solvability_calc.h
+++ b/src/main/evaluation/solvability_calc.h
@@ -11,7 +11,7 @@
class solvability_calc {
public:
- explicit solvability_calc(const sol_rules&);
+ explicit solvability_calc(const sol_rules&, uint64_t);
void calculate_solvability_percentage();
@@ -63,6 +63,7 @@ private:
const sol_rules& rules;
const static double coeff_ewma_weight;
+ const uint64_t cache_capacity;
};
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp
index 2e271cc..db0c6de 100644
--- a/src/main/game/global_cache.cpp
+++ b/src/main/game/global_cache.cpp
@@ -219,7 +219,7 @@ item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) {
);
}
-lru_cache::lru_cache(const game_state& gs, size_t max_num_items_)
+lru_cache::lru_cache(const game_state& gs, uint64_t max_num_items_)
: max_num_items(max_num_items_), cache(get_init_tuple(gs)), states_removed_from_cache(0) {
}
@@ -232,7 +232,7 @@ pair<item_list::iterator, bool> lru_cache::insert(const game_state& gs) {
// If the least recently used node is 'live' (i.e. a parent), relocates
// it to the head of the list until this is no longer the case
- for (size_t i = 0; prev(cache.end())->live; i++) {
+ for (uint64_t i = 0; prev(cache.end())->live; i++) {
cache.relocate(cache.begin(), prev(cache.end()));
if (i == max_num_items) {
@@ -254,11 +254,24 @@ void lru_cache::clear() {
cache.clear();
}
+item_list::size_type lru_cache::size() const {
+ return cache.size();
+}
+
+item_list::size_type lru_cache::bucket_count() const {
+ return cache.get<1>().bucket_count();
+}
+
void lru_cache::set_non_live(item_list::iterator state_iter) {
+#ifndef NDEBUG
+ bool succ =
+#endif
cache.modify(state_iter, [](auto& v){ v.live = false; });
- // TODO assert
+#ifndef NDEBUG
+ assert(succ);
+#endif
}
-int lru_cache::get_states_removed_from_cache() const {
+uint64_t lru_cache::get_states_removed_from_cache() const {
return states_removed_from_cache;
}
diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h
index 7798f78..dd4670e 100644
--- a/src/main/game/global_cache.h
+++ b/src/main/game/global_cache.h
@@ -55,19 +55,21 @@ public:
>
> item_list;
- explicit lru_cache(const game_state&, std::size_t);
+ explicit lru_cache(const game_state&, uint64_t);
std::pair<item_list::iterator, bool> insert(const game_state&);
bool contains(const game_state&) const;
void clear();
+ item_list::size_type size() const;
+ item_list::size_type bucket_count() const;
void set_non_live(item_list::iterator);
- int get_states_removed_from_cache() const;
+ uint64_t get_states_removed_from_cache() const;
private:
static item_list::ctor_args_list get_init_tuple(const game_state&);
- std::size_t max_num_items;
+ uint64_t max_num_items;
item_list cache;
- int states_removed_from_cache;
+ uint64_t states_removed_from_cache;
};
#endif //SOLVITAIRE_GLOBAL_CACHE_H
diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp
index 4b0942a..1013f50 100644
--- a/src/main/input-output/input/command_line_helper.cpp
+++ b/src/main/input-output/input/command_line_helper.cpp
@@ -37,7 +37,9 @@ command_line_helper::command_line_helper()
"deal based on a seed. Must supply either 'random',"
"'solvability', 'benchmark' or list of deals to be solved.")
("classify", "outputs a simple 'solvable/not solvable' "
- "classification")
+ "classification")
+ ("cache-capacity", po::value<uint64_t>(), "sets an upper bound on the number of states allowed in "
+ "the cache")
("solvability", "calculates the solvability "
"percentage of the supplied solitaire game. Must supply "
"either 'random', 'benchmark', 'solvability' or list of deals to be "
@@ -102,6 +104,12 @@ bool command_line_helper::parse(int argc, const char* argv[]) {
random_deal = -1;
}
+ if (vm.count("cache-capacity")) {
+ cache_capacity = vm["cache-capacity"].as<uint64_t>();
+ } else {
+ cache_capacity = 1000000000; // One billion
+ }
+
solvability = (vm.count("solvability") != 0);
benchmark = (vm.count("benchmark") != 0);
@@ -199,6 +207,10 @@ bool command_line_helper::get_classify() {
return classify;
}
+uint64_t command_line_helper::get_cache_capacity() {
+ return cache_capacity;
+}
+
bool command_line_helper::get_solvability() {
return solvability;
}
diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h
index 911cd5a..ca58d5f 100644
--- a/src/main/input-output/input/command_line_helper.h
+++ b/src/main/input-output/input/command_line_helper.h
@@ -20,6 +20,7 @@ public:
bool get_solvability();
bool get_available_game_types();
bool get_benchmark();
+ uint64_t get_cache_capacity();
std::string get_describe_game_rules();
private:
@@ -44,6 +45,7 @@ private:
bool solvability;
bool available_game_types;
bool benchmark;
+ uint64_t cache_capacity;
};
#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H
diff --git a/src/main/main.cpp b/src/main/main.cpp
index d71db6c..8c2956d 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -19,9 +19,9 @@ using namespace boost;
namespace po = boost::program_options;
const optional<sol_rules> gen_rules(command_line_helper&);
-void solve_random_game(int, const sol_rules&, bool);
-void solve_input_files(vector<string>, const sol_rules&, bool);
-void solve_game(const game_state&, bool);
+void solve_random_game(int, const sol_rules&, bool, uint64_t);
+void solve_input_files(vector<string>, const sol_rules&, bool, uint64_t);
+void solve_game(const game_state&, bool, uint64_t);
// Decides what to do given supplied command-line options
int main(int argc, const char* argv[]) {
@@ -50,16 +50,16 @@ int main(int argc, const char* argv[]) {
// If the user has asked for a solvability percentage, calculates it
if (clh.get_solvability()) {
- solvability_calc solv_c(*rules);
+ solvability_calc solv_c(*rules, clh.get_cache_capacity());
solv_c.calculate_solvability_percentage();
}
// If a random deal seed has been supplied, solves it
else if (clh.get_random_deal() != -1) {
- solve_random_game(clh.get_random_deal(), *rules, clh.get_classify());
+ solve_random_game(clh.get_random_deal(), *rules, clh.get_classify(), clh.get_cache_capacity());
}
// If the benchmark option has been supplied, generates it
else if (clh.get_benchmark()) {
- benchmark::run(*rules);
+ benchmark::run(*rules, clh.get_cache_capacity());
}
// Otherwise there are supplied input files which should be solved
else {
@@ -68,7 +68,7 @@ int main(int argc, const char* argv[]) {
// If there are no input files, solve a random deal based on the
// supplied seed
assert (!input_files.empty());
- solve_input_files(input_files, *rules, clh.get_classify());
+ solve_input_files(input_files, *rules, clh.get_classify(), clh.get_cache_capacity());
}
return EXIT_SUCCESS;
@@ -90,13 +90,14 @@ const optional<sol_rules> gen_rules(command_line_helper& clh) {
}
}
-void solve_random_game(int seed, const sol_rules& rules, bool classify) {
+void solve_random_game(int seed, const sol_rules& rules, bool classify, uint64_t cache_capacity) {
LOG_INFO ("Attempting to solve with seed: " << seed << "...");
game_state gs(rules, seed);
- solve_game(gs, classify);
+ solve_game(gs, classify, cache_capacity);
}
-void solve_input_files(const vector<string> input_files, const sol_rules& rules, bool classify) {
+void solve_input_files(const vector<string> input_files, const sol_rules& rules,
+ bool classify, uint64_t cache_capacity) {
for (const string& input_file : input_files) {
try {
// Reads in the input file to a json doc
@@ -106,7 +107,7 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules,
game_state gs(rules, in_doc);
LOG_INFO ("Attempting to solve " << input_file << "...");
- solve_game(gs, classify);
+ solve_game(gs, classify, cache_capacity);
} catch (const runtime_error& error) {
string errmsg = "Error parsing deal file: ";
@@ -116,8 +117,8 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules,
}
}
-void solve_game(const game_state& gs, bool classify) {
- solver solv(gs);
+void solve_game(const game_state& gs, bool classify, uint64_t cache_capacity) {
+ solver solv(gs, cache_capacity);
bool solution = solv.run() == solver::sol_state::solved;
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index d7c5465..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"
@@ -20,12 +21,12 @@ using std::pair;
using boost::optional;
using std::max;
-solver::solver(const game_state& gs)
- : cache(lru_cache(gs, 1000000))
+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)
@@ -75,7 +76,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver)
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()) {
@@ -124,10 +125,6 @@ void solver::add_child(move m, optional<lru_cache::item_list::iterator> i) {
current_node->children.emplace_back(current_node, m, i);
}
-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.
@@ -200,6 +197,8 @@ void solver::print_solution_info() const {
<< "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";
}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 73d5e41..bc3af0a 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -25,7 +25,7 @@ public:
boost::optional<lru_cache::item_list::iterator> parent_state_iter; // Optional, as dominance moves aren't cached
};
- explicit solver(const game_state&);
+ explicit solver(const game_state&, uint64_t);
~solver();
sol_state run(boost::optional<std::atomic<bool> &> = boost::none);
@@ -39,7 +39,6 @@ private:
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;
game_state state;