-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalOptimisationOffspring.cpp
More file actions
342 lines (304 loc) · 8.25 KB
/
GlobalOptimisationOffspring.cpp
File metadata and controls
342 lines (304 loc) · 8.25 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
/*
* OptimisationOffspring.cpp
* zContrast
*
* Created by Andrew Logsdail on 02/06/2011.
* Copyright 2011 University of Birmingham. All rights reserved.
*
*/
/**
02/06/2011 - Outsourced offspring from Optimisation.cpp
- Needs breaking down into smaller classes
- We have imported fitness calculation into this class
**/
#include "GlobalOptimisationOffspring.h"
using namespace std;
void OptimisationOffspring::setVariables(int *s, vector<string> *o, bool b, int tournament_size,
const string type, const string parents, const string fitness)
/**
Method to set variables for mutation.
Inputs: Seed for Random Number
String - Mating Type
String - Parent Selection
String - Fitness Type
Print to Screen
**/
{
// Initiate Random Numbers
idum = s;
output_content = o;
// Set Tournament size
tsize = tournament_size;
//////////////////////////
bOUniformCrossover = false;
bOTournament = false;
bORoulette = false;
bFExponential = false;
bFLinear = false;
bFTanh = false;
// Assign values now
bScreen = b;
// CROSSOVER //
if (cmpStr(type,"uniform"))
{
bOUniformCrossover = true;
}
else
{
#pragma omp critical
{
output_content->push_back("Invalid mating type: " + type);
}
//return EXIT_FAILURE;
}
// CROSSOVER SELECTION //
if (cmpStr(parents,"tournament"))
{
bOTournament = true;
}
else if (cmpStr(parents,"roulette"))
{
bORoulette = true;
}
else
{
#pragma omp critical
{
output_content->push_back("Invalid crossover selection type: " + parents);
}
//return EXIT_FAILURE;
}
// FITNESS TESTS //
if (cmpStr(fitness,"exponential"))
{
bFExponential = true;
}
else if (cmpStr(fitness,"linear"))
{
bFLinear = true;
}
else if (cmpStr(fitness,"tanh"))
{
bFTanh = true;
}
else
{
#pragma omp critical
{
output_content->push_back("Invalid fitness type: " + fitness);
}
//return EXIT_FAILURE;
}
//return EXIT_SUCCESS;
}
vector<RotationPoint> OptimisationOffspring::getOffspringPoints(int numberOfOffspring,
LinearStruct limits,
const std::vector<RotationPoint> points_copy)
// Sets RotationPoint variables to offspring values
{
if (bScreen)
{
string snum;
string spoints;
int ipoints = points_copy.size();
NumberToString(numberOfOffspring,snum);
NumberToString(ipoints,spoints);
#pragma omp critical
{
output_content->push_back("Working out offspring : " + snum + ". Points size is : " + spoints);
}
}
// Looks like the problem we are having is "points" is being overwritten or emptied mid run?
// So what we'll do is create a copy of it just for this method - now takes points in
vector<RotationPoint> new_offspring;
// Let's see if this clears up problems. We'll return a new copy off the offspring to be assigned
// offspring.clear();
if (!bOUniformCrossover)
{
#pragma omp critical
{
output_content->push_back("Error: No mating scheme defined");
}
new_offspring = getRandomPoints(numberOfOffspring,idum,limits);
}
else
{
int one = 0;
int two= 0;
int option = 0;
RotationPoint rp = emptyRotationPoint();
/**
// Parallel loop to generate uniform crossover //
#pragma omp parallel for default(none) \
private(one,two,option,rp) shared(points_copy,new_offspring)
**/
for (int i = 0; i < numberOfOffspring; i++)
{
one = two = 1;
// Make sure we don't get the same parents //
while (one == two)
{
// Hopefully this will deal with infinite loops
// Going to remove bottom else loop as a result
one = randomNumber(points_copy.size(),idum);
two = randomNumber(points_copy.size(),idum);
// Select parents by tournament method //
if (bOTournament)
{
vector<RotationPoint> tourn;
vector<int> positions;
for (int j = 0; j < tsize; j++)
{
int pos = randomNumber(points_copy.size(),idum);
positions.push_back(pos);
/** #pragma omp critical
{ **/
tourn.push_back(points_copy[pos]);
/** } **/
}
int t = getMinimum(tourn,emptyRotationPoint(),output_content);
one = positions[t];
positions.erase(positions.begin()+t);
tourn.erase(tourn.begin()+t);
t = getMinimum(tourn,emptyRotationPoint(),output_content);
two = positions[t];
positions.clear();
tourn.clear();
}
else if (bORoulette)
// #pragma omp critical
// Select parents via roulette method instead of tournament //
{
// int counter = 0; // We are going to use this as an escape route
// int counter_limit = 1000; // For this weird infinite loop we are seeing
float compareF = randomNumber(idum);
string vS;
string fitnessS;
string compareS;
float fitnessF = fitness(one,points_copy);
while (fitnessF >= compareF)
{
NumberToString(one,vS);
NumberToString(fitnessF,fitnessS);
NumberToString(compareF,compareS);
#pragma omp critical
{
output_content->push_back("Roulette failed (1) : " + vS + " : " + fitnessS + " > " + compareS);
}
one = randomNumber(points_copy.size(),idum);
fitnessF = fitness(one,points_copy);
compareF = randomNumber(idum);
/** We've had problems with this infinitely looping **/
// counter++;
// if (counter > counter_limit)
// {
// cout << "Exceeded roulette limit (1), breaking out. Points size is " << points_copy.size() << endl;
// compareF = 1;
// }
/** So this gives us a break out **/
}
// Reset counter
// counter = 0;
fitnessF = fitness(two,points_copy);
compareF = randomNumber(idum);
while (fitnessF >= compareF)
{
NumberToString(two,vS);
NumberToString(fitnessF,fitnessS);
NumberToString(compareF,compareS);
#pragma omp critical
{
output_content->push_back("Roulette failed (2) : " + vS + " : " + fitnessS + " > " + compareS);
}
two = randomNumber(points_copy.size(),idum);
fitnessF = fitness(two,points_copy);
compareF = randomNumber(idum);
/** We've had problems with this infinitely looping **/
// counter++;
// if (counter > counter_limit)
// {
// cout << "Exceeded roulette limit (2), breaking out. Points size is " << points_copy.size() << endl;
// compareF = 1;
// }
/** So this gives us a break out **/
}
}
}
// Create children if the parents aren't the same //
/** #pragma omp critical
{ **/
rp = points_copy[one];
int option_total = 0;
for (int j = 0; j < 3; j++)
{
option = randomNumber((int) 2,idum);
if ((j == 0) && (option == 1))
{
rp.theta = points_copy[two].theta;
}
else if ((j == 1) && (option == 1))
{
rp.phi = points_copy[two].phi;
}
else if ((j == 2) && (option == 1))
{
rp.psi = points_copy[two].psi;
}
option_total += option;
}
// Set value. If previously calculated set as so.
if (option_total == 3)
{
rp.value = points_copy[two].value;
}
else if (option_total != 0)
{
rp.value = 88888;
}
// Push back new point //
/** } **/
new_offspring.push_back(rp);
}
}
if (bScreen)
{
string offspringS;
int offspringI = new_offspring.size();
NumberToString(offspringI,offspringS);
#pragma omp critical
{
output_content->push_back("Offspring generated : " + offspringS);
}
}
return new_offspring;
}
float OptimisationOffspring::fitness(const int rp, const vector<RotationPoint> points)
// Work out fitness of a value compared to overall population
// Input: Current Rotation Point Index
// : Current Selection of Points
// Output : float of fitness value
{
int worst = getMaximum(points,output_content); // MAX
int best = getMinimum(points,emptyRotationPoint(),output_content); // MIN
float p = ((points[rp].value - points[best].value)/(points[worst].value - points[best].value)); // Normalise
// EXPONENTIAL FACTOR //
const float alpha = 3;
// Should we soft code this? Probably //
////////////////////////
if (bFLinear)
{
return (1-(0.7*p)); // Linear function
}
else if (bFExponential)
{
return exp(-alpha*p); // Exponential function
}
else if (bFTanh)
{
return 0.5*(1-tanh((2*p)-1)); // Hyperbolic Tangent function
}
else
{
return p;
}
}