diff options
Diffstat (limited to 'src/main/solver')
| -rw-r--r-- | src/main/solver/solver.cpp | 216 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 35 |
2 files changed, 163 insertions, 88 deletions
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 |
