Skip to content

Commit d34d3e9

Browse files
Apply Runic formatting to benchmark/ and examples/
The fredrikekre/runic-action checks the whole repo, not just src/test/, so the previous formatting commit missed benchmark/benchmarks.jl and the example scripts. Format them in-place to make FormatCheck.yml pass cleanly. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
1 parent 6bb6b65 commit d34d3e9

4 files changed

Lines changed: 66 additions & 65 deletions

File tree

benchmark/benchmarks.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Random.seed!(2);
66
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
77

88
N = 3
9-
cmaes = CMAES(;mu=20, lambda=100)
10-
ga = GA(populationSize=100, ɛ=0.1, selection=rouletteinv, crossover=intermediate(0.25), mutation=domainrange(fill(0.5,N)))
11-
es = ES(initStrategy=IsotropicStrategy(N), recombination=average, srecombination=average, mutation=gaussian, smutation=gaussian, μ=10, ρ=3, λ=100, selection=:plus)
9+
cmaes = CMAES(; mu = 20, lambda = 100)
10+
ga = GA(populationSize = 100, ɛ = 0.1, selection = rouletteinv, crossover = intermediate(0.25), mutation = domainrange(fill(0.5, N)))
11+
es = ES(initStrategy = IsotropicStrategy(N), recombination = average, srecombination = average, mutation = gaussian, smutation = gaussian, μ = 10, ρ = 3, λ = 100, selection = :plus)
1212
de = DE(populationSize = 100)
1313

1414
suite = BenchmarkGroup()

examples/120bits.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using Evolutionary, Test, Random
1717
function fitness(oset, bsize = 6)
1818
scores = Dict(i => (i != bsize ? 3i : 20) for i in 0:bsize)
1919
# count 1s in blocks
20-
ht = Dict{Int,Int}()
20+
ht = Dict{Int, Int}()
2121
sl = sr = 0
2222
for i in oset
2323
if i > 60
@@ -43,34 +43,35 @@ function fitness(oset, bsize = 6)
4343
error("Problem")
4444
end
4545
ss += scores[cnt]
46-
if (cnt == 1) && ((blk+1)*bsize in oset)
46+
if (cnt == 1) && ((blk + 1) * bsize in oset)
4747
ss += 9
4848
end
4949
#println("$blk, $cnt, $(scores[cnt]), $ss")
5050
end
5151
ss -= 2(sl > 13 ? sl : 0)
5252
ss -= 2(sr > 13 ? sr : 0)
53-
ss
53+
return ss
5454
end
5555

56-
# Bit string examples
56+
# Bit string examples
5757
bstr = "000100 001010 000010 111111 000100 101000 000010 000000 000001 000000 000010 000001 001000 000000 000000 010000 000000 000010 000000 000000"
58-
idxs = [i for (i,j) in enumerate(filter(c->c != ' ', bstr)) if j == '1']
58+
idxs = [i for (i, j) in enumerate(filter(c -> c != ' ', bstr)) if j == '1']
5959
@test fitness(idxs) == 50
6060

6161
# optimal solution
62-
idxs_opt = [6*i for i in 1:20]
62+
idxs_opt = [6 * i for i in 1:20]
6363
@test fitness(idxs_opt) == 240
6464

6565
# GA solution
66-
mthd = GA(populationSize = 100, selection = tournament(5),
67-
crossover = SSX, crossoverRate = 0.99,
68-
mutation = replace(collect(1:120)), mutationRate = 0.1)
66+
mthd = GA(
67+
populationSize = 100, selection = tournament(5),
68+
crossover = SSX, crossoverRate = 0.99,
69+
mutation = replace(collect(1:120)), mutationRate = 0.1
70+
)
6971

7072
Random.seed!(42)
71-
opts = Evolutionary.Options(show_trace=false, successive_f_tol=30)
72-
init_pop = ()->randperm(120)[1:20]
73-
res = Evolutionary.optimize(idxs->-fitness(idxs), init_pop, mthd, opts)
73+
opts = Evolutionary.Options(show_trace = false, successive_f_tol = 30)
74+
init_pop = () -> randperm(120)[1:20]
75+
res = Evolutionary.optimize(idxs -> -fitness(idxs), init_pop, mthd, opts)
7476
println(res)
7577
Evolutionary.minimizer(res) |> sort
76-

examples/beam.jl

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@
33

44
using Evolutionary, Test, Random
55

6-
beam(x) = 1.10471*x[2]*x[1]^2+0.04811*x[3]*x[4]*(14+x[2])
6+
beam(x) = 1.10471 * x[2] * x[1]^2 + 0.04811 * x[3] * x[4] * (14 + x[2])
77
lx = [0.125, 0.1, 0.1, 0.125]
88
ux = [5.0, 10.0, 10.0, 5.0]
99

10-
P = 6000; L = 14; E = 30e+6; G = 12e+6;
10+
P = 6000; L = 14; E = 30.0e+6; G = 12.0e+6;
1111
τₘₐₓ = 13600; σₘₐₓ = 30000; δₘₐₓ = 0.25;
12-
M(x) = P*(L+x[2]/2)
13-
R(x) = sqrt(0.25*(x[2]^2+(x[1]+x[3])^2))
14-
J(x) = 2/sqrt(2)*x[1]*x[2]*(x[2]^2/12+0.25*(x[1]+x[3])^2)
15-
P_c(x) = (4.013*E/(6*L^2))*x[3]*x[4]^3*(1-0.25*x[3]*sqrt(E/G)/L)
16-
τ₁(x) = P/(sqrt(2)*x[1]*x[2])
17-
τ₂(x) = M(x)*R(x)/J(x)
18-
τ(x) = sqrt(τ₁(x)^2+τ₁(x)*τ₂(x)*x[2]/R(x)+τ₂(x)^2)
19-
σ(x) = 6*P*L/(x[4]*x[3]^2)
20-
δ(x) = 4*P*L^3/(E*x[4]*x[3]^3)
21-
22-
con_c1(x) = [τ(x), σ(x), x[4]-x[1], 0.10471*x[1]^2+0.04811*x[3]*x[4]*(14.0+x[2]), P_c(x), δ(x)]
23-
lc = [-Inf, -Inf, 0.0, -Inf, P, -Inf]
24-
uc = [τₘₐₓ, σₘₐₓ, Inf, 5.0, Inf, δₘₐₓ]
25-
26-
con_c2(x) = [τ(x)-τₘₐₓ, σ(x)-σₘₐₓ, x[1]-x[4], 0.10471*x[1]^2+0.04811*x[3]*x[4]*(14.0+x[2])-5.0, P-P_c(x), δ(x)-δₘₐₓ]
12+
M(x) = P * (L + x[2] / 2)
13+
R(x) = sqrt(0.25 * (x[2]^2 + (x[1] + x[3])^2))
14+
J(x) = 2 / sqrt(2) * x[1] * x[2] * (x[2]^2 / 12 + 0.25 * (x[1] + x[3])^2)
15+
P_c(x) = (4.013 * E / (6 * L^2)) * x[3] * x[4]^3 * (1 - 0.25 * x[3] * sqrt(E / G) / L)
16+
τ₁(x) = P / (sqrt(2) * x[1] * x[2])
17+
τ₂(x) = M(x) * R(x) / J(x)
18+
τ(x) = sqrt(τ₁(x)^2 + τ₁(x) * τ₂(x) * x[2] / R(x) + τ₂(x)^2)
19+
σ(x) = 6 * P * L / (x[4] * x[3]^2)
20+
δ(x) = 4 * P * L^3 / (E * x[4] * x[3]^3)
21+
22+
con_c1(x) = [τ(x), σ(x), x[4] - x[1], 0.10471 * x[1]^2 + 0.04811 * x[3] * x[4] * (14.0 + x[2]), P_c(x), δ(x)]
23+
lc = [-Inf, -Inf, 0.0, -Inf, P, -Inf]
24+
uc = [τₘₐₓ, σₘₐₓ, Inf, 5.0, Inf, δₘₐₓ]
25+
26+
con_c2(x) = [τ(x) - τₘₐₓ, σ(x) - σₘₐₓ, x[1] - x[4], 0.10471 * x[1]^2 + 0.04811 * x[3] * x[4] * (14.0 + x[2]) - 5.0, P - P_c(x), δ(x) - δₘₐₓ]
2727
lc = fill(-Inf, 6)
2828
uc = fill(0.0, 6)
2929

30-
opts = Evolutionary.Options(iterations=2000, successive_f_tol=40)
31-
mthd = CMAES(mu = 90, c_mu=0.3)
32-
mthd = GA(populationSize = 120, ɛ = 0.03, crossoverRate=0.8, mutationRate=0.01, selection=rouletteinv, crossover=LX(0.0,4.0), mutation = PM(lx,ux,1.0))
33-
mthd = DE(n=2)
30+
opts = Evolutionary.Options(iterations = 2000, successive_f_tol = 40)
31+
mthd = CMAES(mu = 90, c_mu = 0.3)
32+
mthd = GA(populationSize = 120, ɛ = 0.03, crossoverRate = 0.8, mutationRate = 0.01, selection = rouletteinv, crossover = LX(0.0, 4.0), mutation = PM(lx, ux, 1.0))
33+
mthd = DE(n = 2)
3434

35-
c = PenaltyConstraints([1e3, 1e3, 1e3, 1e3, 1e2, 1e1, 1e3, 1e1, 1e1, 1e0], lx, ux, lc, uc, con_c1)
36-
c = PenaltyConstraints([1e3, 1e3, 1e3, 1e3, 1e1, 1e1, 1e3, 1e1, 1e1, 1e0], Evolutionary.ConstraintBounds(lx,ux,lc,[]), con_c2)
37-
c = WorstFitnessConstraints(Evolutionary.ConstraintBounds(lx,ux,lc,uc), con_c1)
38-
c = WorstFitnessConstraints(Evolutionary.ConstraintBounds(lx,ux,lc,uc), con_c2)
35+
c = PenaltyConstraints([1.0e3, 1.0e3, 1.0e3, 1.0e3, 1.0e2, 1.0e1, 1.0e3, 1.0e1, 1.0e1, 1.0e0], lx, ux, lc, uc, con_c1)
36+
c = PenaltyConstraints([1.0e3, 1.0e3, 1.0e3, 1.0e3, 1.0e1, 1.0e1, 1.0e3, 1.0e1, 1.0e1, 1.0e0], Evolutionary.ConstraintBounds(lx, ux, lc, []), con_c2)
37+
c = WorstFitnessConstraints(Evolutionary.ConstraintBounds(lx, ux, lc, uc), con_c1)
38+
c = WorstFitnessConstraints(Evolutionary.ConstraintBounds(lx, ux, lc, uc), con_c2)
3939

4040
Random.seed!(0);
4141
result = Evolutionary.optimize(beam, c, mthd, opts)

examples/mixedint.jl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,45 @@
55

66
using Evolutionary, Test, Random
77

8-
f1(x) = 2*x[1]+x[2]
8+
f1(x) = 2 * x[1] + x[2]
99
lx = [0.0, 0]
1010
ux = [1.6, 1]
1111

12-
cons(x) = [1.25-x[1]^2-x[2], x[1]+x[2]]
12+
cons(x) = [1.25 - x[1]^2 - x[2], x[1] + x[2]]
1313
lc = [-Inf, -Inf]
1414
uc = [0.0, 1.6]
1515

1616
tc = [Float64, Int]
1717

18-
cb = Evolutionary.ConstraintBounds(lx,ux,lc,uc)
19-
c = MixedTypePenaltyConstraints(PenaltyConstraints(1e3, cb, cons), tc)
18+
cb = Evolutionary.ConstraintBounds(lx, ux, lc, uc)
19+
c = MixedTypePenaltyConstraints(PenaltyConstraints(1.0e3, cb, cons), tc)
2020
c = MixedTypePenaltyConstraints(WorstFitnessConstraints(cb, cons), tc)
21-
init = ()->Real[rand(Float64), rand(0:1)]
21+
init = () -> Real[rand(Float64), rand(0:1)]
2222

23-
opts = Evolutionary.Options(iterations=500, abstol=1e-5)
24-
mthd = GA(populationSize=40, crossoverRate=0.8, mutationRate=0.05, selection=susinv, crossover=MILX(), mutation=MIPM(lx,ux))
23+
opts = Evolutionary.Options(iterations = 500, abstol = 1.0e-5)
24+
mthd = GA(populationSize = 40, crossoverRate = 0.8, mutationRate = 0.05, selection = susinv, crossover = MILX(), mutation = MIPM(lx, ux))
2525

2626
Random.seed!(10);
2727
result = Evolutionary.optimize(f1, c, init, mthd, opts)
28-
@test minimum(result) 2.0 atol=1e-1
29-
@test Evolutionary.minimizer(result) [0.5, 1] atol=1e-1
28+
@test minimum(result) 2.0 atol = 1.0e-1
29+
@test Evolutionary.minimizer(result) [0.5, 1] atol = 1.0e-1
3030

3131
#-----------------------------------------------------------------
3232

33-
f8(z) = sum((z[4:end-1].-1).^2) - log(z[end]+1) + sum((z[i]-i)^2 for i in 1:3)
33+
f8(z) = sum((z[4:(end - 1)] .- 1) .^ 2) - log(z[end] + 1) + sum((z[i] - i)^2 for i in 1:3)
3434

35-
@test f8(Real[0.2, 1.280624, 1.954483, 1, 0, 0, 1]) 3.55746 atol=1e-6
35+
@test f8(Real[0.2, 1.280624, 1.954483, 1, 0, 0, 1]) 3.55746 atol = 1.0e-6
3636

3737
cons(z) = [
38-
sum(z[1:end-1]),
39-
sum(v->v^2, z[[1,2,3,6]]),
40-
z[1]+z[4],
41-
z[2]+z[5],
42-
z[3]+z[6],
43-
z[1]+z[7],
44-
z[2]^2+z[5]^2,
45-
z[3]^2+z[6]^2,
46-
z[2]^2+z[6]^2,
38+
sum(z[1:(end - 1)]),
39+
sum(v -> v^2, z[[1, 2, 3, 6]]),
40+
z[1] + z[4],
41+
z[2] + z[5],
42+
z[3] + z[6],
43+
z[1] + z[7],
44+
z[2]^2 + z[5]^2,
45+
z[3]^2 + z[6]^2,
46+
z[2]^2 + z[6]^2,
4747
]
4848

4949
lx = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
@@ -54,13 +54,13 @@ uc = [5.0, 5.5, 1.2, 1.8, 2.5, 1.2, 1.64, 4.25, 4.64]
5454

5555
tc = [Float64, Float64, Float64, Int, Int, Int, Int]
5656

57-
cb = Evolutionary.ConstraintBounds(lx,ux,lc,uc)
57+
cb = Evolutionary.ConstraintBounds(lx, ux, lc, uc)
5858
# c = MixedTypePenaltyConstraints(PenaltyConstraints(1e2, cb, cons), tc)
5959
c = MixedTypePenaltyConstraints(WorstFitnessConstraints(cb, cons), tc)
60-
init = ()->Real[rand(Float64,3); rand(0:1,4)]
60+
init = () -> Real[rand(Float64, 3); rand(0:1, 4)]
6161

62-
opts = Evolutionary.Options(iterations=2000)
63-
mthd = GA(populationSize=300, ɛ=0.05, crossoverRate=0.8, mutationRate=0.01, selection=susinv, crossover=MILX(0.0,0.5,0.3), mutation=MIPM(lx,ux))
62+
opts = Evolutionary.Options(iterations = 2000)
63+
mthd = GA(populationSize = 300, ɛ = 0.05, crossoverRate = 0.8, mutationRate = 0.01, selection = susinv, crossover = MILX(0.0, 0.5, 0.3), mutation = MIPM(lx, ux))
6464

6565
Random.seed!(544);
6666
result = Evolutionary.optimize(f8, c, init, mthd, opts)

0 commit comments

Comments
 (0)