-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPS_04 TikTak_IS.jl
337 lines (266 loc) · 7.88 KB
/
PS_04 TikTak_IS.jl
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
#PS_04 TikTak. With errors
#From the paper:
#Step 0. Initialization:
#1. Determine bounds for each parameter.
#2. Generate a sequence of Sobol’ points with length N .
#3. Evaluate the function value at each of these N Sobol’ points. Keep the set of N ∗
#Sobol’ points that have the lowest function values, and order them in descending
#order, as s1, . . . , sN ∗ , with f (s1) ≤ · · · ≤ f (sN ∗ ).
#4. Set the global iteration number to i = 1.
#Step 1. Global stage:
#1.Select the ith value (vector) in the Sobol’ sequence: s_i.
#2. If i > 1, read the function value (and corresponding parameter vector) of the smallest
#recorded local minimum from the “wisdom.dat” text file. Denote the lowest function
#value found so far (as of iteration i − 1) as f_low and the corresponding parameter
#vector as p_low.
#3. Generate a starting point (i.e., initial guess) S_i for the local search by using the
#convex combination of the Sobol’ point s_i and the parameter value p _low that gen-
#erated the best local minimum found so far: S_i = (1 − θi)s_i + θ p_low. The weight
#parameter θi ∈ [0, θ] with θ < 1 increases with i.
#Step 2: Local stage:
#– Select a local optimizer Nelder-Mead and implement a local search at the identified starting point Si until a local minimum
#is found.
#– Select a stopping criterion for the local search algorithm (in this paper, we use
#tolerances of either 10^(-3) as convergence criteria).
#– Open the wisdom.dat file and record the local minimum (function value and pa-
#rameters).
#Step 3. Stopping rule:
#– Repeat Steps 1 and 2 until local searches are completed from starting points that
#use each of the N ∗ Sobol’ points.
#– Return the point with the lowest function value as global minimum.
using Random
using Parameters
using Sobol
using Optim
# Define the Griewank fn with n=2
function griewank(x)
n = length(x)
sum_term = sum((x[i]^2)/2000 for i in 1:n)
prod_term = prod(cos(x[i]/sqrt(i)) for i in 1:n)
return 1 + sum_term - prod_term
end
# Set the bounds
lb = [-100.0, -100.0]
ub = [100.0, 100.0]
N = 2000
# Generate Sobol seq
sobol_gen = SobolSeq(2)
sobol_pts = [next!(sobol_gen) for _ in 1:N]
# Evaluate fn
fn_val = zeros(N)
for i in 1:N
x = (ub .- lb) .* sobol_pts[i] .+ lb
fn_val[i] = griewank(x)
end
# Sort Sobol pnt and fn values
sort_ind = sortperm(fn_val)
sobol_pts_sort = sobol_pts[sort_ind,:]
fn_val_sort = fn_val[sort_ind]
# Number of local searches
n = N
# step 1: global stage
# Set initial values
i = 1
θ = 0.3
p_low = [-100.0, -100.0]
f_low = griewank(p_low)
# Matrix to store results
matrix = zeros(N, 3)
while i <= N
s_i = sobol_pts_sort[i,:]
##Error to fix
S_i = (1 .- θ*i) .* s_i .+ (θ*i) .* p_low'
# step 2: local stage
for j in 1:N
x = (1 .- θ) .* sobol_pts[j, :] .+ θ .* p_low'
res = optimize(x -> griewank(x), lb, ub, x, Optim.Options(iterations=100), method = NelderMead())
f = res.minimum
if f < f_low
f_low = f
best_x = res.minimizer
end
matrix[j, 1:3] = [f, x[1], x[2]]
end
i += 1
end
# Find best solution
f_best, x_best_idx = findmin(matrix[:, 1])
x_best = matrix[x_best_idx, 2:3]
println("Best sol: ", x_best)
println("Min: ", f_best)
using Plots
gr()
x = range(-100, 100, length=1000)
y = range(-100, 100, length=1000)
Z = [griewank([x[i], y[j]]) for j in eachindex(y), i in eachindex(x)]
pl = plot(x, y, Z, st=:contourf, title="Griewank fn")
contour!(x, y, Z, levels=50)
display(pl)
# Griewark for n=5
function griewank(x)
n = length(x)
sum_term = sum((x[i]^2)/2000 for i in 1:n)
prod_term = prod(cos(x[i]/sqrt(i)) for i in 1:n)
return 1 + sum_term - prod_term
end
n = 5
lb = [-100.0 for _ in 1:n]
ub = [100.0 for _ in 1:n]
sobol_gen = SobolSeq(n)
sobol_pts = [next!(sobol_gen) for _ in 1:N]
fn_val = zeros(N)
for i in 1:N
x = (ub .- lb) .* sobol_pts[i] .+ lb
fn_val[i] = griewank(x)
end
sort_ind = sortperm(fn_val)
sobol_pts_sort = sobol_pts[sort_ind,:]
fn_val_sort = fn_val[sort_ind]
n = N
# Set initial values
i = 1
θ = 0.3
p_low = [-100.0 for _ in 1:n]
f_low = griewank(p_low)
# Matrix to store results
matrix = zeros(N, n + 1)
while i <= N
s_i = sobol_pts_sort[i,:]
# the same error
S_i = (1 .- θ*i) .* s_i .+ (θ*i) .* p_low'
# step 2: local stage
for j in 1:N
x = (1 .- θ) .* sobol_pts[j, :] .+ θ .* p_low'
res = optimize(x -> griewank(x), lb, ub, x, Optim.Options(iterations=100), method = NelderMead())
f = res.minimum
if f < f_low
f_low = f
best_x = res.minimizer
end
matrix[j, 1:(n+1)] = [f, best_x...]
end
i += 1
end
f_best, x_best_idx = findmin(matrix[:, 1])
x_best = matrix[x_best_idx, 2:(n+1)]
println("Best sol", x_best)
println("Min", f_best)
# Rastrigin fn n=2
function rastrigin(x)
n = length(x)
A = 10
return A * n + sum(x[i]^2 - A * cos(2 * pi * x[i]) for i in 1:n)
end
# Set bounds
n = 2
lb = [-5.12 for _ in 1:n]
ub = [5.12 for _ in 1:n]
N = 2000
# Generate Sobol seq
sobol_gen = SobolSeq(n)
sobol_pts = [next!(sobol_gen) for _ in 1:N]
# Evaluate the fn
fn_val = zeros(N)
for i in 1:N
x = (ub .- lb) .* sobol_pts[i] .+ lb
fn_val[i] = rastrigin(x)
end
# Sort Sobol pnts and fn values
sort_ind = sortperm(fn_val)
sobol_pts_sort = sobol_pts[sort_ind,:]
fn_val_sort = fn_val[sort_ind]
# Number of local searches
n = N
# step 1: global stage
# Set initial values
i = 1
θ = 0.3
p_low = [-5.12, -5.12]
f_low = rastrigin(p_low)
# Matrix to store results
matrix = zeros(N, 3)
while i <= N
s_i = sobol_pts_sort[i,:]
#error
S_i = (1 .- θ*i) .* s_i .+ (θ*i) .* p_low'
# step 2: local stage
for j in 1:N
x = (1 .- θ) .* sobol_pts[j, :] .+ θ .* p_low'
res = optimize(x -> rastrigin(x), lb, ub, x, Optim.Options(iterations=100), method = NelderMead())
f = res.minimum
if f < f_low
f_low = f
best_x = res.minimizer
end
matrix[j, 1:3] = [f, x[1], x[2]]
end
i += 1
end
# Find best solution
f_best, x_best_idx = findmin(matrix[:, 1])
x_best = matrix[x_best_idx, 2:3]
println("Best sol", x_best)
println("Min", f_best)
# Plotting
using Plots
gr()
x = range(-5.12, 5.12, length=1000)
y = range(-5.12, 5.12, length=1000)
Z = [rastrigin([x[i], y[j]]) for j in eachindex(y), i in eachindex(x)]
pl_rast = plot(x, y, Z, st=:contourf, title="Rastrigin fn")
contour!(x, y, Z, levels=50)
display(pl_rast)
# Rastrigin fn with n=5
function rastrigin(x)
n = length(x)
A = 10
return A * n + sum(x[i]^2 - A * cos(2 * pi * x[i]) for i in 1:n)
end
n = 5
lb = [-5.12 for _ in 1:n]
ub = [5.12 for _ in 1:n]
N = 2000
# Generate Sobol seq
sobol_gen = SobolSeq(n)
sobol_pts = [next!(sobol_gen) for _ in 1:N]
# Evaluate the fn
fn_val = zeros(N)
for i in 1:N
x = (ub .- lb) .* sobol_pts[i] .+ lb
fn_val[i] = rastrigin(x)
end
sort_ind = sortperm(fn_val)
sobol_pts_sort = sobol_pts[sort_ind,:]
fn_val_sort = fn_val[sort_ind]
# Number of local searches
n = N
# step 1: global stage
# Set initial values
i = 1
θ = 0.3
p_low = [-5.12 for _ in 1:5]
f_low = rastrigin(p_low)
# Matrix to store results
matrix = zeros(N, 6)
while i <= N
s_i = sobol_pts_sort[i,:]
#error
S_i = (1 .- θ*i) .* s_i .+ (θ*i) .* p_low'
# Step 2: Local stage
for j in 1:N
x = (1 .- θ) .* sobol_pts[j, :] .+ θ .* p_low'
res = optimize(x -> rastrigin(x), lb, ub, x, Optim.Options(iterations=100), method = NelderMead())
f = res.minimum
if f < f_low
f_low = f
best_x = res.minimizer
end
matrix[j, 1:6] = [f, x...]
end
i += 1
end
# Find best solution
f_best, x_best_idx = findmin(matrix[:, 1])
x_best = matrix[x_best_idx, 2:6]
println("Best sol", x_best)
println("Min", f_best)