-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalOptimisationUtils.cpp
More file actions
212 lines (173 loc) · 4.81 KB
/
GlobalOptimisationUtils.cpp
File metadata and controls
212 lines (173 loc) · 4.81 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
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
/*
* OptimisationUtils.cpp
* zContrast
*
* Created by Andrew Logsdail on 02/06/2011.
* Copyright 2011 University of Birmingham. All rights reserved.
*
*/
/**
02/06/2011
- Removed parallelisation from random point selection
**/
#include "GlobalOptimisationUtils.h"
using namespace std;
vector<RotationPoint> getRandomPoints(const int s, int *idum, LinearStruct ls)
// Sets RotationPoint variables randomly
// Returns vector of values;
{
RotationPoint rp;
vector<RotationPoint> vRP;
/**
#pragma omp parallel for ordered default(none) \
private(rp) \
shared(vRP)
**/
for (int i = 0; i < s; i++)
{
rp = getRandomPoint(idum, ls);
// #pragma omp critical
// {
vRP.push_back(rp);
// }
}
return vRP;
}
RotationPoint getRandomPoint(int *idum, LinearStruct ls)
// Sets RotationPoint variables randomly
// Inputs - size - number of points needed
// ls - All variables associated with rotation maxima
{
int randomNum;
RotationPoint temp;
randomNum = randomNumber((ls.theta_no_steps + 1),idum);
temp.theta = ls.theta_min + (randomNum * ls.theta_step);
//randomNum = randomNumber((searchLimits.phi_no_steps + 1),idum);
//temp.phi = searchLimits.phi_min + (randomNum * searchLimits.phi_step);
// This will distribute points evenly *hopefully* across the surface of a sphere.
// Needs testing - should generate better convergence to minima
temp.phi = ls.phi_max + 1;
while ((temp.phi > ls.phi_max) || (temp.phi < ls.phi_min))
{
randomNum = (int) round(acos((randomNumberF(2,idum)) - 1) * RAD2DEG);
randomNum = (int) round(randomNum / ls.phi_step);
temp.phi = ls.phi_min + (randomNum * ls.phi_step);
}
randomNum = randomNumber((ls.psi_no_steps + 1),idum);
temp.psi = ls.psi_min + (randomNum * ls.psi_step);
return tidy(temp);
}
int getMinimum(const vector<RotationPoint> &vec, const RotationPoint rp, vector<string> *o)
// Find minimum in vector
// Returns data position from that point in vector current
{
vector<RotationPoint> v = vec;
int v_size = v.size();
int low = 0;
if (v_size == 0)
{
emptyVectorError(o);
}
else
{
// If we have been given the value to use as the previous minimum, find this and compare to other values
// Hopefully this shouldn't slow things down. I'll parallelise trivially to make sure.
if (rp.value != -1)
{
float tx, ty, tz;
// Parallelised for a little extra speed //
#pragma omp parallel for ordered default(none) \
shared(low,v_size,v) private(tx,ty,tz)
for (int i = 0; i < v_size; i++)
{
tx = fabs(v[i].theta - rp.theta);
ty = fabs(v[i].phi - rp.phi);
tz = fabs(v[i].psi - rp.psi);
if ((tx < ERRORBAR) && (ty < ERRORBAR) && (tz < ERRORBAR))
{
low = i;
}
}
}
// Parallelised for a little extra speed //
#pragma omp parallel for ordered default(none) \
shared(low,v_size,v)
for (int i = 0; i < v_size; i++)
{
if (v[low].value > v[i].value)
{
low = i;
}
}
}
return low;
}
int getMaximum(const vector<RotationPoint> &vec, vector<string> *o)
// Find minimum in vector
// Returns data position from that point in vector current
{
vector<RotationPoint> v = vec;
int v_size = v.size();
int high = 0;
if (v_size == 0)
{
emptyVectorError(o);
}
else
{
// Parallelised for a little extra speed //
#pragma omp parallel for ordered default(none) \
shared(high,v_size,v)
for (int i = high + 1; i < v_size; i++)
{
if (v[high].value < v[i].value)
{
high = i;
}
}
}
return high;
}
RotationPoint emptyRotationPoint()
// Return empty RP
{
RotationPoint r;
r.theta = -1;
r.phi = -1;
r.psi = -1;
r.value = -1;
return r;
}
RotationPoint tidy(RotationPoint rp)
// Get rid of rounding errors from our value.
// Only really adds cosmetic value
// Inputs: RotationPoint - To be tidied
// Returns: RotationPoint - Tidied
{
// Check modulus of remainder //
float error = fabs(fmod(rp.theta,1));
if (error < ERRORBAR || (1-error) < ERRORBAR)
rp.theta = round(rp.theta);
//Y////////////////////
// Check modulus of remainder //
error = fabs(fmod(rp.phi,1));
if (error < ERRORBAR || (1-error) < ERRORBAR)
rp.phi = round(rp.phi);
//Z////////////////////
// Check modulus of remainder //
error = fabs(fmod(rp.psi,1));
if (error < ERRORBAR || (1-error) < ERRORBAR)
rp.psi = round(rp.psi);
///////////////////////
return rp;
}
void emptyVectorError(vector<string> *o)
// Displays error that we are going to check an empty vector for values
{
o->push_back("!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!");
o->push_back("!! Vector has no entries so we will be !!");
o->push_back("!! seeing a segmentation fault. Did you specify !!");
o->push_back("!! a comparative value? !!");
//o->push_back("!! Currently : " + getReference());
o->push_back("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}