aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver
diff options
context:
space:
mode:
authorthecharlesblake2018-10-14 17:03:57 +0100
committerthecharlesblake2018-10-14 17:03:57 +0100
commit1e979990239a846618811f27c002345e9d654fed (patch)
tree439d5cec4643476a3e76af30fb65b88d2f03cbd6 /src/main/solver
parentMerge pull request #100 from thecharlesblake/maximal-built-group (diff)
downloadmetasol-1e979990239a846618811f27c002345e9d654fed.tar.gz
metasol-1e979990239a846618811f27c002345e9d654fed.zip

Add terminated status

Diffstat (limited to 'src/main/solver')
-rw-r--r--src/main/solver/solver.cpp18
-rw-r--r--src/main/solver/solver.h4
2 files changed, 19 insertions, 3 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index e0a5990..c4e8c84 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -10,6 +10,7 @@
#include <malloc.h>
#include <chrono>
#include <iomanip>
+#include <signal.h>
#include "solver.h"
#include "../game/move.h"
@@ -29,6 +30,13 @@ using boost::optional;
using std::fixed;
using std::setprecision;
+static bool sigint = false;
+
+void sigint_handler(int i) {
+ // So it doesn't complain about unused param
+ sigint = i == 1 ? true : true;
+}
+
solver::solver(const game_state& gs, uint64_t cache_capacity)
: cache(lru_cache(gs, cache_capacity))
, init_state(gs)
@@ -52,6 +60,10 @@ solver::node::node(const move m)
}
solver::result solver::run(boost::optional<millisec> timeout) {
+ // Set interrupt handler
+ signal(SIGINT, sigint_handler);
+
+ // Set timings
const clock::time_point start_time = clock::now();
result::type res_type = timeout ? dfs(start_time + *timeout) : dfs();
res.sol_type = res_type;
@@ -66,9 +78,10 @@ solver::result::type solver::dfs(boost::optional<clock::time_point> 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 (end_time && clock::now() >= *end_time) {
return result::type::TIMEOUT;
+ } else if (sigint) {
+ return result::type::TERMINATED;
}
#ifndef NDEBUG
@@ -225,6 +238,9 @@ std::ostream& operator<< (std::ostream& out, const solver::result::type& rt) {
case solver::result::type::MEM_LIMIT:
out << "memory-limit-reached";
break;
+ case solver::result::type::TERMINATED:
+ out << "terminated";
+ break;
}
return out;
}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 6b5704e..74c73a8 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -26,7 +26,7 @@ public:
};
struct result {
- enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT };
+ enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED };
type sol_type;
uint64_t states_searched;
@@ -73,6 +73,6 @@ private:
std::ostream& operator<< (std::ostream&, const solver::result::type&);
std::ostream& operator<< (std::ostream&, const solver::result&);
-
+void sigint_handler(int i);
#endif //SOLVITAIRE_SOLVER_H