aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver
diff options
context:
space:
mode:
authorTom Harley2019-01-27 15:37:06 +0000
committerTom Harley2019-01-27 15:37:06 +0000
commita996283af7c21baaff0e114d58c34db820902c6f (patch)
tree2fe5b6f7a800d4a7b8a65ee345f8c98aa138e36b /src/main/solver
parentSplit code into 3 files (diff)
downloadmetasol-a996283af7c21baaff0e114d58c34db820902c6f.tar.gz
metasol-a996283af7c21baaff0e114d58c34db820902c6f.zip

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.

Diffstat (limited to 'src/main/solver')
-rw-r--r--src/main/solver/solver.cpp318
-rw-r--r--src/main/solver/solver.h82
2 files changed, 0 insertions, 400 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
deleted file mode 100644
index e10396f..0000000
--- a/src/main/solver/solver.cpp
+++ /dev/null
@@ -1,318 +0,0 @@
-//
-// Created by thecharlesblake on 10/24/17.
-//
-
-#include <string>
-#include <ostream>
-#include <iostream>
-#include <algorithm>
-#include <list>
-#include <malloc.h>
-#include <chrono>
-#include <iomanip>
-#include <signal.h>
-
-#include "solver.h"
-#include "../game/move.h"
-#include "../input-output/output/log_helper.h"
-#include "../input-output/output/state_printer.h"
-#include "../input-output/input/command_line_helper.h"
-
-using std::atomic;
-using std::vector;
-using std::cout;
-using std::clog;
-using std::pair;
-using std::max;
-using std::begin;
-using std::end;
-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)
- , state(gs)
- , frontier()
- , root(move(move::mtype::null))
- , current_node() {
- frontier.push_back(root);
- current_node = begin(frontier);
- res.states_searched = 0;
- res.unique_states_searched = 0;
- res.backtracks = 0;
- res.dominance_moves = 0;
- res.states_removed_from_cache = 0;
- res.max_depth = 0;
- res.depth = 0;
-}
-
-solver::node::node(const move m)
- : mv(m), child_moves(), cache_state() {
-}
-
-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;
- res.states_removed_from_cache = cache.get_states_removed_from_cache();
- res.cache_size = cache.size();
- res.cache_bucket_count = cache.bucket_count();
- res.time = std::chrono::duration_cast<millisec>(clock::now() - start_time);
- return res;
-}
-
-solver::result::type solver::dfs(boost::optional<clock::time_point> end_time) {
- bool states_exhausted = false;
-
- while(!(state.is_solved() || states_exhausted)) {
- if (end_time && clock::now() >= *end_time) {
- return result::type::TIMEOUT;
- } else if (sigint) {
- return result::type::TERMINATED;
- }
-
-#ifndef NDEBUG
- if (current_node->mv.type == move::mtype::dominance) {
- LOG_DEBUG("(dominance move)");
- }
- 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<move> dominance_move = state.get_dominance_move();
- if (dominance_move) {
- // Adds the dominance move as a child of the current search node;
- current_node->child_moves.emplace_back(*dominance_move);
- } else {
- try {
- // Caches the current state
- pair<lru_cache::item_list::iterator, bool> insert_res = cache.insert(state);
- current_node->cache_state = insert_res.first;
- bool is_new_state = insert_res.second;
-
- if (is_new_state) {
- // Gets the legal moves in the current state
- vector<move> next_moves = state.get_legal_moves(current_node->mv);
-
- // If there are none, reverts to the last node with children
- if (next_moves.empty()) {
- states_exhausted = revert_to_last_node_with_children(insert_res.first);
- } else {
- current_node->child_moves = std::move(next_moves);
- }
- }
- // If the state is not a new one, reverts to the last node with children
- else {
- res.unique_states_searched--;
- states_exhausted = revert_to_last_node_with_children();
- }
- } catch (const std::runtime_error e) {
- return result::type::MEM_LIMIT;
- }
- }
-
- // Sets the current node to one of its children
- assert(states_exhausted == current_node->child_moves.empty());
- if (!states_exhausted) {
- set_to_child();
- state.make_move(current_node->mv);
- res.depth++;
- res.max_depth = max(res.depth, res.max_depth);
- if (current_node->mv.type == move::mtype::dominance) res.dominance_moves++;
- }
-
- res.states_searched++;
- res.unique_states_searched++;
- }
-
- if (state.is_solved()) {
- return result::type::SOLVED;
- } else {
- assert(states_exhausted);
- return result::type::UNSOLVABLE;
- }
-}
-
-// Called when the current node's children have been exhausted. Travels back up
-// the search tree until it finds a node which still has children. Returns true
-// unless all children have been exhausted.
-
-// If an iterator to the current state is supplied to the function, will also
-// make sure to turn the 'live' bit off upon backtracking
-bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::iterator> cur_state) {
- if (current_node == begin(frontier))
- return true;
-
- // Turns the 'live' bit false on the state we are backtracking out of
- if (cur_state) cache.set_non_live(*cur_state);
-
- state.undo_move(current_node->mv);
- res.depth--;
- res.backtracks++;
-
-#ifndef NDEBUG
- // Checks that the state after the undo is in the cache
- // (as long as the move wasn't a dominance move)
-
- if (current_node->mv.type != move::mtype::dominance) {
- if (cache.get_states_removed_from_cache() == 0) {
- assert(cache.contains(state));
- }
- LOG_DEBUG("(undo move)");
- } else {
- LOG_DEBUG("(undo dominance move)");
- }
- LOG_DEBUG(state);
-#endif
-
- // Gets a reference to the parent state which can be supplied if this function is
- // called recursively. This ensures that the cached state's 'live' bit is set as appropriate
- optional<lru_cache::item_list::iterator> p_state = prev(current_node)->cache_state;
-
- // Reverts the current node to its parent and removes it
- frontier.pop_back();
- current_node = prev(end(frontier));
-
- // If the current node now has no children, repeat
- if (current_node->child_moves.empty()) {
- return revert_to_last_node_with_children(p_state);
- } else {
- return false;
- }
-}
-
-void solver::set_to_child() {
- assert(!current_node->child_moves.empty());
-
- move b = current_node->child_moves.back();
- current_node->child_moves.pop_back();
- frontier.emplace_back(b);
-
- current_node = prev(end(frontier));
-}
-
-void solver::print_solution() const {
- std::flush(clog);
- std::flush(cout);
-
- auto i = begin(frontier);
- game_state state_copy = init_state;
-
- cout << "Solution:\n";
- cout << state_copy << "\n";
-
- if (res.states_searched > 1) {
- while (++i != end(frontier)) {
- state_copy.make_move(i->mv);
- cout << state_copy << "\n";
- }
- }
- cout << "\n";
-}
-
-std::ostream& operator<< (std::ostream& out, const solver::result::type& rt) {
- switch(rt) {
- case solver::result::type::TIMEOUT:
- out << "timed-out";
- break;
- case solver::result::type::SOLVED:
- out << "solved";
- break;
- case solver::result::type::UNSOLVABLE:
- out << "unsolvable";
- break;
- case solver::result::type::MEM_LIMIT:
- out << "memory-limit-reached";
- break;
- case solver::result::type::TERMINATED:
- out << "terminated";
- break;
- }
- return out;
-}
-
-std::ostream& operator<< (std::ostream& out, const solver::result& r) {
- return out
- << "Solution Type: " << r.sol_type << "\n"
- << "States Searched: " << r.states_searched << "\n"
- << "Unique States Searched: " << r.unique_states_searched << "\n"
- << "Backtracks: " << r.backtracks << "\n"
- << "Dominance Moves: " << r.dominance_moves << "\n"
- << "States Removed From Cache: " << r.states_removed_from_cache << "\n"
- << "Final States In Cache: " << r.cache_size << "\n"
- << "Final Buckets In Cache: " << r.cache_bucket_count << "\n"
- << "Maximum Search Depth: " << r.max_depth << "\n"
- << "Final Search Depth: " << r.depth << "\n"
- << "Time Taken (milliseconds): " << r.time.count() << "\n";
-}
-
-void solver::print_header(long t, command_line_helper::streamliner_opt stream_opt) {
- cout << "Calculating solvability percentage...\n\n";
- if (stream_opt == command_line_helper::streamliner_opt::SMART) {
- cout << ", (Streamliner Results:) "
- "Attempted Seed"
- ", Outcome"
- ", Time Taken(ms)"
- ", States Searched"
- ", Unique States Searched"
- ", Backtracks"
- ", Dominance Moves"
- ", States Removed From Cache"
- ", Final States In Cache"
- ", Final Buckets In Cache"
- ", Maximum Search Depth"
- ", Final Search Depth"
- ", (Non-Streamliner Results:) ";
- }
- cout << "Attempted Seed"
- ", Outcome"
- ", Time Taken(ms)"
- ", States Searched"
- ", Unique States Searched"
- ", Backtracks"
- ", Dominance Moves"
- ", States Removed From Cache"
- ", Final States In Cache"
- ", Final Buckets In Cache"
- ", Maximum Search Depth"
- ", Final Search Depth"
- ", Overall Result"
- "\n--- Timeout = " << t << " milliseconds ---\n";
- cout << fixed << setprecision(3);
-}
-
-void solver::print_result_csv(solver::result res) {
- cout << ", " << res.sol_type
- << ", " << res.time.count()
- << ", " << res.states_searched
- << ", " << res.unique_states_searched
- << ", " << res.backtracks
- << ", " << res.dominance_moves
- << ", " << res.states_removed_from_cache
- << ", " << res.cache_size
- << ", " << res.cache_bucket_count
- << ", " << res.max_depth
- << ", " << res.depth;
-}
-
-void solver::print_null_seed_info() {
- cout << ", , , , , , , , , , , ";
-}
-
-const vector<solver::node> solver::get_frontier() const {
- return frontier;
-}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
deleted file mode 100644
index fec3f4b..0000000
--- a/src/main/solver/solver.h
+++ /dev/null
@@ -1,82 +0,0 @@
-//
-// Created by thecharlesblake on 10/24/17.
-//
-
-#ifndef SOLVITAIRE_SOLVER_H
-#define SOLVITAIRE_SOLVER_H
-
-#include <string>
-#include <vector>
-#include <atomic>
-#include <chrono>
-
-#include "../game/global_cache.h"
-#include "../game/sol_rules.h"
-#include "../input-output/input/command_line_helper.h"
-
-class solver {
-public:
- lru_cache cache;
-
- struct node {
- node(move);
- const move mv;
- std::vector<move> child_moves;
- boost::optional<lru_cache::item_list::iterator> cache_state; // Optional, as dominance moves aren't cached
- };
-
- struct result {
-#ifdef C2NIM
- enum type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED };
-#else
- enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED };
-#endif
-
- type sol_type;
- uint64_t states_searched;
- uint64_t unique_states_searched;
- uint64_t backtracks;
- uint64_t dominance_moves;
- uint64_t states_removed_from_cache;
- lru_cache::item_list::size_type cache_size;
- lru_cache::item_list::size_type cache_bucket_count;
- uint64_t max_depth;
- uint64_t depth;
- std::chrono::milliseconds time;
- };
-
- explicit solver(const game_state&, uint64_t);
-
- result run(boost::optional<std::chrono::milliseconds> = boost::none);
-
- void print_solution() const;
- static void print_header(long, command_line_helper::streamliner_opt);
- static void print_result_csv(solver::result);
- static void print_null_seed_info();
- const std::vector<node> get_frontier() const;
-
- const game_state init_state;
-
-private:
- typedef std::chrono::high_resolution_clock clock;
- typedef std::chrono::milliseconds millisec;
-
- result::type dfs(boost::optional<clock::time_point> = boost::none);
-
- bool revert_to_last_node_with_children(boost::optional<lru_cache::item_list::iterator> = boost::none);
- void set_to_child();
-
- game_state state;
- std::vector<node> frontier;
-
- result res;
-
- node root;
- std::vector<node>::iterator current_node;
-};
-
-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