diff options
| author | Charlie Blake | 2018-07-26 10:34:37 +0100 |
|---|---|---|
| committer | GitHub | 2018-07-26 10:34:37 +0100 |
| commit | 67a805eb61428f1601e2cf45dd3380f93189ad86 (patch) | |
| tree | 86d3b0eb65395404f0e9e252b7b5384aed8cba2e /src | |
| parent | Merge pull request #78 from thecharlesblake/add-canfield (diff) | |
| parent | Update gen results script to work with changes (diff) | |
| download | metasol-67a805eb61428f1601e2cf45dd3380f93189ad86.tar.gz metasol-67a805eb61428f1601e2cf45dd3380f93189ad86.zip | |
Merge pull request #80 from thecharlesblake/fix-timing-bug
Fix timing bug
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/evaluation/benchmark.cpp | 14 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.cpp | 290 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.h | 30 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.cpp | 19 | ||||
| -rw-r--r-- | src/main/main.cpp | 50 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 216 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 35 |
7 files changed, 310 insertions, 344 deletions
diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp index 9e1b7a9..f66573c 100644 --- a/src/main/evaluation/benchmark.cpp +++ b/src/main/evaluation/benchmark.cpp @@ -31,23 +31,23 @@ void benchmark::run(const sol_rules &rules, uint64_t cache_capacity, game_state: solver sol(gs, cache_capacity); auto start = chrono::steady_clock::now(); - solver::sol_state result = sol.run(); + solver::result result = sol.run(); auto end = chrono::steady_clock::now(); microsec elapsed_micros = chrono::duration_cast<chrono::microseconds>(end - start); sol_times.insert(static_cast<int>(elapsed_micros.count())); total_time += static_cast<int>(elapsed_micros.count()); - states_searched.insert(sol.get_states_searched()); - total_states += sol.get_states_searched(); - switch (result) { - case solver::sol_state::solved: + states_searched.insert(result.states_searched); + total_states += result.states_searched; + switch (result.sol_type) { + case solver::result::type::SOLVED: total_solvable++; break; - case solver::sol_state::unsolvable: + case solver::result::type::UNSOLVABLE: total_unsolvable++; break; - case solver::sol_state::timed_out: + default: assert(false); break; } diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index 16c10c0..85a8f34 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -4,6 +4,7 @@ #include <future> #include <iomanip> #include <iostream> +#include <omp.h> #include "solvability_calc.h" #include "../solver/solver.h" @@ -14,6 +15,7 @@ using boost::optional; typedef command_line_helper::streamliner_opt cmd_sos; typedef game_state::streamliner_options sos; +typedef std::chrono::milliseconds millisec; /////////////////// // SETUP METHODS // @@ -27,47 +29,6 @@ solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) // PRINTING METHODS // ////////////////////// -void solvability_calc::print_header(long t, cmd_sos stream_opt) const { - cout << "Calculating solvability percentage...\n\n" - "Lower Bound" - ", Upper Bound" - ", Solvable" - ", Unsolvable" - ", Timed-out" - ", Memory Limited, "; - if (stream_opt == cmd_sos::SMART) { - cout << "| (Streamliner Results:) " - "Attempted Seed" - ", Outcome" - ", Time Taken(ms)" - ", States Searched" - ", Unique States Searched" - ", Backtracks" - ", Dominance Moves" - ", States Removed From Cache" - ", Final States In Cache" - ", Final Buckets In Cache" - ", Maximum Search Depth" - ", Final Search Depth" - "| (Non-Streamliner Results:) "; - } - cout << "Attempted Seed" - ", Outcome" - ", Time Taken(ms)" - ", States Searched" - ", Unique States Searched" - ", Backtracks" - ", Dominance Moves" - ", States Removed From Cache" - ", Final States In Cache" - ", Final Buckets In Cache" - ", Maximum Search Depth" - ", Final Search Depth" - ", Seeds In Progress ..." - "\n--- Timeout = " << t << " milliseconds ---\n"; - cout << fixed << setprecision(3); -} - void solvability_calc::print_general_info(const seed_results& seed_res) { pair<double, double> interval = binomial_ci::wilson( seed_res.solvable, @@ -82,37 +43,12 @@ void solvability_calc::print_general_info(const seed_results& seed_res) { << ", " << seed_res.mem_limit; } -void solvability_calc::print_seed_info(sol_result res) { - cout << ", " << res.seed; - switch (res.sol_type) { - case sol_result::type::TIMEOUT: - cout << ", timed-out"; - break; - case sol_result::type::SOLVED: - cout << ", solved"; - break; - case sol_result::type::UNSOLVABLE: - cout << ", unsolvable"; - break; - case sol_result::type::MEM_LIMIT: - cout << ", mem-limit"; - break; - } - cout << ", " << res.time.count(); - - cout << ", " << res.sol_info.states_searched - << ", " << res.sol_info.unique_states_searched - << ", " << res.sol_info.backtracks - << ", " << res.sol_info.dominance_moves - << ", " << res.sol_info.states_removed_from_cache - << ", " << res.sol_info.cache_size - << ", " << res.sol_info.cache_bucket_count - << ", " << res.sol_info.max_depth - << ", " << res.sol_info.depth; -} +void solvability_calc::print_seed_info(seed_result seed_res) { + int seed = seed_res.first; + auto& res = seed_res.second; -void solvability_calc::print_null_seed_info() { - cout << ", , , , , , , , , , , , "; + cout << ", " << seed; + solver::print_result_csv(res); } void solvability_calc::print_seeds_in_prog(std::set<int>& seeds_in_progress) { @@ -131,149 +67,89 @@ void solvability_calc::print_seeds_in_prog(std::set<int>& seeds_in_progress) { // SOLVING METHODS // ///////////////////// -void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, - cmd_sos stream_opt, const vector<int>& resume) { - vector<int> resume_seeds(begin(resume) + 3, end(resume)); +void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count_, uint cores, + cmd_sos stream_opt_, const vector<int>& resume) { + resume_seeds = vector<int>(begin(resume) + 3, end(resume)); sort(begin(resume_seeds), end(resume_seeds)); - atomic<int> current_seed(resume_seeds.back()); + current_seed = resume_seeds.back(); resume_seeds.pop_back(); - set<int> seeds_in_progress; - seed_results seed_res(resume); - mutex results_mutex; - - millisec timeout(timeout_); - print_header(timeout.count(), stream_opt); - - // Spin off 'cores' threads. Each one runs solve_seed and then print_row repeatedly, taking the seed from am - // atomic int - vector<future<void>> futures; - - sol_rules sr = rules; - uint64_t cc = cache_capacity; - - for (uint i = 0; i < cores; i++) { - futures.push_back(async( - launch::async, - [¤t_seed, &seed_res, &seeds_in_progress, &results_mutex, seed_count, timeout, sr, cc, i, - resume_seeds, stream_opt](){ - - int my_seed = resume_seeds.size() > i ? resume_seeds[i] : current_seed++; - - while (my_seed < seed_count) { - results_mutex.lock(); - seeds_in_progress.insert(my_seed); - results_mutex.unlock(); - - optional<sol_result> stream_res, no_stream_res; - sol_result final_res; - - if (stream_opt == cmd_sos::SMART) { - stream_res = solve_seed(my_seed, (timeout/10), sr, cc, sos::BOTH); - - switch (stream_res->sol_type) { - case sol_result::type::UNSOLVABLE: - case sol_result::type::TIMEOUT: - no_stream_res = solve_seed(my_seed, timeout, sr, cc, sos::NONE); - final_res = *no_stream_res; - break; - default: - final_res = *stream_res; - break; - } - } else { - no_stream_res = solve_seed(my_seed, timeout, sr, cc, - command_line_helper::convert_streamliners(stream_opt)); - final_res = *no_stream_res; - } - - results_mutex.lock(); - - seeds_in_progress.erase(my_seed); - seed_res.add_result(final_res.sol_type); - - print_general_info(seed_res); - if (stream_opt == cmd_sos::SMART) { - print_seed_info(*stream_res); - if (no_stream_res) print_seed_info(*no_stream_res); - else print_null_seed_info(); - } else { - print_seed_info(*no_stream_res); - } - print_seeds_in_prog(seeds_in_progress); - - results_mutex.unlock(); - - my_seed = current_seed++; - } - } - )); - } + seed_res.solvable = resume[0]; + seed_res.unsolvable = resume[1]; + seed_res.timed_out = resume[2]; + + timeout = millisec(timeout_); + solver::print_header(timeout.count(), stream_opt_); + seed_count = seed_count_; + stream_opt = stream_opt_; + + vector<std::thread> threads(cores); + + for (uint c = 0; c < cores; c++) + threads[c] = std::thread(solver_thread, this, c); - for (auto& f : futures) f.wait(); + for (uint c = 0; c < cores; c++) + threads[c].join(); } -solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec timeout, const sol_rules& rules, - uint64_t cache_capacity, - game_state::streamliner_options stream_opt) { - game_state gs(rules, seed, stream_opt); - solver sol(gs, cache_capacity); - atomic<bool> terminate_solver(false); +void solvability_calc::solver_thread(solvability_calc* sc, uint core) { + int my_seed = sc->resume_seeds.size() > core ? sc->resume_seeds[core] : sc->current_seed++; - sol_result res = sol_result(); - res.seed = seed; + while (my_seed < sc->seed_count) { + sc->results_mutex.lock(); + sc->seeds_in_progress.insert(my_seed); + sc->results_mutex.unlock(); - future<tuple<bool, solver::solution_info>> future = async( - launch::async, - [&sol, &terminate_solver](){ - bool solved = sol.run(terminate_solver) == solver::sol_state::solved; - return make_tuple(solved, sol.get_solution_info()); - } - ); - - future_status status; - do { - auto start = chrono::steady_clock::now(); - status = future.wait_for(timeout); - - if (status == future_status::timeout) { - terminate_solver = true; - } else if (status == future_status::ready) { - bool exception = false; - tuple<bool, solver::solution_info> result; - - try { - result = future.get(); - } catch(runtime_error& e) { - exception = true; - } + optional<seed_result> stream_res, no_stream_res, final_res; - auto end = chrono::steady_clock::now(); - millisec elapsed_millis = - chrono::duration_cast<chrono::milliseconds>(end - start); - - if (exception) { - res.sol_type = sol_result::type::MEM_LIMIT; - res.time = elapsed_millis; - res.sol_info = get<1>(result); - } else if (terminate_solver) { - res.sol_type = sol_result::type::TIMEOUT; - res.time = timeout; - res.sol_info = get<1>(result); - } else { - res.time = elapsed_millis; - if (get<0>(result)) { - res.sol_type = sol_result::type::SOLVED; - } else { - res.sol_type = sol_result::type::UNSOLVABLE; - } - res.sol_info = get<1>(result); + if (sc->stream_opt == cmd_sos::SMART) { + stream_res = solve_seed(my_seed, (sc->timeout/10), sc->rules, sc->cache_capacity, sos::BOTH); + + switch (stream_res->second.sol_type) { + case solver::result::type::UNSOLVABLE: + case solver::result::type::TIMEOUT: + no_stream_res = solve_seed(my_seed, sc->timeout, sc->rules, sc->cache_capacity, sos::NONE); + final_res = *no_stream_res; + break; + default: + final_res = *stream_res; + break; } + } else { + no_stream_res = solve_seed(my_seed, sc->timeout, sc->rules, sc->cache_capacity, + command_line_helper::convert_streamliners(sc->stream_opt)); + final_res = *no_stream_res; + } + + sc->results_mutex.lock(); + + sc->seeds_in_progress.erase(my_seed); + sc->seed_res.add_result(final_res->second.sol_type); + + print_general_info(sc->seed_res); + if (sc->stream_opt == cmd_sos::SMART) { + print_seed_info(*stream_res); + if (no_stream_res) print_seed_info(*no_stream_res); + else solver::print_null_seed_info(); + } else { + print_seed_info(*no_stream_res); } - } while (status != future_status::ready); + print_seeds_in_prog(sc->seeds_in_progress); + + sc->results_mutex.unlock(); + + my_seed = sc->current_seed++; + } +} + +solvability_calc::seed_result solvability_calc::solve_seed(int seed, millisec timeout, const sol_rules& rules, + uint64_t cache_capacity, + game_state::streamliner_options stream_opt) { + game_state gs(rules, seed, stream_opt); + solver sol(gs, cache_capacity); - return res; + return seed_result(seed, sol.run(optional<std::chrono::milliseconds>(timeout))); } @@ -281,21 +157,21 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim // SEED RESULTS CLASS // //////////////////////// -solvability_calc::seed_results::seed_results(vector<int> v) : solvable(v[0]), unsolvable(v[1]), timed_out(v[2]), mem_limit(0) { +solvability_calc::seed_results::seed_results() : solvable(0), unsolvable(0), timed_out(0), mem_limit(0) { } -void solvability_calc::seed_results::add_result(sol_result::type t) { +void solvability_calc::seed_results::add_result(solver::result::type t) { switch (t) { - case sol_result::type::TIMEOUT: + case solver::result::type::TIMEOUT: timed_out++; break; - case sol_result::type::SOLVED: + case solver::result::type::SOLVED: solvable++; break; - case sol_result::type::UNSOLVABLE: + case solver::result::type::UNSOLVABLE: unsolvable++; break; - case sol_result::type ::MEM_LIMIT: + case solver::result::type ::MEM_LIMIT: mem_limit++; break; } diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index 3d01c1b..25eb844 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -21,20 +21,11 @@ public: void calculate_solvability_percentage(uint64_t, int, uint, command_line_helper::streamliner_opt, const std::vector<int>&); private: - typedef std::chrono::milliseconds millisec; - - struct sol_result { - enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT }; - - int seed; - type sol_type; - millisec time; - solver::solution_info sol_info; - }; + typedef std::pair<int, solver::result> seed_result; struct seed_results { - explicit seed_results(std::vector<int>); - void add_result(sol_result::type); + seed_results(); + void add_result(solver::result::type); std::atomic<int> solvable; std::atomic<int> unsolvable; @@ -43,17 +34,24 @@ private: }; // Printing methods - void print_header(long, command_line_helper::streamliner_opt) const; static void print_general_info(const seed_results&); - static void print_seed_info(sol_result); - static void print_null_seed_info(); + static void print_seed_info(seed_result); static void print_seeds_in_prog(std::set<int>&); // Solving methods - static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t, game_state::streamliner_options); + static void solver_thread(solvability_calc*, uint core); + static seed_result solve_seed(int, std::chrono::milliseconds, const sol_rules&, uint64_t, game_state::streamliner_options); const sol_rules& rules; const uint64_t cache_capacity; + std::chrono::milliseconds timeout; + std::mutex results_mutex; + seed_results seed_res; + std::set<int> seeds_in_progress; + std::vector<int> resume_seeds; + std::atomic<int> current_seed; + int seed_count; + command_line_helper::streamliner_opt stream_opt; }; diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 67224d2..5a66aa6 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -43,7 +43,7 @@ command_line_helper::command_line_helper() ("solvability", po::value<int>(), "calculates the solvability " "percentage of the supplied solitaire game, given a limit for the number of seeds. Must supply " "either 'random', 'benchmark', 'solvability' or list of deals to be solved.") - ("timeout", po::value<uint64_t>(), "adds a per-game timeout to the solvability percentage generation") + ("timeout", po::value<uint64_t>(), "adds a timeout to searches") ("resume", po::value<vector<int>>()->multitoken(), "resumes the solvability percentage calculation from a " "previous run. Must be supplied with the solvability option. " "Syntax: [sol unsol intract in-progress-1 in-progress-2 ...]") @@ -52,8 +52,8 @@ command_line_helper::command_line_helper() ("streamliners", po::value<string>(), "Applies streamliners to the search. Options include 'none', 'both', 'suit-symmetry'," " 'auto-foundations', and 'smart-solvability'. Defaults to 'none', unless '--solvability' is" - " also supplied, in which case defaults to 'smart-solvability'. 'smart-solvability' mode is only" - " available with '--solvability', and runs first with both streamliners and a 10% timeout. If this" + " also supplied, in which case defaults to 'smart-solvability'. 'smart-solvability' mode" + " runs first with both streamliners and a 10% timeout. If this" " is unsuccessful (unsolvable or timeout), then runs again without streamliners.") ("benchmark", "outputs performance statistics for the solver on the " "supplied solitaire game. Must supply " @@ -152,14 +152,7 @@ bool command_line_helper::parse(int argc, const char* argv[]) { if (s == "auto-foundations" ) streamliners = streamliner_opt::AUTO_FOUNDATIONS; else if (s == "suit-symmetry") streamliners = streamliner_opt::SUIT_SYMMETRY; else if (s == "both") streamliners = streamliner_opt::BOTH; - else if (s == "smart-solvability") { - if (solvability == -1) { - print_streamliner_solvability_error(); - return false; - } else { - streamliners = streamliner_opt::SMART; - } - } + else if (s == "smart-solvability") streamliners = streamliner_opt::SMART; else if (s == "none") streamliners = streamliner_opt::NONE; else { print_streamliner_error(s); @@ -260,10 +253,6 @@ void command_line_helper::print_streamliner_error(const string& str) { " 'auto-foundations', and 'smart-solvability'"); } -void command_line_helper::print_streamliner_solvability_error() { - LOG_ERROR ("Smart streamliner mode can only be used with '--solvability'"); -} - const vector<string> command_line_helper::get_input_files() { return input_files; } diff --git a/src/main/main.cpp b/src/main/main.cpp index 5c8708e..57540d6 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -18,10 +18,12 @@ using namespace boost; namespace po = boost::program_options; +typedef std::chrono::milliseconds millisec; + const optional<sol_rules> gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&, command_line_helper&); void solve_input_files(vector<string>, const sol_rules&, command_line_helper&); -void solve_game(const game_state&, command_line_helper&); +void solve_game(const game_state&, command_line_helper&, int); // Decides what to do given supplied command-line options int main(int argc, const char* argv[]) { @@ -95,7 +97,7 @@ void solve_random_game(int seed, const sol_rules& rules, command_line_helper& cl LOG_INFO ("Attempting to solve with seed: " << seed << "..."); game_state::streamliner_options stream_opts = clh.get_streamliners_game_state(); game_state gs(rules, seed, stream_opts); - solve_game(gs, clh); + solve_game(gs, clh, seed); } void solve_input_files(const vector<string> input_files, const sol_rules& rules, command_line_helper& clh) { @@ -110,7 +112,7 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules, game_state gs(rules, in_doc, stream_opts); LOG_INFO ("Attempting to solve " << input_file << "..."); - solve_game(gs, clh); + solve_game(gs, clh, -1); } catch (const runtime_error& error) { string errmsg = "Error parsing deal file: "; @@ -120,18 +122,44 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules, } } -void solve_game(const game_state& gs, command_line_helper& clh) { +void solve_game(const game_state& gs, command_line_helper& clh, int seed) { solver solv(gs, clh.get_cache_capacity()); - bool solution = solv.run() == solver::sol_state::solved; + solver::result result, streamliner_result; + bool smart = clh.get_streamliners() == command_line_helper::streamliner_opt::SMART; + bool run_again = false; - if (solution) { - if (!clh.get_classify()) solv.print_solution(); - cout << "Solved\n"; + if (smart) { + result = solv.run(millisec(clh.get_timeout() / 10)); + run_again = result.sol_type != solver::result::type::SOLVED; + if (run_again) + streamliner_result = solv.run(millisec(clh.get_timeout())); } else { - if (!clh.get_classify()) cout << "Deal:\n" << gs << "\n"; - cout << "No Possible Solution\n"; + result = solv.run(millisec(clh.get_timeout())); } - cout << solv.get_solution_info(); + if (clh.get_classify()) { + cout << seed; + solver::print_result_csv(result); + if (smart) { + if (run_again) { + solver::print_result_csv(streamliner_result); + } else { + solver::print_null_seed_info(); + } + } + cout << "\n"; + } else { + if (run_again) { + cout << "Unsolvable using streamliner. Running again...\n"; + result = streamliner_result; + } + + if (result.sol_type == solver::result::type::SOLVED) { + solv.print_solution(); + } else { + cout << "Deal:\n" << gs << "\n"; + } + cout << result; + } } diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 3bfd6bd..3aaceb8 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -8,11 +8,14 @@ #include <algorithm> #include <list> #include <malloc.h> +#include <chrono> +#include <iomanip> #include "solver.h" #include "../game/move.h" #include "../input-output/output/log_helper.h" #include "../input-output/output/state_printer.h" +#include "../input-output/input/command_line_helper.h" using std::atomic; using std::vector; @@ -23,35 +26,49 @@ using std::max; using std::begin; using std::end; using boost::optional; +using std::fixed; +using std::setprecision; solver::solver(const game_state& gs, uint64_t cache_capacity) : cache(lru_cache(gs, cache_capacity)) , init_state(gs) , state(gs) , frontier() - , states_searched(0) - , unique_states_searched(0) - , backtracks(0) - , dominance_moves(0) - , depth(0) - , max_depth(0) , root(move(move::mtype::null)) , current_node() { frontier.push_back(root); current_node = begin(frontier); + res.states_searched = 0; + res.unique_states_searched = 0; + res.backtracks = 0; + res.dominance_moves = 0; + res.states_removed_from_cache = 0; + res.max_depth = 0; + res.depth = 0; } solver::node::node(const move m) : mv(m), child_moves(), cache_state() { } -solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) { +solver::result solver::run(boost::optional<millisec> timeout) { + const clock::time_point start_time = clock::now(); + result::type res_type = timeout ? dfs(start_time + *timeout) : dfs(); + res.sol_type = res_type; + res.states_removed_from_cache = cache.get_states_removed_from_cache(); + res.cache_size = cache.size(); + res.cache_bucket_count = cache.bucket_count(); + res.time = std::chrono::duration_cast<millisec>(clock::now() - start_time); + return res; +} + +solver::result::type solver::dfs(boost::optional<clock::time_point> end_time) { 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 sol_state::timed_out; + if (end_time && clock::now() >= *end_time) { + return result::type::TIMEOUT; } #ifndef NDEBUG @@ -68,26 +85,30 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) // Adds the dominance move as a child of the current search node; current_node->child_moves.emplace_back(*dominance_move); } else { - // Caches the current state - pair<lru_cache::item_list::iterator, bool> insert_res = cache.insert(state); - current_node->cache_state = insert_res.first; - bool is_new_state = insert_res.second; - - if (is_new_state) { - // Gets the legal moves in the current state - vector<move> next_moves = state.get_legal_moves(current_node->mv); - - // If there are none, reverts to the last node with children - if (next_moves.empty()) { - states_exhausted = revert_to_last_node_with_children(insert_res.first); - } else { - current_node->child_moves = std::move(next_moves); + try { + // Caches the current state + pair<lru_cache::item_list::iterator, bool> insert_res = cache.insert(state); + current_node->cache_state = insert_res.first; + bool is_new_state = insert_res.second; + + if (is_new_state) { + // Gets the legal moves in the current state + vector<move> next_moves = state.get_legal_moves(current_node->mv); + + // If there are none, reverts to the last node with children + if (next_moves.empty()) { + states_exhausted = revert_to_last_node_with_children(insert_res.first); + } else { + current_node->child_moves = std::move(next_moves); + } } - } - // If the state is not a new one, reverts to the last node with children - else { - unique_states_searched--; - states_exhausted = revert_to_last_node_with_children(); + // If the state is not a new one, reverts to the last node with children + else { + res.unique_states_searched--; + states_exhausted = revert_to_last_node_with_children(); + } + } catch (const std::runtime_error e) { + return result::type::MEM_LIMIT; } } @@ -96,20 +117,20 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) if (!states_exhausted) { set_to_child(); state.make_move(current_node->mv); - depth++; - max_depth = max(depth, max_depth); - if (current_node->mv.type == move::mtype::dominance) dominance_moves++; + res.depth++; + res.max_depth = max(res.depth, res.max_depth); + if (current_node->mv.type == move::mtype::dominance) res.dominance_moves++; } - states_searched++; - unique_states_searched++; + res.states_searched++; + res.unique_states_searched++; } if (state.is_solved()) { - return sol_state::solved; + return result::type::SOLVED; } else { assert(states_exhausted); - return sol_state::unsolvable; + return result::type::UNSOLVABLE; } } @@ -127,8 +148,8 @@ bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::it if (cur_state) cache.set_non_live(*cur_state); state.undo_move(current_node->mv); - depth--; - backtracks++; + res.depth--; + res.backtracks++; #ifndef NDEBUG // Checks that the state after the undo is in the cache @@ -181,7 +202,7 @@ void solver::print_solution() const { cout << "Solution:\n"; cout << state_copy << "\n"; - if (states_searched > 1) { + if (res.states_searched > 1) { while (++i != end(frontier)) { state_copy.make_move(i->mv); cout << state_copy << "\n"; @@ -190,49 +211,98 @@ void solver::print_solution() const { cout << "\n"; } -std::ostream& operator<< (std::ostream& out, const solver::solution_info& si) { - return out - << "States Searched: " << si.states_searched << "\n" - << "Unique States Searched: " << si.unique_states_searched << "\n" - << "Backtracks: " << si.backtracks << "\n" - << "Dominance Moves: " << si.dominance_moves << "\n" - << "States Removed From Cache: " << si.states_removed_from_cache << "\n" - << "Final States In Cache: " << si.cache_size << "\n" - << "Final Buckets In Cache: " << si.cache_bucket_count << "\n" - << "Maximum Search Depth: " << si.max_depth << "\n" - << "Final Search Depth: " << si.depth << "\n"; -} - -solver::solution_info solver::get_solution_info() { - solution_info si; - si.states_searched = states_searched; - si.unique_states_searched = unique_states_searched; - si.backtracks = backtracks; - si.dominance_moves = dominance_moves; - si.states_removed_from_cache = cache.get_states_removed_from_cache(); - si.cache_size = cache.size(); - si.cache_bucket_count = cache.bucket_count(); - si.max_depth = max_depth; - si.depth = depth; - return si; +std::ostream& operator<< (std::ostream& out, const solver::result::type& rt) { + switch(rt) { + case solver::result::type::TIMEOUT: + out << "timed-out"; + break; + case solver::result::type::SOLVED: + out << "solved"; + break; + case solver::result::type::UNSOLVABLE: + out << "unsolvable"; + break; + case solver::result::type::MEM_LIMIT: + out << "memory-limit-reached"; + break; + } + return out; } -const vector<solver::node> solver::get_frontier() const { - return frontier; +std::ostream& operator<< (std::ostream& out, const solver::result& r) { + return out + << "Solution Type: " << r.sol_type << "\n" + << "States Searched: " << r.states_searched << "\n" + << "Unique States Searched: " << r.unique_states_searched << "\n" + << "Backtracks: " << r.backtracks << "\n" + << "Dominance Moves: " << r.dominance_moves << "\n" + << "States Removed From Cache: " << r.states_removed_from_cache << "\n" + << "Final States In Cache: " << r.cache_size << "\n" + << "Final Buckets In Cache: " << r.cache_bucket_count << "\n" + << "Maximum Search Depth: " << r.max_depth << "\n" + << "Final Search Depth: " << r.depth << "\n" + << "Time Taken (milliseconds): " << r.time.count() << "\n"; } -int solver::get_states_searched() const { - return states_searched; +void solver::print_header(long t, command_line_helper::streamliner_opt stream_opt) { + cout << "Calculating solvability percentage...\n\n" + "Lower Bound" + ", Upper Bound" + ", Solvable" + ", Unsolvable" + ", Timed-out" + ", Memory Limited, "; + if (stream_opt == command_line_helper::streamliner_opt::SMART) { + cout << "| (Streamliner Results:) " + "Attempted Seed" + ", Outcome" + ", Time Taken(ms)" + ", States Searched" + ", Unique States Searched" + ", Backtracks" + ", Dominance Moves" + ", States Removed From Cache" + ", Final States In Cache" + ", Final Buckets In Cache" + ", Maximum Search Depth" + ", Final Search Depth" + "| (Non-Streamliner Results:) "; + } + cout << "Attempted Seed" + ", Outcome" + ", Time Taken(ms)" + ", States Searched" + ", Unique States Searched" + ", Backtracks" + ", Dominance Moves" + ", States Removed From Cache" + ", Final States In Cache" + ", Final Buckets In Cache" + ", Maximum Search Depth" + ", Final Search Depth" + ", Seeds In Progress ..." + "\n--- Timeout = " << t << " milliseconds ---\n"; + cout << fixed << setprecision(3); } -int solver::get_unique_states_searched() const { - return unique_states_searched; +void solver::print_result_csv(solver::result res) { + cout << ", " << res.sol_type + << ", " << res.time.count() + << ", " << res.states_searched + << ", " << res.unique_states_searched + << ", " << res.backtracks + << ", " << res.dominance_moves + << ", " << res.states_removed_from_cache + << ", " << res.cache_size + << ", " << res.cache_bucket_count + << ", " << res.max_depth + << ", " << res.depth; } -int solver::get_cache_size() const { - return cache.size(); +void solver::print_null_seed_info() { + cout << ", , , , , , , , , , , , "; } -int solver::get_states_rem_from_cache() const { - return cache.get_states_removed_from_cache(); +const vector<solver::node> solver::get_frontier() const { + return frontier; } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 3aa0d95..90796f7 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -8,13 +8,14 @@ #include <string> #include <vector> #include <atomic> +#include <chrono> #include "../game/global_cache.h" #include "../game/sol_rules.h" +#include "../input-output/input/command_line_helper.h" class solver { public: - enum class sol_state {solved, unsolvable, timed_out}; lru_cache cache; struct node { @@ -24,7 +25,10 @@ public: boost::optional<lru_cache::item_list::iterator> cache_state; // Optional, as dominance moves aren't cached }; - struct solution_info { + struct result { + enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT }; + + type sol_type; int states_searched; int unique_states_searched; int backtracks; @@ -34,22 +38,25 @@ public: lru_cache::item_list::size_type cache_bucket_count; int max_depth; int depth; + std::chrono::milliseconds time; }; - friend std::ostream& operator<< (std::ostream&, const solution_info&); explicit solver(const game_state&, uint64_t); - sol_state run(boost::optional<std::atomic<bool> &> = boost::none); + result run(boost::optional<std::chrono::milliseconds> = boost::none); void print_solution() const; - int get_states_searched() const; - int get_unique_states_searched() const; - int get_cache_size() const; - int get_states_rem_from_cache() const; + static void print_header(long, command_line_helper::streamliner_opt); + static void print_result_csv(solver::result); + static void print_null_seed_info(); const std::vector<node> get_frontier() const; - solution_info get_solution_info(); private: + typedef std::chrono::high_resolution_clock clock; + typedef std::chrono::milliseconds millisec; + + result::type dfs(boost::optional<clock::time_point> = boost::none); + bool revert_to_last_node_with_children(boost::optional<lru_cache::item_list::iterator> = boost::none); void set_to_child(); @@ -57,16 +64,14 @@ private: game_state state; std::vector<node> frontier; - int states_searched; - int unique_states_searched; - int backtracks; - int dominance_moves; - int depth; - int max_depth; + result res; node root; std::vector<node>::iterator current_node; }; +std::ostream& operator<< (std::ostream&, const solver::result::type&); +std::ostream& operator<< (std::ostream&, const solver::result&); + #endif //SOLVITAIRE_SOLVER_H |
