-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalMinimisationDetails.cpp
More file actions
223 lines (193 loc) · 4.55 KB
/
LocalMinimisationDetails.cpp
File metadata and controls
223 lines (193 loc) · 4.55 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
212
213
214
215
216
217
218
219
220
221
222
223
/*
* aMinimisationDetails.cpp
* zContrast
*
* Created by Andrew Logsdail on 13/08/2010.
* Copyright 2010 University of Birmingham. All rights reserved.
*
*/
/**
Updates:
20/09/2011
- Removed calculation of number of steps for Powell's
- This is passed in now to save calculation.
21/09/2011
- Added method to setTempPoint for local minimisation routines
**/
#include "LocalMinimisationDetails.h"
using namespace std;
void LocalMinimisationDetails::init()
// Set default values
{
///// INITIALISATIONS /////
//lastPoint.theta = 88888;
//lastPoint.phi = 88888;
//lastPoint.psi = 88888;
//lastPoint.value = 88888;
total_vectors = 0;
uni_count = 0;
uni_current = 0;
///////////////////////////
}
void LocalMinimisationDetails::setBMulti()
// Set up for multivariate calculation
{
bMulti = true;
bUni = false;
bPowell = false;
setDirection();
}
void LocalMinimisationDetails::setBUni()
// Set up for univariate calculation
{
bMulti = false;
bPowell = false;
bUni = true;
setDirection();
}
void LocalMinimisationDetails::setBPowell()
// Set up for minimisation via Powell's method
{
bMulti = false;
bUni = true;
bPowell = true;
setDirection();
}
void LocalMinimisationDetails::setDirection()
// Sets degrees between each univariate search
// Updated to just be three perpendicular axes x, y and z
{
total_vectors = 3; // x, y and z
// total_vectors++;
search_vectors.resize(total_vectors);
search_directions.resize(total_vectors);
if (bMulti)
{
for (int i = 0; i < total_vectors; i++)
{
search_vectors[i].theta = search_vectors[i].phi = search_vectors[i].psi = 1;
}
}
else
{
// X AXES //
search_vectors[0].theta = 1;
search_vectors[0].phi = 0;
search_vectors[0].psi = 0;
// Y AXES //
search_vectors[1].phi = 1;
search_vectors[1].theta = 0;
search_vectors[1].psi = 0;
// Z AXES //
search_vectors[2].psi = 1;
search_vectors[2].phi = 0;
search_vectors[2].theta = 0;
}
if (bPowell)
{
total_vectors++;
}
}
bool LocalMinimisationDetails::getUniFinished()
// Check if search is finished for univariate minimisation
// Returns boolean yes or no
{
if (uni_count == total_vectors)
{
//cout << "Yes: " << uni_count << endl;
uni_count = uni_current = 0;
return true;
}
//cout << "No: " << uni_count << endl;
return false;
}
void LocalMinimisationDetails::nextUnivariate(const int steps)
// Gives new direction for univariate calculation
// Inputs(rp) - Last rotation point minima
{
//cout << diffx << " " << diffy << " " << diffz;
//cout << steps << endl;
// Create Total Displacement for Powell's Search Vector //
if (bPowell && (uni_current < (total_vectors-1)))
{
search_directions[uni_current] = steps;
}
// CHECK IF WE ARE AT THE SAME POINT AS LAST TIME
if (steps == 0)
{
uni_count++;
//setLastPoint(rp);
}
else
{
uni_count = 0;
}
uni_current++;
if (uni_current == total_vectors)
{
uni_current = 0;
}
}
Direction LocalMinimisationDetails::getDirections()
// Method to get directions vectors
// Returns Direction variable
{
if ((!bPowell) || ((bPowell) && (uni_current < (total_vectors-1))))
{
return search_vectors[uni_current];
}
else
{
return minimisePowell();
}
}
Direction LocalMinimisationDetails::minimisePowell()
// Calculates cumulative search vector, and minimises it if possible
// Returns Direction Variable
{
int i = 0;
int max = 0;
// FIND OUT MAXIMUM SEARCH VECTOR //
for (i = 1; i < (total_vectors - 1); i++)
{
if (fabs(search_directions[i]) > search_directions[max])
{
max = i;
}
}
int remain;
int j;
//cout << "Search Directions:" << search_directions[0] << " " << search_directions[1] << " " << search_directions[2] << endl;
// DIVIDE BY ALL SEARCH VECTORS TO SEE IF FITS to BE MINIMISED//
for (i = 2; i <= fabs(search_directions[max]); i++)
{
remain = 0;
for (j = 0; j < (total_vectors - 1); j++)
{
remain += search_directions[j]%i;
}
// IF REMAINDER IS ZERO WE HAVE SUCCESS //
if (remain == 0)
{
for (j = 0; j < (total_vectors - 1); j++)
{
search_directions[j] /= i;
}
}
}
// New direction
Direction d;
d.theta = 0;
d.phi = 0;
d.psi = 0;
////////////////
// cout << "Search Directions:" << search_directions[0] << " " << search_directions[1] << " " << search_directions[2] << endl;
for (int i = 0; i < (total_vectors - 1); i++)
{
d.theta += search_directions[i]*search_vectors[i].theta;
d.phi += search_directions[i]*search_vectors[i].phi;
d.psi += search_directions[i]*search_vectors[i].psi;
}
//cout << "Powell's Vector:" << d.theta << " " << d.phi << " " << d.psi << endl;
return d;
}