-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphere.cpp
332 lines (245 loc) · 10.6 KB
/
Sphere.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <cmath>
#include <stdlib.h>
#include <iostream>
#include <boost/math/distributions.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/exponential_distribution.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/gamma_distribution.hpp>
#include <boost/random/lognormal_distribution.hpp>
#include <boost/math/distributions/pareto.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/random.hpp>
#include "Sphere.h"
#include "Searcher.h"
#include "Coordinate.h"
#include "Agent.h"
using namespace boost;
using namespace boost::math;
using boost::math::normal;
using namespace boost::numeric::ublas;
using namespace std;
#define PI 3.14159
Sphere::Sphere( float r, int nt, float dr, int sp) : SearchSpace(nt)
{
detection_radius = dr;
radius = r;
search_pattern = sp;
}
void Sphere::updateTargetPositions()
{
}
void Sphere::updateSearcherPositionsLogNormal()
{
for (int i = 0; i < n_searchers; i++ )
{
float x_pos = searchers[i].getXPos();
float y_pos = searchers[i].getYPos();
float z_pos = searchers[i].getZPos();
float new_step_x = ((rand()%10000)*2.0/10000)-1.0;
float new_step_y = ((rand()%10000)*2.0/10000)-1.0;
float new_step_z = ((rand()%10000)*2.0/10000)-1.0;
float scale = getFromLogNormalPDF();
float scale_step = sqrt(new_step_x*new_step_x+new_step_y*new_step_y+new_step_z*new_step_z)*100;
new_step_x = scale*new_step_x/scale_step;
new_step_y = scale*new_step_y/scale_step;
new_step_z = scale*new_step_z/scale_step;
x_pos = x_pos + new_step_x;
y_pos = y_pos + new_step_y;
z_pos = z_pos + new_step_z;
if (sqrt(x_pos*x_pos + y_pos*y_pos +z_pos*z_pos) < radius)
{
searchers[i].setXPos(x_pos);
searchers[i].setYPos(y_pos);
searchers[i].setZPos(z_pos);
}
}
}
void Sphere::updateSearcherPositionsBeta()
{
for (int i = 0; i < n_searchers; i++ )
{
float x_pos = searchers[i].getXPos();
float y_pos = searchers[i].getYPos();
float z_pos = searchers[i].getZPos();
float new_step_x = ((rand()%10000)*2.0/10000)-1.0;
float new_step_y = ((rand()%10000)*2.0/10000)-1.0;
float new_step_z = ((rand()%10000)*2.0/10000)-1.0;
float scale = getFromBetaPDF();
float scale_step = sqrt(new_step_x*new_step_x+new_step_y*new_step_y+new_step_z*new_step_z)*100;
new_step_x = scale*new_step_x/scale_step;
new_step_y = scale*new_step_y/scale_step;
new_step_z = scale*new_step_z/scale_step;
x_pos = x_pos + new_step_x;
y_pos = y_pos + new_step_y;
z_pos = z_pos + new_step_z;
if (sqrt(x_pos*x_pos + y_pos*y_pos +z_pos*z_pos) < radius)
{
searchers[i].setXPos(x_pos);
searchers[i].setYPos(y_pos);
searchers[i].setZPos(z_pos);
}
}
}
void Sphere::updateSearcherPositions()
{
//cout << "Number of searchers: " << n_searchers << endl;
for (int i = 0; i < n_searchers; i++ )
{
float x_pos = searchers[i].getXPos();
float y_pos = searchers[i].getYPos();
float z_pos = searchers[i].getZPos();
float old_x = x_pos;
float old_y = y_pos;
float old_z = z_pos;
float new_step_x = ((rand()%10000)*2.0/10000)-1.0;
float new_step_y = ((rand()%10000)*2.0/10000)-1.0;
float new_step_z = ((rand()%10000)*2.0/10000)-1.0;
float scale = 0.0;
switch(search_pattern)
{
case 1:
scale = getFromBetaPDF();
break;
case 2:
scale = getFromGammaPDF();
break;
case 3:
scale = getFromLogNormalPDF();
break;
case 4:
//scale = 0.1033;
scale = getFromNormalPDF();
break;
case 5:
scale = getFromGenParetoPDF();
break;
case 6:
scale = getFromExponentialPDF();
break;
default:
cout << "Sphere::updateSearcherPositions(): Invalid search pattern type: " << search_pattern << endl;
exit(1);
break;
}
//cout << scale << endl;
float scale_step = sqrt(new_step_x*new_step_x+new_step_y*new_step_y+new_step_z*new_step_z)/100.0;
new_step_x = scale*new_step_x/scale_step;
new_step_y = scale*new_step_y/scale_step;
new_step_z = scale*new_step_z/scale_step;
x_pos = x_pos + new_step_x;
y_pos = y_pos + new_step_y;
z_pos = z_pos + new_step_z;
//if (sqrt(x_pos*x_pos + y_pos*y_pos +z_pos*z_pos) < radius)
{
// Sets the found flag on targets within distance d of the searcher's path
testpath(old_x, old_y, old_z, x_pos, y_pos, z_pos);
total_distance_travelled += norm(old_x-x_pos, old_y-y_pos, old_z-z_pos);
searchers[i].setXPos(x_pos);
searchers[i].setYPos(y_pos);
searchers[i].setZPos(z_pos);
cout << "Step length: " << norm(old_x-x_pos, old_y-y_pos, old_z-z_pos) << endl;
searchers[i].addToPath(new Coordinate(searchers[i].getXPos(),searchers[i].getYPos(),searchers[i].getZPos(),searchers[i].getTime())); // Do it here so we avoid concurrent access problems
}
}
}
void Sphere::updateSearcherPositionsBrownian()
{
for (int i = 0; i < n_searchers; i++ )
{
float x_pos = searchers[i].getXPos();
float y_pos = searchers[i].getYPos();
float z_pos = searchers[i].getZPos();
//float phi = searchers[i].getPhiAngle();
//float psi = searchers[i].getPsiAngle();
//float theta = searchers[i].getThetaAngle();
//cout << "(" << x_pos << ", " << y_pos << ", " << z_pos << ")";
float speed = searchers[i].getSpeed();
//float direction = searchers[i].getDirection();
//float phi_delta = (float)(2*PI*rand()/(1.0f*RAND_MAX));
//float psi_delta = (float)(2*PI*rand()/(1.0f*RAND_MAX));
//float theta_delta = (float)(2*PI*rand()/(1.0f*RAND_MAX));
//cout << "phi delta" << phi_delta << endl;
//cout << "theta delta" << theta_delta << endl;
//cout << "psi delta" << psi_delta << endl;
//direction=direction+direction_delta;
// OpenGL uses right handed rotation
//float x_rotation[4][4] = {{1, 0, 0, 0},
// {0, cos(phi_delta), sin(phi_delta), 0},
// {0, -sin(phi_delta), cos(phi_delta), 0},
// {0, 0, 0, 1}};
//float y_rotation[4][4] = {{cos(theta_delta), 0, -sin(theta_delta), 0},
// {0, 1, 0, 0},
// {sin(theta_delta), 0, cos(theta_delta), 0},
// {0, 0, 0, 1}};
//float z_rotation[4][4] = {{cos(psi_delta), sin(psi_delta), 0, 0},
// {-sin(psi_delta), cos(psi_delta), 0, 0},
// {0, 0, 1, 0},
// {0, 0, 0, 1}};
// Multiply the matricies
//float **C, **total_rotation;
//float total_rotation[4][4] = {{1,2,3,4},{5,6,7,8},{7,3,2,7},{9,8,7,6}};
//C = matmul4by4(x_rotation, y_rotation);
// Make a copy of the matrix
//float tempM[4][4];
//for (int k = 0; k < 4; k++ )
// for (int j = 0; j < 4; j++ )
// tempM[k][j] = C[k][j];
//total_rotation = matmul4by4(tempM, z_rotation);
//float position_vector[4] = {x_pos, y_pos, z_pos, 1};
// Apply the rotation transformation to the position vector for this searcher
//float new_vector[4];
//new_vector[0] = (total_rotation[0][0]*position_vector[0]+total_rotation[0][1]*position_vector[1]+total_rotation[0][2]*position_vector[2]+total_rotation[0][3]*position_vector[3]);
//new_vector[1] = (total_rotation[1][0]*position_vector[0]+total_rotation[1][1]*position_vector[1]+total_rotation[1][2]*position_vector[2]+total_rotation[1][3]*position_vector[3]);
//new_vector[2] = (total_rotation[2][0]*position_vector[0]+total_rotation[2][1]*position_vector[1]+total_rotation[2][2]*position_vector[2]+total_rotation[2][3]*position_vector[3]);
//new_vector[3] = (total_rotation[3][0]*position_vector[0]+total_rotation[3][1]*position_vector[1]+total_rotation[3][2]*position_vector[2]+total_rotation[3][3]*position_vector[3]);
//x_pos = x_pos + speed * cos( psi );
//y_pos = y_pos - speed * sin( psi );
//float new_x_pos = new_vector[0];
//float new_y_pos = new_vector[1];
//float new_z_pos = new_vector[2];
float new_step_x = ((rand()%10000)*2.0/10000)-1.0;
float new_step_y = ((rand()%10000)*2.0/10000)-1.0;
float new_step_z = ((rand()%10000)*2.0/10000)-1.0;
//cout << new_step_x << ", " << new_step_y << ", " << new_step_z << endl;
float scale_step = sqrt(new_step_x*new_step_x+new_step_y*new_step_y+new_step_z*new_step_z)*100;
// Make the vector a unit vector
new_step_x = new_step_x/scale_step;
new_step_y = new_step_y/scale_step;
new_step_z = new_step_z/scale_step;
//float step_length = sqrt(new_step_x*new_step_x + new_step_y*new_step_y + new_step_z*new_step_z);
//float new_length = sqrt(new_x_pos*new_x_pos + new_y_pos*new_y_pos +new_z_pos*new_z_pos);
//float new_step_x = new_x_pos*speed;
//float new_step_y = new_y_pos*speed;
//float new_step_z = new_z_pos*speed;
x_pos = x_pos + new_step_x;
y_pos = y_pos + new_step_y;
z_pos = z_pos + new_step_z;
// cout << "Step scale: " << scale_step << endl;
// cout << "Step length: " << step_length << endl;
//cout << "Rotated: (" << x_pos << ", " << y_pos << ", " << z_pos << ")" << endl;
//cout << " Coords: (" << x_pos << ", " << y_pos << ", " << z_pos << ")" << endl;
//cout << length << endl;
if (sqrt(x_pos*x_pos + y_pos*y_pos +z_pos*z_pos) < radius)
{
searchers[i].setSpeed(speed);
searchers[i].setXPos(x_pos);
searchers[i].setYPos(y_pos);
searchers[i].setZPos(z_pos);
}
else
{
//cout << "Searcher " << i << ": " << sqrt(x_pos*x_pos + y_pos*y_pos) << "exceeded boundary " << radius << endl;
}
}
}
float Sphere::getRadius()
{
return radius;
}
void placeTargets()
{
}
Sphere::~Sphere()
{
}