aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt17
-rw-r--r--src/main/evaluation/benchmark.cpp4
-rw-r--r--src/main/evaluation/benchmark.h2
-rw-r--r--src/main/evaluation/binomial_ci.cpp45
-rw-r--r--src/main/evaluation/binomial_ci.h19
-rw-r--r--src/main/evaluation/solvability_calc.cpp130
-rw-r--r--src/main/evaluation/solvability_calc.h18
-rw-r--r--src/main/game/global_cache.cpp16
-rw-r--r--src/main/game/search-state/game_state.auto_foundation_moves.cpp4
-rw-r--r--src/main/game/search-state/game_state.cpp66
-rw-r--r--src/main/game/search-state/game_state.h8
-rw-r--r--src/main/input-output/input/command_line_helper.cpp26
-rw-r--r--src/main/input-output/input/command_line_helper.h4
-rw-r--r--src/main/main.cpp38
14 files changed, 213 insertions, 184 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index dea7de8..73b7fcc 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
@@ -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/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 <cmath>
+#include <algorithm>
+#include "binomial_ci.h"
+
+using std::pow;
+using std::min;
+using std::max;
+using std::pair;
+
+pair<double, double> binomial_ci::wilson(int s_, int f_) {
+ auto s = static_cast<double>(s_);
+ auto f = static_cast<double>(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<double, double> binomial_ci::wilson(int s, int f, int i) {
+ pair<double, double> a = wilson(s, f + i);
+ pair<double, double> b = wilson(s + i, f);
+ return {a.first, b.second};
+}
+
+pair<double, double> 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<double, double> binomial_ci::agresti_coull(int s, int f, int i) {
+ pair<double, double> a = agresti_coull(s, f + i);
+ pair<double, double> 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 <utility>
+
+struct binomial_ci {
+ static std::pair<double, double> wilson(int, int);
+ static std::pair<double, double> wilson(int, int, int);
+ static std::pair<double, double> agresti_coull(int, int);
+ static std::pair<double, double> 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 ffe883b..7c83ae8 100644
--- a/src/main/evaluation/solvability_calc.cpp
+++ b/src/main/evaluation/solvability_calc.cpp
@@ -4,12 +4,13 @@
#include <future>
#include <iomanip>
#include <iostream>
-#include <cmath>
#include "solvability_calc.h"
#include "../solver/solver.h"
+#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,16 +49,15 @@ 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<int>& 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);
+void solvability_calc::print_row(const seed_results& seed_res, sol_result res,
+ optional<sol_result::type> streamliner_result, set<int>& seeds_in_progress) {
+ pair<double, double> interval = binomial_ci::wilson(
+ seed_res.solvable,
+ seed_res.unsolvable,
+ seed_res.intractable);
- cout << lower_bound * 100
- << ", " << upper_bound * 100
+ cout << interval.first * 100
+ << ", " << interval.second * 100
<< ", " << seed_res.solvable
<< ", " << seed_res.unsolvable
<< ", " << seed_res.intractable
@@ -72,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
@@ -99,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(int timeout_, uint cores, const vector<int>& resume) {
+void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores,
+ bool streamliners, const vector<int>& resume) {
vector<int> resume_seeds(begin(resume) + 3, end(resume));
sort(begin(resume_seeds), end(resume_seeds));
@@ -111,7 +126,7 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores
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
@@ -123,25 +138,30 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores
for (uint i = 0; i < cores; i++) {
futures.push_back(async(
launch::async,
- [&current_seed, &seed_res, &seeds_in_progress, &results_mutex, timeout, sr, cc, i, resume_seeds](){
+ [&current_seed, &seed_res, &seeds_in_progress, &results_mutex, seed_count, timeout, sr, cc, i,
+ resume_seeds, streamliners](){
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();
- sol_result res = solve_seed(my_seed, timeout, sr, cc);
+ sol_result res = solve_seed(my_seed, timeout, sr, cc, streamliners);
+
+ optional<sol_result::type> 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++;
@@ -149,12 +169,13 @@ 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,
- 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<bool> terminate_solver(false);
@@ -234,56 +255,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<int>(lround(d)); }
diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h
index ef3492c..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(int timeout, uint cores, const std::vector<int>&);
+ void calculate_solvability_percentage(uint64_t, int, uint, bool, const std::vector<int>&);
private:
typedef std::chrono::milliseconds millisec;
@@ -32,7 +32,7 @@ private:
};
struct seed_results {
- seed_results(std::vector<int>);
+ explicit seed_results(std::vector<int>);
void add_result(sol_result::type);
std::atomic<int> solvable;
@@ -43,19 +43,11 @@ private:
};
// Printing methods
- void print_header(long) const;
- static void print_row(const seed_results&, sol_result, std::set<int>&);
+ void print_header(long, bool) const;
+ static void print_row(const seed_results&, sol_result, boost::optional<sol_result::type>, std::set<int>&);
// 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);
+ 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..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 <boost/functional/hash.hpp>
#include <boost/optional.hpp>
+#include <boost/random.hpp>
#include "game_state.h"
#include "document.h"
@@ -27,9 +28,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;
@@ -40,10 +41,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 +103,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<card> 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 +186,7 @@ game_state::game_state(const sol_rules& s_rules, int seed)
game_state::game_state(const sol_rules& s_rules,
std::initializer_list<pile> 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) {
@@ -196,31 +199,38 @@ game_state::game_state(const sol_rules& s_rules,
// Generates a randomly ordered vector of cards
vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank,
bool two_decks) {
- vector<uint8_t> values;
- vector<uint8_t*> 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<card> deck;
- for (uint8_t *i : v_ptrs) {
- auto r = static_cast<card::rank_t>(((*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);
+ game_state::shuffle(begin(deck), end(deck), rng);
return deck;
}
+template<class RandomIt, class URBG>
+void game_state::shuffle(RandomIt first, RandomIt last, URBG&& g) {
+ typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
+ typedef boost::random::uniform_int_distribution<diff_t> 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 7c0f9ca..f335de1 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<pile>);
@@ -58,8 +58,9 @@ public:
private:
/* Constructors (& helper function) */
- explicit game_state(const sol_rules&);
+ explicit game_state(const sol_rules&, bool);
static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
+ template<class RandomIt, class URBG> static void shuffle(RandomIt, RandomIt, URBG&&);
/* Pile order logic */
@@ -116,6 +117,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 2abef54..ec9a375 100644
--- a/src/main/input-output/input/command_line_helper.cpp
+++ b/src/main/input-output/input/command_line_helper.cpp
@@ -41,14 +41,17 @@ command_line_helper::command_line_helper()
("cache-capacity", po::value<uint64_t>(), "sets an upper bound on the number of states allowed in "
"the cache")
("solvability", po::value<int>(), "calculates the solvability "
- "percentage of the supplied solitaire game, given a timeout in milliseconds. Must supply "
- "either 'random', 'benchmark', 'solvability' or list of deals to be "
- "solved.")
+ "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<uint64_t>(), "adds a per-game timeout to the solvability percentage generation")
("resume", po::value<vector<int>>()->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<uint>(), "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 "
@@ -121,6 +124,12 @@ bool command_line_helper::parse(int argc, const char* argv[]) {
solvability = -1;
}
+ if (vm.count("timeout")) {
+ timeout = vm["timeout"].as<uint64_t>();
+ } else {
+ timeout = 604800000; // 1 week in milliseconds
+ }
+
if (vm.count("cores")) {
cores = vm["cores"].as<uint>();
} else {
@@ -134,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);
@@ -248,6 +258,10 @@ int command_line_helper::get_solvability() {
return solvability;
}
+uint64_t command_line_helper::get_timeout() {
+ return timeout;
+}
+
vector<int> command_line_helper::get_resume() {
return resume;
}
@@ -267,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 3ce239d..c12bb69 100644
--- a/src/main/input-output/input/command_line_helper.h
+++ b/src/main/input-output/input/command_line_helper.h
@@ -21,9 +21,11 @@ public:
uint get_cores();
bool get_available_game_types();
bool get_benchmark();
+ bool get_streamliners();
std::vector<int> get_resume();
uint64_t get_cache_capacity();
std::string get_describe_game_rules();
+ uint64_t get_timeout();
private:
bool assess_errors();
@@ -50,7 +52,9 @@ private:
uint cores;
bool available_game_types;
bool benchmark;
+ bool streamliners;
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..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<sol_rules> gen_rules(command_line_helper&);
-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);
+void solve_random_game(int, const sol_rules&, command_line_helper&);
+void solve_input_files(vector<string>, 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[]) {
@@ -51,15 +51,16 @@ 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_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 {
@@ -67,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;
@@ -90,24 +91,23 @@ const optional<sol_rules> 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<string> input_files, const sol_rules& rules,
- bool classify, uint64_t cache_capacity) {
+void solve_input_files(const vector<string> 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: ";
@@ -117,16 +117,16 @@ void solve_input_files(const vector<string> 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";
}