From c400250031038c6020c1174b5e1d1e6da1d9f89c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 22 Feb 2018 23:02:05 +0000 Subject: Add --classify option to command line helper --- src/main/input-output/input/command_line_helper.cpp | 10 +++++++++- src/main/input-output/input/command_line_helper.h | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src') 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(), "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>(); } @@ -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 -- cgit v1.2.1 From a72154a12176305753384d4e6b697c9a41866bc4 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 22 Feb 2018 23:02:46 +0000 Subject: Remove optimisation testing from c++ code --- src/test/foundations_dominance_test.cpp | 5 ---- src/test/test_helper.cpp | 44 --------------------------------- src/test/test_helper.h | 8 ------ 3 files changed, 57 deletions(-) (limited to 'src') 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/test_helper.cpp b/src/test/test_helper.cpp index 37fc32d..246de38 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -110,47 +110,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 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 test_helper::run_many_games(std::string preset) { -#ifdef NDEBUG - const int run_count = 2000; -#else - const int run_count = 5; -#endif - - vector 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 cards); - - static void optimisation_validity_test(optimisation); - - static std::vector run_many_games(std::string); }; -- cgit v1.2.1 From bca053ac856431e594cf01dc6fda5e13bf1ecc28 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 22 Feb 2018 23:05:00 +0000 Subject: Add reduce suit streamliner: replace card symmetry --- src/main/game/global_cache.cpp | 18 +++++++++--------- src/main/main.cpp | 38 ++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 5d2fd79..4811853 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -82,13 +82,9 @@ void cached_game_state::add_card(card c, const game_state& gs) { #else auto& target = data; #endif - + switch (gs.rules.build_pol) { -#ifdef NO_CARD_SYMMETRY - default: - target.emplace_back(c); - break; -#else +#ifdef SUIT_REDUCTION_STREAMLINER case pol::SAME_SUIT: target.emplace_back(c); break; @@ -98,6 +94,10 @@ void cached_game_state::add_card(card c, const game_state& gs) { default: target.emplace_back(0, c.get_rank()); break; +#else + default: + target.emplace_back(c); + break; #endif } } @@ -152,9 +152,7 @@ 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 boost_hasher; -#ifdef NO_CARD_SYMMETRY - uint8_t suit_val = c.get_suit(); -#else +#ifdef SUIT_REDUCTION_STREAMLINER uint8_t suit_val; switch (init_gs.rules.build_pol) { case pol::SAME_SUIT: @@ -166,6 +164,8 @@ size_t hasher::hash_value(card const& c) const { default: suit_val = 0; } +#else + uint8_t suit_val = c.get_suit(); #endif auto raw_val = static_cast(suit_val * 13 + c.get_rank()); 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 gen_rules(command_line_helper&); -void solve_random_game(int, const sol_rules&); -void solve_input_files(vector, const sol_rules&); -void solve_game(const game_state&); +void solve_random_game(int, const sol_rules&, bool); +void solve_input_files(vector, 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 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 input_files, const sol_rules& rules) { +void solve_input_files(const vector 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 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 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"; + } } } -- cgit v1.2.1 From 2818331334330b65e94d69f228a13e312f0c5abd Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 23 Feb 2018 11:46:25 +0000 Subject: Remove card symmetry from tests --- src/test/global_cache_test.cpp | 16 ------------- src/test/test_helper.cpp | 53 ------------------------------------------ 2 files changed, 69 deletions(-) (limited to 'src') 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 246de38..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{{c}}); - const game_state s_dif_suit(rules, initializer_list{{c_dif_col}}); - const game_state s_same_col(rules, initializer_list{{c_same_col}}); - const game_state s_dif_rank(rules, initializer_list{{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 cards) { sol_rules rules = rules_parser::from_preset("simple-free-cell"); -- cgit v1.2.1 From fcc2577710349922d82cd37fa09c76ee47d78734 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 23 Feb 2018 11:46:42 +0000 Subject: Enable suit reduction for non-foundation games --- src/main/game/global_cache.cpp | 66 ++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 4811853..048cbc9 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -82,24 +82,28 @@ void cached_game_state::add_card(card c, const game_state& gs) { #else auto& target = data; #endif - - switch (gs.rules.build_pol) { -#ifdef SUIT_REDUCTION_STREAMLINER - 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; -#else - default: - target.emplace_back(c); - 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,20 +156,26 @@ 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 boost_hasher; -#ifdef SUIT_REDUCTION_STREAMLINER + // 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(); } -#else - uint8_t suit_val = c.get_suit(); #endif auto raw_val = static_cast(suit_val * 13 + c.get_rank()); -- cgit v1.2.1