From e1ea0d49c5804921cd4910e891ed263172608d49 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 09:49:34 +0100 Subject: Make timeout operate using single thread --- src/main/evaluation/benchmark.cpp | 14 +-- src/main/evaluation/solvability_calc.cpp | 122 ++++++----------------- src/main/evaluation/solvability_calc.h | 17 +--- src/main/main.cpp | 19 ++-- src/main/solver/solver.cpp | 161 ++++++++++++++++--------------- 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(end - start); sol_times.insert(static_cast(elapsed_micros.count())); total_time += static_cast(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 stream_res, no_stream_res; - sol_result final_res; + optional 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 terminate_solver(false); - - sol_result res = sol_result(); - res.seed = seed; - - future> 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 result; - - try { - result = future.get(); - } catch(runtime_error& e) { - exception = true; - } - - auto end = chrono::steady_clock::now(); - millisec elapsed_millis = - chrono::duration_cast(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(timeout))); } @@ -284,18 +220,18 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim solvability_calc::seed_results::seed_results(vector 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&); 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 seed_result; struct seed_results { explicit seed_results(std::vector); - void add_result(sol_result::type); + void add_result(solver::result::type); std::atomic solvable; std::atomic 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&); // 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 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 #include #include +#include #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 &> terminate_solver) { +solver::result solver::run(boost::optional 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(clock::now() - start_time); + return res; +} + +solver::result::type solver::dfs(boost::optional 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 &> 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 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 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 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 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 &> 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(optionalmv); - 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::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 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 &> = boost::none); + result run(boost::optional = 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 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 = boost::none); + bool revert_to_last_node_with_children(boost::optional = boost::none); void set_to_child(); @@ -57,16 +59,14 @@ private: game_state state; std::vector frontier; - int states_searched; - int unique_states_searched; - int backtracks; - int dominance_moves; - int depth; - int max_depth; + result res; node root; std::vector::iterator current_node; }; +std::ostream& operator<< (std::ostream&, const solver::result::type&); +std::ostream& operator<< (std::ostream&, const solver::result&); + #endif //SOLVITAIRE_SOLVER_H -- cgit v1.2.1 From 69160b593d56beee5c1a3d8468a3ce5b43da686c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 10:27:50 +0100 Subject: Add open mp to --sol --- CMakeLists.txt | 2 +- src/main/evaluation/solvability_calc.cpp | 109 ++++++++++++++----------------- src/main/evaluation/solvability_calc.h | 2 +- src/main/solver/solver.h | 1 + 4 files changed, 53 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1815077..4d00921 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,7 +138,7 @@ endif() ############################################################################### set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -DBOOST_LOG_DYN_LINK \ --lboost_system -pthread -pedantic -Wall -Wextra \ +-lboost_system -pthread -pedantic -Wall -Wextra -fopenmp \ -Wcast-align -Wcast-qual -Wformat=2 -Winit-self \ -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept \ -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow \ diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index 5d52447..e07f990 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "solvability_calc.h" #include "../solver/solver.h" @@ -136,71 +138,60 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s 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> futures; - sol_rules sr = rules; uint64_t cc = cache_capacity; +#pragma omp parallel for 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 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->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; - 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->second.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++; - } + + 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 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->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; + 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->second.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++; + } } - - for (auto& f : futures) f.wait(); } solvability_calc::seed_result solvability_calc::solve_seed(int seed, millisec timeout, const sol_rules& rules, diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index 823d262..128b0b3 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -20,8 +20,8 @@ public: void calculate_solvability_percentage(uint64_t, int, uint, command_line_helper::streamliner_opt, const std::vector&); -private: typedef std::pair seed_result; +private: struct seed_results { explicit seed_results(std::vector); diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 75ee603..4035d6a 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "../game/global_cache.h" #include "../game/sol_rules.h" -- cgit v1.2.1 From 5f6d6a789f09b255718a4e859226d3b5d6f8703a Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 11:14:49 +0100 Subject: Replace openmp with std::thread --- src/main/evaluation/solvability_calc.cpp | 124 ++++++++++++++++--------------- src/main/evaluation/solvability_calc.h | 15 +++- 2 files changed, 76 insertions(+), 63 deletions(-) diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index e07f990..c981644 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "solvability_calc.h" @@ -30,7 +29,7 @@ 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 { +void solvability_calc::print_header(long t) const { cout << "Calculating solvability percentage...\n\n" "Lower Bound" ", Upper Bound" @@ -123,74 +122,79 @@ void solvability_calc::print_seeds_in_prog(std::set& 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& resume) { - vector 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& resume) { + resume_seeds = vector(begin(resume) + 3, end(resume)); sort(begin(resume_seeds), end(resume_seeds)); - atomic current_seed(resume_seeds.back()); + current_seed = resume_seeds.back(); resume_seeds.pop_back(); - set seeds_in_progress; - seed_results seed_res(resume); - mutex results_mutex; - - millisec timeout(timeout_); - print_header(timeout.count(), stream_opt); - - sol_rules sr = rules; - uint64_t cc = cache_capacity; - -#pragma omp parallel for - for (uint i = 0; i < cores; i++) { - - 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 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->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; - 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; - } + seed_res.solvable = resume[0]; + seed_res.unsolvable = resume[1]; + seed_res.timed_out = resume[2]; + + timeout = millisec(timeout_); + print_header(timeout.count()); + seed_count = seed_count_; + stream_opt = stream_opt_; + + vector threads(cores); + + for (uint c = 0; c < cores; c++) + threads[c] = std::thread(solver_thread, this, c); + + for (uint c = 0; c < cores; c++) + threads[c].join(); +} - results_mutex.lock(); +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++; - seeds_in_progress.erase(my_seed); - seed_res.add_result(final_res->second.sol_type); + while (my_seed < sc->seed_count) { + sc->results_mutex.lock(); + sc->seeds_in_progress.insert(my_seed); + sc->results_mutex.unlock(); - 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); + optional stream_res, no_stream_res, final_res; + + 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; } - print_seeds_in_prog(seeds_in_progress); + } 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; + } - results_mutex.unlock(); + sc->results_mutex.lock(); - my_seed = current_seed++; + 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 print_null_seed_info(); + } else { + print_seed_info(*no_stream_res); } + print_seeds_in_prog(sc->seeds_in_progress); + + sc->results_mutex.unlock(); + + my_seed = sc->current_seed++; } } @@ -208,7 +212,7 @@ solvability_calc::seed_result solvability_calc::solve_seed(int seed, millisec ti // SEED RESULTS CLASS // //////////////////////// -solvability_calc::seed_results::seed_results(vector 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(solver::result::type t) { diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index 128b0b3..cbc3394 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -20,11 +20,11 @@ public: void calculate_solvability_percentage(uint64_t, int, uint, command_line_helper::streamliner_opt, const std::vector&); - typedef std::pair seed_result; private: + typedef std::pair seed_result; struct seed_results { - explicit seed_results(std::vector); + seed_results(); void add_result(solver::result::type); std::atomic solvable; @@ -34,17 +34,26 @@ private: }; // Printing methods - void print_header(long, command_line_helper::streamliner_opt) const; + void print_header(long) const; static void print_general_info(const seed_results&); static void print_seed_info(seed_result); static void print_null_seed_info(); static void print_seeds_in_prog(std::set&); // Solving methods + 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 seeds_in_progress; + std::vector resume_seeds; + std::atomic current_seed; + int seed_count; + command_line_helper::streamliner_opt stream_opt; }; -- cgit v1.2.1 From a42d76f885565035608d093bbc4ef8cb224382ff Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 12:12:05 +0100 Subject: remove openmp cmake flag --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d00921..1815077 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,7 +138,7 @@ endif() ############################################################################### set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -DBOOST_LOG_DYN_LINK \ --lboost_system -pthread -pedantic -Wall -Wextra -fopenmp \ +-lboost_system -pthread -pedantic -Wall -Wextra \ -Wcast-align -Wcast-qual -Wformat=2 -Winit-self \ -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept \ -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow \ -- cgit v1.2.1 From 4c1a4d02f0a5a4d6daf31744ee8012ff78011196 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 14:28:05 +0100 Subject: Add timeout to individual games --- src/main/input-output/input/command_line_helper.cpp | 2 +- src/main/main.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 67224d2..65bbcd6 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(), "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(), "adds a per-game timeout to the solvability percentage generation") + ("timeout", po::value(), "adds a timeout to searches") ("resume", po::value>()->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 ...]") diff --git a/src/main/main.cpp b/src/main/main.cpp index e8f287b..ef7131b 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -18,6 +18,8 @@ using namespace boost; namespace po = boost::program_options; +typedef std::chrono::milliseconds millisec; + const optional gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&, command_line_helper&); void solve_input_files(vector, const sol_rules&, command_line_helper&); @@ -123,7 +125,7 @@ void solve_input_files(const vector input_files, const sol_rules& rules, void solve_game(const game_state& gs, command_line_helper& clh) { solver solv(gs, clh.get_cache_capacity()); - solver::result result = solv.run(); + solver::result result = solv.run(millisec(clh.get_timeout())); if (!clh.get_classify()) { if (result.sol_type == solver::result::type::SOLVED) { -- cgit v1.2.1 From 0c5edfac929ae8e68090af1ef5a4031e9f02f2c2 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 15:00:18 +0100 Subject: Add csv output to single deal and init sol script --- solvability.sh | 17 +++++++++ src/main/evaluation/solvability_calc.cpp | 63 ++------------------------------ src/main/evaluation/solvability_calc.h | 2 - src/main/main.cpp | 16 +++++--- src/main/solver/solver.cpp | 63 ++++++++++++++++++++++++++++++++ src/main/solver/solver.h | 4 ++ 6 files changed, 98 insertions(+), 67 deletions(-) create mode 100755 solvability.sh diff --git a/solvability.sh b/solvability.sh new file mode 100755 index 0000000..0775654 --- /dev/null +++ b/solvability.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ "$#" != 3 ]; then + # e.g.: ./solvability 1000 40 "sol --type alpha-star --timeout 6000" + echo "Usage: \"\"" + exit 1 +fi + +seeds="$1" +cores="$2" +sol_command="$3" + +# Features: smart-sol + +echo "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" + +parallel -j"$cores" "$sol_command" --ra {} --class ::: $(seq "$seeds") 2> /dev/null diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index c981644..85a8f34 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -29,47 +29,6 @@ solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) // PRINTING METHODS // ////////////////////// -void solvability_calc::print_header(long t) 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 interval = binomial_ci::wilson( seed_res.solvable, @@ -88,22 +47,8 @@ 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() { - cout << ", , , , , , , , , , , , "; + cout << ", " << seed; + solver::print_result_csv(res); } void solvability_calc::print_seeds_in_prog(std::set& seeds_in_progress) { @@ -135,7 +80,7 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s seed_res.timed_out = resume[2]; timeout = millisec(timeout_); - print_header(timeout.count()); + solver::print_header(timeout.count(), stream_opt_); seed_count = seed_count_; stream_opt = stream_opt_; @@ -186,7 +131,7 @@ void solvability_calc::solver_thread(solvability_calc* sc, uint core) { if (sc->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 solver::print_null_seed_info(); } else { print_seed_info(*no_stream_res); } diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index cbc3394..25eb844 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -34,10 +34,8 @@ private: }; // Printing methods - void print_header(long) const; static void print_general_info(const seed_results&); static void print_seed_info(seed_result); - static void print_null_seed_info(); static void print_seeds_in_prog(std::set&); // Solving methods diff --git a/src/main/main.cpp b/src/main/main.cpp index ef7131b..55feedf 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -23,7 +23,7 @@ typedef std::chrono::milliseconds millisec; const optional gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&, command_line_helper&); void solve_input_files(vector, 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[]) { @@ -97,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 input_files, const sol_rules& rules, command_line_helper& clh) { @@ -112,7 +112,7 @@ void solve_input_files(const vector 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: "; @@ -122,17 +122,21 @@ void solve_input_files(const vector 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()); solver::result result = solv.run(millisec(clh.get_timeout())); - if (!clh.get_classify()) { + if (clh.get_classify()) { + cout << seed; + solver::print_result_csv(result); + cout << "\n"; + } else { if (result.sol_type == solver::result::type::SOLVED) { solv.print_solution(); } else { cout << "Deal:\n" << gs << "\n"; } + cout << result; } - cout << result; } diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index abe55f5..3aaceb8 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -9,11 +9,13 @@ #include #include #include +#include #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; @@ -24,6 +26,8 @@ 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)) @@ -240,6 +244,65 @@ std::ostream& operator<< (std::ostream& out, const solver::result& r) { << "Time Taken (milliseconds): " << r.time.count() << "\n"; } +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); +} + +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; +} + +void solver::print_null_seed_info() { + cout << ", , , , , , , , , , , , "; +} + const vector solver::get_frontier() const { return frontier; } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 4035d6a..90796f7 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -12,6 +12,7 @@ #include "../game/global_cache.h" #include "../game/sol_rules.h" +#include "../input-output/input/command_line_helper.h" class solver { public: @@ -45,6 +46,9 @@ public: result run(boost::optional = boost::none); void print_solution() 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 get_frontier() const; private: -- cgit v1.2.1 From 4f7d08f7ff56df1b5f4217680e6f5c71ea8972ec Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 16:39:53 +0100 Subject: Add correct solvability script output --- solvability.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/solvability.sh b/solvability.sh index 0775654..e0d157a 100755 --- a/solvability.sh +++ b/solvability.sh @@ -1,17 +1,18 @@ #!/bin/bash -if [ "$#" != 3 ]; then - # e.g.: ./solvability 1000 40 "sol --type alpha-star --timeout 6000" - echo "Usage: \"\"" +if [ "$#" != 4 ]; then + # e.g.: ./solvability 1000 40 "sol --type alpha-star --timeout 6000" results.csv + echo "Usage: \"\" " exit 1 fi seeds="$1" cores="$2" sol_command="$3" +out="$4" # Features: smart-sol -echo "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" +echo "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" > "$out" -parallel -j"$cores" "$sol_command" --ra {} --class ::: $(seq "$seeds") 2> /dev/null +parallel -j"$cores" "$sol_command" --ra {} --class ::: $(seq "$seeds") >> "$out" 2> /dev/null -- cgit v1.2.1 From 3a95f6d9a3be80795c095256b47c51be241df87e Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 25 Jul 2018 17:39:27 +0100 Subject: Update gen results script to work with changes --- generate-results.sh | 20 +++++++++++++++-- solvability.sh | 6 ++---- .../input-output/input/command_line_helper.cpp | 17 +++------------ src/main/main.cpp | 25 +++++++++++++++++++++- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/generate-results.sh b/generate-results.sh index cf41873..0392450 100755 --- a/generate-results.sh +++ b/generate-results.sh @@ -1,5 +1,16 @@ #!/bin/bash +trap 'exit 130' INT + +if [ "$#" != 3 ]; then + echo "Usage: " + exit 1 +fi + +seeds="$1" +cores="$2" +timeout="$3" + declare -a arr=( "alpha-star" "bakers-game" @@ -50,6 +61,8 @@ declare -a arr=( ## Rules incorrect +#"martha" +#"penguin" ## Too difficult (?) @@ -60,12 +73,15 @@ declare -a arr=( DATE=`date +%Y-%m-%d-%H-%M-%S` -mkdir -p results/$DATE +out_dir=results/$DATE + +mkdir -p "$out_dir" +echo "Writing to $out_dir/" for i in "${arr[@]}" do echo "Running $i ..." - sol --type "$i" --solvability 20 --time 60 --stream none > results/$DATE/$i.csv + ./solvability.sh "$seeds" "$cores" "./cmake-build-release/bin/solvitaire --type $i --timeout $timeout" "$out_dir"/$i.csv done echo "Done!" diff --git a/solvability.sh b/solvability.sh index e0d157a..3fc9f7f 100755 --- a/solvability.sh +++ b/solvability.sh @@ -1,8 +1,8 @@ #!/bin/bash if [ "$#" != 4 ]; then - # e.g.: ./solvability 1000 40 "sol --type alpha-star --timeout 6000" results.csv echo "Usage: \"\" " + echo "e.g.: ./solvability.sh 1000 40 \"sol --type alpha-star --timeout 6000\" results.csv" exit 1 fi @@ -11,8 +11,6 @@ cores="$2" sol_command="$3" out="$4" -# Features: smart-sol - echo "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" > "$out" -parallel -j"$cores" "$sol_command" --ra {} --class ::: $(seq "$seeds") >> "$out" 2> /dev/null +parallel -j"$cores" "$sol_command" --str smart-solvability --ra {} --class ::: $(seq "$seeds") >> "$out" 2> /dev/null diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 65bbcd6..5a66aa6 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -52,8 +52,8 @@ command_line_helper::command_line_helper() ("streamliners", po::value(), "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 command_line_helper::get_input_files() { return input_files; } diff --git a/src/main/main.cpp b/src/main/main.cpp index 55feedf..57540d6 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -125,13 +125,36 @@ void solve_input_files(const vector input_files, const sol_rules& rules, void solve_game(const game_state& gs, command_line_helper& clh, int seed) { solver solv(gs, clh.get_cache_capacity()); - solver::result result = solv.run(millisec(clh.get_timeout())); + solver::result result, streamliner_result; + bool smart = clh.get_streamliners() == command_line_helper::streamliner_opt::SMART; + bool run_again = false; + + 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 { + result = solv.run(millisec(clh.get_timeout())); + } 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 { -- cgit v1.2.1