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
|
//
// 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) {
}
void solvability_calc::calculate_solvability_percentage() const {
cout << "Calculating solvability percentage...\n\n"
"[Lower Bound, Upper Bound] | (Solvable/Unsolvable/Intractable) "
"| Current seed\n";
cout << fixed << setprecision(3);
int solvable = 0;
int unsolvable = 0;
int intractable = 0;
for(int seed = 0; seed < INT_MAX; seed++) {
game_state gs(rules, 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(chrono::seconds(45));
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 lower_bound = sol_lower_bound(solvable, unsolvable, intractable);
double upper_bound = sol_upper_bound(solvable, unsolvable, intractable);
cout << "[" << lower_bound * 100 << ", " << upper_bound * 100
<< "] | ("<< solvable << "/" << unsolvable << "/" << intractable
<< ") | " << seed << "\n";
}
}
double solvability_calc::sol_lower_bound(int solvable, int unsolvable, int intractable) {
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 solvable, int unsolvable, int intractable) {
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;
}
}
|