/
abc.cpp
304 lines (249 loc) · 7.53 KB
/
abc.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
#include <Rcpp.h>
using namespace Rcpp;
// Fitness computing
double CalculateFitness(double x) {
return (x>=0) ? 1/(x+1) : 1 + fabs(x);
}
// Sets the current best
void MemorizeBetsSource(
double & GlobalMin,
NumericVector & GlobalParams,
NumericVector & f,
NumericMatrix & Foods,
int & unchanged
) {
double GlobalMinOld=GlobalMin;
for (int i=0;i<f.size(); i++)
if (f[i] < GlobalMin) {
GlobalMin = f[i];
GlobalParams = Foods(i,_);
}
if (GlobalMin == GlobalMinOld) ++unchanged;
else unchanged = 0;
return;
}
// Initializes a food source
void init(
int index,
NumericVector & fitness,
NumericVector & f,
IntegerVector & trial,
Function & fun,
NumericMatrix & Foods,
const NumericVector & lb,
const NumericVector & ub
) {
for (int j=0; j<lb.size(); j++)
Foods.at(index,j) = runif(1, lb.at(j), ub.at(j))[0];
f[index] = as<double>(fun(Foods(index,_)));
fitness[index] = CalculateFitness(f[index]);
trial[index] = 0;
return;
}
// Sends employed Bees
void SendEmployedBees(
double & GlobalMin,
NumericVector & GlobalParams,
NumericVector & fitness,
NumericVector & f,
IntegerVector & trial,
Function & fun,
NumericMatrix & Foods,
const NumericVector & lb,
const NumericVector & ub
) {
int param2change, neighbour;
double ObjValSol, FitnessSol;
NumericVector solution(Foods.ncol());
for (int i=0;i<Foods.nrow();i++) {
// Random parameter to change
param2change = (int)(unif_rand()*Foods.ncol());
// Random neighbour to select
neighbour = i;
while (neighbour == i)
neighbour = (int)(unif_rand()*Foods.nrow());
// Suggesting new solution
solution = Foods(i,_);
solution[param2change] = Foods(i,param2change) +
(Foods(i, param2change) - Foods(neighbour, param2change))*(unif_rand()-.5)*2;
// Truncating
if (solution[param2change] < lb.at(param2change))
solution[param2change] = lb.at(param2change);
if (solution[param2change] > ub.at(param2change))
solution[param2change] = ub.at(param2change);
// Comparing current solution with new one
ObjValSol = as<double>(fun(solution));
FitnessSol = CalculateFitness(ObjValSol);
if (FitnessSol > fitness[i]) {
Foods(i,_) = solution;
fitness[i] = FitnessSol;
f[i] = ObjValSol;
trial[i] = 0;
} else {
trial[i]+=1;
}
}
return;
}
void CalculateProbabilities(
NumericMatrix & Foods,
NumericVector & fitness,
NumericVector & prob
) {
double maxfit = fitness[0];
for (int i=0;i<Foods.nrow();i++)
if (fitness[i] > maxfit) maxfit = fitness[i];
for (int i=0;i<Foods.nrow();i++)
prob[i] = (0.9*((fitness[i] + 1e-40)/(maxfit + 1e-40))) + 0.1;
return;
}
void SendOnlookerBees(
double & GlobalMin,
NumericVector & GlobalParams,
NumericVector & fitness,
NumericVector & f,
IntegerVector & trial,
NumericVector & prob,
Function & fun,
NumericMatrix & Foods,
const NumericVector & lb,
const NumericVector & ub) {
int param2change, neighbour;
double ObjValSol, FitnessSol;
NumericVector solution(Foods.ncol());
int t = 0, i=0;
while (t < Foods.nrow()) {
// Randomly choose a food source
if (unif_rand() < prob[i]) {
t++;
// Random parameter to change
param2change = (int)(unif_rand()*Foods.ncol());
// Random neighbour to select
neighbour = i;
while (neighbour == i)
neighbour = (int)(unif_rand()*Foods.nrow());
// Suggesting new solution
solution = Foods(i,_);
solution[param2change] = Foods(i,param2change) +
(Foods(i, param2change) - Foods(neighbour, param2change))*(unif_rand()-.5)*2;
// Truncating
if (solution[param2change] < lb.at(param2change))
solution[param2change] = lb.at(param2change);
if (solution[param2change] > ub.at(param2change))
solution[param2change] = ub.at(param2change);
// Comparing current solution with new one
ObjValSol = as<double>(fun(solution));
FitnessSol = CalculateFitness(ObjValSol);
if (FitnessSol > fitness[i]) {
Foods(i,_) = solution;
fitness[i] = FitnessSol;
f[i] = ObjValSol;
trial[i] = 0;
} else {
trial[i]+=1;
}
} else { /* if */
i++;
if (i==Foods.nrow()) i=0;
}
} /* while */
return;
}
void SendScoutBees(
NumericVector & fitness,
NumericVector & f,
IntegerVector & trial,
NumericVector & prob,
Function & fun,
NumericMatrix & Foods,
const NumericVector & lb,
const NumericVector & ub,
int limit
) {
int maxtrialindex = 0;
for(int i=0;i<Foods.nrow();i++) {
if (trial[i] > trial[maxtrialindex])
maxtrialindex = i;
}
// If it has reach the max, then init again
if (trial[maxtrialindex]>=limit)
init(maxtrialindex, fitness, f, trial, fun, Foods, lb, ub);
return;
}
// [[Rcpp::export(name=".abc_cpp")]]
List abc_cpp(
NumericVector & par,
Function & fn,
const NumericVector & lb,
const NumericVector & ub,
int FoodNumber = 20,
int limit = 100,
int maxCycle = 1000,
int criter = 50 // , double tol=1e-10
) {
// Initialize:
NumericMatrix Foods(FoodNumber, par.size());
NumericVector f(FoodNumber), fitness(FoodNumber), prob(FoodNumber);
IntegerVector trial(FoodNumber);
NumericMatrix ans(maxCycle, par.size());
// Initializing
NumericVector GlobalParams = clone(par);
double GlobalMin = as<double>(fn(GlobalParams));
// Should be distributed equally
for (int i=0;i<FoodNumber;i++) {
for (int j=0;j<par.size();j++)
Foods.at(i,j) = lb.at(j) + (ub.at(j) - lb.at(j))/(FoodNumber-1.0)*i;
// Checking if it is defined
f[i] = as<double>(fn(Foods(i,_)));
if (NumericVector::is_na(f[i]))
stop("Undefined value for -fn-. Check the function's domain.");
fitness[i] = CalculateFitness(f[i]);
trial[i] = 0;
}
int unchanged = 0;
MemorizeBetsSource(GlobalMin, GlobalParams, f, Foods, unchanged);
ans(0,_) = GlobalParams;
unchanged = 0;
// double OldGlobalMin = GlobalMin;
int i=0;
while (++i<maxCycle) {
SendEmployedBees(GlobalMin, GlobalParams, fitness, f, trial, fn, Foods, lb, ub);
CalculateProbabilities(Foods, fitness, prob);
SendOnlookerBees(GlobalMin, GlobalParams, fitness, f, trial, prob, fn, Foods, lb, ub);
MemorizeBetsSource(GlobalMin, GlobalParams, f, Foods, unchanged);
// Storing and breaking
ans(i,_) = GlobalParams;
if (unchanged>=criter) break;
// If not, then send them out again
SendScoutBees(fitness, f, trial, prob, fn, Foods, lb, ub, limit);
}
return List::create(
_["Foods"] = Foods,
_["f"] = f,
_["fitness"]= fitness,
_["trial"] = trial,
_["value"] = GlobalMin,
_["par"] = GlobalParams,
_["counts"] = i,
_["hist"] = ans(Range(0,i),_)
);
}
/***R
fun <- function(x) {
-cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))
}
library(microbenchmark)
library(ABCoptim)
microbenchmark(
abc_cpp(c(1,1),fun, ub = 5,lb = -5, criter = 20)[-8],
abc_optim(c(1,1), fun, ub = 5, lb=-5, criter=100, maxCycle = 100, FoodNumber = 20), times=100,
unit="relative"
)
# ans <- abc_cpp(c(1,1),fun, upper = 50,lower = -50, criter = 50, MNC = 500, SN = 20)
#
# fs <- function(x) sum(x^2)
# ans <- abc_cpp(rep(500,5), fs, lower = -10, upper=10, criter=200)
# tail(ans$ans)
#
# plot(ans$ans,type = "l")
*/