-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcubeBased.cpp
More file actions
165 lines (142 loc) · 5.57 KB
/
cubeBased.cpp
File metadata and controls
165 lines (142 loc) · 5.57 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Created by Yinan Zhou on 6/9/21.
//
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <ctime>
#include <vector>
#include "cubeBased.h"
std::map<int, double> K_size = {{1, 100},
{2, 1.5 * pow(2,17)},
{3, 1.3 * pow(2,11)},
{4, 1.3 * pow(2,11)},
{5, 1.3 * pow(2,11)},
{6, 1.3 * pow(2,11)},
{7, 1.3 * pow(2,11)},
{8, 1.3 * pow(2,11)},
{9, 1.3 * pow(2,11)},
{10, 1.3 * pow(2,11)}};
double log_frequency = pow(10,8);
CubeBasedEstimator::CubeBasedEstimator(int d, double v, bool f) {
dimension = d;
target_volume = v;
K = K_size.at(dimension);
// the case for fixed N = 10^6
if (f) {
double fixed_N = pow(10,6);
K = std::round(std::pow(fixed_N, 1.0/dimension));
}
cube_l = 2.0/K;
max_to_surface = sqrt(dimension) * cube_l / 2;
in_threshold = 1 - max_to_surface;
out_threshold = 1 + max_to_surface;
for (int d = 0; d < dimension; d++) {
cube_center.push_back(-1 + cube_l/2);
}
original_cube_center = cube_center;
in_cubes_count = 0;
out_cubes_count = 0;
processed_cubes_count = 0;
total_cubes = pow(K,dimension);
}
bool CubeBasedEstimator::has_vertex_in() {
// given a cube with center outside the sphere, does it have a vertex in the sphere?
// find the vertex closest to the origin and check that vertex
std::vector<double> vertex = cube_center;
double vertex_distance = 0;
for (int d = 0; d < dimension; d++) {
if (vertex.at(d) > 0) {
vertex.at(d) = vertex.at(d) - cube_l/2;
} else {
vertex.at(d) = vertex.at(d) + cube_l/2;
}
vertex_distance += vertex.at(d) * vertex.at(d);
if (vertex_distance > 1) {
return false;
}
}
return true;
}
bool CubeBasedEstimator::has_vertex_out() {
// given a cube with center inside the sphere, does it have a vertex outside the sphere?
// find the vertex furthest from the origin and check that vertex
std::vector<double> vertex = cube_center;
double vertex_distance = 0;
for (int d = 0; d < dimension; d++) {
if (vertex.at(d) >= 0) {
vertex.at(d) = vertex.at(d) + cube_l/2;
} else {
vertex.at(d) = vertex.at(d) - cube_l/2;
}
vertex_distance += vertex.at(d) * vertex.at(d);
if (vertex_distance > 1) {
return true;
}
}
return false;
}
int CubeBasedEstimator::phi_cube() {
double center_distance = 0;
for (int d = 0; d < dimension; d++) {
center_distance += cube_center.at(d) * cube_center.at(d);
}
center_distance = sqrt(center_distance);
if (center_distance >= out_threshold) return -1;
if (center_distance <= in_threshold) return 1;
if (center_distance > 1 && !has_vertex_in()) return -1;
if (center_distance < 1 && !has_vertex_out()) return 1;
return 0;
}
void CubeBasedEstimator::iterate_through_all_cubes(int current_d) {
int cube_type;
// reset
cube_center.at(current_d-1) = original_cube_center.at(current_d-1);
if (current_d == dimension) {
int k = 0;
do {
cube_type = phi_cube();
processed_cubes_count += 1;
if (cube_type == 1) {
in_cubes_count += 1;
} else if (cube_type == -1) {
out_cubes_count += 1;
}
cube_center.at(current_d - 1) = cube_center.at(current_d - 1) + cube_l;
k += 1;
} while (k < K);
}
else {
for (int k = 0; k < K - 1; k++) {
iterate_through_all_cubes(current_d + 1);
cube_center.at(current_d - 1) = cube_center.at(current_d - 1) + cube_l;
}
iterate_through_all_cubes(current_d + 1);
}
// if (std::fmod(processed_cubes_count, log_frequency) == 0) {
// std::cout << "processed: " << processed_cubes_count << std::endl;
// }
}
void CubeBasedEstimator::cube_based_estimate() {
std::clock_t start;
double duration;
start = std::clock();
std::cout << "total cubes: " << total_cubes << std::endl;
iterate_through_all_cubes(1);
double whole_cube_volume = pow(2,dimension);
// std::cout << in_cubes_count << " " << out_cubes_count << std::endl;
double on_cubes_count = total_cubes - in_cubes_count - out_cubes_count;
// std::cout << std::setprecision(15) << "In: " << in_cubes_count/total_cubes<< std::endl;
// std::cout << std::setprecision(15) << "Out: " << out_cubes_count/total_cubes<< std::endl;
// std::cout << std::setprecision(15) << "On: " << on_cubes_count/total_cubes<< std::endl;
double upper_bound = whole_cube_volume * (total_cubes - out_cubes_count) / total_cubes;
double lower_bound = whole_cube_volume * in_cubes_count / total_cubes;
double estimate = whole_cube_volume * (in_cubes_count + on_cubes_count/2) / total_cubes;
std::cout << std::setprecision(15) << "lower bound: " << lower_bound << std::endl;
std::cout << std::setprecision(15) << "upper bound: " << upper_bound << std::endl;
std::cout << std::setprecision(15) << "Precision: " << upper_bound - lower_bound << std::endl;
std::cout << std::setprecision(15) << "Estimate: " << estimate << ", accuracy: " << std::abs(estimate - target_volume) << std::endl;
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout<<"execution time: "<< duration <<'\n';
}