diff options
| author | Charlie Blake | 2018-07-25 09:49:34 +0100 |
|---|---|---|
| committer | Charlie Blake | 2018-07-26 10:32:38 +0100 |
| commit | e1ea0d49c5804921cd4910e891ed263172608d49 (patch) | |
| tree | 44df2ad6c36873e091a7c3a2892c4edf2c69901a | |
| parent | Merge pull request #78 from thecharlesblake/add-canfield (diff) | |
| download | metasol-e1ea0d49c5804921cd4910e891ed263172608d49.tar.gz metasol-e1ea0d49c5804921cd4910e891ed263172608d49.zip | |
Make timeout operate using single thread
| -rw-r--r-- | src/main/evaluation/benchmark.cpp | 14 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.cpp | 122 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.h | 17 | ||||
| -rw-r--r-- | src/main/main.cpp | 19 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 161 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 30 |
6 files changed, 148 insertions, 215 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..5d52447 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -14,6 +14,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 // @@ -82,33 +83,22 @@ 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; + + cout << ", " << seed + << ", " << 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; } void solvability_calc::print_null_seed_info() { @@ -166,15 +156,14 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s seeds_in_progress.insert(my_seed); results_mutex.unlock(); - optional<sol_result> stream_res, no_stream_res; - sol_result final_res; + optional<seed_result> stream_res, no_stream_res, 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: + switch (stream_res->second.sol_type) { + case solver::result::type::UNSOLVABLE: + case solver::result::type::TIMEOUT: no_stream_res = solve_seed(my_seed, timeout, sr, cc, sos::NONE); final_res = *no_stream_res; break; @@ -191,7 +180,7 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s results_mutex.lock(); seeds_in_progress.erase(my_seed); - seed_res.add_result(final_res.sol_type); + seed_res.add_result(final_res->second.sol_type); print_general_info(seed_res); if (stream_opt == cmd_sos::SMART) { @@ -214,66 +203,13 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s for (auto& f : futures) f.wait(); } -solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec timeout, const sol_rules& rules, +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); - atomic<bool> terminate_solver(false); - - sol_result res = sol_result(); - res.seed = seed; - - 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; - } - - 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); - } - } - } while (status != future_status::ready); - return res; + return seed_result(seed, sol.run(optional<std::chrono::milliseconds>(timeout))); } @@ -284,18 +220,18 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim solvability_calc::seed_results::seed_results(vector<int> v) : solvable(v[0]), unsolvable(v[1]), timed_out(v[2]), 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..823d262 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); + void add_result(solver::result::type); std::atomic<int> solvable; std::atomic<int> unsolvable; @@ -45,12 +36,12 @@ 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_seed_info(seed_result); static void print_null_seed_info(); 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 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; diff --git a/src/main/main.cpp b/src/main/main.cpp index 5c8708e..e8f287b 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -123,15 +123,14 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules, void solve_game(const game_state& gs, command_line_helper& clh) { solver solv(gs, clh.get_cache_capacity()); - bool solution = solv.run() == solver::sol_state::solved; - - if (solution) { - if (!clh.get_classify()) solv.print_solution(); - cout << "Solved\n"; - } else { - if (!clh.get_classify()) cout << "Deal:\n" << gs << "\n"; - cout << "No Possible Solution\n"; - } + solver::result result = solv.run(); - cout << solv.get_solution_info(); + if (!clh.get_classify()) { + 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..abe55f5 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -8,6 +8,7 @@ #include <algorithm> #include <list> #include <malloc.h> +#include <chrono> #include "solver.h" #include "../game/move.h" @@ -29,29 +30,41 @@ solver::solver(const game_state& gs, uint64_t 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 +81,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 +113,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 +144,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 +198,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 +207,39 @@ 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"; +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; } -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& 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"; } const vector<solver::node> solver::get_frontier() const { return frontier; } - -int solver::get_states_searched() const { - return states_searched; -} - -int solver::get_unique_states_searched() const { - return unique_states_searched; -} - -int solver::get_cache_size() const { - return cache.size(); -} - -int solver::get_states_rem_from_cache() const { - return cache.get_states_removed_from_cache(); -} diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 3aa0d95..75ee603 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -14,7 +14,6 @@ class solver { public: - enum class sol_state {solved, unsolvable, timed_out}; lru_cache cache; struct node { @@ -24,7 +23,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 +36,22 @@ 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; 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 +59,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 |
