-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmes.py
More file actions
executable file
·736 lines (534 loc) · 25.5 KB
/
Copy pathAlgorithmes.py
File metadata and controls
executable file
·736 lines (534 loc) · 25.5 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import numpy as np
import random
import k_means
def SSE(data, k, solution):
sse_sum = 0
for d in data:
min_distance=np.linalg.norm(d-solution[0])
for centroid_ind in range(1,k):
distance = np.linalg.norm(d-solution[centroid_ind])
if (distance < min_distance):
min_distance = distance
sse_sum += min_distance
return sse_sum
def SSE1(data, k, solution):
sse_sum = 0
for d in data:
min_distance = min([np.linalg.norm(d-i) \
for i in solution])
sse_sum+=min_distance
return sse_sum
def fit(data, k, solution, n_objective_call):
n_objective_call +=1
return 1/(1+SSE(data, k, solution)) , n_objective_call
def generate_solution(k, d, Xmin, Xmax):
solution=np.empty((k,d))
for i in range(k):
for j in range(d):
solution[i,j] = Xmin[j] + (random.uniform(0,1) * (Xmax[j]-Xmin[j]))
return solution
def voisinage(k, d, solution, indice_solution, population, SN):
#voisinage d'origine (basique)
V=np.empty((k,d))
g = random.randint(0,SN-1)
while g == indice_solution :
g = random.randint(0,SN-1)
for i in range(k):
Fi = random.uniform(-1 , 1)
for j in range(d):
V[i,j]=solution[i,j] + (Fi*(solution[i,j]-population[g,i,j]))
return V
def calcul_proba(fitness, SN):
proba = np.empty(SN)
somme_fit = 0
for fitness_empl in fitness:
somme_fit += fitness_empl
somme_proba = 0
for i in range(SN):
somme_proba += (fitness[i] / somme_fit)
proba[i] = somme_proba
return proba
def choix_onlooker1(population, fitness, proba, SN):
alea = random.uniform(0,1)
j = 0
for j in range(SN): #parcourir le tableau des probabilites
if (alea < proba[j]):
return j
def voisinage_gabc(k, d, solution, indice_solution, population, Gbest, SN):
#voisinage Global Best
V=np.empty((k,d))
g = random.randint(0,SN-1)
while g == indice_solution :
g = random.randint(0,SN-1)
for i in range(k):
Fi = random.uniform(-1 , 1)
Psy = random.uniform(0 , 1.5)
for j in range(d):
V[i,j]=solution[i,j] + (Fi*(solution[i,j]-population[g,i,j])) + (Psy * (Gbest[i,j] - solution[i,j]))
return V
def binomial(k, d, mutant, parent, CR):
cross = np.empty((k,d))
j0 = random.randint(0,k-1)
for j in range(k):
if ((random.uniform(0,1) <= CR) or (j == j0)):
cross[j] = mutant[j]
else:
cross[j] = parent[j]
return cross
def voisinage_employed(k , d , solution, indice_solution, population, F1, SN):
V = np.empty((k,d))
i3 = random.randint(0,SN-1)
while i3 == indice_solution :
i3 = random.randint(0,SN-1)
i1 = random.randint(0,SN-1)
while i1 == indice_solution :
i1 = random.randint(0,SN-1)
i2 = random.randint(0,SN-1)
while i2 == indice_solution :
i2 = random.randint(0,SN-1)
for i in range(k):
kij = random.uniform(0,1)
for j in range(d):
V[i,j]=solution[i,j]+ (kij * (population[i1,i,j] - solution[i,j])) + (F1*(population[i2,i,j] - population[i3,i,j]))
return V
def voisinage_onlooker(k, d, solution, indice_solution, population, Gbest, SN):
#voisinage Global Best
V=np.empty((k,d))
g = random.randint(0,SN-1)
while g == indice_solution :
g = random.randint(0,SN-1)
for i in range(k):
Fi = random.uniform(-1 , 1)
Psy = random.uniform(0 , 1.5)
for j in range(d):
V[i,j]=solution[i,j] + (Fi*(solution[i,j]-population[g,i,j])) + (Psy * (Gbest[i,j] - solution[i,j]))
return V
def mutation(k, d, solution, indice_solution, population, Gbest, SN):
V = np.empty((k,d))
k1 = random.randint(0,SN-1)
while(k1 == indice_solution):
k1 = random.randint(0,SN-1)
k2 = random.randint(0,SN-1)
while(k2 == indice_solution):
k2 = random.randint(0,SN-1)
for i in range(k):
for j in range(d):
V[i,j]=(random.uniform(0,1)*(solution[i,j]-population[k1,i,j])) + (random.uniform(0,1)*(Gbest[i,j]-population[k2,i,j]))
return V
def current_to_rand_1(k, d, indice_solution, population, Gbest, F1):
V = np.empty((k,d))
pop_size = population.shape[0]
i = indice_solution
i1 = random.randint(0,pop_size-1)
while i1 == indice_solution :
i1 = random.randint(0,pop_size-1)
i2 = random.randint(0,pop_size-1)
while i2 == indice_solution :
i2 = random.randint(0,pop_size-1)
i3 = random.randint(0,pop_size-1)
while i3 == indice_solution :
i3 = random.randint(0,pop_size-1)
for g in range(k):
kij = random.uniform(0,1)
for j in range(d):
V[g,j] = population[i,g,j]+(kij*(population[i3,g,j]-population[i,g,j]))+(F1*(population[i1,g,j]-population[i2,g,j]))
return V
def rand_1(k, d, indice_solution, population, Gbest, F):
U=np.empty((k,d))
pop_size = population.shape[0]
i3 = random.randint(0,pop_size-1)
while i3 == indice_solution :
i3 = random.randint(0,pop_size-1)
i1 = random.randint(0,pop_size-1)
while i1 == indice_solution :
i1 = random.randint(0,pop_size-1)
i2 = random.randint(0,pop_size-1)
while i2 == indice_solution :
i2 = random.randint(0,pop_size-1)
for g in range(k):
for j in range(d):
U[g,j] = population[i3,g,j] + (F*(population[i1,g,j]-population[i2,g,j]))
return U
# ABC ----------------------------------------------------------------------
def ABC(data, k, MAX_FE = 10000, limit = 50, SN = 100):
n_objective_call = 0
essais = np.zeros(SN, dtype=int)
d=np.size(data[0])
Xmin = np.empty(d)
Xmax = np.empty(d)
for i in range(d):
Xmax[i] = np.amax(data.T[i])
Xmin[i] = np.amin(data.T[i])
population=np.empty((SN,k,d))
fitness=np.empty((SN))
Gbest = generate_solution(k, d, Xmin, Xmax)
Fbest, n_objective_call = fit(data, k, Gbest, n_objective_call)
for i in range(SN):
solution = generate_solution(k, d, Xmin, Xmax)
fitness_solution, n_objective_call = fit(data, k, solution, n_objective_call)
population[i] = solution
fitness[i] = fitness_solution
if (fitness_solution > Fbest):
Gbest = solution
Fbest = fitness_solution
while (n_objective_call <= MAX_FE):
#phase des employees :
for i, solution_courante in enumerate(population):
fitness_solution_courante = fitness[i]
voisine = voisinage(k , d , solution_courante , i, population, SN)
fitness_voisine, n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_voisine > fitness_solution_courante):
population[i] = voisine
fitness[i] = fitness_voisine
essais[i] = 0
if (fitness_voisine > Fbest):
Gbest = voisine
Fbest = fitness_voisine
else:
essais[i] += 1
#calcul des probabilite
proba = np.empty(SN)
proba = calcul_proba(fitness, SN)
#phase des onlookers
for i in range(SN):
#selection d'une solution
indice_choisi = choix_onlooker1(population, fitness, proba, SN)
onlooker_courant = population[indice_choisi]
fitness_onlooker_courant = fitness[indice_choisi]
#Exploitation de la solution
voisine = voisinage(k , d , onlooker_courant, indice_choisi , population, SN)
fitness_voisine , n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_onlooker_courant < fitness_voisine):
population[indice_choisi] = voisine
fitness[indice_choisi] = fitness_voisine
essais[indice_choisi] = 0
if (fitness_voisine > Fbest):
Fbest = fitness_voisine
Gbest = voisine
else:
essais[indice_choisi] += 1
#Phase scout
for i in range(SN):
if (essais[i]>limit):
essais[i]=0
nouvelle_solution = generate_solution(k, d, Xmin, Xmax)
population[i] = nouvelle_solution
nouvelle_fitness , n_objective_call = fit(data, k, nouvelle_solution, n_objective_call)
fitness[i] = nouvelle_fitness
if (nouvelle_fitness > Fbest):
Gbest = nouvelle_solution
Fbest = nouvelle_fitness
return Gbest
# GABC---------------------------------------------------------------------------
def GABC(data, k, MAX_FE = 10000, limit = 50, SN = 100):
n_objective_call = 0
essais = np.zeros(SN, dtype=int)
d=np.size(data[0])
Xmin = np.empty(d)
Xmax = np.empty(d)
for i in range(d):
Xmax[i] = np.amax(data.T[i])
Xmin[i] = np.amin(data.T[i])
population=np.empty((SN,k,d))
fitness=np.empty((SN))
Gbest = generate_solution(k, d, Xmin, Xmax)
Fbest , n_objective_call = fit(data, k, Gbest, n_objective_call)
for i in range(SN):
solution = generate_solution(k, d, Xmin, Xmax)
fitness_solution , n_objective_call = fit(data, k, solution, n_objective_call)
population[i] = solution
fitness[i] = fitness_solution
if (fitness_solution > Fbest):
Gbest = solution
Fbest = fitness_solution
while (n_objective_call <= MAX_FE):
#phase des employees :
for i, solution_courante in enumerate(population):
fitness_solution_courante = fitness[i]
voisine = voisinage_gabc(k , d , solution_courante , i, population, Gbest, SN)
fitness_voisine , n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_voisine > fitness_solution_courante):
population[i] = voisine
fitness[i] = fitness_voisine
essais[i] = 0
if (fitness_voisine > Fbest):
Gbest = voisine
Fbest = fitness_voisine
else:
essais[i] += 1
#calcul des probabilites
proba = np.empty(SN)
proba = calcul_proba(fitness, SN)
#phase des onlookers
for i in range(SN):
#selection d'une solution
indice_choisi = choix_onlooker1(population, fitness, proba, SN)
onlooker_courant = population[indice_choisi]
fitness_onlooker_courant = fitness[indice_choisi]
#Exploitation de la solution
voisine = voisinage_gabc(k , d , onlooker_courant, indice_choisi , population, Gbest, SN)
fitness_voisine , n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_onlooker_courant < fitness_voisine):
population[indice_choisi] = voisine
fitness[indice_choisi] = fitness_voisine
essais[indice_choisi] = 0
if (fitness_voisine > Fbest):
Fbest = fitness_voisine
Gbest = voisine
else:
essais[indice_choisi] += 1
#Phase scout
for i in range(SN):
if (essais[i]>limit):
essais[i]=0
nouvelle_solution = generate_solution(k, d, Xmin, Xmax)
population[i] = nouvelle_solution
nouvelle_fitness , n_objective_call = fit(data, k, nouvelle_solution, n_objective_call)
fitness[i] = nouvelle_fitness
if (nouvelle_fitness > Fbest):
Gbest = nouvelle_solution
Fbest = nouvelle_fitness
return Gbest
#ABC_DE -----------------------------------------------------------------
def ABC_DE(data, k, MAX_FE = 10000, limit = 50, SN = 100):
n_objective_call = 0
essais = np.zeros(SN, dtype=int)
d=np.size(data[0])
Xmin = np.empty(d)
Xmax = np.empty(d)
for i in range(d):
Xmax[i] = np.amax(data.T[i])
Xmin[i] = np.amin(data.T[i])
population=np.empty((SN,k,d));
fitness=np.empty((SN))
Gbest = generate_solution(k, d, Xmin, Xmax)
Fbest , n_objective_call = fit(data, k, Gbest, n_objective_call)
for i in range(SN):
solution = generate_solution(k, d, Xmin, Xmax)
fitness_solution , n_objective_call = fit(data, k, solution, n_objective_call)
population[i] = solution
fitness[i] = fitness_solution
if (fitness_solution > Fbest):
Gbest = solution
Fbest = fitness_solution
while (n_objective_call <= MAX_FE):
F1 = random.uniform(0,1)
#phase des employes :
for i, solution_courante in enumerate(population):
fitness_solution_courante = fitness[i]
voisine = voisinage_employed(k , d , solution_courante , i, population, F1, SN)
fitness_voisine , n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_voisine > fitness_solution_courante):
population[i] = voisine
fitness[i] = fitness_voisine
essais[i] = 0
if (fitness_voisine > Fbest):
Gbest = voisine
Fbest = fitness_voisine
else:
essais[i] += 1
#calcul des probabilités
proba = np.empty(SN)
proba = calcul_proba(fitness, SN)
#phase des onlookers
for i in range(SN):
#selection d'une solution
indice_choisi = choix_onlooker1(population, fitness, proba, SN)
onlooker_courant = population[indice_choisi]
fitness_onlooker_courant = fitness[indice_choisi]
#Exploitation de la solution
voisine = voisinage_onlooker(k , d , onlooker_courant, indice_choisi , population, Gbest, SN)
fitness_voisine , n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_onlooker_courant < fitness_voisine):
population[indice_choisi] = voisine
fitness[indice_choisi] = fitness_voisine
essais[indice_choisi] = 0
if (fitness_voisine > Fbest):
Fbest = fitness_voisine
Gbest = voisine
else:
essais[indice_choisi] += 1
#Phase mutation
for i in range(SN):
S_new = mutation(k, d, solution, i, population, Gbest, SN)
F_new , n_objective_call = fit(data, k, S_new, n_objective_call)
if (fitness[i] < F_new):
population[i] = S_new
fitness[i] = F_new
essais[i] = 0
if (F_new > Fbest):
Fbest = F_new
Gbest = S_new
else:
essais[i] += 1
#Phase scout
for i in range(SN):
if (essais[i]>limit):
essais[i]=0
nouvelle_solution = generate_solution(k, d, Xmin, Xmax)
population[i] = nouvelle_solution
nouvelle_fitness , n_objective_call = fit(data, k, nouvelle_solution, n_objective_call)
fitness[i] = nouvelle_fitness
if (nouvelle_fitness > Fbest):
Gbest = nouvelle_solution
Fbest = nouvelle_fitness
return Gbest
#ABC_DE_K -----------------------------------------------------------------
def ABC_DE_K(data, k, MAX_FE = 10000, limit = 50, SN = 100):
n_objective_call = 0
essais = np.zeros(SN, dtype=int)
d=np.size(data[0])
Xmin = np.empty(d)
Xmax = np.empty(d)
for i in range(d):
Xmax[i] = np.amax(data.T[i])
Xmin[i] = np.amin(data.T[i])
population=np.empty((SN,k,d));
fitness=np.empty((SN))
Gbest = np.array(k_means.k_means(data.tolist(), k))
Fbest , n_objective_call = fit(data, k, Gbest, n_objective_call)
for i in range(SN):
solution = generate_solution(k, d, Xmin, Xmax)
fitness_solution , n_objective_call = fit(data, k, solution, n_objective_call)
population[i] = solution
fitness[i] = fitness_solution
if (fitness_solution > Fbest):
Gbest = solution
Fbest = fitness_solution
while (n_objective_call <= MAX_FE):
F1 = random.uniform(0,1)
#phase des employĂŠes :
for i, solution_courante in enumerate(population):
fitness_solution_courante = fitness[i]
voisine = voisinage_employed(k , d , solution_courante , i, population, F1, SN)
fitness_voisine , n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_voisine > fitness_solution_courante):
population[i] = voisine
fitness[i] = fitness_voisine
essais[i] = 0
if (fitness_voisine > Fbest):
Gbest = voisine
Fbest = fitness_voisine
else:
essais[i] += 1
#calcul des probabilites
proba = np.empty(SN)
proba = calcul_proba(fitness, SN)
#phase des onlookers
for i in range(SN):
#selection d'une solution
indice_choisi = choix_onlooker1(population, fitness, proba, SN)
onlooker_courant = population[indice_choisi]
fitness_onlooker_courant = fitness[indice_choisi]
#Exploitation de la solution
voisine = voisinage_onlooker(k , d , onlooker_courant, indice_choisi , population, Gbest, SN)
fitness_voisine , n_objective_call = fit(data, k, voisine, n_objective_call)
if (fitness_onlooker_courant < fitness_voisine):
population[indice_choisi] = voisine
fitness[indice_choisi] = fitness_voisine
essais[indice_choisi] = 0
if (fitness_voisine > Fbest):
Fbest = fitness_voisine
Gbest = voisine
else:
essais[indice_choisi] += 1
#Phase mutation
for i in range(SN):
S_new = mutation(k, d, solution, i, population, Gbest, SN)
F_new , n_objective_call = fit(data, k, S_new, n_objective_call)
if (fitness[i] < F_new):
population[i] = S_new
fitness[i] = F_new
essais[i] = 0
if (F_new > Fbest):
Fbest = F_new
Gbest = S_new
else:
essais[i] += 1
#Phase scout
for i in range(SN):
if (essais[i]>limit):
essais[i]=0
nouvelle_solution = generate_solution(k, d, Xmin, Xmax)
population[i] = nouvelle_solution
nouvelle_fitness , n_objective_call = fit(data, k, nouvelle_solution, n_objective_call)
fitness[i] = nouvelle_fitness
if (nouvelle_fitness > Fbest):
Gbest = nouvelle_solution
Fbest = nouvelle_fitness
return Gbest
def DE_rand_1_bin(data , k, MAX_FE = 10000, pop_size = 50, F = 0.6, CR = 0.9):
#Génération de la population initiale
n_objective_call = 0
d=np.size(data[0])
Xmin = np.empty(d)
Xmax = np.empty(d)
for i in range(d):
Xmax[i] = np.amax(data.T[i])
Xmin[i] = np.amin(data.T[i])
population=np.empty((pop_size,k,d));
fitness=np.empty((pop_size))
Gbest = generate_solution(k, d, Xmin, Xmax)
Fbest , n_objective_call = fit(data, k, Gbest, n_objective_call)
for i in range(pop_size):
solution = generate_solution(k, d, Xmin, Xmax)
fitness_solution , n_objective_call = fit(data, k, solution, n_objective_call)
population[i] = solution
fitness[i] = fitness_solution
if (fitness_solution > Fbest):
Gbest = solution
Fbest = fitness_solution
while(n_objective_call <= MAX_FE):
for i in range(pop_size):
fitness_parent = fitness[i]
#génération du mutant
trial_vector = rand_1(k, d, i, population, Gbest, F)
#génération du descendant
enfant = binomial(k, d, trial_vector, population[i], CR)
fitness_enfant , n_objective_call = fit(data, k , enfant, n_objective_call)
#sélection entre le parent et le descendant
if(fitness_enfant > fitness_parent):
population[i] = enfant
fitness[i] = fitness_enfant
if(fitness_enfant > Fbest):
Gbest = enfant
Fbest = fitness_enfant
return Gbest
#current to rand ---------------------------------------------------
def DE_current_to_rand_1(data , k, MAX_FE = 10000, pop_size = 50):
#generation de la population initiale
n_objective_call = 0
d=np.size(data[0])
Xmin = np.empty(d)
Xmax = np.empty(d)
for i in range(d):
Xmax[i] = np.amax(data.T[i])
Xmin[i] = np.amin(data.T[i])
population=np.empty((pop_size,k,d))
fitness=np.empty((pop_size))
Gbest = generate_solution(k, d, Xmin, Xmax)
Fbest , n_objective_call = fit(data, k, Gbest, n_objective_call)
for i in range(pop_size):
solution = generate_solution(k, d, Xmin, Xmax)
fitness_solution , n_objective_call = fit(data, k, solution, n_objective_call)
population[i] = solution
fitness[i] = fitness_solution
if (fitness_solution > Fbest):
Gbest = solution
Fbest = fitness_solution
while(n_objective_call <= MAX_FE):
F1 = random.uniform(0,1)
for i in range(pop_size):
fitness_parent = fitness[i]
#generation du mutant
trial_vector = current_to_rand_1(k, d, i, population, Gbest, F1)
enfant = trial_vector
fitness_enfant , n_objective_call = fit(data, k , enfant, n_objective_call)
#sélection entre le mutant et le parent
if(fitness_enfant > fitness_parent):
population[i] = enfant
fitness[i] = fitness_enfant
if(fitness_enfant > Fbest):
Gbest = enfant
Fbest = fitness_enfant
return Gbest