aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/input-output/input/command_line_helper.cpp18
-rw-r--r--src/main/input-output/input/command_line_helper.h3
-rw-r--r--src/main/main.cpp64
-rw-r--r--src/main/solver/solver.cpp76
-rw-r--r--src/main/solver/solver.h8
5 files changed, 116 insertions, 53 deletions
diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp
index 05cb86e..7853b55 100644
--- a/src/main/input-output/input/command_line_helper.cpp
+++ b/src/main/input-output/input/command_line_helper.cpp
@@ -30,6 +30,8 @@ command_line_helper::command_line_helper()
("random", po::value<int>(), "create and solve a random solitaire "
"deal based on a seed. Must supply either 'random',"
"'solvability' or list of deals to be solved.")
+ ("shortest-sols", "for each instance returns the shortest possible "
+ "solution. cannot be supplied alongside '--solvability'")
("solvability", "calculates the solvability "
"percentage of the supplied solitaire game. Must supply "
"either 'random', 'solvability' or list of deals to be "
@@ -88,6 +90,8 @@ bool command_line_helper::parse(int argc, const char* argv[]) {
solvability = (vm.count("solvability") != 0);
+ shortest_sols = (vm.count("shortest-sols") != 0);
+
// Handle logic error scenarios
return assess_errors();
}
@@ -117,6 +121,11 @@ bool command_line_helper::assess_errors() {
return false;
}
+ if (shortest_sols && solvability) {
+ print_shortest_sols_error();
+ return false;
+ }
+
if (!solitaire_type.empty()) {
return assess_sol_type();
} else {
@@ -159,6 +168,11 @@ void command_line_helper::print_too_many_opts_error() {
print_help();
}
+void command_line_helper::print_shortest_sols_error() {
+ LOG_ERROR ("Error: User cannot supply '--shortest-sols' and '--solvability'");
+ print_help();
+}
+
const vector<string> command_line_helper::get_input_files() {
return input_files;
}
@@ -186,3 +200,7 @@ bool command_line_helper::get_classify() {
bool command_line_helper::get_solvability() {
return solvability;
}
+
+bool command_line_helper::get_short_sols() {
+ return shortest_sols;
+}
diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h
index 36049bf..f3ed443 100644
--- a/src/main/input-output/input/command_line_helper.h
+++ b/src/main/input-output/input/command_line_helper.h
@@ -19,6 +19,7 @@ public:
bool get_help();
bool get_classify();
bool get_solvability();
+ bool get_short_sols();
private:
bool assess_errors();
@@ -27,6 +28,7 @@ private:
void print_sol_type_rules_error();
void print_no_opts_error();
void print_too_many_opts_error();
+ void print_shortest_sols_error();
boost::program_options::options_description cmdline_options;
boost::program_options::options_description main_options;
@@ -39,6 +41,7 @@ private:
bool help;
bool classify;
bool solvability;
+ bool shortest_sols;
};
#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H
diff --git a/src/main/main.cpp b/src/main/main.cpp
index 0a0173d..b6fa535 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -21,9 +21,9 @@ using namespace boost;
namespace po = boost::program_options;
const optional<sol_rules> gen_rules(command_line_helper&);
-void solve_random_game(int, const sol_rules&, bool);
-void solve_input_files(vector<string>, const sol_rules&, bool);
-void solve_game(const game_state&, bool);
+void solve_random_game(int, const sol_rules&, bool, bool);
+void solve_input_files(vector<string>, const sol_rules&, bool, bool);
+void solve_game(const game_state&, bool, bool);
void calculate_solvability_percentage(const sol_rules&);
double sol_lower_bound(int, int, int);
double sol_upper_bound(int, int, int);
@@ -47,7 +47,8 @@ int main(int argc, const char* argv[]) {
}
// 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.get_classify());
+ solve_random_game(clh.get_random_deal(), *rules, clh.get_short_sols(),
+ clh.get_classify());
}
// Otherwise there are supplied input files which should be solved
else {
@@ -56,7 +57,8 @@ int main(int argc, const char* argv[]) {
// 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.get_classify());
+ solve_input_files(input_files, *rules, clh.get_short_sols(),
+ clh.get_classify());
}
return EXIT_SUCCESS;
@@ -77,14 +79,15 @@ const optional<sol_rules> gen_rules(command_line_helper& clh) {
}
}
-void solve_random_game(int seed, const sol_rules& rules, bool classify) {
+void solve_random_game(int seed, const sol_rules& rules, bool short_sol,
+ bool classify) {
LOG_INFO ("Attempting to solve with seed: " << seed << "...");
game_state gs(rules, seed);
- solve_game(gs, classify);
+ solve_game(gs, short_sol, classify);
}
void solve_input_files(const vector<string> input_files, const sol_rules& rules,
- bool classify) {
+ bool short_sol, bool classify) {
for (const string& input_file : input_files) {
try {
// Reads in the input file to a json doc
@@ -94,7 +97,7 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules,
game_state gs(rules, in_doc);
LOG_INFO ("Attempting to solve " << input_file << "...");
- solve_game(gs, classify);
+ solve_game(gs, short_sol, classify);
} catch (const runtime_error& error) {
string errmsg = "Error parsing deal file: ";
@@ -104,26 +107,35 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules,
}
}
-void solve_game(const game_state& gs, bool classify) {
- solver solv(gs);
- bool solution = solv.run();
+void solve_game(const game_state& gs, bool short_sol, bool classify) {
+ solver s(gs);
+ solver* solv = &s;
+ bool solution;
+ if (short_sol) {
+ uint bound = 1;
+ solver::sol_state ss;
+ do {
+ LOG_INFO("Depth: " << bound);
+ solver s_(gs);
+ solv = &s_;
+ ss = s_.run_with_cutoff(none, bound++);
+ } while (ss == solver::sol_state::cutoff);
+ solution = ss == solver::sol_state::solved;
+ } else {
+ solution = solv->run() == solver::sol_state::solved;
+ }
ProfilerStop();
- if (classify) {
- if (solution) {
- cout << "Solved\n";
- } else {
- cout << "No solution\n";
- }
+ if (solution) {
+ if (!classify) solv->print_solution();
+ cout << "Solved\n";
} else {
- if (solution) {
- solv.print_solution();
- } else {
- cout << "Deal:\n" << gs
- << "\nNo Possible Solution\nStates Searched: "
- << solv.get_states_searched() << "\n";
- }
+ if (!classify) cout << "Deal:\n" << gs << "\n";
+ cout << "No Possible Solution\n";
}
+
+ cout << "States Searched: " << solv->get_states_searched() << "\n";
+ cout << "Final Depth: " << solv->get_final_depth() << "\n";
}
void calculate_solvability_percentage(const sol_rules& rules) {
@@ -143,7 +155,7 @@ void calculate_solvability_percentage(const sol_rules& rules) {
future<bool> future = std::async(launch::async,
[&sol, &terminate_solver](){
- return sol.run(terminate_solver);
+ return sol.run(terminate_solver) == solver::sol_state::solved;
});
future_status status;
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 463d059..474800c 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -19,6 +19,7 @@ solver::solver(const game_state& gs)
, init_state(gs)
, state(gs)
, states_searched(1)
+ , depth(0)
, root(nullptr, game_state::move(255, 255, 255))
, current_node(&root) {
}
@@ -36,12 +37,19 @@ solver::node::node(node* p, const game_state::move m)
: parent(p), move(m), children() {
}
-bool solver::run(optional<atomic<bool> &> terminate_solver) {
+solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
+ return run_with_cutoff(terminate_solver, none);
+}
+
+solver::sol_state solver::run_with_cutoff(
+ boost::optional<std::atomic<bool> &> terminate_solver,
+ boost::optional<uint> cutoff_depth) {
+ bool cutoff_triggered = false;
bool states_exhausted = false;
- while(!state.is_solved() && !states_exhausted) {
+ 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 false;
+ return sol_state::cutoff;
}
#ifndef NDEBUG
@@ -51,29 +59,34 @@ bool solver::run(optional<atomic<bool> &> terminate_solver) {
LOG_DEBUG(state);
#endif
- // If there is a dominance move available, adds it to the search tree
- // and repeats. Doesn't cache the state.
- optional<game_state::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);
+ if (cutoff_depth && depth >= *cutoff_depth) {
+ cutoff_triggered = true;
+ states_exhausted = revert_to_last_node_with_children();//TODO-
} else {
- // Caches the current state
- bool is_new_state = cache.insert(state);
- if (is_new_state) {
- // Gets the legal moves in the current state
- vector<game_state::move> next_moves = get_next_moves();
-
- // If there are none, reverts to the last node with children
- if (next_moves.empty()) {
- states_exhausted = revert_to_last_node_with_children();
- } else {
- add_children(next_moves);
+ // If there is a dominance move available, adds it to the search tree
+ // and repeats. Doesn't cache the state.
+ optional<game_state::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);//TODO+
+ } else {
+ // Caches the current state
+ bool is_new_state = cache.insert(state);
+ if (is_new_state) {
+ // Gets the legal moves in the current state
+ vector<game_state::move> next_moves = get_next_moves();
+
+ // If there are none, reverts to the last node with children
+ if (next_moves.empty()) {
+ states_exhausted = revert_to_last_node_with_children();//TODO-
+ } else {
+ add_children(next_moves);//TODO+
+ }
+ }
+ // If the state is not a new one, reverts to the last node with children
+ else {
+ states_exhausted = revert_to_last_node_with_children();//TODO-
}
- }
- // If the state is not a new one, reverts to the last node with children
- else {
- states_exhausted = revert_to_last_node_with_children();
}
}
@@ -82,12 +95,18 @@ bool solver::run(optional<atomic<bool> &> terminate_solver) {
if (!states_exhausted) {
current_node = &current_node->children.back();
state.make_move(current_node->move);
+ depth++;
}
states_searched++;
}
- return state.is_solved();
+ if (state.is_solved()) {
+ return sol_state::solved;
+ } else {
+ assert(states_exhausted);
+ return cutoff_triggered ? sol_state::cutoff : sol_state::unsolvable;
+ }
}
void solver::add_children(std::vector<game_state::move>& moves) {
@@ -117,6 +136,7 @@ bool solver::revert_to_last_node_with_children() {
return true;
state.undo_move(current_node->move);
+ depth--;
#ifndef NDEBUG
// Checks that the state after the undo is in the cache
@@ -161,13 +181,17 @@ void solver::print_solution() const {
n = &n->children.back();
}
}
- cout << "States Searched: " << states_searched << "\n";
+ cout << "\n";
}
int solver::get_states_searched() const {
return states_searched;
}
+int solver::get_final_depth() const {
+ return depth;
+}
+
const solver::node& solver::get_search_tree() const {
return root;
}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 2a024ac..b1455e5 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -14,6 +14,7 @@
class solver {
public:
+ enum class sol_state {solved, unsolvable, cutoff};
global_cache cache;
struct node {
@@ -26,9 +27,13 @@ public:
explicit solver(const game_state&);
~solver();
- bool run(boost::optional<std::atomic<bool> &> = boost::none);
+ sol_state run(boost::optional<std::atomic<bool> &> = boost::none);
+ sol_state run_with_cutoff(boost::optional<std::atomic<bool> &>,
+ boost::optional<uint> = boost::none);
+
void print_solution() const;
int get_states_searched() const;
+ int get_final_depth() const;
const node& get_search_tree() const;
private:
@@ -40,6 +45,7 @@ private:
const game_state init_state;
game_state state;
int states_searched;
+ uint depth;
node root;
node* current_node;