diff options
| author | Tom Harley | 2019-01-27 15:37:06 +0000 |
|---|---|---|
| committer | Tom Harley | 2019-01-27 15:37:06 +0000 |
| commit | a996283af7c21baaff0e114d58c34db820902c6f (patch) | |
| tree | 2fe5b6f7a800d4a7b8a65ee345f8c98aa138e36b /src/main/evaluation | |
| parent | Split code into 3 files (diff) | |
| download | metasol-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/evaluation')
| -rw-r--r-- | src/main/evaluation/benchmark.cpp | 63 | ||||
| -rw-r--r-- | src/main/evaluation/benchmark.h | 19 | ||||
| -rw-r--r-- | src/main/evaluation/binomial_ci.cpp | 45 | ||||
| -rw-r--r-- | src/main/evaluation/binomial_ci.h | 19 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.cpp | 186 | ||||
| -rw-r--r-- | src/main/evaluation/solvability_calc.h | 58 |
6 files changed, 0 insertions, 390 deletions
diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp deleted file mode 100644 index f66573c..0000000 --- a/src/main/evaluation/benchmark.cpp +++ /dev/null @@ -1,63 +0,0 @@ -// -// Created by thecharlesblake on 4/5/18. -// - -#include <climits> -#include <iostream> -#include <numeric> - -#include "benchmark.h" -#include "../game/search-state/game_state.h" -#include "../solver/solver.h" - -using namespace std; -typedef chrono::microseconds microsec; - -void benchmark::run(const sol_rules &rules, uint64_t cache_capacity, game_state::streamliner_options streamliners) { - cout << "Seed " - "| Median/Mean Solution Time(μs) " - "| Median/Mean States Searched " - "| Solvable/Unsolvable"; - - multiset<int> sol_times; - multiset<int> states_searched; - int total_time = 0; - int total_states = 0; - int total_solvable = 0; - int total_unsolvable = 0; - - for(int seed = 1; seed <= 1000; seed++) { - game_state gs(rules, seed, streamliners); - solver sol(gs, cache_capacity); - - auto start = chrono::steady_clock::now(); - solver::result result = sol.run(); - auto end = chrono::steady_clock::now(); - microsec elapsed_micros = - chrono::duration_cast<chrono::microseconds>(end - start); - - sol_times.insert(static_cast<int>(elapsed_micros.count())); - total_time += static_cast<int>(elapsed_micros.count()); - states_searched.insert(result.states_searched); - total_states += result.states_searched; - switch (result.sol_type) { - case solver::result::type::SOLVED: - total_solvable++; - break; - case solver::result::type::UNSOLVABLE: - total_unsolvable++; - break; - default: - assert(false); - break; - } - - cout << "\n" << seed - << " | " << *next(begin(sol_times), (sol_times.size())/2) - << "/" << total_time/sol_times.size() - << " | " << *next(begin(states_searched), (states_searched.size())/2) - << "/" << total_states/sol_times.size() - << " | " << total_solvable - << "/" << total_unsolvable; - } -} diff --git a/src/main/evaluation/benchmark.h b/src/main/evaluation/benchmark.h deleted file mode 100644 index b7b9a1d..0000000 --- a/src/main/evaluation/benchmark.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 4/5/18. -// - -#ifndef SOLVITAIRE_BENCHMARK_H -#define SOLVITAIRE_BENCHMARK_H - -#include <chrono> - -#include "../game/sol_rules.h" -#include "../game/search-state/game_state.h" - -class benchmark { -public: - static void run(const sol_rules &rules, uint64_t, game_state::streamliner_options); -}; - - -#endif //SOLVITAIRE_BENCHMARK_H diff --git a/src/main/evaluation/binomial_ci.cpp b/src/main/evaluation/binomial_ci.cpp deleted file mode 100644 index c381c05..0000000 --- a/src/main/evaluation/binomial_ci.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// -// Created by thecharlesblake on 20/06/18. -// - -#include <cmath> -#include <algorithm> -#include "binomial_ci.h" - -using std::pow; -using std::min; -using std::max; -using std::pair; - -pair<double, double> binomial_ci::wilson(int s_, int f_) { - auto s = static_cast<double>(s_); - auto f = static_cast<double>(f_); - double z = 1.96; - double z2 = pow(z, 2); - double n = s + f; - double p = (s + (z2 / 2.0)) / (n + z2); - double half_ci = (z / (n + z2)) * sqrt(((s * f) / n) + (z2 / 4.0)); - return { p - half_ci - , p + half_ci }; -} - -pair<double, double> binomial_ci::wilson(int s, int f, int i) { - pair<double, double> a = wilson(s, f + i); - pair<double, double> b = wilson(s + i, f); - return {a.first, b.second}; -} - -pair<double, double> binomial_ci::agresti_coull(int s, int f) { - double z = 1.96; - double n_ = s + f + pow(z, 2); - double p = (s + pow(z, 2)/2)/n_; - double half_ci = z * sqrt((p - pow(p, 2))/n_); - return { max(0.0, p - half_ci) - , min(1.0, p + half_ci) }; -} - -pair<double, double> binomial_ci::agresti_coull(int s, int f, int i) { - pair<double, double> a = agresti_coull(s, f + i); - pair<double, double> b = agresti_coull(s + i, f); - return {a.first, b.second}; -} diff --git a/src/main/evaluation/binomial_ci.h b/src/main/evaluation/binomial_ci.h deleted file mode 100644 index 6e2dceb..0000000 --- a/src/main/evaluation/binomial_ci.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 20/06/18. -// - -#ifndef SOLVITAIRE_BINOMIAL_CI_H -#define SOLVITAIRE_BINOMIAL_CI_H - - -#include <utility> - -struct binomial_ci { - static std::pair<double, double> wilson(int, int); - static std::pair<double, double> wilson(int, int, int); - static std::pair<double, double> agresti_coull(int, int); - static std::pair<double, double> agresti_coull(int, int, int); -}; - - -#endif //SOLVITAIRE_BINOMIAL_CI_H diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp deleted file mode 100644 index 14219ab..0000000 --- a/src/main/evaluation/solvability_calc.cpp +++ /dev/null @@ -1,186 +0,0 @@ -// -// Created by thecharlesblake on 3/21/18. -// -#include <future> -#include <iomanip> -#include <iostream> -#include <omp.h> - -#include "solvability_calc.h" -#include "../solver/solver.h" -#include "binomial_ci.h" - -using namespace std; -using boost::optional; - -typedef command_line_helper::streamliner_opt cmd_sos; -typedef game_state::streamliner_options sos; -typedef std::chrono::milliseconds millisec; - -/////////////////// -// SETUP METHODS // -/////////////////// - -solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) : - rules(r), cache_capacity(cache_capacity_) { -} - -////////////////////// -// PRINTING METHODS // -////////////////////// - -void solvability_calc::print_general_info(const seed_results& seed_res) { - pair<double, double> interval = binomial_ci::wilson( - seed_res.solvable, - seed_res.unsolvable, - (seed_res.timed_out + seed_res.mem_limit)); - - cout << interval.first * 100 - << ", " << interval.second * 100 - << ", " << seed_res.solvable - << ", " << seed_res.unsolvable - << ", " << seed_res.timed_out - << ", " << seed_res.mem_limit; -} - -void solvability_calc::print_seed_info(seed_result seed_res) { - auto& res = seed_res.second; - - solver::print_result_csv(res); -} - -void solvability_calc::print_seeds_in_prog(std::set<int>& seeds_in_progress) { - for(auto iter = begin(seeds_in_progress); iter != end(seeds_in_progress); ++iter) { - cout << ", " << *iter; - } - - cout << "\n"; - - std::flush(clog); - std::flush(cout); -} - - -///////////////////// -// SOLVING METHODS // -///////////////////// - -void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count_, uint cores, - cmd_sos stream_opt_, const vector<int>& resume) { - resume_seeds = vector<int>(begin(resume) + 3, end(resume)); - sort(begin(resume_seeds), end(resume_seeds)); - - current_seed = resume_seeds.back(); - resume_seeds.pop_back(); - - seed_res.solvable = resume[0]; - seed_res.unsolvable = resume[1]; - seed_res.timed_out = resume[2]; - - timeout = millisec(timeout_); - solver::print_header(timeout.count(), stream_opt_); - seed_count = seed_count_; - stream_opt = stream_opt_; - - vector<std::thread> threads(cores); - - for (uint c = 0; c < cores; c++) - threads[c] = std::thread(solver_thread, this, c); - - for (uint c = 0; c < cores; c++) - threads[c].join(); -} - -void solvability_calc::solver_thread(solvability_calc* sc, uint core) { - int my_seed = sc->resume_seeds.size() > core ? sc->resume_seeds[core] : sc->current_seed++; - - while (my_seed < sc->seed_count) { - sc->results_mutex.lock(); - sc->seeds_in_progress.insert(my_seed); - sc->results_mutex.unlock(); - - optional<seed_result> stream_res, no_stream_res, final_res; - - if (sc->stream_opt == cmd_sos::SMART) { - stream_res = solve_seed(my_seed, (sc->timeout/10), sc->rules, sc->cache_capacity, sos::BOTH); - - switch (stream_res->second.sol_type) { - case solver::result::type::UNSOLVABLE: - case solver::result::type::TIMEOUT: - no_stream_res = solve_seed(my_seed, sc->timeout, sc->rules, sc->cache_capacity, sos::NONE); - final_res = *no_stream_res; - break; - default: - final_res = *stream_res; - break; - } - } else { - no_stream_res = solve_seed(my_seed, sc->timeout, sc->rules, sc->cache_capacity, - command_line_helper::convert_streamliners(sc->stream_opt)); - final_res = *no_stream_res; - } - - sc->results_mutex.lock(); - - sc->seeds_in_progress.erase(my_seed); - sc->seed_res.add_result(final_res->second.sol_type); - - cout << my_seed; - //print_general_info(sc->seed_res); - if (sc->stream_opt == cmd_sos::SMART) { - print_seed_info(*stream_res); - if (no_stream_res) { - print_seed_info(*no_stream_res); - cout << ", " << no_stream_res->second.sol_type; - } else { - solver::print_null_seed_info(); - cout << ", " << stream_res->second.sol_type; - } - } else { - print_seed_info(*no_stream_res); - cout << ", " << no_stream_res->second.sol_type; - } - //print_seeds_in_prog(sc->seeds_in_progress); - cout << "\n"; - - sc->results_mutex.unlock(); - - my_seed = sc->current_seed++; - } -} - -solvability_calc::seed_result solvability_calc::solve_seed(int seed, millisec timeout, const sol_rules& rules, - uint64_t cache_capacity, - game_state::streamliner_options stream_opt) { - game_state gs(rules, seed, stream_opt); - solver sol(gs, cache_capacity); - - return seed_result(seed, sol.run(optional<std::chrono::milliseconds>(timeout))); -} - - -//////////////////////// -// SEED RESULTS CLASS // -//////////////////////// - -solvability_calc::seed_results::seed_results() : solvable(0), unsolvable(0), timed_out(0), mem_limit(0) { -} - -void solvability_calc::seed_results::add_result(solver::result::type t) { - switch (t) { - case solver::result::type::TIMEOUT: - timed_out++; - break; - case solver::result::type::SOLVED: - solvable++; - break; - case solver::result::type::UNSOLVABLE: - unsolvable++; - break; - case solver::result::type ::MEM_LIMIT: - mem_limit++; - break; - case solver::result::type ::TERMINATED: - break; - } -} diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h deleted file mode 100644 index 25eb844..0000000 --- a/src/main/evaluation/solvability_calc.h +++ /dev/null @@ -1,58 +0,0 @@ -// -// Created by thecharlesblake on 3/21/18. -// - -#ifndef SOLVITAIRE_SOLVABILITY_H -#define SOLVITAIRE_SOLVABILITY_H - -#include <vector> -#include <set> -#include <chrono> -#include <mutex> - -#include "../game/sol_rules.h" -#include "../solver/solver.h" -#include "../input-output/input/command_line_helper.h" - -class solvability_calc { -public: - explicit solvability_calc(const sol_rules&, uint64_t); - - void calculate_solvability_percentage(uint64_t, int, uint, command_line_helper::streamliner_opt, const std::vector<int>&); - -private: - typedef std::pair<int, solver::result> seed_result; - - struct seed_results { - seed_results(); - void add_result(solver::result::type); - - std::atomic<int> solvable; - std::atomic<int> unsolvable; - std::atomic<int> timed_out; - std::atomic<int> mem_limit; - }; - - // Printing methods - static void print_general_info(const seed_results&); - static void print_seed_info(seed_result); - static void print_seeds_in_prog(std::set<int>&); - - // Solving methods - static void solver_thread(solvability_calc*, uint core); - static seed_result solve_seed(int, std::chrono::milliseconds, const sol_rules&, uint64_t, game_state::streamliner_options); - - const sol_rules& rules; - const uint64_t cache_capacity; - std::chrono::milliseconds timeout; - std::mutex results_mutex; - seed_results seed_res; - std::set<int> seeds_in_progress; - std::vector<int> resume_seeds; - std::atomic<int> current_seed; - int seed_count; - command_line_helper::streamliner_opt stream_opt; -}; - - -#endif //SOLVITAIRE_SOLVABILITY_H |
