-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimate_legal_big.c
97 lines (79 loc) · 2.29 KB
/
estimate_legal_big.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "bigstates.h"
#include "random.h"
#define N 10000
static unsigned int random_choice(unsigned int n)
{
unsigned int k;
unsigned int r = 0xffffffffU % n;
do {
k = gg_urand();
} while (k < r);
return k % n;
}
int
main(int argc, char **argv)
{
static bstate states[N];
static bstate out_states[3 * N];
int num_outstates = 0;
int j;
int k;
int x, y;
double sum_p = 0.0;
double sum_squared_p = 0.0;
int num_legal_final_states;
int height;
int width;
int num_iterations = 100;
if (argc < 5) {
fprintf(stderr, "Usage: estimate_legal_stratified <height> <width> <num_samples> <seed>\n");
return 1;
}
height = atoi(argv[1]);
width = atoi(argv[2]);
num_iterations = atoi(argv[3]);
gg_srand(atoi(argv[4]));
setwidth(height);
for (j = 0; j < num_iterations; j++) {
double p = 1.0;
for (k = 0; k < N; k++)
memcpy(states[k], *startstate(), sizeof(bstate));
for (y = 0; y < width; y++)
for (x = 0; x < height; x++) {
num_outstates = 0;
for (k = 0; k < N; k++) {
bstate expanded_states[3];
int num_expanded_states;
int m;
num_expanded_states = expandstate(states[k], x, expanded_states);
for (m = 0; m < num_expanded_states; m++) {
memcpy(out_states[num_outstates++], expanded_states[m], sizeof(bstate));
}
}
p *= (double) num_outstates / (3 * N);
for (k = 0; k < N; k++)
memcpy(states[k], out_states[random_choice(num_outstates)], sizeof(bstate));
}
num_legal_final_states = 0;
for (k = 0; k < num_outstates; k++)
num_legal_final_states += finalstate(out_states[k]);
p *= (double) num_legal_final_states / num_outstates;
sum_p += p;
sum_squared_p += p * p;
if ((j + 1) % 10 == 0) {
double std = sqrt(sum_squared_p - sum_p * sum_p / (j + 1)) / j;
printf("%d %10.8lg %lg %lg\n", j + 1, sum_p / (j + 1),
std, std * sqrt(j));
}
}
printf("Estimated legal probability: %10.8lg\n", sum_p / num_iterations);
printf("Standard deviation: %lg\n",
sqrt(sum_squared_p - sum_p * sum_p / num_iterations) / num_iterations);
printf("Standard deviation per sample: %lg\n",
sqrt((sum_squared_p - sum_p * sum_p / num_iterations) / num_iterations));
return 0;
}