aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorCharlie Blake2018-02-23 11:51:06 +0000
committerGitHub2018-02-23 11:51:06 +0000
commitdd731b92b0a8f984ddbef11d31bc70acfa266510 (patch)
tree071d463dc62ec584ef275759f7dbc450027a99d7 /src/main
parentMerge pull request #39 from thecharlesblake/update-objectives (diff)
parentEnable suit reduction for non-foundation games (diff)
downloadmetasol-dd731b92b0a8f984ddbef11d31bc70acfa266510.tar.gz
metasol-dd731b92b0a8f984ddbef11d31bc70acfa266510.zip

Merge pull request #40 from thecharlesblake/test-optimisations

Test optimisations

Diffstat (limited to 'src/main')
-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
4 files changed, 72 insertions, 42 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";
+ }
}
}