diff options
| author | thecharlesblake | 2018-03-08 14:46:37 +0000 |
|---|---|---|
| committer | thecharlesblake | 2018-03-08 14:46:37 +0000 |
| commit | 451f136ec56c8405f05fd86da0112a71fce0de31 (patch) | |
| tree | 785962a41d293299d092b9976085fe192c9266d6 | |
| parent | Add command line option to calculate solvability (diff) | |
| download | metasol-451f136ec56c8405f05fd86da0112a71fce0de31.tar.gz metasol-451f136ec56c8405f05fd86da0112a71fce0de31.zip | |
Add solvability code to main
| -rw-r--r-- | src/main/main.cpp | 82 |
1 files changed, 79 insertions, 3 deletions
diff --git a/src/main/main.cpp b/src/main/main.cpp index 690a20b..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" @@ -22,6 +25,9 @@ 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[]) { @@ -121,6 +127,76 @@ void solve_game(const game_state& gs, bool classify) { } void calculate_solvability_percentage(const sol_rules& rules) { - cout << "TODO: calculate solvability percentage"; - cout << rules.hole; + 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; + } } |
