-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.cpp
More file actions
507 lines (449 loc) · 14.7 KB
/
Search.cpp
File metadata and controls
507 lines (449 loc) · 14.7 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include "Search.h"
/**
Updates:
23/08/2011
- copyFromHistory updated to catch values outside search error and throw non
critical error. PBCs will stop this happening.
- same applied to addCurrentToHistory
20/09/2011
- edited setOptimisationStartingPoints and getComplete to pass number of steps back for Powell's method
- This needs consolidation and tidying up. Remove asignment for tempPoint maybe in setOptimisationStartingPoints
21/09/2011
- Removed horrible local minimisation section from setOptimisationStartingPoints
- Updated locallyMinimised to save results for local minimisation
**/
using namespace std;
void Search::init(int *s, vector<string> *o)
// Constructor
// Make sure our variables start false
{
// Initiate random numbers in optimisation
optimisation.init(s,o);
// Clear all variables
clearUp();
// Set Booleans to false
bOptimisation = bGlobal = bPeriodic = bLinear = bInTheLoop = bOptimising = false;
historyCopyCounter = 0;
powellStepCounter = 0;
// Set outputfile
output_content = o;
}
void Search::clearUp()
// Reset variables which are reused
{
// ROTATIONS CLEAROUT //
rotations.clear();
current_results.clear();
// CLEAR BOOLEAN OPERATORS //
bComplete = bOptimised = false;
}
void Search::getVariables(SearchV seV, const Variables &v)
// Reads in variables for search and works from there
// Inputs(filename) - input file
// (v) - Variable class containing already gathered information
// Returns boolean of success or failure
{
var = v;
var.bRotate = true;
bOptimisation = seV.bMinimise;
bGlobal = seV.bGlobal_search;
if (bGlobal)
{
optimisation.setVariables(seV.ga_v);
}
bScreen = seV.bPrint_to_screen;
bPeriodic = seV.bPeriodic;
if (cmpStr(seV.minimise_type,"uni"))
{
miniDetails.setBUni();
}
else if (cmpStr(seV.minimise_type,"powell"))
{
miniDetails.setBPowell();
}
else if (cmpStr(seV.minimise_type,"multi"))
{
miniDetails.setBMulti();
}
else if (cmpStr(seV.minimise_type,"linear"))
{
bLinear = true;
}
else if (!bGlobal)
{
#pragma omp critical
{
output_content->push_back("No search method defined in this line: " + seV.minimise_type);
}
setComplete();
}
Create_Rotations rot;
// Check limits are in correct order, then work out ranges
optimisation.setSearchLimits(seV.steps);
miniDetails.setSearchLimits(seV.steps);
// Resize history vector to hold all values;
vector<float> psi (seV.steps.psi_no_steps + 1, -1);
vector< vector<float> > phi (seV.steps.phi_no_steps + 1, psi);
vector< vector< vector<float> > > theta (seV.steps.theta_no_steps + 1, phi);
historyNew = theta;
//////////////
if (bOptimisation || bGlobal)
{
optimisation.setReference(seV.comparison);
}
if (bGlobal)
{
optimisation.setRandomStartPoints();
}
if (bOptimisation)
{
setOptimisationStartingPoints();
}
else
{
// cout << "Starting Points" << endl;
setStartingPoints(seV.steps);
}
}
void Search::setOptimisationStartingPoints()
// Designates startings points for first time run of the class
{
if (!bOptimising)
{
// Random start point for Global Minimisation
if (bGlobal)
{
optimisation.setTempPoint(optimisation.getNewPoint());
}
//////////////////////////// Local Minimisation
else if (bOptimisation)
{
optimisation.setTempPoint(var.rotateX,var.rotateY,var.rotateZ,LARGE);
}
/////////////////////////////
if (bScreen)
{
optimisation.printTempPoint("Not Optimising: ");
}
}
else
{
if (bScreen)
{
optimisation.printTempPoint("Optimising: ");
}
}
setStartingPoints(optimisation.minimisationPoints(miniDetails.getDirections()));
}
void Search::setStartingPoints(const LinearStruct linearStruct)
// Sets starting rotation coordinates
// Takes linearStruct file and uses all variables.
{
Create_Rotations rot;
//////////////////////////////////////
if (bLinear)
{
rotations = rot.createPoints(linearStruct);
// cout << rotations.size() << endl;
}
else if (bOptimisation)
{
rotations = rot.createPoints(miniDetails.getBMulti(),linearStruct,optimisation.getSearchLimits(),bPeriodic);
}
else if (bGlobal)
{
rotations = optimisation.getNewPoints();
}
//////////////////////////////////////
// Check to see if we have already calculated points - if so we can remove them from needing calculating
copyFromHistory();
}
Variables Search::getNext(const unsigned int current)
// Returns next variable setup for calculations
// Outputs Variable class ready to go
{
if ((current+1) == rotations.size())
{
bComplete = true;
}
RotationPoint *rp = &rotations[current];
var.rotateX = rp->theta;
var.rotateY = rp->phi;
var.rotateZ = rp->psi;
return var;
}
bool Search::getComplete()
// Checks to see if different structures are now completed. Important method and brutal to understand...
// Outputs boolean - if complete
{
int i;
if ((bOptimisation) && (bComplete))
{
bOptimising = true;
// GET MINIMUM STRUCTURE
// i = optimisation.getMinimum(current_results,optimisation.getTempPoint());
i = getMinimum(current_results,optimisation.getTempPoint(),output_content);
// COMPARE TO LAST MINIMUM
float tx, ty, tz;
tx = fabs(current_results[i].theta - optimisation.getTempPoint().theta);
ty = fabs(current_results[i].phi - optimisation.getTempPoint().phi);
tz = fabs(current_results[i].psi - optimisation.getTempPoint().psi);
// cout << powellStepCounter << endl;
// CHECK IF THE CHANGES ARE ESSENTIALLY ZERO FROM THE LAST POINT
if ((tx < ERRORBAR) && (ty < ERRORBAR) && (tz < ERRORBAR))
{
// CHANGE FLAGS //
if (miniDetails.getBMulti())
{
locallyMinimised(i);
}
else if (miniDetails.getBUni())
{
miniDetails.nextUnivariate(powellStepCounter);
// CHECK IF WE HAVE COMPLETED LOOP //
if (miniDetails.getUniFinished())
{
locallyMinimised(i);
}
else
{
powellStepCounter = 0;
optimisation.setTempPoint(current_results[i]); // SAVE MINIMUM FROM LAST SEARCH AS NOT MINIMISED YET//
addCurrentToHistory(); // ADD HISTORY TO CATALOGUE //
clearUp(); // CLEAR ALL CURRENT DATA //
setOptimisationStartingPoints(); // SET NEW STARTING POINTS USING LAST OPTIMISATION //
}
}
/////////////////////////////////
}
else
{
powellStepCounter++;
optimisation.setTempPoint(current_results[i]); // SAVE MINIMUM FROM LAST SEARCH AS NOT MINIMISED YET//
addCurrentToHistory(); // ADD HISTORY TO CATALOGUE //
clearUp(); // CLEAR ALL CURRENT DATA //
setOptimisationStartingPoints(); // SET NEW STARTING POINTS USING LAST OPTIMISATION //
}
}
else if (bGlobal && !bOptimisation)
{
// CHECK TO SEE IF WE HAVE ACQUIRED RESULTS YET //
// THIS HELPS WITH INITIAL RUN THROUGH //
if (current_results.size() != 0)
{
optimisation.setPoints(current_results); // ADD ALL POINTS TO OPTIMISATION //
addCurrentToHistory(); // APPEND NEW INFORMATION TO HISTORY //
// USE OPTIMISED TO SIGNIFY COMPLETION //
if (optimisation.getPopulationFull() && optimisation.getMutantsFull() && optimisation.getOffspringFull())
{
bOptimised = true;
}
else
{
clearUp();
setStartingPoints(optimisation.getSearchLimits());
}
}
}
if (bGlobal && bOptimised)
{
// CLEAR ALL DATA //
clearUp();
// POPULTATION IS NOT FULL //
if (bOptimisation && (!optimisation.getPopulationFull() || !optimisation.getMutantsFull() || !optimisation.getOffspringFull()))
{
setOptimisationStartingPoints();
}
// ELSE IF NOT CONVERGED RESULTS //
else if (optimisation.getPopulationFull() && optimisation.getMutantsFull() && optimisation.getOffspringFull() && !optimisation.getConverged())
{
optimisation.newPopulation(); // CALCUATE NEW POPULATION //
//bInTheLoop = true; // NOTE WE ARE NOW UP AND RUNNING //
// SET NEW START POINTS //
if (bOptimisation)
{
setOptimisationStartingPoints();
}
else
{
setStartingPoints(optimisation.getSearchLimits());
}
}
}
if (bLinear && bComplete)
{
return true; // WHILE COMPLETE AND NOT OPTIMISING (ONLY DOES ONE LOOP) //
}
else if (bOptimisation && bOptimised)
{
return true; // OR WHILE OPTIMISING AND OPTIMISED //
}
else if (bGlobal && optimisation.getConverged())
{
return true; // OR FINISHED GLOBAL SEARCH //
}
else if (!bLinear && !bOptimisation && !bGlobal && bComplete)
{
return true; // OR JUST A ONE OFF COMPARISON //
}
// ELSE WE HAVE NOT FINISHED //
else
{
return false;
}
}
void Search::locallyMinimised(const int &i)
// Change flags and save result if completed minimisation
// Inputs - int i : position of minima in current_results
{
bOptimising = false;
bOptimised = true;
// SAVE POINT FOR NEXT POPULATION //
if (bGlobal)
{
optimisation.setPoint(current_results[i]);
}
else
// Otherwise this is the minimum if we are not doing Global Search
{
optimisation.setMinimumPoint(current_results[i]);
}
}
void Search::addCurrentToHistory()
// Adds current repository to history so can be double checked if need be
{
LinearStruct ls = optimisation.getSearchLimits();
int current_results_size = current_results.size();
for (signed int i = 0; i < current_results_size; i++)
{
//We need to add a check in here for values outside the search area
//Otherwise with pbc turned off we get a segmentation fault
if ((rotations[i].theta >= ls.theta_min) &&
(rotations[i].theta <= ls.theta_max) &&
(rotations[i].phi >= ls.phi_min) &&
(rotations[i].phi <= ls.phi_max) &&
(rotations[i].psi >= ls.psi_min) &&
(rotations[i].psi <= ls.psi_max))
// So this should pick up everything inside the search area or on the boundary
{
// These declarations need to be moved outside the for loop!
int tx_steps = (int) round((current_results[i].theta - ls.theta_min) / ls.theta_step);
int ty_steps = (int) round((current_results[i].phi - ls.phi_min) / ls.phi_step);
int tz_steps = (int) round((current_results[i].psi - ls.psi_min) / ls.psi_step);
//cout << tx_steps << " " << ty_steps << " " << tz_steps << endl;
//cout << historyNew[tx_steps][ty_steps][tz_steps] << " " << current_results[i].value << endl;
if (historyNew[tx_steps][ty_steps][tz_steps] == -1)
{
//cout << tx_steps << " " << ty_steps << " " << tz_steps << endl;
historyNew[tx_steps][ty_steps][tz_steps] = current_results[i].value;
// Addition to deal with Gimball lock
// i.e. When x- and z- axes line up (at phi = -pi/2 or pi/2)
if (bPeriodic)
{
if ((ty_steps == 0) || (ty_steps == ls.phi_no_steps)) // We are at a pole.
{
float theta_psi_combined = current_results[i].theta + current_results[i].psi;
// Many views are equivalent here. If I rotate +ve around x, followed by the same -ve around z I will always have the same view
// Therefore let's give this value to all the rotations to save time.
for (signed int j = 0; j <= ls.theta_no_steps; j++ )
{
float theta_rotate = j * ls.theta_step;
float psi_rotate = theta_psi_combined - theta_rotate;
if (psi_rotate < ls.psi_min)
{
while (psi_rotate < ls.psi_min)
{
psi_rotate += (ls.psi_range + 1);
}
}
else if (psi_rotate > ls.psi_max)
{
while (psi_rotate > ls.psi_max)
{
psi_rotate -= (ls.psi_range + 1);
}
}
int r_tz_steps = (int) round(fmod(psi_rotate,ls.psi_step));
if (r_tz_steps == 0)
{
tz_steps = (int) round(psi_rotate / ls.psi_step);
// j is equivalent to the use of tx_steps above
// we have recalculated tz_steps
historyNew[j][ty_steps][tz_steps] = current_results[i].value;
}
}
}
}
/////////////////////////////
}
}
else
// We'll print an error statement that we're working outside the search area
// So the value won't be saved. Think about turning pbcs on.
{
printHistoryArrayError(rotations[i]);
}
}
}
void Search::copyFromHistory()
// Copy results from history so we do not need to recalculate
{
LinearStruct ls = optimisation.getSearchLimits();
for (unsigned int i = 0; i < rotations.size(); i++)
{
//cout << rotations[i].theta << " " << rotations[i].phi << " " << rotations[i].psi << endl;
//We need to add a check in here for values outside the search area
//Otherwise with pbc turned off we get a segmentation fault
if ((rotations[i].theta >= ls.theta_min) &&
(rotations[i].theta <= ls.theta_max) &&
(rotations[i].phi >= ls.phi_min) &&
(rotations[i].phi <= ls.phi_max) &&
(rotations[i].psi >= ls.psi_min) &&
(rotations[i].psi <= ls.psi_max))
// So this should pick up everything inside the search area or on the boundary
{
// Move these declarations outside the loop Andrew!
int tx_steps = (int) round((rotations[i].theta - ls.theta_min) / ls.theta_step);
int ty_steps = (int) round((rotations[i].phi - ls.phi_min) / ls.phi_step);
int tz_steps = (int) round((rotations[i].psi - ls.psi_min) / ls.psi_step);
//cout << tx_steps << " " << ty_steps << " " << tz_steps << endl;
//cout << historyNew[tx_steps][ty_steps][tz_steps] << endl;
if (historyNew[tx_steps][ty_steps][tz_steps] != -1)
{
//cout << tx_steps << " " << ty_steps << " " << tz_steps << endl;
rotations[i].value = historyNew[tx_steps][ty_steps][tz_steps];
current_results.push_back(rotations[i]);
rotations.erase(rotations.begin()+i);
i--;
historyCopyCounter++;
}
}
else
// We'll print an error statement that we're working outside the search area
// So the value won't be saved. Think about turning pbcs on.
{
printHistoryArrayError(rotations[i]);
}
}
// Stops an infinite loop when all required values have been previously calculated
if (rotations.size() == 0)
{
bComplete = true;
}
}
void Search::printHistoryArrayError(RotationPoint rp)
// Prints out error message for problems accessing History array
// Takes the current orientation as input
{
string thetaS;
string phiS;
string psiS;
NumberToString(rp.theta,thetaS);
NumberToString(rp.phi,phiS);
NumberToString(rp.psi,psiS);
#pragma omp critical
{
output_content->push_back("Error in adding current orientation to history");
output_content->push_back("(" + thetaS + ", " + phiS + ", " + psiS + ") is outside the search area. Think about enabling PBCs as this value won't be saved.");
}
}