-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSAOBL_CppSourceRounded.cpp
314 lines (258 loc) · 8.69 KB
/
MSAOBL_CppSourceRounded.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
// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
// http://gallery.rcpp.org/
//
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::plugins(openmp)]]
// [[Rcpp::depends(RcppParallel)]]
#define _USE_MATH_DEFINES
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <random>
#include <Rcpp.h>
#include <omp.h>
#include "functionWrappers.h"
double levyFlightDist(double beta, double s)
{
return ((beta - 1)*tgamma(beta - 1)*sin((M_PI*(beta - 1)) / 2)) / M_PI * pow(s, beta);
}
double randomFromInterval(double max, double min)
{
return (max - min) * ((double)rand() / (double)RAND_MAX) + min;
}
class Moth
{
public:
Moth() = default;
Moth(std::vector<double> newMothCoords)
:_coords(newMothCoords)
{
_fitness = 0;
}
std::vector<double> getCoords()
{
return this->_coords;
}
double getFitness()
{
return this->_fitness;
}
void computeFitness(const std::unique_ptr<FunctionBase>& optFunction)
{
this->_fitness = optFunction->use(this->_coords);
}
void levyFlightMove(double maxWalkStep, size_t currGen, const std::vector<double>& upperBounds, const std::vector<double>& lowerBounds)
{
double levy = levyFlightDist(1.5, 2.0); // s = 2, zistit dobru hodnotu
double alpha = maxWalkStep / pow(currGen, 2);
for (size_t i = 0; i < this->_coords.size(); i++)
{
this->_coords[i] = round(this->_coords[i] + levy * alpha);
if(this->_coords[i] < lowerBounds[i])
{
this->_coords[i] = lowerBounds[i];
}
if(this->_coords[i] > upperBounds[i])
{
this->_coords[i] = upperBounds[i];
}
}
}
void EqFiveSixMove(double scaleFactor, double goldenRatio, const Moth& bestMoth, const std::vector<double>& upperBounds, const std::vector<double>& lowerBounds)
{
for (size_t i = 0; i < this->_coords.size(); i++)
{
this->_coords[i] = round(scaleFactor * (this->_coords[i] + goldenRatio * (bestMoth._coords[i] - this->_coords[i])));
if(this->_coords[i] < lowerBounds[i])
{
this->_coords[i] = lowerBounds[i];
}
if(this->_coords[i] > upperBounds[i])
{
this->_coords[i] = upperBounds[i];
}
}
}
const bool operator<(const Moth& rhs) const
{
return this->_fitness < rhs._fitness;
}
Moth bestMothOBL(const Moth& bestMoth, const std::vector<double>& upperBounds, const std::vector<double>& lowerBounds, const std::unique_ptr<FunctionBase>& optFunc)
{
std::vector<double> newMothCoords(this->_coords.size(), 0.0);
for (size_t i = 0; i < this->_coords.size(); i++)
{
newMothCoords[i] = round(2 * bestMoth._coords[i] - this->_coords[i]);
if(newMothCoords[i] < lowerBounds[i])
{
newMothCoords[i] = lowerBounds[i];
}
if(newMothCoords[i] > upperBounds[i])
{
newMothCoords[i] = upperBounds[i];
}
}
Moth returnMoth(newMothCoords);
returnMoth.computeFitness(optFunc);
return returnMoth;
}
Moth boundsOBL(const std::vector<double>& upperBounds, const std::vector<double>& lowerBounds, const std::unique_ptr<FunctionBase>& optFunc)
{
std::vector<double> newMothCoords(this->_coords.size(), 0.0);
for (size_t i = 0; i < this->_coords.size(); i++)
{
newMothCoords[i] = round(upperBounds[i] + lowerBounds[i] - this->_coords[i]);
}
Moth returnMoth(newMothCoords);
returnMoth.computeFitness(optFunc);
return returnMoth;
}
private:
std::vector<double> _coords;
double _fitness;
};
std::vector<Moth> generatePop(size_t popSize, const std::vector<double>& upperBounds, const std::vector<double>& lowerBounds)
{
std::vector<Moth> returnVector;
size_t dimensions = upperBounds.size();
for (size_t i = 0; i < popSize; i++)
{
Rcpp::NumericVector currentMothCoords(dimensions);
for (size_t j = 0; j < dimensions; j++)
{
currentMothCoords[j] = R::runif(lowerBounds[j],upperBounds[j]);
}
returnVector.emplace_back(Rcpp::as<std::vector<double>>(currentMothCoords));
}
return returnVector;
}
// [[Rcpp::export]]
Rcpp::List MothSearchOptCppRounded(SEXP optFunc, size_t popSize, int maxGeneration, double stopValue, double maxWalkStep, int OBLStopPercent, Rcpp::NumericVector rUpperBounds, Rcpp::NumericVector rLowerBounds, int numThreads)
{
size_t currentGeneration = 1;
std::random_device rd{};
std::mt19937 gen{ rd() };
std::normal_distribution<double> distribution(0.5, 0.1);
double goldenRatio = 1 + sqrt(5) / 2;
double scaleFactor = distribution(gen);
size_t OBLStop = maxGeneration * OBLStopPercent / 100;
std::vector<double> upperBounds = Rcpp::as<std::vector<double>>(rUpperBounds);
std::vector<double> lowerBounds = Rcpp::as<std::vector<double>>(rLowerBounds);
Moth bestMoth;
std::vector<Moth> population = generatePop(popSize, upperBounds, lowerBounds);
std::vector<double> bestMothFitnesses;
std::vector<std::vector<double>> bestMothCoords;
std::vector<std::vector<double>> populationFitnesses;
std::vector<std::vector<std::vector<double>>> populationCoords;
if(numThreads != 0)
{
omp_set_num_threads(numThreads);
}
bool parallel = false;
std::unique_ptr<FunctionBase> _optFunc;
if(TYPEOF(optFunc) == EXTPTRSXP)
{
parallel = true;
Rcpp::XPtr<function> funcXPtr(optFunc);
_optFunc = std::move(std::unique_ptr<FunctionCpp>(new FunctionCpp(funcXPtr)));
}
else
{
_optFunc = std::move(std::unique_ptr<FunctionR>(new FunctionR(optFunc)));
}
#pragma omp parallel for if(parallel)
for (int i = 0; i < population.size(); i++)
{
population[i].computeFitness(_optFunc);
}
//sort population by fitness
std::sort(population.begin(), population.end());
//output inits
std::vector<double> thisGenFitnesess;
std::vector<std::vector<double>> thisGenCoords;
bestMothFitnesses.push_back(population[1].getFitness());
bestMothCoords.push_back(population[1].getCoords());
for(auto currMoth : population)
{
thisGenFitnesess.push_back(currMoth.getFitness());
thisGenCoords.push_back(currMoth.getCoords());
}
populationFitnesses.push_back(thisGenFitnesess);
populationCoords.push_back(thisGenCoords);
for (; currentGeneration < maxGeneration; currentGeneration++)
{
std::sort(population.begin(), population.end());
if(!(bestMoth < population[0]) || currentGeneration == 1)
{
bestMoth = population[0];
}
#pragma omp parallel for if(parallel)
for (int i = 0; i < population.size() / 2; i++)
{
population[i].levyFlightMove(maxWalkStep, currentGeneration, upperBounds, lowerBounds);
population[i].computeFitness(_optFunc);
if(currentGeneration < OBLStop)
{
try {
Moth OBLMoth = population[i].bestMothOBL(bestMoth, upperBounds, lowerBounds, _optFunc);
if (OBLMoth < population[i])
{
std::swap(OBLMoth, population[i]);
}
}
catch (const std::exception& e)
{
std::terminate(); //to do something useful
}
}
}
#pragma omp parallel for if(parallel)
for (int j = population.size() / 2 + 1; j < population.size(); j++)
{
double randDecider = ((double)rand() / (RAND_MAX));
if (randDecider < 0.5)
{
population[j].EqFiveSixMove(scaleFactor, goldenRatio, bestMoth, upperBounds, lowerBounds);
}
else
{
population[j].EqFiveSixMove(scaleFactor, 1 / goldenRatio, bestMoth, upperBounds, lowerBounds);
}
population[j].computeFitness(_optFunc);
if(currentGeneration < OBLStop)
{
Moth OBLMoth = population[j].boundsOBL(upperBounds, lowerBounds, _optFunc);
if (OBLMoth < population[j])
{
std::swap(OBLMoth, population[j]);
}
}
}
bestMothFitnesses.push_back(bestMoth.getFitness());
bestMothCoords.push_back(bestMoth.getCoords());
for(auto currMoth : population)
{
thisGenFitnesess.push_back(currMoth.getFitness());
thisGenCoords.push_back(currMoth.getCoords());
}
populationFitnesses.push_back(thisGenFitnesess);
populationCoords.push_back(thisGenCoords);
if(bestMoth.getFitness() <= stopValue)
{
break;
}
}
return Rcpp::List::create(
Rcpp::Named("BM_Fitness") = bestMothFitnesses,
Rcpp::Named("BM_Coords") = bestMothCoords,
Rcpp::Named("POP_Fitness") = populationFitnesses,
Rcpp::Named("POP_Coords") = populationCoords
);
}