-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.cpp
More file actions
72 lines (63 loc) · 2.18 KB
/
Box.cpp
File metadata and controls
72 lines (63 loc) · 2.18 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
// Box.cpp
// Class to represent boxes
#include "Box.h"
void Box::setLowerUpper(int dim) {
for (int i = 0; i < dim; i++) {
lower.push_back(fracDist * point[i]);
upper.push_back(1.0 - fracDist * (1.0 - point[i]));
}
}
void Box::generateRandomNumbers(int dim) {
default_random_engine generator((int) time(0));
double lowerBound = 0.0, upperBound = 0.0, randomNumber = 0.0;
for (int i = 0; i < dim; i++) {
/*** 2BI. Generate 3 random points inside the box ***/
lowerBound = lower[i];
upperBound = upper[i];
for (int k = 0; k < 3; k++) {
uniform_real_distribution<double> distribution(lowerBound, upperBound);
randomNumber = distribution(generator);
randomInside[k].push_back(randomNumber);
}
}
/*** 2BII. Generate 1 random point inside the unit hypercube, and outside the box ***/
int numberCoordsIn;
vector <double> tempRandomOutside;
tempRandomOutside.resize(dim);
do {
numberCoordsIn = 0;
// Generate point in unit hypercube
for (int i = 0; i < dim; i++) {
lowerBound = lower[i];
upperBound = upper[i];
uniform_real_distribution<double> distribution(0.0, 1.0);
randomNumber = distribution(generator);
tempRandomOutside[i] = randomNumber;
if ((randomNumber > lowerBound) && (randomNumber < upperBound)) {
numberCoordsIn++;
}
}
}
while (numberCoordsIn == dim);
randomOutside = tempRandomOutside;
}
void Box::outputData(ofstream& output, int dim, char delimit){
// Output 3 points inside box
for (int j = 0; j < 3; j++) {
output << makeLine(randomInside[j], dim, delimit) << '\n';
}
// Output 1 point inside unit hypercube, and outside box
output << makeLine(randomOutside, dim, delimit) << '\n';
}
string Box::makeLine(const vector <double>& data, int dim, char delimit) {
string line = "";
ostringstream strs;
string str;
for (int j = 0; j < dim; j++) {
strs.str("");
strs << data[j];
str = strs.str();
line += str + delimit;
}
return line;
}