diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/evaluation/solvability_calc.cpp | 38 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.h | 5 | ||||
| -rw-r--r-- | src/main/game/move.h | 3 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.cpp | 32 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.h | 7 | ||||
| -rw-r--r-- | src/main/main.cpp | 2 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 82 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 17 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 16 |
9 files changed, 119 insertions, 83 deletions
diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index f8f5ca4..7597dc9 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -26,7 +26,7 @@ solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) void solvability_calc::print_header(long t) const { cerr << "Calculating solvability percentage...\n\n" "[Lower Bound% - Upper Bound%], Solvable/Unsolvable/Intractable Count | " - "Solved seed: Solvable/Unsolvable/Intractable, Time Taken (ms), Unique Search States | " + "Solved seed: Solvable/Unsolvable/Intractable, Time Taken (ms), Unique Search States, States Removed From Cache | " "Seeds In Progress" "\n--- Timeout = " << t << " milliseconds ---\n"; cerr << fixed << setprecision(3); @@ -60,8 +60,8 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s break; } cerr << "\t" << res.time.count(); - if (res.unique_search_states == -1) cerr << ", N/A"; - else cerr << ", " << res.unique_search_states; + if (res.unique_search_states == -1) cerr << ", N/A, N/A"; + else cerr << ", " << res.unique_search_states << ", " << res.states_rem_from_cache; cerr << " | ["; sip_mutex.lock(); @@ -78,8 +78,12 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s // SOLVING METHODS // ///////////////////// -void solvability_calc::calculate_solvability_percentage(int timeout_, int cores) { - atomic<int> current_seed(0); +void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores, const vector<int>& resume) { + vector<int> resume_seeds(begin(resume) + 3, end(resume)); + sort(begin(resume_seeds), end(resume_seeds)); + + atomic<int> current_seed(resume_seeds.back()); + resume_seeds.pop_back(); set<int> seeds_in_progress; mutex sip_mutex; @@ -90,20 +94,19 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, int cores) // 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; - seed_results seed_res; + seed_results seed_res(resume); sol_rules sr = rules; uint64_t cc = cache_capacity; - for (int i = 0; i < cores; i++) { + for (uint i = 0; i < cores; i++) { futures.push_back(async( launch::async, - [¤t_seed, &seed_res, &seeds_in_progress, &sip_mutex, timeout, sr, cc](){ + [¤t_seed, &seed_res, &seeds_in_progress, &sip_mutex, timeout, sr, cc, i, resume_seeds](){ - int my_seed = 0; - while (my_seed < INT_MAX) { - my_seed = current_seed++; + int my_seed = resume_seeds.size() > i ? resume_seeds[i] : current_seed++; + while (my_seed < INT_MAX) { sip_mutex.lock(); seeds_in_progress.insert(my_seed); sip_mutex.unlock(); @@ -115,6 +118,8 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, int cores) sip_mutex.unlock(); print_row(seed_res, res, seeds_in_progress, sip_mutex); + + my_seed = current_seed++; } } )); @@ -132,11 +137,11 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim sol_result res = sol_result(); res.seed = seed; - future<tuple<bool, int>> future = async( + future<tuple<bool, int, int>> future = async( launch::async, [&sol, &terminate_solver](){ bool solved = sol.run(terminate_solver) == solver::sol_state::solved; - return make_tuple(solved, sol.get_unique_states_searched()); + return make_tuple(solved, sol.get_unique_states_searched(), sol.get_states_rem_from_cache()); } ); @@ -149,7 +154,7 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim terminate_solver = true; } else if (status == future_status::ready) { bool exception = false; - tuple<bool, int> result; + tuple<bool, int, int> result; try { result = future.get(); @@ -162,11 +167,13 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim seed_res.add_result(res.sol_type); res.time = timeout; res.unique_search_states = -1; + res.states_rem_from_cache = -1; } else if (terminate_solver) { res.sol_type = sol_result::type::timeout; seed_res.add_result(res.sol_type); res.time = timeout; res.unique_search_states = get<1>(result); + res.states_rem_from_cache = get<2>(result); } else { auto end = chrono::steady_clock::now(); millisec elapsed_millis = @@ -182,6 +189,7 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim } seed_res.add_result(res.sol_type); res.unique_search_states = get<1>(result); + res.states_rem_from_cache = get<2>(result); } } } while (status != future_status::ready); @@ -194,7 +202,7 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim // SEED RESULTS CLASS // //////////////////////// -solvability_calc::seed_results::seed_results() : solvable(0), unsolvable(0), intractable(0) { +solvability_calc::seed_results::seed_results(vector<int> v) : solvable(v[0]), unsolvable(v[1]), intractable(v[2]) { } void solvability_calc::seed_results::add_result(sol_result::type t) { diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index fe7e29b..367c469 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -16,7 +16,7 @@ class solvability_calc { public: explicit solvability_calc(const sol_rules&, uint64_t); - void calculate_solvability_percentage(int timeout, int cores); + void calculate_solvability_percentage(int timeout, uint cores, const std::vector<int>&); private: typedef std::chrono::milliseconds millisec; @@ -28,10 +28,11 @@ private: type sol_type; millisec time; int unique_search_states; + int states_rem_from_cache; }; struct seed_results { - seed_results(); + seed_results(std::vector<int>); void add_result(sol_result::type); std::atomic<int> solvable; diff --git a/src/main/game/move.h b/src/main/game/move.h index b852811..1e3a9d1 100644 --- a/src/main/game/move.h +++ b/src/main/game/move.h @@ -8,7 +8,7 @@ #include "pile.h" struct move { - enum class mtype {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null}; + enum class mtype : uint8_t {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null}; explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1); friend bool operator==(const move&, const move&); @@ -19,5 +19,4 @@ struct move { pile::size_type count; }; - #endif //SOLVITAIRE_MOVE_H diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index cfbfd2f..2abef54 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -44,7 +44,10 @@ command_line_helper::command_line_helper() "percentage of the supplied solitaire game, given a timeout in milliseconds. Must supply " "either 'random', 'benchmark', 'solvability' or list of deals to be " "solved.") - ("cores", po::value<int>(), "the number of cores for the solvability percentages to be run across. " + ("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 ...]") + ("cores", po::value<uint>(), "the number of cores for the solvability percentages to be run across. " "Must be supplied with the solvability option.") ("benchmark", "outputs performance statistics for the solver on the " "supplied solitaire game. Must supply " @@ -119,11 +122,18 @@ bool command_line_helper::parse(int argc, const char* argv[]) { } if (vm.count("cores")) { - cores = vm["cores"].as<int>(); + cores = vm["cores"].as<uint>(); } else { cores = 1; } + if (vm.count("resume")) { + resume = vm["resume"].as<vector<int>>(); + } else { + resume = {0, 0, 0}; + for (uint i = 0; i < cores; i++) resume.push_back(i); + } + benchmark = (vm.count("benchmark") != 0); @@ -139,6 +149,11 @@ bool command_line_helper::assess_errors() { if (available_game_types || !describe_game_rules.empty()) return true; + if (solvability > 0 && resume.size() != 3 + cores) { + print_resume_error(); + return false; + } + // The user must either supply input files, a random seed, or ask for the // solvability percentage, or benchmark int opt_count = (random_deal != -1) + !input_files.empty() + (solvability > 0) + benchmark; @@ -196,7 +211,12 @@ 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, the 'benchmark' option,, " - "or the '--solvability' option, not multiple"); + "or the '--solvability' option, not multiple"); + print_help(); +} + +void command_line_helper::print_resume_error() { + LOG_ERROR ("Error: Resume contains " << resume.size() << " options, and should contain " << cores + 3); print_help(); } @@ -228,7 +248,11 @@ int command_line_helper::get_solvability() { return solvability; } -int command_line_helper::get_cores() { +vector<int> command_line_helper::get_resume() { + return resume; +} + +uint command_line_helper::get_cores() { return cores; } diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index f3320da..3ce239d 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -18,9 +18,10 @@ public: int get_random_deal(); bool get_classify(); int get_solvability(); - int get_cores(); + uint get_cores(); bool get_available_game_types(); bool get_benchmark(); + std::vector<int> get_resume(); uint64_t get_cache_capacity(); std::string get_describe_game_rules(); @@ -31,6 +32,7 @@ private: void print_sol_type_rules_error(); void print_no_opts_error(); void print_too_many_opts_error(); + void print_resume_error(); boost::program_options::options_description cmdline_options; boost::program_options::options_description main_options; @@ -44,7 +46,8 @@ private: bool help; bool classify; int solvability; - int cores; + std::vector<int> resume; + uint cores; bool available_game_types; bool benchmark; uint64_t cache_capacity; diff --git a/src/main/main.cpp b/src/main/main.cpp index f8f3d8f..c3bf652 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -51,7 +51,7 @@ int main(int argc, const char* argv[]) { // If the user has asked for a solvability percentage, calculates it if (clh.get_solvability() > 0) { solvability_calc solv_c(*rules, clh.get_cache_capacity()); - solv_c.calculate_solvability_percentage(clh.get_solvability(), clh.get_cores()); + solv_c.calculate_solvability_percentage(clh.get_solvability(), clh.get_cores(), clh.get_resume()); } // If a random deal seed has been supplied, solves it else if (clh.get_random_deal() != -1) { diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index e44188d..53c7d1b 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -18,34 +18,30 @@ using std::vector; using std::cout; using std::clog; using std::pair; -using boost::optional; using std::max; +using std::begin; +using std::end; +using boost::optional; 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(nullptr, move(move::mtype::null)) - , current_node(&root) { -} - -solver::~solver() { - // This code looks to be unnecessary, but on alpha-star 6212 without it, - // for reasons that I cannot fathom, it segfaults. - while (current_node != nullptr) { - current_node->children.clear(); - current_node = current_node->parent; - } + , root(move(move::mtype::null)) + , current_node() { + frontier.push_back(root); + current_node = begin(frontier); } -solver::node::node(node* p, const move m, optional<lru_cache::item_list::iterator> c) - : parent(p), mv(m), children(), parent_state_iter(c) { +solver::node::node(const move m) + : mv(m), child_moves(), cache_state() { } solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) { @@ -68,12 +64,14 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) // and repeats. Doesn't cache the state. optional<move> dominance_move = state.get_dominance_move(); if (dominance_move) { - // Adds the dominance move as a child of the current search node - add_child(*dominance_move); + // 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); @@ -82,7 +80,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) if (next_moves.empty()) { states_exhausted = revert_to_last_node_with_children(insert_res.first); } else { - add_children(next_moves, insert_res.first); + current_node->child_moves = std::move(next_moves); } } // If the state is not a new one, reverts to the last node with children @@ -93,9 +91,9 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) } // Sets the current node to one of its children - assert(states_exhausted == current_node->children.empty()); + assert(states_exhausted == current_node->child_moves.empty()); if (!states_exhausted) { - current_node = ¤t_node->children.back(); + set_to_child(); state.make_move(current_node->mv); depth++; max_depth = max(depth, max_depth); @@ -114,17 +112,6 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) } } -void solver::add_children(std::vector<move>& moves, optional<lru_cache::item_list::iterator> i) { - current_node->children.reserve(moves.size()); - for (auto move : moves) { - add_child(move, i); - } -} - -void solver::add_child(move m, optional<lru_cache::item_list::iterator> i) { - current_node->children.emplace_back(current_node, m, i); -} - // Called when the current node's children have been exhausted. Travels back up // the search tree until it finds a node which still has children. Returns true // unless all children have been exhausted. @@ -132,7 +119,7 @@ void solver::add_child(move m, optional<lru_cache::item_list::iterator> i) { // If an iterator to the current state is supplied to the function, will also // make sure to turn the 'live' bit off upon backtracking bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::iterator> cur_state) { - if (!current_node->parent) + if (current_node == begin(frontier)) return true; // Turns the 'live' bit false on the state we are backtracking out of @@ -157,35 +144,44 @@ bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::it // Gets a reference to the parent state which can be supplied if this function is // called recursively. This ensures that the cached state's 'live' bit is set as appropriate - optional<lru_cache::item_list::iterator> p_state = current_node->parent_state_iter; + optional<lru_cache::item_list::iterator> p_state = prev(current_node)->cache_state; // Reverts the current node to its parent and removes it - current_node = current_node->parent; - current_node->children.pop_back(); + frontier.pop_back(); + current_node = prev(end(frontier)); // If the current node now has no children, repeat - if (current_node->children.empty()) { + if (current_node->child_moves.empty()) { return revert_to_last_node_with_children(p_state); } else { return false; } } +void solver::set_to_child() { + assert(!current_node->child_moves.empty()); + + move b = current_node->child_moves.back(); + current_node->child_moves.pop_back(); + frontier.emplace_back(b); + + current_node = prev(end(frontier)); +} + void solver::print_solution() const { std::flush(clog); std::flush(cout); - const node* n = &root.children.back(); + auto i = begin(frontier); game_state state_copy = init_state; cout << "Solution:\n"; cout << state_copy << "\n"; if (states_searched > 1) { - while (!n->children.empty()) { - state_copy.make_move(n->mv); + while (++i != end(frontier)) { + state_copy.make_move(i->mv); cout << state_copy << "\n"; - n = &n->children.back(); } } cout << "\n"; @@ -203,8 +199,8 @@ void solver::print_solution_info() const { << "Final Search Depth: " << depth << "\n"; } -const solver::node& solver::get_search_tree() const { - return root; +const vector<solver::node> solver::get_frontier() const { + return frontier; } int solver::get_states_searched() const { @@ -218,3 +214,7 @@ int solver::get_unique_states_searched() const { 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 961072f..14dedc5 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -18,15 +18,13 @@ public: lru_cache cache; struct node { - node(node*, move, boost::optional<lru_cache::item_list::iterator> = boost::none); - node* parent; + node(move); const move mv; - std::vector<node> children; - boost::optional<lru_cache::item_list::iterator> parent_state_iter; // Optional, as dominance moves aren't cached + std::vector<move> child_moves; + boost::optional<lru_cache::item_list::iterator> cache_state; // Optional, as dominance moves aren't cached }; explicit solver(const game_state&, uint64_t); - ~solver(); sol_state run(boost::optional<std::atomic<bool> &> = boost::none); @@ -35,15 +33,16 @@ public: int get_states_searched() const; int get_unique_states_searched() const; int get_cache_size() const; - const node& get_search_tree() const; + int get_states_rem_from_cache() const; + const std::vector<node> get_frontier() const; private: bool revert_to_last_node_with_children(boost::optional<lru_cache::item_list::iterator> = boost::none); - void add_children(std::vector<move>&, boost::optional<lru_cache::item_list::iterator> = boost::none); - void add_child(move, boost::optional<lru_cache::item_list::iterator> = boost::none); + void set_to_child(); const game_state init_state; game_state state; + std::vector<node> frontier; int states_searched; int unique_states_searched; @@ -53,7 +52,7 @@ private: int max_depth; node root; - node* current_node; + std::vector<node>::iterator current_node; }; diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index b58c410..eaa0aba 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -49,15 +49,17 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, solver sol(gs, 1000000); sol.run(); - solver::node const *n = &sol.get_search_tree(); + auto frontier = sol.get_frontier(); + auto i = std::begin(frontier); + for (card c : cards) { - n = &n->children.back(); - ASSERT_TRUE(n->mv.type == move::mtype::dominance); - ASSERT_TRUE(gs.get_data()[n->mv.from].top_card() == c); - ASSERT_TRUE(n->mv.to >= 0 && n->mv.to <= 4); - gs.make_move(n->mv); + i++; + ASSERT_TRUE(i->mv.type == move::mtype::dominance); + ASSERT_TRUE(gs.get_data()[i->mv.from].top_card() == c); + ASSERT_TRUE(i->mv.to >= 0 && i->mv.to <= 4); + gs.make_move(i->mv); } - ASSERT_TRUE(n->children.empty()); + ASSERT_TRUE(i->child_moves.empty()); } void test_helper::run_built_group_test(sol_rules sr, |
