forked from VicentePerezSoloviev/SPEDA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJADE.py
More file actions
168 lines (149 loc) · 6.81 KB
/
JADE.py
File metadata and controls
168 lines (149 loc) · 6.81 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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# solution = np.array([0.5, 0.1, -0.3, 0.9, 0.4, -0.6, 0.2])
# dim_theta = solution.shape[0]
from benchmarks import Benchmarking
num_vars = 30
benchmarking = Benchmarking(num_vars)
# Cr = crossover rate
# F = mutation rate
# NP = n population
def differential_evolution(thetas_limit, target_vectors_init, objective_function, Cr=0.5, F=0.5, NP=10, max_gen=100, cr_type=''):
n_params = len(thetas_limit)
# Generate random target vectors
# target_vectors = np.random.rand(NP, n_params)
# target_vectors = np.interp(target_vectors, (0,1), (-1,1))
target_vectors = target_vectors_init.copy()
# Variable donor vectors
donor_vector = np.zeros(n_params)
# Variable trial vectors
trial_vector = np.zeros(n_params)
best_fitness = np.inf
list_best_fitness = []
for gen in range(max_gen):
for pop in range(NP):
# print("Target vectors :", target_vectors[pop])
# Untuk novelti ini bisa ditambahkan selectionnya berdasarkan ranking
index_choice = [i for i in range(NP) if i != pop]
a, b, c = np.random.choice(index_choice, 3)
donor_vector = target_vectors[a] - F * (target_vectors[b] - target_vectors[c])
# print("Donor vectors :", donor_vector)
n = np.random.randint(n_params)
L = np.random.randint(1, n_params)
n_end = n + L
cross_points = np.random.rand(n_params) < Cr
trial_vector = np.where(cross_points, donor_vector, target_vectors[pop])
# print("Trial vector", trial_vector)
target_fitness = objective_function(target_vectors[pop])
trial_fitness = objective_function(trial_vector)
# print("Target fitness :", target_fitness)
# print("Trial fitness :", trial_fitness)
if trial_fitness < target_fitness:
target_vectors[pop] = trial_vector.copy()
best_fitness = trial_fitness
else:
best_fitness = target_fitness
print("Generation:", str(gen), "Best fitness:", best_fitness)
list_best_fitness.append(best_fitness)
return list_best_fitness
def jade(thetas_limit, target_vectors_init, objective_function, uCR=0.5, uF=0.6, c=0.1, NP=10, max_gen=100):
n_params = len(thetas_limit)
# Generate random target vectors
# target_vectors = np.random.rand(NP, n_params)
target_vectors = target_vectors_init.copy()
target_fitness = np.zeros(NP)
# target_vectors = np.interp(target_vectors, (0,1), (-1,1))
# Variable donor vectors
donor_vector = np.zeros(n_params)
# Variable trial vectors
trial_vector = np.zeros(n_params)
best_fitness = np.inf
list_best_fitness = []
Fi = np.zeros(NP)
CRi = np.zeros(NP)
onethirdNP = NP // 3
for gen in range(max_gen):
# Success F dan Success CR
sCR = []
sF = []
random_onethird_idx = np.random.choice(np.arange(0, NP), size=onethirdNP, replace=False).tolist()
# Generate adaptive parameter Fi dan CRi
for pop in range(NP):
CRi[pop] = np.random.normal(uCR, 0.1)
if pop in random_onethird_idx:
Fi[pop] = np.interp(np.random.rand(), (0, 1), (0, 1.2))
else:
Fi[pop] = np.random.normal(uF, 0.1)
# print("Onethird ", random_onethird_idx)
# print("CRi ", CRi)
# print("Fi ", Fi)
# Evaluate target vectors
for pop in range(NP):
target_fitness[pop] = objective_function(target_vectors[pop])
for pop in range(NP):
# print("Target vectors :", target_vectors[pop])
current_best_idx = np.argmin(target_fitness)
index_choice = [i for i in range(NP) if i != pop]
a, b = np.random.choice(index_choice, 2)
donor_vector = target_vectors[pop] + Fi[pop] * (target_vectors[current_best_idx] - target_vectors[pop]) + \
Fi[pop] * (target_vectors[a] - target_vectors[b])
# print("Donor vectors :", donor_vector)
cross_points = np.random.rand(n_params) <= CRi[pop]
trial_vector = np.where(cross_points, donor_vector, target_vectors[pop])
# print("Trial vector", trial_vector)
trial_fitness = objective_function(trial_vector)
# print("Target fitness :", target_fitness)
# print("Trial fitness :", trial_fitness)
if trial_fitness < target_fitness[pop]:
target_vectors[pop] = trial_vector.copy()
sCR.append(CRi[pop])
sF.append(Fi[pop])
best_fitness = trial_fitness
else:
best_fitness = target_fitness[pop]
# Update uCR dan uF
# print("uCR : ", sCR)
# print("uF ", sF)
uCR = (1 - c) * uCR + c * np.mean(sCR)
# print(sCR, uCR, sF)
uF = (1 - c) * uF + c * (np.sum(np.power(sF, 2)) / np.sum(sF))
# print("Generation:", str(gen), "Best fitness:", best_fitness)
list_best_fitness.append(best_fitness)
return list_best_fitness
limits = [(-100, 100)] * num_vars
# print(limits)
'''size_gen = 200
target_vectors = np.random.rand(size_gen, num_vars)
target_vectors = np.interp(target_vectors, (0, 1), (-1, 1))
print("Differential Evolution")
result_de = differential_evolution(limits, target_vectors, benchmarking.rastrigins_function, NP=size_gen)
print("JADE")
result_jade = jade(limits, target_vectors, benchmarking.rastrigins_function,
uCR=0.2, uF=0.2, c=0.2, NP=size_gen)
fig, ax = plt.subplots()
ax.plot(result_de, label="DE")
ax.plot(result_jade, label="JADE")
ax.legend()
plt.show()
print("Best DE :", result_de[-1])
print("Best JADE :", result_jade[-1])'''
dt_results = pd.DataFrame(columns=['uCR', 'uF', 'c', 'size_gen', 'it', 'cost', 'cost_function'])
index = 0
filename = 'results_jade_Funcs.csv'
for cost_f in [['cec1', benchmarking.cec14_1],
['cec4', benchmarking.cec14_4],
['cec8', benchmarking.cec14_8]]:
for uCR in [0.25, 0.5, 0.75]:
for uF in [0.25, 0.5, 0.75]:
for c in [0.25, 0.5, 0.75]:
for size_gen in [200, 500, 1000]:
for it in range(15):
target_vectors = np.random.rand(size_gen, num_vars)
target_vectors = np.interp(target_vectors, (0, 1), (-1, 1))
result_jade = jade(limits, target_vectors, cost_f[1],
uCR=uCR, uF=uF, c=c, NP=size_gen, max_gen=int(150000/size_gen))
dt_results.loc[0] = [uCR, uF, c, size_gen, it, min(result_jade), cost_f[0]]
print(uCR, uF, c, size_gen, it, min(result_jade), cost_f[0])
index += 1
dt_results.to_csv(filename)