Skip to content

Commit c5a9c9c

Browse files
Format .jl files (UB-SSDC-Lab#60)
Co-authored-by: GrantHecht <GrantHecht@users.noreply.github.com>
1 parent d1bac6b commit c5a9c9c

5 files changed

Lines changed: 48 additions & 27 deletions

File tree

scripts/de_testing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mutation_strategy = SelfMutationParameters(
5555
crossover_strategy = SelfBinomialCrossoverParameters(;
5656
dist=Uniform(0.0, 1.0),
5757
#transform = GlobalOptimization.NoTransformation()
58-
transform = GlobalOptimization.UncorrelatedCovarianceTransformation(0.5, 0.8, N; ps = 1.0),
58+
transform=GlobalOptimization.UncorrelatedCovarianceTransformation(0.5, 0.8, N; ps=1.0),
5959
#transform = GlobalOptimization.CovarianceTransformation(0.1, 0.5, N)
6060
)
6161

src/DE/crossover.jl

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct UncorrelatedCovarianceTransformation <: RotationMatrixBasedCrossoverTrans
191191
UncorrelatedCovarianceTransformation(1.0, 0.5, 0.95, [1.0630691323565e-311 1.0630691151907e-311 … 1.063069230316e-311 1.063069119645e-311; 1.0630691158705e-311 1.063069115333e-311 … 1.063069172704e-311 1.0630692904614e-311; … ; 1.063069115428e-311 1.063069114246e-311 … 1.063069124886e-311 1.0630694190924e-311; 1.0630691153804e-311 1.0630691141986e-311 … 1.0630691348624e-311 1.063069428614e-311], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt16[], UInt16[])
192192
```
193193
"""
194-
function UncorrelatedCovarianceTransformation(pb, a, num_dims; ps = 1.0)
194+
function UncorrelatedCovarianceTransformation(pb, a, num_dims; ps=1.0)
195195
if ps <= 0.0 || ps > 1.0
196196
throw(ArgumentError("ps must be in the range (0, 1]."))
197197
end
@@ -202,13 +202,23 @@ struct UncorrelatedCovarianceTransformation <: RotationMatrixBasedCrossoverTrans
202202
throw(ArgumentError("a must be in the range (0, 1]."))
203203
end
204204
B = Matrix{Float64}(undef, num_dims, num_dims)
205-
return new(ps, pb, a, B, zeros(num_dims), zeros(num_dims), Vector{UInt16}(undef, 0), Vector{UInt16}(undef, 0))
205+
return new(
206+
ps,
207+
pb,
208+
a,
209+
B,
210+
zeros(num_dims),
211+
zeros(num_dims),
212+
Vector{UInt16}(undef, 0),
213+
Vector{UInt16}(undef, 0),
214+
)
206215
end
207-
208216
end
209217

210218
initialize!(transformation::NoTransformation, population_size) = nothing
211-
function initialize!(transformation::RotationMatrixBasedCrossoverTransformation, population_size)
219+
function initialize!(
220+
transformation::RotationMatrixBasedCrossoverTransformation, population_size
221+
)
212222
resize!(transformation.idxs, population_size)
213223
transformation.idxs .= 1:population_size
214224
return nothing
@@ -241,7 +251,9 @@ function update_transformation!(transformation::CovarianceTransformation, popula
241251

242252
return nothing
243253
end
244-
function update_transformation!(transformation::UncorrelatedCovarianceTransformation, population)
254+
function update_transformation!(
255+
transformation::UncorrelatedCovarianceTransformation, population
256+
)
245257
# get correlation for each pair of vectors in population
246258
cor_mat = cor(stack(population.current_generation.candidates))
247259

@@ -262,8 +274,8 @@ function update_transformation!(transformation::UncorrelatedCovarianceTransforma
262274

263275
# find points where two candidates are strongly correlated
264276

265-
@inbounds for j in axes(cor_mat,2)
266-
for i in j+1:size(cor_mat,1)
277+
@inbounds for j in axes(cor_mat, 2)
278+
for i in (j + 1):size(cor_mat, 1)
267279
abs_x = abs(cor_mat[i, j])
268280
if abs_x >= transformation.a
269281
#Base.push!(transformation.pairs, SVector(i, j))
@@ -274,7 +286,6 @@ function update_transformation!(transformation::UncorrelatedCovarianceTransforma
274286
end
275287
end
276288

277-
278289
# if we're removing all but one idx, set transformation to identity and return
279290
if length(transformation.cidxs) >= length(transformation.idxs) - 1
280291
fill_identity!(transformation.B)
@@ -288,7 +299,11 @@ function update_transformation!(transformation::UncorrelatedCovarianceTransforma
288299
setdiff!(transformation.idxs, transformation.cidxs)
289300

290301
# get number of candidates to use for covariance based on remaining candidates
291-
n = clamp(ceil(Int, transformation.ps * length(transformation.idxs)), 2, length(transformation.idxs))
302+
n = clamp(
303+
ceil(Int, transformation.ps * length(transformation.idxs)),
304+
2,
305+
length(transformation.idxs),
306+
)
292307

293308
# Get indices of n best remaining candidates
294309
idxs = view(transformation.idxs, 1:n)
@@ -321,7 +336,9 @@ function to_transformed(transformation::RotationMatrixBasedCrossoverTransformati
321336
end
322337

323338
from_transformed!(transformation::NoTransformation, mt, m) = nothing
324-
function from_transformed!(transformation::RotationMatrixBasedCrossoverTransformation, mt, m)
339+
function from_transformed!(
340+
transformation::RotationMatrixBasedCrossoverTransformation, mt, m
341+
)
325342
mul!(m, transformation.B, mt)
326343
return nothing
327344
end

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Fills the `mat` in-place with the identity matrix.
3636
function fill_identity!(mat)
3737
@inbounds for j in axes(mat, 2)
3838
for i in axes(mat, 1)
39-
mat[i,j] = ifelse(i == j, 1.0, 0.0)
39+
mat[i, j] = ifelse(i == j, 1.0, 0.0)
4040
end
4141
end
4242
return nothing

test/base.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,12 +750,11 @@ end
750750
@testset showtiming = true "Utils" begin
751751
# Test general utilities from utils.jl
752752

753-
mat = ones(3,3)
753+
mat = ones(3, 3)
754754
@test GlobalOptimization.all_correlated(mat, 0.8) == true
755-
mat[3,1] = 0.1
755+
mat[3, 1] = 0.1
756756
@test GlobalOptimization.all_correlated(mat, 0.8) == false
757757

758758
GlobalOptimization.fill_identity!(mat)
759-
@test isapprox(mat, [1. 0 0; 0 1. 0; 0 0 1.], atol=1e-8)
760-
761-
end
759+
@test isapprox(mat, [1.0 0 0; 0 1.0 0; 0 0 1.0], atol=1e-8)
760+
end

test/de.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,15 @@ end
302302
@test newm == collect(orig_c)
303303

304304
# Test UncorrelatedCovarianceTransformation constructors
305-
@test_throws ArgumentError GlobalOptimization.UncorrelatedCovarianceTransformation(0.0, 0.5, 2)
306-
@test_throws ArgumentError GlobalOptimization.UncorrelatedCovarianceTransformation(0.5, 0.0, 2)
307-
@test_throws ArgumentError GlobalOptimization.UncorrelatedCovarianceTransformation(0.5, 0.5, 2; ps=0.0)
305+
@test_throws ArgumentError GlobalOptimization.UncorrelatedCovarianceTransformation(
306+
0.0, 0.5, 2
307+
)
308+
@test_throws ArgumentError GlobalOptimization.UncorrelatedCovarianceTransformation(
309+
0.5, 0.0, 2
310+
)
311+
@test_throws ArgumentError GlobalOptimization.UncorrelatedCovarianceTransformation(
312+
0.5, 0.5, 2; ps=0.0
313+
)
308314
# Make all candidates identical so covariance is zero matrix
309315

310316
# Test UncorrelatedCovarianceTransformation initialize
@@ -338,11 +344,11 @@ end
338344
ct5 = GlobalOptimization.UncorrelatedCovarianceTransformation(1.0, 0.8, 3)
339345
GlobalOptimization.initialize!(ct5, 5)
340346
pop2 = GlobalOptimization.DEPopulation(5, 3)
341-
pop2.current_generation.candidates[1] .= SVector(0.333686, 0.22681, 0.939183)
342-
pop2.current_generation.candidates[2] .= SVector(0.877709, 0.0211374, 0.791849)
343-
pop2.current_generation.candidates[3] .= SVector(0.745745, 0.846223, 0.937728)
344-
pop2.current_generation.candidates[4] .= SVector(0.181283, 0.716657, 0.202931)
345-
pop2.current_generation.candidates[5] .= SVector(0.771286, 0.184219, 0.11133)
347+
pop2.current_generation.candidates[1] .= SVector(0.333686, 0.22681, 0.939183)
348+
pop2.current_generation.candidates[2] .= SVector(0.877709, 0.0211374, 0.791849)
349+
pop2.current_generation.candidates[3] .= SVector(0.745745, 0.846223, 0.937728)
350+
pop2.current_generation.candidates[4] .= SVector(0.181283, 0.716657, 0.202931)
351+
pop2.current_generation.candidates[5] .= SVector(0.771286, 0.184219, 0.11133)
346352
pop2.current_generation.candidates_fitness .= [1, 1, 2, 3, 4]
347353
GlobalOptimization.update_transformation!(ct5, pop2)
348354
@test isapprox(
@@ -352,7 +358,7 @@ end
352358
0.13752647954958294 0.0030534951859484526 0.99049338392028;
353359
0.9679481354178778 0.21257893123736155 0.1337408132734163
354360
];
355-
atol=1.0e-8
361+
atol=1.0e-8,
356362
)
357363

358364
# Reduce threshold for 'correlated' terms so that we are removing all but one candidate, ensure transform is identity
@@ -361,7 +367,6 @@ end
361367
GlobalOptimization.update_transformation!(ct5, pop2)
362368
@test isapprox(ct5.B, [1.0 0 0; 0 1.0 0; 0 0 1.0]; atol=1.0e-8)
363369

364-
365370
# Test to_transformed always transforms when pb=1.0
366371
orig_c = pop.current_generation.candidates[1]
367372
orig_m = pop.mutants.candidates[1]

0 commit comments

Comments
 (0)