aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt13
-rw-r--r--build-instructions.md2
-rw-r--r--optimisation-test.py71
-rw-r--r--src/main/game/global_cache.cpp64
-rw-r--r--src/main/input-output/input/command_line_helper.cpp10
-rw-r--r--src/main/input-output/input/command_line_helper.h2
-rw-r--r--src/main/main.cpp38
-rw-r--r--src/test/foundations_dominance_test.cpp5
-rw-r--r--src/test/global_cache_test.cpp16
-rw-r--r--src/test/test_helper.cpp97
-rw-r--r--src/test/test_helper.h8
11 files changed, 148 insertions, 178 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0dace0a..ff12767 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -133,21 +133,16 @@ endif()
add_executable(solvitaire ${main} ${sources})
target_link_libraries(solvitaire LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler)
-# add a separete executable for the 'no card symmetry' version of the solver
-add_executable(solvitaire-no-card-symmetry ${main} ${sources})
-target_link_libraries(solvitaire-no-card-symmetry LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler)
-set_target_properties(solvitaire-no-card-symmetry PROPERTIES COMPILE_FLAGS -DNO_CARD_SYMMETRY)
+# 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} -lprofiler)
+set_target_properties(solvitaire-suit-reduction-streamliner PROPERTIES COMPILE_FLAGS -DSUIT_REDUCTION_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} -lprofiler)
set_target_properties(solvitaire-no-pile-symmetry PROPERTIES COMPILE_FLAGS -DNO_PILE_SYMMETRY)
-# add a separete executable for the 'no symmetry' version of the solver
-add_executable(solvitaire-no-symmetry ${main} ${sources})
-target_link_libraries(solvitaire-no-symmetry LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler)
-set_target_properties(solvitaire-no-symmetry PROPERTIES COMPILE_FLAGS "-DNO_CARD_SYMMETRY -DNO_PILE_SYMMETRY")
-
# add a separete executable for the 'no reduced state' version of the solver
add_executable(solvitaire-no-reduced-state ${main} ${sources})
target_link_libraries(solvitaire-no-reduced-state LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler)
diff --git a/build-instructions.md b/build-instructions.md
index b2a8b2d..7a76227 100644
--- a/build-instructions.md
+++ b/build-instructions.md
@@ -50,7 +50,7 @@ Note: this doesn't have to (and probably shouldn't) be run from within the proje
## Profiling
-env CPUPROFILE=solvitaire.prof CPUPROFILESIGNAL=12 CPUPROFILE_FREQUENCY=1000 cmake-build-release/bin/solvitaire --type black-hole --random 4 &
+env CPUPROFILE=solvitaire.prof CPUPROFILESIGNAL=12 CPUPROFILE_FREQUENCY=1000 cmake-build-release/bin/solvitaire --type black-hole --random 4 --classify &
killall -12 solvitaire (start profiling)
killall -12 solvitaire (stop profiling)
pprof --callgrind src/ solvitaire.prof.0 > solvitaire.callgrind (ignore any warnings)
diff --git a/optimisation-test.py b/optimisation-test.py
new file mode 100644
index 0000000..6f936ef
--- /dev/null
+++ b/optimisation-test.py
@@ -0,0 +1,71 @@
+import sys
+from subprocess import run, CalledProcessError, DEVNULL, PIPE, Popen
+
+runCount = 7500
+
+# Loops through each canonical solitaire
+
+for optimisation in ["-no-reduced-state",
+ "-no-pile-symmetry",
+ "-no-auto-foundations"]:
+ for preset in [
+ "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"]:
+
+ differs = []
+
+ for attempt in range(runCount):
+ try:
+ op_p = Popen("./cmake-build-release/bin/solvitaire" + optimisation
+ + " --type " + preset + " --random " + str(attempt)
+ + " --classify",
+ shell=True,
+ stdout=PIPE,
+ stderr=DEVNULL)
+ out_str = op_p.stdout.readline()
+ if out_str == b'Solved\n':
+ op_res = True
+ elif out_str == b'No solution\n':
+ op_res = False
+ else:
+ raise Exception('Invalid return value!')
+
+ no_op_p = Popen("./cmake-build-release/bin/solvitaire"
+ + " --type " + preset + " --random " + str(attempt)
+ + " --classify",
+ shell=True,
+ stdout=PIPE,
+ stderr=DEVNULL)
+ out_str = no_op_p.stdout.readline()
+ if out_str == b'Solved\n':
+ no_op_res = True
+ elif out_str == b'No solution\n':
+ no_op_res = False
+ else:
+ raise Exception('Invalid return value!')
+
+ if op_res != no_op_res:
+ differs.append(attempt)
+
+ except CalledProcessError:
+ print("Error running " + preset + " with seed " + str(attempt) + "!")
+ sys.exit()
+ except:
+ print("Unknown error in script")
+ sys.exit()
+
+ run("pkill solvitaire", shell=True)
+
+ if not differs :
+ print(preset + ": No difference in solutions for " + optimisation)
+ else :
+ print(preset + ": Optimisation " + optimisation + " differs for the following seeds:")
+ print(differs)
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);
};