From fea5d4c3a84e3d83ed761579a7a7cc31279cc165 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 15:43:21 +0100 Subject: Set solver seed limit --- src/main/evaluation/solvability_calc.cpp | 10 ++++++---- src/main/evaluation/solvability_calc.h | 2 +- src/main/input-output/input/command_line_helper.cpp | 15 +++++++++++++-- src/main/input-output/input/command_line_helper.h | 2 ++ src/main/main.cpp | 3 ++- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index ffe883b..3fcdf44 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -99,7 +99,7 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s // SOLVING METHODS // ///////////////////// -void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores, const vector& resume) { +void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, const vector& resume) { vector resume_seeds(begin(resume) + 3, end(resume)); sort(begin(resume_seeds), end(resume_seeds)); @@ -123,11 +123,12 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores for (uint i = 0; i < cores; i++) { futures.push_back(async( launch::async, - [¤t_seed, &seed_res, &seeds_in_progress, &results_mutex, timeout, sr, cc, i, resume_seeds](){ + [¤t_seed, &seed_res, &seeds_in_progress, &results_mutex, seed_count, timeout, sr, cc, i, + resume_seeds](){ int my_seed = resume_seeds.size() > i ? resume_seeds[i] : current_seed++; - while (my_seed < INT_MAX) { + while (my_seed < seed_count) { results_mutex.lock(); seeds_in_progress.insert(my_seed); results_mutex.unlock(); @@ -149,7 +150,8 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores } )); } - futures[0].wait(); // Should wait indefinitely + + for (auto& f : futures) f.wait(); } solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec timeout, diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index ef3492c..eabeb99 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -17,7 +17,7 @@ class solvability_calc { public: explicit solvability_calc(const sol_rules&, uint64_t); - void calculate_solvability_percentage(int timeout, uint cores, const std::vector&); + void calculate_solvability_percentage(uint64_t, int, uint, const std::vector&); private: typedef std::chrono::milliseconds millisec; diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 2abef54..21eec88 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -38,12 +38,13 @@ command_line_helper::command_line_helper() "'solvability', 'benchmark' or list of deals to be solved.") ("classify", "outputs a simple 'solvable/not solvable' " "classification") - ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " + ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " "the cache") ("solvability", po::value(), "calculates the solvability " - "percentage of the supplied solitaire game, given a timeout in milliseconds. Must supply " + "percentage of the supplied solitaire game, given a limit for the number of seeds. Must supply " "either 'random', 'benchmark', 'solvability' or list of deals to be " "solved.") + ("timeout", po::value(), "adds a per-game timeout to the solvability percentage generation") ("resume", po::value>()->multitoken(), "resumes the solvability percentage calculation from a " "previous run. Must be supplied with the solvability option. " "Syntax: [sol unsol intract in-progress-1 in-progress-2 ...]") @@ -121,6 +122,12 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solvability = -1; } + if (vm.count("timeout")) { + timeout = vm["timeout"].as(); + } else { + timeout = 604800000; // 1 week in milliseconds + } + if (vm.count("cores")) { cores = vm["cores"].as(); } else { @@ -248,6 +255,10 @@ int command_line_helper::get_solvability() { return solvability; } +uint64_t command_line_helper::get_timeout() { + return timeout; +} + vector command_line_helper::get_resume() { return resume; } diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index 3ce239d..744935f 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -24,6 +24,7 @@ public: std::vector get_resume(); uint64_t get_cache_capacity(); std::string get_describe_game_rules(); + uint64_t get_timeout(); private: bool assess_errors(); @@ -51,6 +52,7 @@ private: bool available_game_types; bool benchmark; uint64_t cache_capacity; + uint64_t timeout; }; #endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index a492c26..d5c5dcc 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -51,7 +51,8 @@ int main(int argc, const char* argv[]) { // If the user has asked for a solvability percentage, calculates it if (clh.get_solvability() > 0) { solvability_calc solv_c(*rules, clh.get_cache_capacity()); - solv_c.calculate_solvability_percentage(clh.get_solvability(), clh.get_cores(), clh.get_resume()); + solv_c.calculate_solvability_percentage(clh.get_timeout(), clh.get_solvability(), clh.get_cores(), + clh.get_resume()); } // If a random deal seed has been supplied, solves it else if (clh.get_random_deal() != -1) { -- cgit v1.2.1 From dbb2dc6f3acadca6d1744a595fe0cd755367c62c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 16:30:12 +0100 Subject: Switch to wilson interval --- CMakeLists.txt | 2 +- src/main/evaluation/binomial_ci.cpp | 45 ++++++++++++++++++++ src/main/evaluation/binomial_ci.h | 19 +++++++++ src/main/evaluation/solvability_calc.cpp | 71 ++++---------------------------- src/main/evaluation/solvability_calc.h | 8 ---- 5 files changed, 73 insertions(+), 72 deletions(-) create mode 100644 src/main/evaluation/binomial_ci.cpp create mode 100644 src/main/evaluation/binomial_ci.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dea7de8..3b9d9fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,7 @@ set(sources src/main/input-output/input/command_line_helper.cpp src/main/game/search-state/game_state.auto_foundation_moves.cpp src/main/game/search-state/game_state.pile_order.cpp src/main/game/move.cpp - src/main/game/move.h) + src/main/game/move.h src/main/evaluation/binomial_ci.cpp src/main/evaluation/binomial_ci.h) set(sources_test src/test/test_helper.cpp src/test/test_helper.h diff --git a/src/main/evaluation/binomial_ci.cpp b/src/main/evaluation/binomial_ci.cpp new file mode 100644 index 0000000..c381c05 --- /dev/null +++ b/src/main/evaluation/binomial_ci.cpp @@ -0,0 +1,45 @@ +// +// Created by thecharlesblake on 20/06/18. +// + +#include +#include +#include "binomial_ci.h" + +using std::pow; +using std::min; +using std::max; +using std::pair; + +pair binomial_ci::wilson(int s_, int f_) { + auto s = static_cast(s_); + auto f = static_cast(f_); + double z = 1.96; + double z2 = pow(z, 2); + double n = s + f; + double p = (s + (z2 / 2.0)) / (n + z2); + double half_ci = (z / (n + z2)) * sqrt(((s * f) / n) + (z2 / 4.0)); + return { p - half_ci + , p + half_ci }; +} + +pair binomial_ci::wilson(int s, int f, int i) { + pair a = wilson(s, f + i); + pair b = wilson(s + i, f); + return {a.first, b.second}; +} + +pair binomial_ci::agresti_coull(int s, int f) { + double z = 1.96; + double n_ = s + f + pow(z, 2); + double p = (s + pow(z, 2)/2)/n_; + double half_ci = z * sqrt((p - pow(p, 2))/n_); + return { max(0.0, p - half_ci) + , min(1.0, p + half_ci) }; +} + +pair binomial_ci::agresti_coull(int s, int f, int i) { + pair a = agresti_coull(s, f + i); + pair b = agresti_coull(s + i, f); + return {a.first, b.second}; +} diff --git a/src/main/evaluation/binomial_ci.h b/src/main/evaluation/binomial_ci.h new file mode 100644 index 0000000..6e2dceb --- /dev/null +++ b/src/main/evaluation/binomial_ci.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 20/06/18. +// + +#ifndef SOLVITAIRE_BINOMIAL_CI_H +#define SOLVITAIRE_BINOMIAL_CI_H + + +#include + +struct binomial_ci { + static std::pair wilson(int, int); + static std::pair wilson(int, int, int); + static std::pair agresti_coull(int, int); + static std::pair agresti_coull(int, int, int); +}; + + +#endif //SOLVITAIRE_BINOMIAL_CI_H diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index 3fcdf44..b246e68 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -4,10 +4,10 @@ #include #include #include -#include #include "solvability_calc.h" #include "../solver/solver.h" +#include "binomial_ci.h" using namespace std; @@ -48,15 +48,13 @@ void solvability_calc::print_header(long t) const { } void solvability_calc::print_row(const seed_results& seed_res, sol_result res, set& seeds_in_progress) { - double lower_bound = sol_lower_bound(seed_res.solvable, - seed_res.unsolvable, - seed_res.intractable); - double upper_bound = sol_upper_bound(seed_res.solvable, - seed_res.unsolvable, - seed_res.intractable); - - cout << lower_bound * 100 - << ", " << upper_bound * 100 + pair interval = binomial_ci::wilson( + seed_res.solvable, + seed_res.unsolvable, + seed_res.intractable); + + cout << interval.first * 100 + << ", " << interval.second * 100 << ", " << seed_res.solvable << ", " << seed_res.unsolvable << ", " << seed_res.intractable @@ -236,56 +234,3 @@ void solvability_calc::seed_results::add_result(sol_result::type t) { break; } } - - -///////////////////////// -// CALCULATION METHODS // -///////////////////////// - -double solvability_calc::sol_lower_bound(int solvables, - int unsolvables, - int intractables) { - int x = solvables; - int n = solvables + unsolvables + intractables; - bool lower_bound = true; - return agresti_coull(x, n, lower_bound); -} - -double solvability_calc::sol_upper_bound(int solvables, - int unsolvables, - int intractables) { - int x = solvables + intractables; - int n = solvables + unsolvables + intractables; - bool lower_bound = false; - return agresti_coull(x, n, lower_bound); -} - -double solvability_calc::sol_ci_size(int solvables, - int unsolvables, - int intractables) { - return sol_upper_bound(solvables, unsolvables, intractables) - - sol_lower_bound(solvables, unsolvables, intractables); -} - -double solvability_calc::agresti_coull(int x, int n, bool lower_bound) { - double z = 1.96; - double n_ = n + pow(z, 2); - double p = (x + pow(z, 2)/2)/n_; - double half_ci = z * sqrt((p - pow(p, 2))/n_); - if (lower_bound) { - double ans = p - half_ci; - return ans < 0 ? 0 : ans; - } else { - double ans = p + half_ci; - return ans > 1 ? 1 : ans; - } -} - -double solvability_calc::agresti_coull_mean(int x, int n) { - double z = 1.96; - double n_ = n + pow(z, 2); - double p = (x + pow(z, 2)/2)/n_; - return p; -} - -int solvability_calc::rnd(double d) { return static_cast(lround(d)); } diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index eabeb99..6ca640d 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -49,14 +49,6 @@ private: // Solving methods static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t); - // Calculation methods - static double sol_lower_bound(int, int, int); - static double sol_upper_bound(int, int, int); - static double sol_ci_size(int, int, int); - static double agresti_coull(int, int, bool); - static double agresti_coull_mean(int, int); - static int rnd(double); - const sol_rules& rules; const uint64_t cache_capacity; }; -- cgit v1.2.1 From abae8e0529cf2e6048e9b2eb8248434e1f9551de Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 17:37:22 +0100 Subject: Add streamliner command line option (inc for spgs) --- CMakeLists.txt | 15 ------ src/main/evaluation/benchmark.cpp | 4 +- src/main/evaluation/benchmark.h | 2 +- src/main/evaluation/solvability_calc.cpp | 55 +++++++++++++++------- src/main/evaluation/solvability_calc.h | 10 ++-- src/main/game/global_cache.cpp | 16 +------ .../game_state.auto_foundation_moves.cpp | 4 +- src/main/game/search-state/game_state.cpp | 20 ++++---- src/main/game/search-state/game_state.h | 7 +-- .../input-output/input/command_line_helper.cpp | 15 ++++-- src/main/input-output/input/command_line_helper.h | 2 + src/main/main.cpp | 37 +++++++-------- 12 files changed, 95 insertions(+), 92 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b9d9fa..73b7fcc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,21 +157,6 @@ add_executable(solvitaire ${main} ${sources}) set_property(TARGET solvitaire PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) # For -flto flag target_link_libraries(solvitaire LINK_PUBLIC ${Boost_LIBRARIES}) -# add a separete executable for the 'suit reduction streamliner' version of the solver -add_executable(solvitaire-suit-reduction-streamliner ${main} ${sources}) -target_link_libraries(solvitaire-suit-reduction-streamliner LINK_PUBLIC ${Boost_LIBRARIES}) -set_target_properties(solvitaire-suit-reduction-streamliner PROPERTIES COMPILE_FLAGS -DSUIT_REDUCTION_STREAMLINER) - -# add a separete executable for the 'auto foundations streamliner' version of the solver -add_executable(solvitaire-auto-foundations-streamliner ${main} ${sources}) -target_link_libraries(solvitaire-auto-foundations-streamliner LINK_PUBLIC ${Boost_LIBRARIES}) -set_target_properties(solvitaire-auto-foundations-streamliner PROPERTIES COMPILE_FLAGS -DAUTO_FOUNDATIONS_STREAMLINER) - -# add a separete executable for the 'both streamliners' version of the solver -add_executable(solvitaire-both-streamliners ${main} ${sources}) -target_link_libraries(solvitaire-both-streamliners LINK_PUBLIC ${Boost_LIBRARIES}) -set_target_properties(solvitaire-both-streamliners PROPERTIES COMPILE_FLAGS "-DSUIT_REDUCTION_STREAMLINER -DAUTO_FOUNDATIONS_STREAMLINER") - # add a separete executable for the 'no pile symmetry' version of the solver add_executable(solvitaire-no-pile-symmetry ${main} ${sources}) target_link_libraries(solvitaire-no-pile-symmetry LINK_PUBLIC ${Boost_LIBRARIES}) diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp index 367caf5..83207fb 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, uint64_t cache_capacity) { +void benchmark::run(const sol_rules &rules, uint64_t cache_capacity, bool streamliners) { cout << "Seed " "| Median/Mean Solution Time(μs)" "| Median/Mean States Searched " @@ -27,7 +27,7 @@ void benchmark::run(const sol_rules &rules, uint64_t cache_capacity) { int total_unsolvable = 0; for(int seed = 1; seed <= 1000; seed++) { - game_state gs(rules, seed); + game_state gs(rules, seed, streamliners); solver sol(gs, cache_capacity); auto start = chrono::steady_clock::now(); diff --git a/src/main/evaluation/benchmark.h b/src/main/evaluation/benchmark.h index 83c9638..70a5b76 100644 --- a/src/main/evaluation/benchmark.h +++ b/src/main/evaluation/benchmark.h @@ -11,7 +11,7 @@ class benchmark { public: - static void run(const sol_rules &rules, uint64_t); + static void run(const sol_rules &rules, uint64_t, bool); }; diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index b246e68..7c83ae8 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -10,6 +10,7 @@ #include "binomial_ci.h" using namespace std; +using boost::optional; /////////////////// // SETUP METHODS // @@ -23,7 +24,7 @@ solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) // PRINTING METHODS // ////////////////////// -void solvability_calc::print_header(long t) const { +void solvability_calc::print_header(long t, bool streamliners) const { cout << "Calculating solvability percentage...\n\n" "Lower Bound" ", Upper Bound" @@ -31,8 +32,9 @@ void solvability_calc::print_header(long t) const { ", Unsolvable" ", Intractable" ", Attempted Seed" - ", Seed State" - ", Time Taken(ms)" + ", Seed State"; + if (streamliners) cout << ", Streamliner Results"; + cout << ", Time Taken(ms)" ", States Searched" ", Unique States Searched" ", Backtracks" @@ -47,7 +49,8 @@ void solvability_calc::print_header(long t) const { cout << fixed << setprecision(3); } -void solvability_calc::print_row(const seed_results& seed_res, sol_result res, set& seeds_in_progress) { +void solvability_calc::print_row(const seed_results& seed_res, sol_result res, + optional streamliner_result, set& seeds_in_progress) { pair interval = binomial_ci::wilson( seed_res.solvable, seed_res.unsolvable, @@ -70,6 +73,19 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s cout << ", unsolvable"; break; } + if (streamliner_result) { + switch (*streamliner_result) { + case sol_result::type::timeout: + cout << ", timed-out"; + break; + case sol_result::type::solved: + cout << ", solved"; + break; + case sol_result::type::unsolvable: + cout << ", unsolvable"; + break; + } + } cout << ", " << res.time.count(); cout << ", " << res.sol_info.states_searched @@ -97,7 +113,8 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s // SOLVING METHODS // ///////////////////// -void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, const vector& resume) { +void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, + bool streamliners, const vector& resume) { vector resume_seeds(begin(resume) + 3, end(resume)); sort(begin(resume_seeds), end(resume_seeds)); @@ -109,7 +126,7 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s mutex results_mutex; millisec timeout(timeout_); - print_header(timeout.count()); + print_header(timeout.count(), streamliners); // Spin off 'cores' threads. Each one runs solve_seed and then print_row repeatedly, taking the seed from am // atomic int @@ -122,7 +139,7 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s futures.push_back(async( launch::async, [¤t_seed, &seed_res, &seeds_in_progress, &results_mutex, seed_count, timeout, sr, cc, i, - resume_seeds](){ + resume_seeds, streamliners](){ int my_seed = resume_seeds.size() > i ? resume_seeds[i] : current_seed++; @@ -131,16 +148,20 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s seeds_in_progress.insert(my_seed); results_mutex.unlock(); - sol_result res = solve_seed(my_seed, timeout, sr, cc); + sol_result res = solve_seed(my_seed, timeout, sr, cc, streamliners); + + optional streamliner_result; + if (streamliners) { + streamliner_result = res.sol_type; + if (res.sol_type == sol_result::type::unsolvable) { + res = solve_seed(my_seed, timeout, sr, cc, false); + } + } results_mutex.lock(); seeds_in_progress.erase(my_seed); - switch (res.sol_type) { - case sol_result::type::solved: seed_res.solvable ++; break; - case sol_result::type::unsolvable: seed_res.unsolvable ++; break; - case sol_result::type::timeout: seed_res.intractable++; break; - } - print_row(seed_res, res, seeds_in_progress); + seed_res.add_result(res.sol_type); + print_row(seed_res, res, streamliner_result, seeds_in_progress); results_mutex.unlock(); my_seed = current_seed++; @@ -152,9 +173,9 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s for (auto& f : futures) f.wait(); } -solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec timeout, - const sol_rules& rules, uint64_t cache_capacity) { - game_state gs(rules, seed); +solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec timeout, const sol_rules& rules, + uint64_t cache_capacity, bool streamliners) { + game_state gs(rules, seed, streamliners); solver sol(gs, cache_capacity); atomic terminate_solver(false); diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index 6ca640d..1c9e800 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -17,7 +17,7 @@ class solvability_calc { public: explicit solvability_calc(const sol_rules&, uint64_t); - void calculate_solvability_percentage(uint64_t, int, uint, const std::vector&); + void calculate_solvability_percentage(uint64_t, int, uint, bool, const std::vector&); private: typedef std::chrono::milliseconds millisec; @@ -32,7 +32,7 @@ private: }; struct seed_results { - seed_results(std::vector); + explicit seed_results(std::vector); void add_result(sol_result::type); std::atomic solvable; @@ -43,11 +43,11 @@ private: }; // Printing methods - void print_header(long) const; - static void print_row(const seed_results&, sol_result, std::set&); + void print_header(long, bool) const; + static void print_row(const seed_results&, sol_result, boost::optional, std::set&); // Solving methods - static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t); + static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t, bool); const sol_rules& rules; const uint64_t cache_capacity; diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index db0c6de..f688c33 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -81,13 +81,7 @@ void cached_game_state::add_card(card c, const game_state& gs) { // If the game is a 'hole-based' game, or suit-reduction is on, reduces // the cached suit of the card where possible - bool is_suit_symmetry = gs.rules.hole; -#ifdef SUIT_REDUCTION_STREAMLINER - is_suit_symmetry = true; -#endif -#ifdef NO_SUIT_SYMMETRY - is_suit_symmetry = false; -#endif + bool is_suit_symmetry = gs.streamliners || gs.rules.hole; if (is_suit_symmetry) { switch (gs.rules.build_pol) { @@ -141,13 +135,7 @@ size_t hasher::hash_value(card const& c) const { // If the game is a 'hole-based' game, or suit-reduction is enabled, hash // the reduced suit - bool is_suit_symmetry = init_gs.rules.hole; -#ifdef SUIT_REDUCTION_STREAMLINER - is_suit_symmetry = true; -#endif -#ifdef NO_SUIT_SYMMETRY - is_suit_symmetry = false; -#endif + bool is_suit_symmetry = init_gs.streamliners || init_gs.rules.hole; uint8_t suit_val; if (is_suit_symmetry) { diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 0b62af4..1bec3fe 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -11,9 +11,7 @@ using std::max; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { -#ifndef AUTO_FOUNDATIONS_STREAMLINER - if (rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) -#endif + if (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) return true; card::suit_t target_suit(target_pile - foundations.front()); diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 0a20f60..5d80d3e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -40,10 +40,12 @@ typedef sol_rules::stock_deal_type sdt; // A private constructor used by both of the public ones. Initializes all of the // piles and pile refs specified by the rules -game_state::game_state(const sol_rules& s_rules) : rules(s_rules), - stock(255), - waste(255), - hole(255) { +game_state::game_state(const sol_rules& s_rules, bool streamliners_) + : rules(s_rules) + , streamliners(streamliners_) + , stock(255) + , waste(255) + , hole (255) { // If there is a hole, creates pile if (rules.hole) { piles.emplace_back(); @@ -100,14 +102,14 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules), } // Constructs an initial game state from a JSON doc -game_state::game_state(const sol_rules& s_rules, const Document& doc) - : game_state(s_rules) { +game_state::game_state(const sol_rules& s_rules, const Document& doc, bool streamliners_) + : game_state(s_rules, streamliners_) { deal_parser::parse(*this, doc); } // Constructs an initial game state from a seed -game_state::game_state(const sol_rules& s_rules, int seed) - : game_state(s_rules) { +game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) + : game_state(s_rules, streamliners_) { vector deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); // If there is a hole, moves the ace of spades to it @@ -183,7 +185,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) game_state::game_state(const sol_rules& s_rules, std::initializer_list il) - : game_state(s_rules) { + : game_state(s_rules, false) { pile::ref pr = 0; for (const pile& p : il) { for (const card c : p.pile_vec) { diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 7c0f9ca..a0a74ea 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -28,9 +28,9 @@ public: /* Constructors */ // Creates a game state representation from a JSON doc - game_state(const sol_rules&, const rapidjson::Document&); + game_state(const sol_rules&, const rapidjson::Document&, bool); // Does the same from a seed - game_state(const sol_rules&, int seed); + game_state(const sol_rules&, int seed, bool); // Does the same but with an initialiser list (useful for testing) game_state(const sol_rules&, std::initializer_list); @@ -58,7 +58,7 @@ public: private: /* Constructors (& helper function) */ - explicit game_state(const sol_rules&); + explicit game_state(const sol_rules&, bool); static std::vector gen_shuffled_deck(int, card::rank_t, bool); /* Pile order logic */ @@ -116,6 +116,7 @@ private: /* Game rules */ const sol_rules rules; + bool streamliners; /* Pile references */ diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 21eec88..ec9a375 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -38,18 +38,20 @@ command_line_helper::command_line_helper() "'solvability', 'benchmark' or list of deals to be solved.") ("classify", "outputs a simple 'solvable/not solvable' " "classification") - ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " + ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " "the cache") ("solvability", po::value(), "calculates the solvability " "percentage of the supplied solitaire game, given a limit for the number of seeds. Must supply " - "either 'random', 'benchmark', 'solvability' or list of deals to be " - "solved.") + "either 'random', 'benchmark', 'solvability' or list of deals to be solved.") ("timeout", po::value(), "adds a per-game timeout to the solvability percentage generation") ("resume", po::value>()->multitoken(), "resumes the solvability percentage calculation from a " "previous run. Must be supplied with the solvability option. " "Syntax: [sol unsol intract in-progress-1 in-progress-2 ...]") ("cores", po::value(), "the number of cores for the solvability percentages to be run across. " - "Must be supplied with the solvability option.") + "Must be supplied with the solvability option.") + ("streamliners", "applies any relevant streamliners to the search. If the --solvability option is supplied," + " runs the streamliner version of the search first and then if unsuccessful, the full" + " search for each seed.") ("benchmark", "outputs performance statistics for the solver on the " "supplied solitaire game. Must supply " "either 'random', 'benchmark', 'solvability' or list of deals to be " @@ -141,6 +143,7 @@ bool command_line_helper::parse(int argc, const char* argv[]) { for (uint i = 0; i < cores; i++) resume.push_back(i); } + streamliners = (vm.count("streamliners") != 0); benchmark = (vm.count("benchmark") != 0); @@ -278,3 +281,7 @@ string command_line_helper::get_describe_game_rules() { bool command_line_helper::get_benchmark() { return benchmark; } + +bool command_line_helper::get_streamliners() { + return streamliners; +} diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index 744935f..c12bb69 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -21,6 +21,7 @@ public: uint get_cores(); bool get_available_game_types(); bool get_benchmark(); + bool get_streamliners(); std::vector get_resume(); uint64_t get_cache_capacity(); std::string get_describe_game_rules(); @@ -51,6 +52,7 @@ private: uint cores; bool available_game_types; bool benchmark; + bool streamliners; uint64_t cache_capacity; uint64_t timeout; }; diff --git a/src/main/main.cpp b/src/main/main.cpp index d5c5dcc..a6ed318 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 gen_rules(command_line_helper&); -void solve_random_game(int, const sol_rules&, bool, uint64_t); -void solve_input_files(vector, const sol_rules&, bool, uint64_t); -void solve_game(const game_state&, bool, uint64_t); +void solve_random_game(int, const sol_rules&, command_line_helper&); +void solve_input_files(vector, const sol_rules&, command_line_helper&); +void solve_game(const game_state&, command_line_helper&); // Decides what to do given supplied command-line options int main(int argc, const char* argv[]) { @@ -52,15 +52,15 @@ int main(int argc, const char* argv[]) { if (clh.get_solvability() > 0) { solvability_calc solv_c(*rules, clh.get_cache_capacity()); solv_c.calculate_solvability_percentage(clh.get_timeout(), clh.get_solvability(), clh.get_cores(), - clh.get_resume()); + clh.get_streamliners(), clh.get_resume()); } // 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(), clh.get_cache_capacity()); + solve_random_game(clh.get_random_deal(), *rules, clh); } // If the benchmark option has been supplied, generates it else if (clh.get_benchmark()) { - benchmark::run(*rules, clh.get_cache_capacity()); + benchmark::run(*rules, clh.get_cache_capacity(), clh.get_streamliners()); } // Otherwise there are supplied input files which should be solved else { @@ -68,8 +68,8 @@ 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(), clh.get_cache_capacity()); + assert(!input_files.empty()); + solve_input_files(input_files, *rules, clh); } return EXIT_SUCCESS; @@ -91,24 +91,23 @@ const optional gen_rules(command_line_helper& clh) { } } -void solve_random_game(int seed, const sol_rules& rules, bool classify, uint64_t cache_capacity) { +void solve_random_game(int seed, const sol_rules& rules, command_line_helper& clh) { LOG_INFO ("Attempting to solve with seed: " << seed << "..."); - game_state gs(rules, seed); - solve_game(gs, classify, cache_capacity); + game_state gs(rules, seed, clh.get_streamliners()); + solve_game(gs, clh); } -void solve_input_files(const vector input_files, const sol_rules& rules, - bool classify, uint64_t cache_capacity) { +void solve_input_files(const vector input_files, const sol_rules& rules, command_line_helper& clh) { for (const string& input_file : input_files) { try { // Reads in the input file to a json doc const Document in_doc = json_helper::get_file_json(input_file); // Attempts to create a game state object from the json - game_state gs(rules, in_doc); + game_state gs(rules, in_doc, clh.get_streamliners()); LOG_INFO ("Attempting to solve " << input_file << "..."); - solve_game(gs, classify, cache_capacity); + solve_game(gs, clh); } catch (const runtime_error& error) { string errmsg = "Error parsing deal file: "; @@ -118,16 +117,16 @@ void solve_input_files(const vector input_files, const sol_rules& rules, } } -void solve_game(const game_state& gs, bool classify, uint64_t cache_capacity) { - solver solv(gs, cache_capacity); +void solve_game(const game_state& gs, command_line_helper& clh) { + solver solv(gs, clh.get_cache_capacity()); bool solution = solv.run() == solver::sol_state::solved; if (solution) { - if (!classify) solv.print_solution(); + if (!clh.get_classify()) solv.print_solution(); cout << "Solved\n"; } else { - if (!classify) cout << "Deal:\n" << gs << "\n"; + if (!clh.get_classify()) cout << "Deal:\n" << gs << "\n"; cout << "No Possible Solution\n"; } -- cgit v1.2.1 From 495ccca135cc139221b4205c7443499fee5ab63e Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 21 Jun 2018 11:53:11 +0100 Subject: Make randomness consistent --- src/main/game/search-state/game_state.cpp | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 5d80d3e..8303516 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -27,9 +27,9 @@ using std::end; using std::rbegin; using std::rend; using std::runtime_error; -using std::default_random_engine; using std::max; using std::ostream; +using std::mt19937; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; @@ -198,28 +198,20 @@ game_state::game_state(const sol_rules& s_rules, // Generates a randomly ordered vector of cards vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, bool two_decks) { - vector values; - vector v_ptrs; - for (int i = 0; i < max_rank * 4; i++) { - values.emplace_back(i); - if (two_decks) values.emplace_back(i); - } - for (auto& v : values) { - v_ptrs.emplace_back(&v); - } - - // Randomly shuffle the pointers - auto rng = default_random_engine(seed+1); - shuffle(begin(v_ptrs), end(v_ptrs), rng); - vector deck; - for (uint8_t *i : v_ptrs) { - auto r = static_cast(((*i) % max_rank) + 1); - card::suit_t s = (*i) / max_rank; - deck.emplace_back(card(s, r)); + + for (int deck_count = 1; deck_count <= (two_decks ? 2 : 1); deck_count++) { + for (card::rank_t rank = 1; rank <= max_rank; rank++) { + for (card::suit_t suit = 0 ; suit < 4; suit++) { + deck.emplace_back(suit, rank); + } + } } assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4))); + + auto rng = mt19937(seed); + shuffle(begin(deck), end(deck), rng); return deck; } -- cgit v1.2.1 From 982c29136530be549f84af290e20523688d7fbdd Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 21 Jun 2018 15:07:07 +0100 Subject: Fixup randomness --- src/main/game/search-state/game_state.cpp | 18 +++++++++++++++++- src/main/game/search-state/game_state.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 8303516..82e413b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -10,6 +10,7 @@ #include #include +#include #include "game_state.h" #include "document.h" @@ -211,10 +212,25 @@ vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4))); auto rng = mt19937(seed); - shuffle(begin(deck), end(deck), rng); + game_state::shuffle(begin(deck), end(deck), rng); return deck; } +template +void game_state::shuffle(RandomIt first, RandomIt last, URBG&& g) { + typedef typename std::iterator_traits::difference_type diff_t; + typedef boost::random::uniform_int_distribution distr_t; + typedef typename distr_t::param_type param_t; + + distr_t D; + diff_t n = last - first; + for (diff_t i = n-1; i > 0; --i) { + using std::swap; + swap(first[i], first[D(g, param_t(0, i))]); + } +} + + //////////////////// // ALTERING STATE // diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index a0a74ea..f335de1 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -60,6 +60,7 @@ private: explicit game_state(const sol_rules&, bool); static std::vector gen_shuffled_deck(int, card::rank_t, bool); + template static void shuffle(RandomIt, RandomIt, URBG&&); /* Pile order logic */ -- cgit v1.2.1