diff options
| -rw-r--r-- | centre.cpp | 50 | ||||
| -rw-r--r-- | metasol.cpp | 63 | ||||
| -rw-r--r-- | metasol.hpp | 55 | ||||
| m--------- | solvitaire | 0 |
4 files changed, 102 insertions, 66 deletions
@@ -18,12 +18,13 @@ Usage: centre <rules_file> [options] centre --help|-h Options: - -h --help Display this help. - -g FILE --game FILE Specify a file describing a game-state. --dealseed SEED Specify a seed to use when randomly generating a game (set to 0 to use an unspecified seed). - --solveseed SEED Specify a seed for the solver to use - (set to 0 to use an unspecified seed). + -h --help Display this help. + -f --forever Run forever, on as many games as possible. + -g FILE --game FILE Specify a file describing a game-state. + --solveseed SEED Specify a seed for the solver to use (set to 0 to use + an unspecified seed). -s FILE --settings FILE Specify a settings file. -v --version Display version.)"; @@ -544,7 +545,7 @@ ms_settings get_settings(user_data *d) { s.reserved_move_count = 0u; s.max_concurrent_threads = 24u; s.max_concurrent_games = 5u; - s.max_votes = 10u; + s.vote_count = 10u; s.user_data = (void *) d; @@ -773,6 +774,15 @@ void json_settings(ms_settings *s, const char *filename) { &s->max_concurrent_games); arp("seed", NUMBER_TYPE, &s->seed); + arp("solveseed", NUMBER_TYPE, &s->seed); + arp("solve seed", NUMBER_TYPE, &s->seed); + arp("solve_seed", NUMBER_TYPE, &s->seed); + arp("solve-seed", NUMBER_TYPE, &s->seed); + + arp("forever", BOOL_TYPE, &s->forever); + arp("run forever", BOOL_TYPE, &s->forever); + arp("run_forever", BOOL_TYPE, &s->forever); + arp("run-forever", BOOL_TYPE, &s->forever); } default: { assert(false); @@ -1106,30 +1116,28 @@ ms_settings make_settings(std::map<std::string, docopt::value> &args, strtoll(args["--solveseed"].asString().c_str(), NULL, 0); } + if (args["--forever"]) { + s.forever = args["--forever"].asBool(); + } + s.rng = std::default_random_engine{s.seed}; return s; } -void parse_args(int argc, char **argv, ms_game_state *gs, ms_rules *r, - ms_settings *s, user_data *d) { - +int main(int argc, char **argv) { auto args = docopt::docopt(USAGE, { argv + 1, argv + argc }, true, VERSION_STR); - *s = make_settings(args, d); - *r = make_rules(args); - *gs = make_game_state(r, args); -} - -int main(int argc, char **argv) { - ms_rules r; - ms_settings s; - ms_game_state gs; - user_data d = get_user_data(); + ms_settings s = make_settings(args, &d); + ms_rules r = make_rules(args); - parse_args(argc, argv, &gs, &r, &s, &d); + if (s.forever) { + return ms_run_many(&r, &s); + } else { + ms_game_state gs = make_game_state(&r, args); + ctpl::thread_pool tp(s.max_concurrent_threads); - ctpl::thread_pool tp(s.max_concurrent_threads); - return ms_run(&gs, &r, &s, &tp); + return ms_run(&gs, &r, &s, &tp); + } } diff --git a/metasol.cpp b/metasol.cpp index b77dee4..40ce566 100644 --- a/metasol.cpp +++ b/metasol.cpp @@ -353,8 +353,11 @@ ms_settings fetch_default_settings() { s.solved_func = NULL; s.reserved_move_count = 1u; s.user_data = NULL; - s.max_concurrent_threads = 4u; - s.max_votes = 100u; + s.max_concurrent_threads = 1u; + s.max_concurrent_games = 1u; + s.vote_count = 100u; + s.forever = false; + s.seed = 0u; return s; } @@ -626,13 +629,19 @@ bool go_through_stock(ms_game_state *gs, ms_rules *r) { } bool make_move(ms_game_state *gs, ms_rules *r, ms_move *m) { + bool result; + if (m->stock) { - return go_through_stock(gs, r); + result = go_through_stock(gs, r); } else { ms_card_pile *from = get_pile_by_index(gs, r, m->from); ms_card_pile *to = get_pile_by_index(gs, r, m->to); - return move_atop(from, to, m->size, true); + result = move_atop(from, to, m->size, true); } + + gs->moves_made.push_back(*m); + + return result; } char const *finished_state_str(finished_state f) { @@ -839,7 +848,7 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s, ctpl::thread_pool *thpool, vote *votes, thread_info *t_infos, ms_game_state *shuffled_states) { - unsigned vote_count = s->max_votes; + unsigned vote_count = s->vote_count; ms_move m; /* @@ -872,7 +881,7 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s, * Print information about each voter. */ debug( "%-15s %-10s %3s\n", "state", "move", "moves left"); - for (unsigned i = 0; i < s->max_votes; ++i) { + for (unsigned i = 0; i < s->vote_count; ++i) { if (votes[i].moves.empty()) { debug("%-15s\n", finished_state_str(votes[i].result)); } else { @@ -905,27 +914,17 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s, int ms_run(ms_game_state *gs, ms_rules *r, ms_settings *s, ctpl::thread_pool *tp) { - /* - * Here is where the memory required for storing all of the information - * about the maximum number of voters is allocated. Currently the largest - * memory quantity that could possibly be required (the case where the max - * number of votes is needed), although if this number is significantly - * larger than the usual quantity required then maybe this should start low - * and be stepped up only if necessary. - */ -# define gimme_mem(t) ((t *) malloc(sizeof(t) * s->max_votes)) - - std::vector<vote> votes(s->max_votes);; + std::vector<vote> votes(s->vote_count); for (vote &v : votes) { v.result = CANCELLED; } - thread_info *t_infos = gimme_mem(thread_info); - ms_game_state *states = gimme_mem(ms_game_state); + std::vector<thread_info> t_infos(s->vote_count); + std::vector<ms_game_state> states(s->vote_count); solve_status x; do { - x = run_loop(gs, r, s, tp, &votes[0], t_infos, states); + x = run_loop(gs, r, s, tp, &votes[0], t_infos.data(), states.data()); } while (x == KEEP_GOING); printf("solveseed: %lu\tdealseed: %lu\tsuccess: %s\n", s->seed, gs->seed, @@ -947,20 +946,38 @@ int ms_run_single(ms_rules *r, ms_settings *s, unsigned long seed) { int ms_run_infinite(int tid, ms_rules *r, ms_settings *s, ctpl::thread_pool *tp, unsigned n, unsigned i) { - for (unsigned long id = i; true; i += n) { + for (unsigned long id = i; true; id += n) { run_single(r, s, id, tp); } return 0; } +ctpl::thread_pool *main_thread_pool; +void exit_run_many(int _) { + main_thread_pool->interrupt(false); +} + int ms_run_many(ms_rules *r, ms_settings *s) { unsigned n = s->max_concurrent_games; ctpl::thread_pool game_tp(n); + std::vector<std::future<int>> fs; ctpl::thread_pool main_tp(s->max_concurrent_threads); - for (unsigned i = 0; i < n; ++i) { - game_tp.push(ms_run_infinite, r, s, &main_tp, n, i); + for (unsigned i = 1; i <= n; ++i) { + fs.push_back(game_tp.push(ms_run_infinite, r, s, &main_tp, n, i)); } + + main_thread_pool = &main_tp; + /* signal(SIGINT, exit_run_many); */ + + /* + * these will run forever, unless stopped by ^C + */ + for (auto &f : fs) { + assert(f.valid()); + f.wait(); + } + return 0; } diff --git a/metasol.hpp b/metasol.hpp index ba1451c..939effd 100644 --- a/metasol.hpp +++ b/metasol.hpp @@ -571,26 +571,6 @@ typedef std::vector<ms_card> ms_card_pile; ms_card ms_str_card(char *str, bool fd); /** - * Representation of a deal-in-progress. - * - * Basically just the exact status of each pile. - */ -typedef struct { - std::vector<ms_card_pile> foundations; - ms_card_pile stock; - ms_card_pile waste; - std::vector<ms_card_pile> tableau; - ms_card_pile hole; - std::vector<ms_card_pile> cells; - ms_card_pile reserve; - - /** - * The seed used for random generation of the game-state. - */ - long seed; -} ms_game_state; - -/** * Representation of a move. * * Stock moves are treated differently from regular moves. @@ -622,6 +602,32 @@ typedef struct { } ms_move; /** + * Representation of a deal-in-progress. + * + * Basically just the exact status of each pile. + */ +typedef struct { + std::vector<ms_card_pile> foundations; + ms_card_pile stock; + ms_card_pile waste; + std::vector<ms_card_pile> tableau; + ms_card_pile hole; + std::vector<ms_card_pile> cells; + ms_card_pile reserve; + + /** + * The seed used for random generation of the game-state. + */ + long seed; + + /** + * The moves made in order to get to this game-state. + */ + std::vector<ms_move> moves_made; + +} ms_game_state; + +/** * The reported status of a voter after a run. */ enum finished_state { @@ -636,7 +642,7 @@ enum finished_state { * the game-state it was given. */ NO_SOLUTION, - + /** * The voter was unable to find a solution _or_ exhaust all options before * stopping for some other reason. @@ -755,7 +761,7 @@ typedef struct { /** * How many votes are used to determine the next move. */ - unsigned max_votes; + unsigned vote_count; /** * The seed used when shuffling face-down cards around. @@ -767,6 +773,11 @@ typedef struct { */ std::default_random_engine rng; + /** + * `true` when running infinitely. + */ + bool forever; + } ms_settings; /** diff --git a/solvitaire b/solvitaire -Subproject 5c1bd898620e9e99f7d9f650f6b6cda5d1432ef +Subproject 4459c337fdc014bc30568b2adf873ad91524a7e |
