From a996283af7c21baaff0e114d58c34db820902c6f Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sun, 27 Jan 2019 15:37:06 +0000 Subject: Reorganise repo All solvitaire content has been shoved into a subdirectory to tidy up the root of the project. This also means solvitaire is treated similarly to other dependencies of the project, and allows other dependencies to be added easily. There's also a lot of other work done here, such as usign a thread pool library to manage the running of the voters. --- solvitaire/src/main/main.cpp | 187 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 solvitaire/src/main/main.cpp (limited to 'solvitaire/src/main/main.cpp') diff --git a/solvitaire/src/main/main.cpp b/solvitaire/src/main/main.cpp new file mode 100644 index 0000000..faed0ca --- /dev/null +++ b/solvitaire/src/main/main.cpp @@ -0,0 +1,187 @@ +#include +#include + +#include "../../lib/rapidjson/document.h" +#include "input-output/input/command_line_helper.h" +#include "input-output/input/sol_preset_types.h" +#include "input-output/input/json-parsing/json_helper.h" +#include "input-output/input/json-parsing/rules_parser.h" +#include "input-output/output/log_helper.h" +#include "solver/solver.h" +#include "evaluation/solvability_calc.h" +#include "evaluation/benchmark.h" + +using namespace rapidjson; + +using namespace std; +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&); +void solve_game(const sol_rules& rules, command_line_helper& clh, optional seed, optional in_doc); +pair solve_game(const sol_rules& rules, uint64_t timeout, uint64_t cache_capacity, + game_state::streamliner_options str_opts, + optional seed, optional in_doc); + +// Decides what to do given supplied command-line options +int main(int argc, const char* argv[]) { + + // Parses the command-line options + command_line_helper clh; + if (!clh.parse(argc, argv)) { + return EXIT_FAILURE; + } + + // If the user has asked for the list of preset game types, prints it + if (clh.get_available_game_types()) { + sol_preset_types::print_available_games(); + return EXIT_SUCCESS; + } + + string game_rule_str = clh.get_describe_game_rules(); + if (!game_rule_str.empty()) { + sol_preset_types::describe_game_rules(game_rule_str); + return EXIT_SUCCESS; + } + + // Generates the rules of the solitaire from the game type + const optional rules = gen_rules(clh); + if (!rules) return EXIT_FAILURE; + + if (clh.get_deal_only()) { + game_state gs(*rules, clh.get_random_deal(), game_state::streamliner_options::NONE); + json_helper::print_game_state_as_json(gs); + return EXIT_SUCCESS; + } + + // 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_timeout(), clh.get_solvability(), clh.get_cores(), + clh.get_streamliners(), clh.get_resume()); + } + // If a random deal seed has been supplied, solves it + else if (clh.get_random_deal() != -1) { + solve_random_game(clh.get_random_deal(), *rules, clh); + } + // If the benchmark option has been supplied, generates it + else if (clh.get_benchmark()) { + benchmark::run(*rules, clh.get_cache_capacity(), clh.get_streamliners_game_state()); + } + // Otherwise there are supplied input files which should be solved + else { + const vector input_files = clh.get_input_files(); + + // If there are no input files, solve a random deal based on the + // supplied seed + assert(!input_files.empty()); + solve_input_files(input_files, *rules, clh); + } + + return EXIT_SUCCESS; +} + +// Generates the game rules given the command line options +const optional gen_rules(command_line_helper& clh) { + try { + if (!clh.get_solitaire_type().empty()) { + return rules_parser::from_preset(clh.get_solitaire_type()); + } else { + return rules_parser::from_file(clh.get_rules_file()); + } + } catch (const runtime_error& error) { + string errmsg = "Error in rules generation: "; + errmsg += error.what(); + LOG_ERROR(errmsg); + return none; + } +} + +void solve_random_game(int seed, const sol_rules& rules, command_line_helper& clh) { + if (!clh.get_classify()) + LOG_INFO ("Attempting to solve with seed: " << seed << "..."); + solve_game(rules, clh, seed, none); +} + +void solve_input_files(const vector input_files, const sol_rules& rules, command_line_helper& clh) { + for (const string& input_file : input_files) { + try { + // Reads in the input file to a json doc + const Document in_doc = json_helper::get_file_json(input_file); + + LOG_INFO ("Attempting to solve " << input_file << "..."); + solve_game(rules, clh, none, in_doc); + + } catch (const runtime_error& error) { + string errmsg = "Error parsing deal file: "; + errmsg += error.what(); + LOG_ERROR(errmsg); + } + } +} + +void solve_game(const sol_rules& rules, command_line_helper& clh, optional seed, optional in_doc) { + typedef pair solve_sol; + + bool smart = clh.get_streamliners() == command_line_helper::streamliner_opt::SMART; + + uint64_t timeout; + game_state::streamliner_options str_opt; + if (smart) { + timeout = clh.get_timeout() / 10; + str_opt = game_state::streamliner_options::BOTH; + } else { + timeout = clh.get_timeout(); + str_opt = clh.get_streamliners_game_state(); + } + solve_sol solution = solve_game(rules, timeout, clh.get_cache_capacity(), str_opt, seed, in_doc); + + bool run_again = smart && solution.second.sol_type != solver::result::type::SOLVED; + cout.flush(); + if (run_again) + if (!clh.get_classify()) cout << "Unsolvable using streamliner. Running again...\n"; + optional streamliner_solution = run_again + ? solve_game(rules, clh.get_timeout(), clh.get_cache_capacity(), game_state::streamliner_options::NONE, seed, in_doc) + : optional(); + + if (clh.get_classify()) { + if (seed) cout << *seed; + solver::print_result_csv(solution.second); + if (smart) { + if (run_again) { + solver::print_result_csv(streamliner_solution->second); + cout << ", " << streamliner_solution->second.sol_type; + } else { + solver::print_null_seed_info(); + cout << ", " << solution.second.sol_type; + } + } else { + cout << ", " << solution.second.sol_type; + } + cout << "\n"; + } else { + pair s = run_again ? *streamliner_solution : solution; + + if (s.second.sol_type == solver::result::type::SOLVED) { + s.first.print_solution(); + } else { + cout << "Deal:\n" << s.first.init_state << "\n"; + } + cout << s.second; + } + cout.flush(); +} + +pair solve_game(const sol_rules& rules, uint64_t timeout, uint64_t cache_capacity, + game_state::streamliner_options str_opts, + optional seed, optional in_doc) { + game_state gs = seed ? game_state(rules, *seed, str_opts) : game_state(rules, *in_doc, str_opts); + solver sol(gs, cache_capacity); + solver::result res = sol.run(std::chrono::milliseconds(timeout)); + return make_pair(sol, res); +} -- cgit v1.2.1