From 74764b8f8a17cb4c0379380acfe5152d60e80f2a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 5 Apr 2018 22:16:10 +0100 Subject: Add benchmark cmd line option --- src/main/evaluation/benchmark.cpp | 64 ++++ src/main/evaluation/benchmark.h | 17 + src/main/evaluation/solvability_calc.cpp | 350 +++++++++++++++++++++ src/main/evaluation/solvability_calc.h | 69 ++++ .../input-output/input/command_line_helper.cpp | 26 +- src/main/input-output/input/command_line_helper.h | 2 + src/main/input-output/input/sol_preset_types.cpp | 69 +--- src/main/main.cpp | 10 +- src/main/solvability/solvability_calc.cpp | 347 -------------------- src/main/solvability/solvability_calc.h | 69 ---- src/main/solver/solver.cpp | 6 + src/main/solver/solver.h | 2 + 12 files changed, 544 insertions(+), 487 deletions(-) create mode 100644 src/main/evaluation/benchmark.cpp create mode 100644 src/main/evaluation/benchmark.h create mode 100644 src/main/evaluation/solvability_calc.cpp create mode 100644 src/main/evaluation/solvability_calc.h delete mode 100644 src/main/solvability/solvability_calc.cpp delete mode 100644 src/main/solvability/solvability_calc.h (limited to 'src') diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp new file mode 100644 index 0000000..79e2edc --- /dev/null +++ b/src/main/evaluation/benchmark.cpp @@ -0,0 +1,64 @@ +// +// Created by thecharlesblake on 4/5/18. +// + +#include +#include + +#include "benchmark.h" +#include "../game/game_state.h" +#include "../solver/solver.h" + +using namespace std; +typedef chrono::milliseconds millisec; + +void benchmark::run(const sol_rules &rules) { + cout << "Seed " + "| Median Solution Time(ms) " + "| Median States Searched " + "| Median (Non-Zero) Solution Depth " + "| Median Backtracks " + "| Solvable/Unsolvable "; + + multiset sol_times; + multiset states_searched; + multiset sol_depths; + multiset backtracks; + int total_solvable = 0; + int total_unsolvable = 0; + + for(int seed = 1; seed < INT_MAX; seed++) { + game_state gs(rules, seed); + solver sol(gs); + + auto start = chrono::steady_clock::now(); + solver::sol_state result = sol.run(); + auto end = chrono::steady_clock::now(); + millisec elapsed_millis = + chrono::duration_cast(end - start); + + sol_times.insert(static_cast(elapsed_millis.count())); + states_searched.insert(sol.get_states_searched()); + if (sol.get_final_depth() != 0) sol_depths.insert(sol.get_final_depth()); + backtracks.insert(sol.get_backtrack_count()); + switch (result) { + case solver::sol_state::solved: + total_solvable++; + break; + case solver::sol_state::unsolvable: + total_unsolvable++; + break; + case solver::sol_state::cutoff: + assert(false); + break; + } + + cout << "\n" << seed + << " | " << *next(begin(sol_times), (sol_times.size())/2) + << " | " << *next(begin(states_searched), (states_searched.size())/2) + << " | " << *next(begin(sol_depths), (sol_depths.size())/2) + << " | " << *next(begin(backtracks), (backtracks.size())/2) + << " | " << total_solvable + << "/" << total_unsolvable; + } +} diff --git a/src/main/evaluation/benchmark.h b/src/main/evaluation/benchmark.h new file mode 100644 index 0000000..f934bba --- /dev/null +++ b/src/main/evaluation/benchmark.h @@ -0,0 +1,17 @@ +// +// Created by thecharlesblake on 4/5/18. +// + +#ifndef SOLVITAIRE_BENCHMARK_H +#define SOLVITAIRE_BENCHMARK_H + + +#include "../game/sol_rules.h" + +class benchmark { +public: + static void run(const sol_rules &rules); +}; + + +#endif //SOLVITAIRE_BENCHMARK_H diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp new file mode 100644 index 0000000..98f23bd --- /dev/null +++ b/src/main/evaluation/solvability_calc.cpp @@ -0,0 +1,350 @@ +// +// Created by thecharlesblake on 3/21/18. +// +#include +#include +#include +#include + +#include "solvability_calc.h" +#include "../solver/solver.h" + +using namespace std; + +/////////////////// +// SETUP METHODS // +/////////////////// + +const double solvability_calc::coeff_ewma_weight = 0.7; + +solvability_calc::solvability_calc(const sol_rules& r) : rules(r) { +} + +////////////////////// +// PRINTING METHODS // +////////////////////// + +void solvability_calc::print_header(long t) const { + cout << "Calculating solvability percentage...\n\n" + "[Lower Bound, Upper Bound] | Solvable/Unsolvable/Intractable " + "| Current seed | (Est. Confidence Interval: Continue/Re-eval)" + "\n--- Timeout = " << t << " milliseconds ---"; + cout << fixed << setprecision(3); +} + +void solvability_calc::print_row(const seed_results& seed_res, int seed, + double cont, double re_eval) const { + double lower_bound = sol_lower_bound(seed_res.solvables(), + seed_res.unsolvables(), + seed_res.intractables()); + double upper_bound = sol_upper_bound(seed_res.solvables(), + seed_res.unsolvables(), + seed_res.intractables()); + + if (cont != -1 && re_eval !=-1) { + cout << "\n" + << "[" << lower_bound * 100 + << ", " << upper_bound * 100 + << "] | " << seed_res.solvables() + << "/" << seed_res.unsolvables() + << "/" << seed_res.intractables() + << " | " << seed + << " | ("; + if (seed_res.intractables() == 0) { + cout << "N/A"; + } else { + cout << cont * 100 + << ", " << re_eval * 100; + } + cout << ")"; + } else { + cout << "\n" + << "[-- re-eval --] | " << seed_res.solvables() + << "/" << seed_res.unsolvables() + << "/" << seed_res.intractables() + << " | " << seed; + } +} + +void solvability_calc::print_begin_re_eval_msg(long t) const { + cout << "\n--- Timeout doubled to " << t + << " ms. Re-evaluating intractable seeds ---"; +} + +void solvability_calc::print_end_re_eval_msg() const { + cout << "\n--- Re-evaluation complete ---"; +} + + +///////////////////// +// SOLVING METHODS // +///////////////////// + +void solvability_calc::calculate_solvability_percentage() { + int current_seed = 0; + millisec timeout(500); + millisec max_timeout(200000); + inc_t_coeffs coeffs(0.4, 0.0, 1.0); + + print_header(timeout.count()); + + seed_results seed_res; + while(current_seed < INT_MAX) { + + double cont_ci_sz = continue_ci_size(coeffs, seed_res, timeout); + double reeval_ci_sz = inc_timeout_ci_size(coeffs, seed_res); + print_row(seed_res, current_seed, cont_ci_sz, reeval_ci_sz); + + while (seed_res.intractables() == 0 + || cont_ci_sz < reeval_ci_sz + || timeout == max_timeout) { + solve_seed(current_seed, timeout, seed_res); + + cont_ci_sz = continue_ci_size(coeffs, seed_res, timeout); + reeval_ci_sz = inc_timeout_ci_size(coeffs, seed_res); + + print_row(seed_res, current_seed, cont_ci_sz, reeval_ci_sz); + current_seed++; + } + + timeout = min(timeout * 2, max_timeout); + + seed_results intract_seed_res; + print_begin_re_eval_msg(timeout.count()); + for (auto intract_it = begin(seed_res.intractable) + ; intract_it != end (seed_res.intractable) + ; intract_it++) { + solve_seed(*intract_it, timeout, intract_seed_res); + print_row(intract_seed_res, *intract_it, -1, -1); + } + print_end_re_eval_msg(); + + seed_res.add_intractable_results(intract_seed_res); + coeffs.update(intract_seed_res, timeout); + } +} + +void solvability_calc::solve_seed(int seed, millisec timeout, + seed_results& seed_res) { + 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(timeout); + auto start = chrono::steady_clock::now(); + + if (status == future_status::timeout) { + terminate_solver = true; + } else if (status == future_status::ready) { + + if (terminate_solver) { + seed_res.intractable.push_back(seed); + } else { + auto end = chrono::steady_clock::now(); + millisec elapsed_millis = + chrono::duration_cast(end - start); + + if (future.get()) { + seed_res.solvable.push_back(seed); + seed_res.solvable_time += elapsed_millis; + } else { + seed_res.unsolvable.push_back(seed); + seed_res.unsolvable_time += elapsed_millis; + } + } + } + } while (status != future_status::ready); +} + +double solvability_calc::continue_ci_size(inc_t_coeffs coeffs, + const seed_results &seed_res, + millisec timeout) const { + // If there are no intractable seeds yet, returns 0 (i.e. min interval), + // as continuing is only option + if (seed_res.intractables() == 0) return 0; + + // Calculates the expected time to solve the intractable seeds with the + // timeout doubled + millisec inc_t_time( + rnd(seed_res.intractables() * coeffs.time * 2 * timeout.count()) + ); + + // Calculates the expected number of seeds we would have attempted to solve + // were we to continue our search (i.e. without doubling timeout) in the + // above time period + millisec time_so_far = seed_res.solvable_time + + seed_res.unsolvable_time + + seed_res.intractables() * timeout; + int total_seeds = seed_res.solvables() + seed_res.unsolvables() + seed_res.intractables(); + auto total_seeds_after = static_cast(total_seeds * (1 + inc_t_time / time_so_far)); + + // Use what we know so far about the solvable/unsolvable instances to + // estimate the mean solvable/unsolvable + double mean_solvable = agresti_coull_mean(seed_res.solvables() , total_seeds); + double mean_unsolvable = agresti_coull_mean(seed_res.unsolvables() , total_seeds); + double mean_intractable = agresti_coull_mean(seed_res.intractables(), total_seeds); + + // Normalise + double mean_totals = mean_solvable + mean_unsolvable + mean_intractable; + mean_solvable /= mean_totals; mean_unsolvable /= mean_totals; mean_intractable /= mean_totals; + + // Calculates the expected number of solvable/unsolvable/intractable if we continue + // for the length of time that would have been used to solve the intractable + // seeds with the timeout doubled + int total_solvable = rnd(mean_solvable * total_seeds_after); + int total_unsolvable = rnd(mean_unsolvable * total_seeds_after); + int total_intractable = rnd(mean_intractable * total_seeds_after); + + // Given the above hypothetical values, calculates the size of the + // confidence interval we would be left with and returns it + return sol_ci_size(total_solvable, total_unsolvable, total_intractable); +} + +double solvability_calc::inc_timeout_ci_size(inc_t_coeffs coeffs, + const seed_results &seed_res) +const { + // Calculates the expected number of solvable/unsolvable/intractable + // if we run the solver on the intractable instances with double the timeout + int expected_solvable = rnd(coeffs.solvable * seed_res.intractables()); + int expected_unsolvable = rnd(coeffs.unsolvable * seed_res.intractables()); + int expected_intractable = rnd(coeffs.intractable * seed_res.intractables()); + + // Given the above values, calculates the total number of + // solvable/unsolvable/intractable we would expect to have afterwards + int total_solvable = seed_res.solvables() + expected_solvable ; + int total_unsolvable = seed_res.unsolvables() + expected_unsolvable ; + int total_intractable = expected_intractable; + + // Given the above hypothetical values, calculates the size of the + // confidence interval we would be left with and returns it + return sol_ci_size(total_solvable, total_unsolvable, total_intractable); +} + + +//////////////////////// +// SEED RESULTS CLASS // +//////////////////////// + +solvability_calc::seed_results::seed_results() + : solvable_time(0), unsolvable_time(0) { +} + +int solvability_calc::seed_results::solvables() const { + return static_cast(solvable.size()); +} + +int solvability_calc::seed_results::unsolvables() const { + return static_cast(unsolvable.size()); +} + +int solvability_calc::seed_results::intractables() const { + return static_cast(intractable.size()); +} + +void solvability_calc::seed_results::add_intractable_results( + const seed_results &ir) { + solvable .insert(end(solvable) , begin(ir.solvable) , end(ir.solvable )); + unsolvable.insert(end(unsolvable), begin(ir.unsolvable), end(ir.unsolvable)); + solvable_time += ir.solvable_time ; + unsolvable_time += ir.unsolvable_time; + + intractable = ir.intractable; +} + +solvability_calc::inc_t_coeffs::inc_t_coeffs(double solv, double intract, + double ti) + : solvable(solv), unsolvable(1-solv-intract), intractable(intract), + time(ti) { +} + +// Updates the coefficients that represent the expected results of running +// the solver on the intractable seeds with a doubled timeout. These updates +// consist of calculating exponentially-wieghted moving averages. +void solvability_calc::inc_t_coeffs::update(const seed_results &res, + solvability_calc::millisec timeout) { + int total_seeds = res.solvables() + res.unsolvables() + res.intractables(); + + double mean_solvable = agresti_coull_mean(res.solvables() , total_seeds); + double mean_unsolvable = agresti_coull_mean(res.unsolvables() , total_seeds); + double mean_intractable = agresti_coull_mean(res.intractables(), total_seeds); + + // Normalise + double mean_totals = mean_solvable + mean_unsolvable + mean_intractable; + mean_solvable /= mean_totals; mean_unsolvable /= mean_totals; mean_intractable /= mean_totals; + + // The time taken if all intractable instances were still intractable with + // the doubled timeout + millisec all_timeout_time = timeout * total_seeds; + millisec total_time = res.solvable_time + + res.unsolvable_time + + res.intractables() * timeout; + double time_frac = total_time / all_timeout_time; + + double w = coeff_ewma_weight, w_ = 1 - w; + solvable = w * mean_solvable + w_ * solvable ; + unsolvable = w * mean_unsolvable + w_ * unsolvable ; + intractable = w * mean_intractable + w_ * intractable; + time = w * time_frac + w_ * time ; +} + + +///////////////////////// +// CALCULATION METHODS // +///////////////////////// + +double solvability_calc::sol_lower_bound(int solvables, + int unsolvables, + int intractables) const { + int x = solvables; + int n = solvables + unsolvables + intractables; + bool lower_bound = true; + return agresti_coull(x, n, lower_bound); +} + +double solvability_calc::sol_upper_bound(int solvables, + int unsolvables, + int intractables) const { + int x = solvables + intractables; + int n = solvables + unsolvables + intractables; + bool lower_bound = false; + return agresti_coull(x, n, lower_bound); +} + +double solvability_calc::sol_ci_size(int solvables, + int unsolvables, + int intractables) const { + return sol_upper_bound(solvables, unsolvables, intractables) + - sol_lower_bound(solvables, unsolvables, intractables); +} + +double solvability_calc::agresti_coull(int x, int n, bool lower_bound) { + double z = 1.96; + double n_ = n + pow(z, 2); + double p = (x + pow(z, 2)/2)/n_; + double half_ci = z * sqrt((p - pow(p, 2))/n_); + if (lower_bound) { + double ans = p - half_ci; + return ans < 0 ? 0 : ans; + } else { + double ans = p + half_ci; + return ans > 1 ? 1 : ans; + } +} + +double solvability_calc::agresti_coull_mean(int x, int n) { + double z = 1.96; + double n_ = n + pow(z, 2); + double p = (x + pow(z, 2)/2)/n_; + return p; +} + +int solvability_calc::rnd(double d) { return static_cast(lround(d)); } diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h new file mode 100644 index 0000000..ec9cda2 --- /dev/null +++ b/src/main/evaluation/solvability_calc.h @@ -0,0 +1,69 @@ +// +// Created by thecharlesblake on 3/21/18. +// + +#ifndef SOLVITAIRE_SOLVABILITY_H +#define SOLVITAIRE_SOLVABILITY_H + +#include + +#include "../game/sol_rules.h" + +class solvability_calc { +public: + explicit solvability_calc(const sol_rules&); + + void calculate_solvability_percentage(); + +private: + typedef std::chrono::milliseconds millisec; + + struct seed_results { + seed_results(); + int solvables() const; + int unsolvables() const; + int intractables() const; + void add_intractable_results(const seed_results&); + + std::vector solvable; + std::vector unsolvable; + std::vector intractable; + millisec solvable_time; + millisec unsolvable_time; + }; + + struct inc_t_coeffs { + inc_t_coeffs(double, double, double); + double solvable; // Expected solvable if we double the timeout + double unsolvable; // Expected unsolvable if we double the timeout + double intractable; // Expected intractable if we double the timeout + double time; // Expected time taken if we double the timeout (as + // fraction of timeout) + void update(const seed_results&, millisec); + }; + + // Printing methods + void print_header(long) const; + void print_row(const seed_results&, int, double, double) const; + void print_begin_re_eval_msg(long) const; + void print_end_re_eval_msg() const; + + // Solving methods + void solve_seed(int, millisec, seed_results&); + double continue_ci_size(inc_t_coeffs, const seed_results &, millisec) const; + double inc_timeout_ci_size(inc_t_coeffs, const seed_results &) const; + + // Calculation methods + double sol_lower_bound(int, int, int) const; + double sol_upper_bound(int, int, int) const; + double sol_ci_size(int, int, int) const; + static double agresti_coull(int, int, bool); + static double agresti_coull_mean(int, int); + static int rnd(double); + + const sol_rules& rules; + const static double coeff_ewma_weight; +}; + + +#endif //SOLVITAIRE_SOLVABILITY_H diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 5518f1f..e3af0cd 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -35,15 +35,19 @@ command_line_helper::command_line_helper() "to be solved. Must supply either 'type' or 'custom-rules' option") ("random", po::value(), "create and solve a random solitaire " "deal based on a seed. Must supply either 'random'," - "'solvability' or list of deals to be solved.") + "'solvability', 'benchmark' or list of deals to be solved.") + ("classify", "outputs a simple 'solvable/not solvable' " + "classification") ("shortest-sols", "for each instance returns the shortest possible " "solution. cannot be supplied alongside '--solvability'") ("solvability", "calculates the solvability " "percentage of the supplied solitaire game. Must supply " - "either 'random', 'solvability' or list of deals to be " + "either 'random', 'benchmark', 'solvability' or list of deals to be " "solved.") - ("classify", "outputs a simple 'solvable/not solvable' " - "classification"); + ("benchmark", "outputs performance statistics for the solver on the " + "supplied solitaire game. Must supply " + "either 'random', 'benchmark', 'solvability' or list of deals to be " + "solved."); po::options_description hidden_options("Hidden options"); hidden_options.add_options() @@ -102,6 +106,8 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solvability = (vm.count("solvability") != 0); + benchmark = (vm.count("benchmark") != 0); + shortest_sols = (vm.count("shortest-sols") != 0); // Handle logic error scenarios @@ -117,8 +123,8 @@ bool command_line_helper::assess_errors() { if (available_game_types || !describe_game_rules.empty()) return true; // 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; + // solvability percentage, or benchmark + int opt_count = (random_deal != -1) + !input_files.empty() + solvability + benchmark; if (opt_count > 1) { print_too_many_opts_error(); @@ -166,7 +172,7 @@ void command_line_helper::print_help() { void command_line_helper::print_no_opts_error() { LOG_ERROR ("Error: User must supply input file(s), the '--random' " - "option, or the '--solvability' option"); + "option, the 'benchmark' option, or the '--solvability' option"); print_help(); } @@ -177,7 +183,7 @@ void command_line_helper::print_sol_type_rules_error() { } void command_line_helper::print_too_many_opts_error() { - LOG_ERROR ("Error: User must supply input file(s), the '--random' option, " + LOG_ERROR ("Error: User must supply input file(s), the '--random' option, the 'benchmark' option,, " "or the '--solvability' option, not multiple"); print_help(); } @@ -226,3 +232,7 @@ string command_line_helper::get_describe_game_rules() { bool command_line_helper::get_short_sols() { return shortest_sols; } + +bool command_line_helper::get_benchmark() { + return benchmark; +} diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index 8773fd8..e436f13 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -21,6 +21,7 @@ public: bool get_solvability(); bool get_short_sols(); bool get_available_game_types(); + bool get_benchmark(); std::string get_describe_game_rules(); private: @@ -46,6 +47,7 @@ private: bool solvability; bool shortest_sols; bool available_game_types; + bool benchmark; }; #endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 406be94..08f79e7 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -228,7 +228,8 @@ map sol_preset_types::mp = { { "tableau piles": { "count": 4, - "build policy": "red-black" + "build policy": "red-black", + "move built group": true }, "stock size": 34, "reserve size": 14, @@ -244,7 +245,8 @@ map sol_preset_types::mp = { { "tableau piles": { "count": 3, - "build policy": "red-black" + "build policy": "red-black", + "move built group": true }, "max rank": 3, "stock size": 1, @@ -377,26 +379,7 @@ map sol_preset_types::mp = { }, { - "klondike", - - R"( -{ - "tableau piles": { - "count": 7, - "build policy": "red-black", - "spaces policy": "kings", - "diagonal deal": true - }, - "stock size": 24, - "stock deal count": 1, - "stock redeal": false -} -)" - - }, - - { - "klondike-built-group", + "klondike-deal-1", R"( { @@ -416,26 +399,7 @@ map sol_preset_types::mp = { }, { - "klondike-deal-3", - - R"( -{ - "tableau piles": { - "count": 7, - "build policy": "red-black", - "spaces policy": "kings", - "diagonal deal": true - }, - "stock size": 24, - "stock deal count": 3, - "stock redeal": true -} -)" - - }, - - { - "klondike-deal-3-built-group", + "klondike", R"( { @@ -708,7 +672,7 @@ map sol_preset_types::mp = { "move built group": true, "spaces policy": "kings" }, - "cells": 4, + "cells": 4 } )" @@ -731,7 +695,7 @@ map sol_preset_types::mp = { }, { - "chamelion", + "chameleon", R"( { @@ -841,23 +805,6 @@ map sol_preset_types::mp = { "foundations removable": true, "reserve size": 7 } -)" - - }, - { - "king-albert-kings-in-spaces", - R"( - -{ - "tableau piles": { - "count": 9, - "build policy": "red-black", - "spaces policy": "kings", - "diagonal deal": true - }, - "foundations removable": true, - "reserve size": 7 -} )" }, diff --git a/src/main/main.cpp b/src/main/main.cpp index 16a5ba1..c279f1f 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -9,7 +9,8 @@ #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" +#include "evaluation/solvability_calc.h" +#include "evaluation/benchmark.h" using namespace rapidjson; @@ -51,11 +52,15 @@ int main(int argc, const char* argv[]) { solvability_calc solv_c(*rules); solv_c.calculate_solvability_percentage(); } - // If a random deal seed has been supplied, solves it + // 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_short_sols(), clh.get_classify()); } + // If the benchmark option has been supplied, generates it + else if (clh.get_benchmark()) { + benchmark::run(*rules); + } // Otherwise there are supplied input files which should be solved else { const vector input_files = clh.get_input_files(); @@ -141,5 +146,6 @@ void solve_game(const game_state& gs, bool short_sol, bool classify) { } cout << "States Searched: " << solv->get_states_searched() << "\n"; + cout << "Backtracks: " << solv->get_backtrack_count() << "\n"; cout << "Final Depth: " << solv->get_final_depth() << "\n"; } diff --git a/src/main/solvability/solvability_calc.cpp b/src/main/solvability/solvability_calc.cpp deleted file mode 100644 index dbfb3a4..0000000 --- a/src/main/solvability/solvability_calc.cpp +++ /dev/null @@ -1,347 +0,0 @@ -// -// Created by thecharlesblake on 3/21/18. -// -#include -#include -#include -#include - -#include "solvability_calc.h" -#include "../solver/solver.h" - -using namespace std; - -/////////////////// -// SETUP METHODS // -/////////////////// - -const double solvability_calc::coeff_ewma_weight = 0.7; - -solvability_calc::solvability_calc(const sol_rules& r) : rules(r) { -} - -////////////////////// -// PRINTING METHODS // -////////////////////// - -void solvability_calc::print_header(long t) const { - cout << "Calculating solvability percentage...\n\n" - "[Lower Bound, Upper Bound] | Solvable/Unsolvable/Intractable " - "| Current seed | (Est. Confidence Interval: Continue/Re-eval)" - "\n--- Timeout = " << t << " milliseconds ---"; - cout << fixed << setprecision(3); -} - -void solvability_calc::print_row(const seed_results& seed_res, int seed, - double cont, double re_eval) const { - double lower_bound = sol_lower_bound(seed_res.solvables(), - seed_res.unsolvables(), - seed_res.intractables()); - double upper_bound = sol_upper_bound(seed_res.solvables(), - seed_res.unsolvables(), - seed_res.intractables()); - - if (cont != -1 && re_eval !=-1) { - cout << "\n" - << "[" << lower_bound * 100 - << ", " << upper_bound * 100 - << "] | " << seed_res.solvables() - << "/" << seed_res.unsolvables() - << "/" << seed_res.intractables() - << " | " << seed - << " | ("; - if (seed_res.intractables() == 0) { - cout << "N/A"; - } else { - cout << cont * 100 - << ", " << re_eval * 100; - } - cout << ")"; - } else { - cout << "\n" - << "[-- re-eval --] | " << seed_res.solvables() - << "/" << seed_res.unsolvables() - << "/" << seed_res.intractables() - << " | " << seed; - } -} - -void solvability_calc::print_begin_re_eval_msg(long t) const { - cout << "\n--- Timeout doubled to " << t - << " ms. Re-evaluating intractable seeds ---"; -} - -void solvability_calc::print_end_re_eval_msg() const { - cout << "\n--- Re-evaluation complete ---"; -} - - -///////////////////// -// SOLVING METHODS // -///////////////////// - -void solvability_calc::calculate_solvability_percentage() { - int current_seed = 10; // TODO:change - millisec timeout(10); - inc_t_coeffs coeffs(0.4, 0.0, 1.0); - - print_header(timeout.count()); - - seed_results seed_res; - while(current_seed < INT_MAX) { - - double cont_ci_sz = continue_ci_size(coeffs, seed_res, timeout); - double reeval_ci_sz = inc_timeout_ci_size(coeffs, seed_res); - print_row(seed_res, current_seed, cont_ci_sz, reeval_ci_sz); - - while (seed_res.intractables() == 0 || cont_ci_sz < reeval_ci_sz) { - solve_seed(current_seed, timeout, seed_res); - - cont_ci_sz = continue_ci_size(coeffs, seed_res, timeout); - reeval_ci_sz = inc_timeout_ci_size(coeffs, seed_res); - - print_row(seed_res, current_seed, cont_ci_sz, reeval_ci_sz); - current_seed++; - } - - timeout *= 2; - - seed_results intract_seed_res; - print_begin_re_eval_msg(timeout.count()); - for (auto intract_it = begin(seed_res.intractable) - ; intract_it != end (seed_res.intractable) - ; intract_it++) { - solve_seed(*intract_it, timeout, intract_seed_res); - print_row(intract_seed_res, *intract_it, -1, -1); - } - print_end_re_eval_msg(); - - seed_res.add_intractable_results(intract_seed_res); - coeffs.update(intract_seed_res, timeout); - } -} - -void solvability_calc::solve_seed(int seed, millisec timeout, - seed_results& seed_res) { - 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(timeout); - auto start = chrono::steady_clock::now(); - - if (status == future_status::timeout) { - terminate_solver = true; - } else if (status == future_status::ready) { - - if (terminate_solver) { - seed_res.intractable.push_back(seed); - } else { - auto end = chrono::steady_clock::now(); - millisec elapsed_millis = - chrono::duration_cast(end - start); - - if (future.get()) { - seed_res.solvable.push_back(seed); - seed_res.solvable_time += elapsed_millis; - } else { - seed_res.unsolvable.push_back(seed); - seed_res.unsolvable_time += elapsed_millis; - } - } - } - } while (status != future_status::ready); -} - -double solvability_calc::continue_ci_size(inc_t_coeffs coeffs, - const seed_results &seed_res, - millisec timeout) const { - // If there are no intractable seeds yet, returns 0 (i.e. min interval), - // as continuing is only option - if (seed_res.intractables() == 0) return 0; - - // Calculates the expected time to solve the intractable seeds with the - // timeout doubled - millisec inc_t_time( - rnd(seed_res.intractables() * coeffs.time * 2 * timeout.count()) - ); - - // Calculates the expected number of seeds we would have attempted to solve - // were we to continue our search (i.e. without doubling timeout) in the - // above time period - millisec time_so_far = seed_res.solvable_time - + seed_res.unsolvable_time - + seed_res.intractables() * timeout; - int total_seeds = seed_res.solvables() + seed_res.unsolvables() + seed_res.intractables(); - auto total_seeds_after = static_cast(total_seeds * (1 + inc_t_time / time_so_far)); - - // Use what we know so far about the solvable/unsolvable instances to - // estimate the mean solvable/unsolvable - double mean_solvable = agresti_coull_mean(seed_res.solvables() , total_seeds); - double mean_unsolvable = agresti_coull_mean(seed_res.unsolvables() , total_seeds); - double mean_intractable = agresti_coull_mean(seed_res.intractables(), total_seeds); - - // Normalise - double mean_totals = mean_solvable + mean_unsolvable + mean_intractable; - mean_solvable /= mean_totals; mean_unsolvable /= mean_totals; mean_intractable /= mean_totals; - - // Calculates the expected number of solvable/unsolvable/intractable if we continue - // for the length of time that would have been used to solve the intractable - // seeds with the timeout doubled - int total_solvable = rnd(mean_solvable * total_seeds_after); - int total_unsolvable = rnd(mean_unsolvable * total_seeds_after); - int total_intractable = rnd(mean_intractable * total_seeds_after); - - // Given the above hypothetical values, calculates the size of the - // confidence interval we would be left with and returns it - return sol_ci_size(total_solvable, total_unsolvable, total_intractable); -} - -double solvability_calc::inc_timeout_ci_size(inc_t_coeffs coeffs, - const seed_results &seed_res) -const { - // Calculates the expected number of solvable/unsolvable/intractable - // if we run the solver on the intractable instances with double the timeout - int expected_solvable = rnd(coeffs.solvable * seed_res.intractables()); - int expected_unsolvable = rnd(coeffs.unsolvable * seed_res.intractables()); - int expected_intractable = rnd(coeffs.intractable * seed_res.intractables()); - - // Given the above values, calculates the total number of - // solvable/unsolvable/intractable we would expect to have afterwards - int total_solvable = seed_res.solvables() + expected_solvable ; - int total_unsolvable = seed_res.unsolvables() + expected_unsolvable ; - int total_intractable = expected_intractable; - - // Given the above hypothetical values, calculates the size of the - // confidence interval we would be left with and returns it - return sol_ci_size(total_solvable, total_unsolvable, total_intractable); -} - - -//////////////////////// -// SEED RESULTS CLASS // -//////////////////////// - -solvability_calc::seed_results::seed_results() - : solvable_time(0), unsolvable_time(0) { -} - -int solvability_calc::seed_results::solvables() const { - return static_cast(solvable.size()); -} - -int solvability_calc::seed_results::unsolvables() const { - return static_cast(unsolvable.size()); -} - -int solvability_calc::seed_results::intractables() const { - return static_cast(intractable.size()); -} - -void solvability_calc::seed_results::add_intractable_results( - const seed_results &ir) { - solvable .insert(end(solvable) , begin(ir.solvable) , end(ir.solvable )); - unsolvable.insert(end(unsolvable), begin(ir.unsolvable), end(ir.unsolvable)); - solvable_time += ir.solvable_time ; - unsolvable_time += ir.unsolvable_time; - - intractable = ir.intractable; -} - -solvability_calc::inc_t_coeffs::inc_t_coeffs(double solv, double intract, - double ti) - : solvable(solv), unsolvable(1-solv-intract), intractable(intract), - time(ti) { -} - -// Updates the coefficients that represent the expected results of running -// the solver on the intractable seeds with a doubled timeout. These updates -// consist of calculating exponentially-wieghted moving averages. -void solvability_calc::inc_t_coeffs::update(const seed_results &res, - solvability_calc::millisec timeout) { - int total_seeds = res.solvables() + res.unsolvables() + res.intractables(); - - double mean_solvable = agresti_coull_mean(res.solvables() , total_seeds); - double mean_unsolvable = agresti_coull_mean(res.unsolvables() , total_seeds); - double mean_intractable = agresti_coull_mean(res.intractables(), total_seeds); - - // Normalise - double mean_totals = mean_solvable + mean_unsolvable + mean_intractable; - mean_solvable /= mean_totals; mean_unsolvable /= mean_totals; mean_intractable /= mean_totals; - - // The time taken if all intractable instances were still intractable with - // the doubled timeout - millisec all_timeout_time = timeout * total_seeds; - millisec total_time = res.solvable_time - + res.unsolvable_time - + res.intractables() * timeout; - double time_frac = total_time / all_timeout_time; - - double w = coeff_ewma_weight, w_ = 1 - w; - solvable = w * mean_solvable + w_ * solvable ; - unsolvable = w * mean_unsolvable + w_ * unsolvable ; - intractable = w * mean_intractable + w_ * intractable; - time = w * time_frac + w_ * time ; -} - - -///////////////////////// -// CALCULATION METHODS // -///////////////////////// - -double solvability_calc::sol_lower_bound(int solvables, - int unsolvables, - int intractables) const { - int x = solvables; - int n = solvables + unsolvables + intractables; - bool lower_bound = true; - return agresti_coull(x, n, lower_bound); -} - -double solvability_calc::sol_upper_bound(int solvables, - int unsolvables, - int intractables) const { - int x = solvables + intractables; - int n = solvables + unsolvables + intractables; - bool lower_bound = false; - return agresti_coull(x, n, lower_bound); -} - -double solvability_calc::sol_ci_size(int solvables, - int unsolvables, - int intractables) const { - return sol_upper_bound(solvables, unsolvables, intractables) - - sol_lower_bound(solvables, unsolvables, intractables); -} - -double solvability_calc::agresti_coull(int x, int n, bool lower_bound) { - double z = 1.96; - double n_ = n + pow(z, 2); - double p = (x + pow(z, 2)/2)/n_; - double half_ci = z * sqrt((p - pow(p, 2))/n_); - if (lower_bound) { - double ans = p - half_ci; - return ans < 0 ? 0 : ans; - } else { - double ans = p + half_ci; - return ans > 1 ? 1 : ans; - } -} - -double solvability_calc::agresti_coull_mean(int x, int n) { - double z = 1.96; - double n_ = n + pow(z, 2); - double p = (x + pow(z, 2)/2)/n_; - return p; -} - -int solvability_calc::rnd(double d) { return static_cast(lround(d)); } diff --git a/src/main/solvability/solvability_calc.h b/src/main/solvability/solvability_calc.h deleted file mode 100644 index ec9cda2..0000000 --- a/src/main/solvability/solvability_calc.h +++ /dev/null @@ -1,69 +0,0 @@ -// -// Created by thecharlesblake on 3/21/18. -// - -#ifndef SOLVITAIRE_SOLVABILITY_H -#define SOLVITAIRE_SOLVABILITY_H - -#include - -#include "../game/sol_rules.h" - -class solvability_calc { -public: - explicit solvability_calc(const sol_rules&); - - void calculate_solvability_percentage(); - -private: - typedef std::chrono::milliseconds millisec; - - struct seed_results { - seed_results(); - int solvables() const; - int unsolvables() const; - int intractables() const; - void add_intractable_results(const seed_results&); - - std::vector solvable; - std::vector unsolvable; - std::vector intractable; - millisec solvable_time; - millisec unsolvable_time; - }; - - struct inc_t_coeffs { - inc_t_coeffs(double, double, double); - double solvable; // Expected solvable if we double the timeout - double unsolvable; // Expected unsolvable if we double the timeout - double intractable; // Expected intractable if we double the timeout - double time; // Expected time taken if we double the timeout (as - // fraction of timeout) - void update(const seed_results&, millisec); - }; - - // Printing methods - void print_header(long) const; - void print_row(const seed_results&, int, double, double) const; - void print_begin_re_eval_msg(long) const; - void print_end_re_eval_msg() const; - - // Solving methods - void solve_seed(int, millisec, seed_results&); - double continue_ci_size(inc_t_coeffs, const seed_results &, millisec) const; - double inc_timeout_ci_size(inc_t_coeffs, const seed_results &) const; - - // Calculation methods - double sol_lower_bound(int, int, int) const; - double sol_upper_bound(int, int, int) const; - double sol_ci_size(int, int, int) const; - static double agresti_coull(int, int, bool); - static double agresti_coull_mean(int, int); - static int rnd(double); - - const sol_rules& rules; - const static double coeff_ewma_weight; -}; - - -#endif //SOLVITAIRE_SOLVABILITY_H diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 2fca763..60b1351 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -19,6 +19,7 @@ solver::solver(const game_state& gs) , init_state(gs) , state(gs) , states_searched(1) + , backtracks(0) , depth(0) , root(nullptr, game_state::move(255, 255, 255)) , current_node(&root) { @@ -137,6 +138,7 @@ bool solver::revert_to_last_node_with_children() { state.undo_move(current_node->move); depth--; + backtracks++; #ifndef NDEBUG // Checks that the state after the undo is in the cache @@ -194,3 +196,7 @@ int solver::get_final_depth() const { const solver::node& solver::get_search_tree() const { return root; } + +int solver::get_backtrack_count() { + return backtracks; +} diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index b1455e5..5eda894 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -35,6 +35,7 @@ public: int get_states_searched() const; int get_final_depth() const; const node& get_search_tree() const; + int get_backtrack_count(); private: bool revert_to_last_node_with_children(); @@ -45,6 +46,7 @@ private: const game_state init_state; game_state state; int states_searched; + int backtracks; uint depth; node root; -- cgit v1.2.1