-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbee.js
377 lines (273 loc) · 8.91 KB
/
bee.js
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
var NP = 20;
var FoodNumber = NP / 2 ;
var limit = 100;
var maxCycle = 10;
// Generating random data..
a=[]; b=[]; c=[];
var x_marker=[];
var y_marker=[];
var z_marker=[];
var layout = {
autosize: false,
width: 1000,
height: 900
};
for(i=-50;i<100;i++){
for (j=-50;j<100;j++){
z = i*i + j*j;
a.push(i);
b.push(j);
c.push(z);
}
}
// Plotting the mesh
var data=[
{
x: [1],
y: [1],
z: [1],
mode: 'markers',
type: 'scatter3d',
marker: {
color: 'rgb(23, 190, 207)',
size: 10
}
},
{
opacity:0.8,
color:'rgb(300,000,200)',
type: 'mesh3d',
x: a,
y: b,
z: c,
}
];
Plotly.newPlot('myDiv', data,layout);
function PlotlyAnimate(foods,objfun){
/*
foods nxD Nj type array
objfun is the callback function
*/
var x_marker = [];
var y_marker = [];
var z_marker = [];
z_marker = objfun(foods)
.tolist();
foods = foods.tolist();
for (var i = 0 ; i<foods.length;i++){
x_marker.push(foods[i][0])
y_marker.push(foods[i][1])
}
var data = [{
x: x_marker,
y: y_marker,
z: z_marker,
mode: 'markers',
type: 'scatter3d',
marker: {
color: 'rgb(23, 190, 207)',
size: 10
}
},];
Plotly.animate('myDiv', {
data,
traces: [0],
layout: {}
}, {
transition: {
duration: 50,
easing: 'cubic-in-out'
},
frame: {
duration: 50
}
},layout);
}
function repmat(arr,FoodNumber){
// Replicate and tile an array.
//arr is the array to be replicated.
// replicate is the array defines the number of replication.
var range = [];
for (var repmat=0; repmat<FoodNumber ; repmat++){
range.push(arr);
}
return nj.stack(range);
}
function calculateFitness(fObjV){
var fFitness = [];
fObjV = fObjV.tolist();
// Find indeces in fObjV greater than 0
fObjV.forEach(element => {
if (element >= 0){
fFitness.push(1/(element+1));
}
else{
fFitness.push(1 + Math.abs(element));
}
});
return nj.array(fFitness);
}
objfun = function(foods){
/*
Foods is the nj array
*/
var S = foods.multiply(foods);
S = S.T; //Transpose
// a= [[1,2],[3,4]] => shape [2,2]
var tmp = [];
for (var i=0; i<S.shape[1]; i++){ //S.shape[1] => Number of elements for example:10
var sum = 0;
for (var j=0; j<S.shape[0];j++){ // S.shape[0] => Dimension for example: 2
sum += S.get(j,i);
}
tmp.push(sum);
}
return nj.array(tmp);
}
var D = 2;
var ub = nj.ones([1,D]).multiply(100);
var lb = nj.ones([1,D]).multiply(-100);
var runtime = 1;
var GlobalMins = nj.zeros([1,runtime]).flatten();
for (var r=0; r<runtime; r++){
rangeinterval = (ub.subtract(lb)).flatten();
// mimic repmat element
var Range = repmat(rangeinterval,FoodNumber);
var Lower = repmat(lb.flatten(),FoodNumber);
var Foods = nj.random([FoodNumber,D]).multiply(Range).add(Lower) ;
var ObjVal =objfun(Foods);
var Fitness = calculateFitness(ObjVal);
var trial = nj.zeros([1,FoodNumber]).reshape(-1);
var minObjVal = Math.min(...(ObjVal.tolist()));
var BestInd = ObjVal.tolist().indexOf(minObjVal);
var GlobalMin = minObjVal;
var GlobalParams = [];
for (var i=0; i<D ; i++){
GlobalParams.push(Foods.get(BestInd,i));
}
GlobalParams = nj.array(GlobalParams);
iter = 0;
// console.log(ObjVal);
// console.log(minObjVal);
// console.log(BestInd);
// console.log(GlobalParams);
while (iter <= maxCycle){
// Employed Bee Phased
for (var i =0; i<FoodNumber-1;i++){
var Param2Change = Math.floor(Math.random() * D);
var neighbour = Math.floor(Math.random() * FoodNumber); // Between 0 and 9
while (neighbour === i){
neighbour = Math.floor(Math.random() * FoodNumber);
}
var sol = [];
for (var j=0; j<D ; j++){
sol.push(Foods.get(i,j));
}
// sol = nj.array(sol);
sol[Param2Change] = Foods.get(i,Param2Change) + (Foods.get(i,Param2Change) - Foods.get(neighbour,Param2Change))*(Math.random()-0.5)*2;
// if generated parameter value is out of boundaries, it is shifted onto the boundaries
sol.forEach((element,ind)=>{
if (element < lb.get(0,ind)){
sol[ind] = lb.get(0,ind);
}
else if (element > ub.get(0,ind)){
sol[ind] = ub.get(0,ind);
}
})
// evaluate new solution
var ObjValSol =objfun(nj.array(sol).reshape(1,D));
var FitnessSol = calculateFitness(ObjValSol);
// Greedy Selection
if (FitnessSol.get(0) > Fitness.get(i)){
for (var k=0; k<D; k++) {
Foods.set(i,k,sol[k]);
}
Fitness.set(i,FitnessSol.get(0));
ObjVal.set(i,ObjValSol.get(0));
trial.set(i,0);
}
else{
trial.set(i, trial.get(i) + 1) ;
}
} // For loop employed bee
var prob = (Fitness.divide(Math.max(...Fitness.tolist()))).multiply(0.9).add(0.1);
// Onlooker Bee Phase
var i = 0;
var t = 0;
while(t < FoodNumber){
if (Math.random() < prob.get(0)){
t +=1;
var Param2Change = Math.floor(Math.random() * D);
var neighbour = Math.floor(Math.random() * FoodNumber); // Between 0 and 9
while (neighbour === i){
neighbour = Math.floor(Math.random() * FoodNumber);
}
var sol = [];
for (var j=0; j<D ; j++){
sol.push(Foods.get(i,j));
}
// sol = nj.array(sol);
sol[Param2Change] = Foods.get(i,Param2Change) + (Foods.get(i,Param2Change) - Foods.get(neighbour,Param2Change))*(Math.random()-0.5)*2;
// if generated parameter value is out of boundaries, it is shifted onto the boundaries
sol.forEach((element,ind)=>{
if (element < lb.get(0,ind)){
sol[ind] = lb.get(0,ind);
}
else if (element > ub.get(0,ind)){
sol[ind] = ub.get(0,ind);
}
})
// evaluate new solution
ObjValSol =objfun(nj.array(sol).reshape(1,D));
FitnessSol = calculateFitness(ObjValSol);
// Greedy Selection
if (FitnessSol.get(0) > Fitness.get(i)){
for (var k=0; k<D; k++) {
Foods.set(i,k,sol[k]);
}
Fitness.set(i,FitnessSol.get(0));
ObjVal.set(i,ObjValSol.get(0));
trial.set(i,0);
}
else{
trial.set(i, trial.get(i) + 1) ;
}
} // end of if (Math.random() < prob.get(0))
i += 1;
if (i === FoodNumber) {
i = 1;
}
}// end of while (t < FoodNumber)
// Best Food source is memorized
var minObjVal = Math.min(...(ObjVal.tolist()));
var BestInd = ObjVal.tolist().indexOf(minObjVal);
if (ObjVal.tolist()[BestInd] < GlobalMin){
GlobalMin = minObjVal;
GlobalParams = [];
for (var i=0; i<D ; i++){
GlobalParams.push(Foods.get(BestInd,i));
}
GlobalParams = nj.array(GlobalParams);
}
// SCOUT BEE PHASE
maxTrial = Math.max(...trial.tolist());
maxTrialInd = trial.tolist().indexOf(maxTrial);
if (trial.get(maxTrialInd) > limit){
trial.set(maxTrialInd,0);
sol =(ub.subtract(lb)).flatten().multiply(nj.random([1,D]).flatten()).add(lb.flatten()).tolist();
ObjValSol =objfun(nj.array(sol).reshape(1,D));
FitnessSol = calculateFitness(ObjValSol);
for (var k=0; k<D; k++) {
Foods.set(maxTrialInd,k,sol[k]);
}
Fitness.set(maxTrialInd,FitnessSol.get(0));
ObjVal.set(maxTrialInd,ObjValSol.get(0));
// fitnessSol => 0.00047995880408433395
}
console.log('Iter',iter,GlobalMin);
iter +=1;
// Plot Animation
PlotlyAnimate(Foods,objfun);
}//end while (iter <= maxCycle) (End of ABC)
GlobalMins.set(r,GlobalMin);
}