aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver
diff options
context:
space:
mode:
authorCharlie Blake2018-01-09 08:58:30 +0000
committerGitHub2018-01-09 08:58:30 +0000
commit93bfbeec309b027331f75d10864c99f19a958cfd (patch)
tree5ad08a4621dbc118480553d53666e3e507ce584e /src/main/solver
parentMerge pull request #27 from thecharlesblake/efficient-implementation (diff)
parentAdd spider to benchmark script (diff)
downloadmetasol-93bfbeec309b027331f75d10864c99f19a958cfd.tar.gz
metasol-93bfbeec309b027331f75d10864c99f19a958cfd.zip

Merge pull request #28 from thecharlesblake/all-canonical-games

All canonical games

Diffstat (limited to 'src/main/solver')
-rw-r--r--src/main/solver/solver.cpp26
-rw-r--r--src/main/solver/solver.h2
2 files changed, 14 insertions, 14 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 23ed693..aaaf530 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -6,10 +6,9 @@
#include <ostream>
#include <iostream>
#include <algorithm>
-#include <utility>
#include "solver.h"
-#include "../input-output/log_helper.h"
+#include "../input-output/output/log_helper.h"
using namespace std;
using namespace boost;
@@ -18,19 +17,19 @@ solver::solver(const game_state& gs, const sol_rules& sr)
: initial_state(gs), rules(sr), states_searched(0) {}
solver::node::node(const game_state::move m,
- vector<game_state::move> &&uc)
+ vector<game_state::move> uc)
: move(m), unsearched_children(std::move(uc)) {
}
bool solver::run() {
game_state state = initial_state;
- frontier.emplace_back(NULL_MOVE, state.get_legal_moves());
states_searched++;
+ LOG_DEBUG (state);
+ frontier.emplace_back(game_state::null_move(), state.get_legal_moves());
while (!frontier.empty()) {
node& current = frontier.back();
- LOG_DEBUG (state);
// If we have a solution, returns true
if (state.is_solved()) return true;
@@ -45,23 +44,24 @@ bool solver::run() {
// Returns to the previous state
frontier.pop_back();
} else {
+ game_state::move next_move = current.unsearched_children[current.unsearched_children.size()-1];
+ current.unsearched_children.pop_back();
+
// Applies the first possible move in this state
- state.make_move(current.unsearched_children.back());
+ state.make_move(next_move);
// Insert the state into the global cache
bool is_new_state = global_cache.insert(state.get_data()).second;
if (is_new_state) {
+ states_searched++;
+ LOG_DEBUG (state);
// If the state is new, creates a new node in the frontier to
// represent the move just made, and those that are now possible
- frontier.emplace_back(current.unsearched_children.back(),
- state.get_legal_moves());
- states_searched++;
+ frontier.emplace_back(next_move, state.get_legal_moves());
} else {
- // If we've seen the state before, undo the move
- state.undo_move(current.unsearched_children.back());
+ // If we've seen the state before, undoes the move
+ state.undo_move(next_move);
}
- // Removes the move just made from the list of possible moves
- current.unsearched_children.pop_back();
}
}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 1ef841d..563a11e 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -20,7 +20,7 @@ public:
> global_cache;
struct node {
- explicit node(const game_state::move, std::vector<game_state::move>&&);
+ explicit node(game_state::move, std::vector<game_state::move>);
const game_state::move move;
std::vector<game_state::move> unsearched_children;
};