diff options
| author | Charlie Blake | 2018-02-23 11:51:06 +0000 |
|---|---|---|
| committer | GitHub | 2018-02-23 11:51:06 +0000 |
| commit | dd731b92b0a8f984ddbef11d31bc70acfa266510 (patch) | |
| tree | 071d463dc62ec584ef275759f7dbc450027a99d7 /src | |
| parent | Merge pull request #39 from thecharlesblake/update-objectives (diff) | |
| parent | Enable suit reduction for non-foundation games (diff) | |
| download | metasol-dd731b92b0a8f984ddbef11d31bc70acfa266510.tar.gz metasol-dd731b92b0a8f984ddbef11d31bc70acfa266510.zip | |
Merge pull request #40 from thecharlesblake/test-optimisations
Test optimisations
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game/global_cache.cpp | 64 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.cpp | 10 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.h | 2 | ||||
| -rw-r--r-- | src/main/main.cpp | 38 | ||||
| -rw-r--r-- | src/test/foundations_dominance_test.cpp | 5 | ||||
| -rw-r--r-- | src/test/global_cache_test.cpp | 16 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 97 | ||||
| -rw-r--r-- | src/test/test_helper.h | 8 |
8 files changed, 72 insertions, 168 deletions
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 5d2fd79..048cbc9 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -83,23 +83,27 @@ void cached_game_state::add_card(card c, const game_state& gs) { auto& target = data; #endif - switch (gs.rules.build_pol) { -#ifdef NO_CARD_SYMMETRY - default: - target.emplace_back(c); - break; -#else - case pol::SAME_SUIT: - target.emplace_back(c); - break; - case pol::RED_BLACK: - target.emplace_back(c.get_colour(), c.get_rank()); - break; - default: - target.emplace_back(0, c.get_rank()); - break; + // If the game is a 'hole-based' game, or suit-reduction is on, reduces + // the cached suit of the card where possible +#ifndef SUIT_REDUCTION_STREAMLINER + if (gs.rules.hole) { #endif + switch (gs.rules.build_pol) { + case pol::SAME_SUIT: + target.emplace_back(c); + break; + case pol::RED_BLACK: + target.emplace_back(c.get_colour(), c.get_rank()); + break; + default: + target.emplace_back(0, c.get_rank()); + break; + } +#ifndef SUIT_REDUCTION_STREAMLINER + } else { + target.emplace_back(c); } +#endif } void cached_game_state::add_card_divider() { @@ -152,19 +156,25 @@ std::size_t hasher::combine(std::size_t& seed, std::size_t value) const { size_t hasher::hash_value(card const& c) const { boost::hash<uint8_t> boost_hasher; -#ifdef NO_CARD_SYMMETRY - uint8_t suit_val = c.get_suit(); -#else + // If the game is a 'hole-based' game, or suit-reduction is enabled, hash + // the reduced suit uint8_t suit_val; - switch (init_gs.rules.build_pol) { - case pol::SAME_SUIT: - suit_val = c.get_suit(); - break; - case pol::RED_BLACK: - suit_val = c.get_colour(); - break; - default: - suit_val = 0; +#ifndef SUIT_REDUCTION_STREAMLINER + if (init_gs.rules.hole) { +#endif + switch (init_gs.rules.build_pol) { + case pol::SAME_SUIT: + suit_val = c.get_suit(); + break; + case pol::RED_BLACK: + suit_val = c.get_colour(); + break; + default: + suit_val = 0; + } +#ifndef SUIT_REDUCTION_STREAMLINER + } else { + suit_val = c.get_suit(); } #endif diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 2391e3a..465bcd4 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -29,7 +29,9 @@ command_line_helper::command_line_helper() "to be solved. Must supply either 'type' or 'rules' file") ("random", po::value<int>(), "create and solve a random solitaire " "deal based on a seed. Must supply either 'random' or list " - "of deals to be sold."); + "of deals to be sold.") + ("classify", "outputs a simple 'solvable/not solvable' " + "classification"); po::options_description hidden_options("Hidden options"); hidden_options.add_options() @@ -60,6 +62,8 @@ bool command_line_helper::parse(int argc, const char* argv[]) { help = (vm.count("help") != 0); + classify = (vm.count("classify") != 0); + if (vm.count("input-files")) { input_files = vm["input-files"].as<vector<string>>(); } @@ -168,3 +172,7 @@ int command_line_helper::get_random_deal() { bool command_line_helper::get_help() { return help; } + +bool command_line_helper::get_classify() { + return classify; +} diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index fd1468c..9905aff 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -17,6 +17,7 @@ public: const std::string get_rules_file(); int get_random_deal(); bool get_help(); + bool get_classify(); private: bool assess_errors(); @@ -35,6 +36,7 @@ private: std::string rules_file; int random_deal; bool help; + bool classify; }; #endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 975e6c8..be03e02 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -18,9 +18,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&); -void solve_input_files(vector<string>, const sol_rules&); -void solve_game(const game_state&); +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); int main(int argc, const char* argv[]) { @@ -40,11 +40,11 @@ int main(int argc, const char* argv[]) { // If there are no input files, solve a random deal based on the // supplied seed if (input_files.empty()) { - solve_random_game(clh.get_random_deal(), *rules); + solve_random_game(clh.get_random_deal(), *rules, clh.get_classify()); } // Otherwise, solve the input files else { - solve_input_files(input_files, *rules); + solve_input_files(input_files, *rules, clh.get_classify()); } return EXIT_SUCCESS; @@ -65,13 +65,14 @@ const optional<sol_rules> gen_rules(command_line_helper& clh) { } } -void solve_random_game(int seed, const sol_rules& rules) { +void solve_random_game(int seed, const sol_rules& rules, bool classify) { LOG_INFO ("Attempting to solve with seed: " << seed << "..."); game_state gs(rules, seed); - solve_game(gs); + solve_game(gs, classify); } -void solve_input_files(const vector<string> input_files, const sol_rules& rules) { +void solve_input_files(const vector<string> input_files, const sol_rules& rules, + bool classify) { for (const string& input_file : input_files) { try { // Reads in the input file to a json doc @@ -81,7 +82,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); + solve_game(gs, classify); } catch (const runtime_error& error) { string errmsg = "Error parsing deal file: "; @@ -91,15 +92,24 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules) } } -void solve_game(const game_state& gs) { +void solve_game(const game_state& gs, bool classify) { solver solv(gs); bool solution = solv.run(); ProfilerStop(); - if (solution) { - solv.print_solution(); + if (classify) { + if (solution) { + cout << "Solved\n"; + } else { + cout << "No solution\n"; + } } else { - cout << "Deal:\n" << gs << "\nNo Possible Solution\nStates Searched: " - << solv.get_states_searched() << "\n"; + if (solution) { + solv.print_solution(); + } else { + cout << "Deal:\n" << gs + << "\nNo Possible Solution\nStates Searched: " + << solv.get_states_searched() << "\n"; + } } } diff --git a/src/test/foundations_dominance_test.cpp b/src/test/foundations_dominance_test.cpp index 48134f5..77a33ff 100644 --- a/src/test/foundations_dominance_test.cpp +++ b/src/test/foundations_dominance_test.cpp @@ -26,9 +26,4 @@ TEST(FoundationsDominance, AnySuit) { test_helper::run_foundations_dominance_test(pol::ANY_SUIT, { "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", "4S","3D","4D"}); -} - -TEST(FoundationsDominance, NoSolutionChange) { - test_helper::optimisation_validity_test( - test_helper::optimisation::AUTO_FOUNDATION_MOVES); }
\ No newline at end of file diff --git a/src/test/global_cache_test.cpp b/src/test/global_cache_test.cpp index 70a6b33..780146b 100644 --- a/src/test/global_cache_test.cpp +++ b/src/test/global_cache_test.cpp @@ -10,22 +10,6 @@ typedef sol_rules::build_policy pol; -TEST(GlobalCache, BuildSameSuit) { - test_helper::run_card_cache_test(pol::SAME_SUIT, false, false); -} - -TEST(GlobalCache, BuildRedBlack) { - test_helper::run_card_cache_test(pol::RED_BLACK, false, true); -} - -TEST(GlobalCache, BuildAny) { - test_helper::run_card_cache_test(pol::ANY_SUIT, true, true); -} - -TEST(GlobalCache, BuildNone) { - test_helper::run_card_cache_test(pol::NO_BUILD, true, true); -} - TEST(GlobalCache, CommutativeTableauPiles) { sol_rules rules; rules.tableau_pile_count = 3; diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 37fc32d..da618d8 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -26,59 +26,6 @@ bool test_helper::is_solvable(const std::string& input_file, const std::string& return sol.run(); } -void test_helper::run_card_cache_test(sol_rules::build_policy bp, - bool assert_dif_col, - bool assert_same_col) { - sol_rules rules; - rules.tableau_pile_count = 1; - rules.build_pol = bp; - - for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { - for (card::rank_t rank = 1; rank <= 13; rank++) { - // c2 is the same as c_ but with a different suit - const card c = card(suit_idx, rank); - const card c_dif_col = card(uint8_t((suit_idx + 1) % 4), rank); - const card c_same_col = card(uint8_t((suit_idx + 2) % 4), rank); - const card c_dif_rank = card(suit_idx, card::rank_t((rank % 13) + 1)); - - const game_state s = game_state(rules, initializer_list<pile>{{c}}); - const game_state s_dif_suit(rules, initializer_list<pile>{{c_dif_col}}); - const game_state s_same_col(rules, initializer_list<pile>{{c_same_col}}); - const game_state s_dif_rank(rules, initializer_list<pile>{{c_dif_rank}}); - - global_cache cache(s); - - cache.insert(s); - - ASSERT_TRUE(cache.contains(s)) << "Contains self"; - - ASSERT_(assert_dif_col, hasher(s)(cached_game_state(s)) - == hasher(s)(cached_game_state(s_dif_suit))) - << "Insert: " << c.to_string() - << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_dif_col, cache.contains(s_dif_suit)) - << "Insert: " << c.to_string() - << ", Comp: " << c_dif_col.to_string(); - - ASSERT_(assert_same_col, hasher(s)(cached_game_state(s)) - == hasher(s)(cached_game_state(s_same_col))) - << "Insert: " << c.to_string() - << ", Comp: " << c_same_col.to_string(); - ASSERT_(assert_same_col, cache.contains(s_same_col)) - << "Insert: " << c.to_string() - << ", Comp: " << c_same_col.to_string(); - - ASSERT_FALSE(hasher(s)(cached_game_state(s)) - == hasher(s)(cached_game_state(s_dif_rank))) - << "Insert: " << c.to_string() - << ", Comp: " << c_dif_rank.to_string(); - ASSERT_FALSE(cache.contains(s_dif_rank)) - << "Insert: " << c.to_string() - << ", Comp: " << c_dif_rank.to_string(); - } - } -} - void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, std::vector<card> cards) { sol_rules rules = rules_parser::from_preset("simple-free-cell"); @@ -110,47 +57,3 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, } ASSERT_TRUE(n->children.empty()); } - -void test_helper::optimisation_validity_test(optimisation op) { - for (string preset : { - "simple-alpha-star", - "simple-bakers-dozen", - "simple-black-hole", - "simple-canfield", - "simple-flower-garden", - "simple-fortunes-favor", - "simple-free-cell", - "simple-somerset", - "simple-spanish-patience", - "simple-spider" - }) { - vector<bool> solutions_op, solutions_no_op; - - switch (op) { - case optimisation::AUTO_FOUNDATION_MOVES: -#define NO_AUTO_FOUNDATIONS - solutions_no_op = run_many_games(preset); -#undef NO_AUTO_FOUNDATIONS - solutions_op = run_many_games(preset); - break; - } - - ASSERT_TRUE(solutions_op == solutions_no_op) << preset; - } -} - -std::vector<bool> test_helper::run_many_games(std::string preset) { -#ifdef NDEBUG - const int run_count = 2000; -#else - const int run_count = 5; -#endif - - vector<bool> solutions; - for (int seed = 0; seed < run_count; seed++) { - solver sol(game_state(rules_parser::from_preset(preset), seed)); - solutions.push_back(sol.run()); - } - - return solutions; -} diff --git a/src/test/test_helper.h b/src/test/test_helper.h index 24acb5f..b0199af 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -12,19 +12,11 @@ class test_helper { public: - enum class optimisation { - AUTO_FOUNDATION_MOVES - }; - static bool is_solvable(const std::string&, const std::string&); static void run_card_cache_test(sol_rules::build_policy, bool, bool); static void run_foundations_dominance_test(sol_rules::build_policy policy, std::vector<card> cards); - - static void optimisation_validity_test(optimisation); - - static std::vector<bool> run_many_games(std::string); }; |
