aboutsummaryrefslogtreecommitdiff
path: root/src/main/solvability/solvability_calc.cpp
blob: 092e4a3ced72457879c231b380e84982d5b6dd53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by thecharlesblake on 3/21/18.
//
#include <future>
#include <iomanip>
#include <iostream>
#include <cmath>

#include "solvability_calc.h"
#include "../solver/solver.h"

using namespace std;


solvability_calc::solvability_calc(const sol_rules& r) : rules(r) {
    reset();
}

void solvability_calc::reset() {
    current_seed = 0;
    timeout = chrono::milliseconds(1000);
    solvable = 0;
    unsolvable = 0;
    intractable = 0;
}

void solvability_calc::calculate_solvability_percentage() {
    reset();
    print_header();

    while(current_seed++ < INT_MAX) {
        solve_seed();

        double lower_bound = sol_lower_bound();
        double upper_bound = sol_upper_bound();

        print_row(lower_bound, upper_bound);
    }
}

void solvability_calc::print_header() const {
    cout << "Calculating solvability percentage...\n\n"
            "[Lower Bound, Upper Bound] | (Solvable/Unsolvable/Intractable) "
            "| Current seed\n";
    cout << fixed << setprecision(3);
}

void solvability_calc::print_row(double lower_bound, double upper_bound) const {
    cout << "[" << lower_bound * 100 << ", " << upper_bound * 100
         << "] | ("<< solvable << "/" << unsolvable << "/" << intractable
         << ") | " << current_seed << "\n";
}

void solvability_calc::solve_seed() {
    game_state gs(rules, current_seed);
    solver sol(gs);
    atomic<bool> terminate_solver(false);

    future<bool> future = std::async(
            launch::async,
            [&sol, &terminate_solver](){
                return sol.run(terminate_solver) == solver::sol_state::solved;
            }
    );

    future_status status;
    do {
        status = future.wait_for(timeout);

        if (status == future_status::timeout) {
            terminate_solver = true;
        } else if (status == future_status::ready) {
            if (terminate_solver) {
                intractable++;
            } else if (future.get()) {
                solvable++;
            } else {
                unsolvable++;
            }
        }
    } while (status != future_status::ready);
}

double solvability_calc::sol_lower_bound() {
    int n = solvable + unsolvable + intractable;
    int x = solvable;
    double z = 1.96;
    bool lower_bound = true;
    return agrestiCoull(n, x, z, lower_bound);
}

double solvability_calc::sol_upper_bound() {
    int n = solvable + unsolvable + intractable;
    int x = solvable + intractable;
    double z = 1.96;
    bool lower_bound = false;
    return agrestiCoull(n, x, z, lower_bound);
}

double solvability_calc::agrestiCoull(int n, int x, double z, bool lower_bound) {
    double n_ = n + pow(z, 2);
    double p = (x + pow(z, 2)/2)/n_;
    double v = z * sqrt((p - pow(p, 2))/n_);
    if (lower_bound) {
        double ans = p - v;
        return ans < 0 ? 0 : ans;
    } else {
        double ans = p + v;
        return ans > 1 ? 1 : ans;
    }
}