aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorCharlie Blake2018-03-08 14:59:30 +0000
committerGitHub2018-03-08 14:59:30 +0000
commitdd97cf54cb0f76679a2323ab599a86fc4f005193 (patch)
treedb04a4886f4f622d4a6cddf65e35ebdd8e37c7a2 /src/main
parentMerge pull request #41 from thecharlesblake/profile-improve (diff)
parentAdd results dir (diff)
downloadmetasol-dd97cf54cb0f76679a2323ab599a86fc4f005193.tar.gz
metasol-dd97cf54cb0f76679a2323ab599a86fc4f005193.zip

Merge pull request #42 from thecharlesblake/solvability-percentages

Solvability percentages

Diffstat (limited to 'src/main')
-rw-r--r--src/main/input-output/input/command_line_helper.cpp40
-rw-r--r--src/main/input-output/input/command_line_helper.h6
-rw-r--r--src/main/main.cpp103
-rw-r--r--src/main/solver/solver.cpp16
-rw-r--r--src/main/solver/solver.h4
5 files changed, 142 insertions, 27 deletions
diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp
index 465bcd4..05cb86e 100644
--- a/src/main/input-output/input/command_line_helper.cpp
+++ b/src/main/input-output/input/command_line_helper.cpp
@@ -28,8 +28,12 @@ command_line_helper::command_line_helper()
"the path to a JSON file describing the rules of the solitaire "
"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.")
+ "deal based on a seed. Must supply either 'random',"
+ "'solvability' or list of deals to be solved.")
+ ("solvability", "calculates the solvability "
+ "percentage of the supplied solitaire game. Must supply "
+ "either 'random', 'solvability' or list of deals to be "
+ "solved.")
("classify", "outputs a simple 'solvable/not solvable' "
"classification");
@@ -82,6 +86,8 @@ bool command_line_helper::parse(int argc, const char* argv[]) {
random_deal = -1;
}
+ solvability = (vm.count("solvability") != 0);
+
// Handle logic error scenarios
return assess_errors();
}
@@ -92,15 +98,15 @@ bool command_line_helper::assess_errors() {
return false;
}
- // The user must either supply input files or a random seed
+ // The user must either supply input files, a random seed, or ask for the
+ // solvability percentage
+ int opt_count = (random_deal != -1) + !input_files.empty() + solvability;
- if (random_deal != -1 && !input_files.empty()) {
- print_rand_plus_input_err();
+ if (opt_count > 1) {
+ print_too_many_opts_error();
return false;
- }
-
- if (input_files.empty() && random_deal == -1) {
- print_no_input_error();
+ } else if (opt_count < 1) {
+ print_no_opts_error();
return false;
}
@@ -135,9 +141,9 @@ void command_line_helper::print_help() {
<< main_options);
}
-void command_line_helper::print_no_input_error() {
- LOG_ERROR ("Error: User must supply input file(s) or the '--random' "
- "option");
+void command_line_helper::print_no_opts_error() {
+ LOG_ERROR ("Error: User must supply input file(s), the '--random' "
+ "option, or the '--solvability' option");
print_help();
}
@@ -147,9 +153,9 @@ void command_line_helper::print_sol_type_rules_error() {
print_help();
}
-void command_line_helper::print_rand_plus_input_err() {
- LOG_ERROR ("Error: User must supply input file(s) or the '--random' option, "
- "not both");
+void command_line_helper::print_too_many_opts_error() {
+ LOG_ERROR ("Error: User must supply input file(s), the '--random' option, "
+ "or the '--solvability' option, not multiple");
print_help();
}
@@ -176,3 +182,7 @@ bool command_line_helper::get_help() {
bool command_line_helper::get_classify() {
return classify;
}
+
+bool command_line_helper::get_solvability() {
+ return solvability;
+}
diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h
index 9905aff..36049bf 100644
--- a/src/main/input-output/input/command_line_helper.h
+++ b/src/main/input-output/input/command_line_helper.h
@@ -18,14 +18,15 @@ public:
int get_random_deal();
bool get_help();
bool get_classify();
+ bool get_solvability();
private:
bool assess_errors();
bool assess_sol_type();
void print_help();
- void print_no_input_error();
void print_sol_type_rules_error();
- void print_rand_plus_input_err();
+ void print_no_opts_error();
+ void print_too_many_opts_error();
boost::program_options::options_description cmdline_options;
boost::program_options::options_description main_options;
@@ -37,6 +38,7 @@ private:
int random_deal;
bool help;
bool classify;
+ bool solvability;
};
#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H
diff --git a/src/main/main.cpp b/src/main/main.cpp
index be03e02..0a0173d 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -1,7 +1,10 @@
+#include <future>
+#include <iomanip>
+#include <math.h>
+
#include <boost/program_options.hpp>
#include <boost/optional.hpp>
#include <rapidjson/document.h>
-
#include <gperftools/profiler.h>
#include "input-output/input/command_line_helper.h"
@@ -21,6 +24,10 @@ const optional<sol_rules> gen_rules(command_line_helper&);
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);
+void calculate_solvability_percentage(const sol_rules&);
+double sol_lower_bound(int, int, int);
+double sol_upper_bound(int, int, int);
+double agrestiCoull(int, int, double, bool);
int main(int argc, const char* argv[]) {
@@ -34,16 +41,21 @@ int main(int argc, const char* argv[]) {
const optional<sol_rules> rules = gen_rules(clh);
if (!rules) return EXIT_FAILURE;
- // Retrieves the input files to be solved
- const vector<string> input_files = clh.get_input_files();
-
- // If there are no input files, solve a random deal based on the
- // supplied seed
- if (input_files.empty()) {
+ // If the user has asked for a solvability percentage, calculates it
+ if (clh.get_solvability()) {
+ calculate_solvability_percentage(*rules);
+ }
+ // 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());
}
- // Otherwise, solve the input files
+ // Otherwise there are supplied input files which should be solved
else {
+ const vector<string> input_files = clh.get_input_files();
+
+ // 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());
}
@@ -113,3 +125,78 @@ void solve_game(const game_state& gs, bool classify) {
}
}
}
+
+void calculate_solvability_percentage(const sol_rules& rules) {
+ cout << "Calculating solvability percentage...\n\n"
+ "[Lower Bound, Upper Bound] | (Solvable/Unsolvable/Intractable) "
+ "| Current seed\n";
+ cout << fixed << setprecision(3);
+
+ int solvable = 0;
+ int unsolvable = 0;
+ int intractable = 0;
+
+ for(int seed = 0; seed < INT_MAX; seed++) {
+ game_state gs(rules, seed);
+ solver sol(gs);
+ atomic<bool> terminate_solver(false);
+
+ future<bool> future = std::async(launch::async,
+ [&sol, &terminate_solver](){
+ return sol.run(terminate_solver);
+ });
+
+ future_status status;
+ do {
+ status = future.wait_for(chrono::seconds(45));
+
+ if (status == future_status::timeout) {
+ terminate_solver = true;
+ } else if (status == future_status::ready) {
+ if (terminate_solver) {
+ intractable++;
+ } else if (future.get()) {
+ solvable++;
+ } else {
+ unsolvable++;
+ }
+ }
+ } while (status != future_status::ready);
+
+ double lower_bound = sol_lower_bound(solvable, unsolvable, intractable);
+ double upper_bound = sol_upper_bound(solvable, unsolvable, intractable);
+
+ cout << "[" << lower_bound * 100 << ", " << upper_bound * 100
+ << "] | ("<< solvable << "/" << unsolvable << "/" << intractable
+ << ") | " << seed << "\n";
+ }
+}
+
+double sol_lower_bound(int solvable, int unsolvable, int intractable) {
+ int n = solvable + unsolvable + intractable;
+ int x = solvable;
+ double z = 1.96;
+ bool lower_bound = true;
+ return agrestiCoull(n, x, z, lower_bound);
+}
+
+double sol_upper_bound(int solvable, int unsolvable, int intractable) {
+ int n = solvable + unsolvable + intractable;
+ int x = solvable + intractable;
+ double z = 1.96;
+ bool lower_bound = false;
+ return agrestiCoull(n, x, z, lower_bound);
+}
+
+double agrestiCoull(int n, int x, double z, bool lower_bound) {
+ double n_ = n + pow(z, 2);
+ double p = (x + pow(z, 2)/2)/n_;
+ double v = z * sqrt((p - pow(p, 2))/n_);
+ if (lower_bound) {
+ double ans = p - v;
+ return ans < 0 ? 0 : ans;
+ } else {
+ double ans = p + v;
+ return ans > 1 ? 1 : ans;
+ }
+}
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index ab24824..463d059 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -23,13 +23,27 @@ solver::solver(const game_state& gs)
, current_node(&root) {
}
+solver::~solver() {
+ // This code looks to be unnecessary, but on alpha-star 6212 without it,
+ // for reasons that I cannot fathom, it segfaults.
+ while (current_node != nullptr) {
+ current_node->children.clear();
+ current_node = current_node->parent;
+ }
+}
+
solver::node::node(node* p, const game_state::move m)
: parent(p), move(m), children() {
}
-bool solver::run() {
+bool solver::run(optional<atomic<bool> &> terminate_solver) {
bool states_exhausted = false;
while(!state.is_solved() && !states_exhausted) {
+ // If the terminate flag was supplied and has been set to true, return
+ if (terminate_solver && *terminate_solver) {
+ return false;
+ }
+
#ifndef NDEBUG
if (current_node->move.is_dominance()) {
LOG_DEBUG("(dominance move)");
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 80d4ef5..2a024ac 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -7,6 +7,7 @@
#include <string>
#include <vector>
+#include <atomic>
#include "../game/global_cache.h"
#include "../game/sol_rules.h"
@@ -23,8 +24,9 @@ public:
};
explicit solver(const game_state&);
+ ~solver();
- bool run();
+ bool run(boost::optional<std::atomic<bool> &> = boost::none);
void print_solution() const;
int get_states_searched() const;
const node& get_search_tree() const;