From ddea5865d75213652264ff25f9f09f252b516f75 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 21 Mar 2018 13:25:56 +0000 Subject: Refactor solvability into own class --- src/main/main.cpp | 87 ++----------------------------------------------------- 1 file changed, 3 insertions(+), 84 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index b6fa535..a1d1037 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,7 +1,3 @@ -#include -#include -#include - #include #include #include @@ -12,6 +8,7 @@ #include "input-output/input/json-parsing/rules_parser.h" #include "input-output/output/log_helper.h" #include "solver/solver.h" +#include "solvability/solvability_calc.h" using namespace rapidjson; @@ -24,10 +21,6 @@ const optional gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&, bool, bool); void solve_input_files(vector, const sol_rules&, bool, bool); void solve_game(const game_state&, bool, 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[]) { @@ -43,7 +36,8 @@ int main(int argc, const char* argv[]) { // If the user has asked for a solvability percentage, calculates it if (clh.get_solvability()) { - calculate_solvability_percentage(*rules); + solvability_calc solv_c(*rules); + solv_c.calculate_solvability_percentage(); } // If a random deal seed has been supplied, solves it else if (clh.get_random_deal() != -1) { @@ -137,78 +131,3 @@ void solve_game(const game_state& gs, bool short_sol, bool classify) { cout << "States Searched: " << solv->get_states_searched() << "\n"; cout << "Final Depth: " << solv->get_final_depth() << "\n"; } - -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 terminate_solver(false); - - future future = std::async(launch::async, - [&sol, &terminate_solver](){ - return sol.run(terminate_solver) == solver::sol_state::solved; - }); - - 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; - } -} -- cgit v1.2.1