diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/evaluation/benchmark.cpp | 4 | ||||
| -rw-r--r-- | src/main/evaluation/benchmark.h | 2 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.cpp | 55 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.h | 10 | ||||
| -rw-r--r-- | src/main/game/global_cache.cpp | 16 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.auto_foundation_moves.cpp | 4 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 20 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.h | 7 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.cpp | 15 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.h | 2 | ||||
| -rw-r--r-- | src/main/main.cpp | 37 |
11 files changed, 95 insertions, 77 deletions
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<int>& seeds_in_progress) { +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, @@ -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<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)); @@ -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<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++; @@ -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<bool> 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<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,11 +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); + 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<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 +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<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) { 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<pile>); @@ -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<card> 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<int>(), "sets an upper bound on the number of states allowed in " + ("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 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<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 " @@ -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<int> 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<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[]) { @@ -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<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: "; @@ -118,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"; } |
