diff options
| author | Charlie Blake | 2018-04-06 10:07:28 +0100 |
|---|---|---|
| committer | GitHub | 2018-04-06 10:07:28 +0100 |
| commit | f8ffadac2ca576bb4e840cfb3b6f6f6bcbcf37fd (patch) | |
| tree | 829c6f93370e4f75c38eff98ad8a59095c512a49 /src | |
| parent | Merge pull request #49 from thecharlesblake/add-more-games (diff) | |
| parent | Add benchmark cmd line option (diff) | |
| download | metasol-f8ffadac2ca576bb4e840cfb3b6f6f6bcbcf37fd.tar.gz metasol-f8ffadac2ca576bb4e840cfb3b6f6f6bcbcf37fd.zip | |
Merge pull request #50 from thecharlesblake/benchmark-option
Add benchmark cmd line option
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/evaluation/benchmark.cpp | 64 | ||||
| -rw-r--r-- | src/main/evaluation/benchmark.h | 17 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.cpp (renamed from src/main/solvability/solvability_calc.cpp) | 11 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.h (renamed from src/main/solvability/solvability_calc.h) | 0 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.cpp | 26 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.h | 2 | ||||
| -rw-r--r-- | src/main/input-output/input/sol_preset_types.cpp | 69 | ||||
| -rw-r--r-- | src/main/main.cpp | 10 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 6 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 2 |
10 files changed, 132 insertions, 75 deletions
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 <climits> +#include <iostream> + +#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<int> sol_times; + multiset<int> states_searched; + multiset<int> sol_depths; + multiset<int> 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<chrono::milliseconds>(end - start); + + sol_times.insert(static_cast<int>(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/solvability/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index dbfb3a4..98f23bd 100644 --- a/src/main/solvability/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -81,8 +81,9 @@ void solvability_calc::print_end_re_eval_msg() const { ///////////////////// void solvability_calc::calculate_solvability_percentage() { - int current_seed = 10; // TODO:change - millisec timeout(10); + 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()); @@ -94,7 +95,9 @@ void solvability_calc::calculate_solvability_percentage() { 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) { + 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); @@ -104,7 +107,7 @@ void solvability_calc::calculate_solvability_percentage() { current_seed++; } - timeout *= 2; + timeout = min(timeout * 2, max_timeout); seed_results intract_seed_res; print_begin_re_eval_msg(timeout.count()); diff --git a/src/main/solvability/solvability_calc.h b/src/main/evaluation/solvability_calc.h index ec9cda2..ec9cda2 100644 --- a/src/main/solvability/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.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<int>(), "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<string, string> 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<string, string> 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<string, string> 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<string, string> 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<string, string> sol_preset_types::mp = { "move built group": true, "spaces policy": "kings" }, - "cells": 4, + "cells": 4 } )" @@ -731,7 +695,7 @@ map<string, string> sol_preset_types::mp = { }, { - "chamelion", + "chameleon", R"( { @@ -845,23 +809,6 @@ map<string, string> sol_preset_types::mp = { }, { - "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 -} -)" - - }, - { "spiderette", R"( 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<string> 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/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; |
