Skip to content

Commit 4e58ac4

Browse files
committed
formatting
1 parent 1350a49 commit 4e58ac4

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

src/ReliabilityOptimization.jl

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616
struct FORM{M}
1717
method::M
1818
end
19-
struct RIA{A, O}
19+
struct RIA{A,O}
2020
optim_alg::A
2121
optim_options::O
2222
end
@@ -43,12 +43,7 @@ function get_forward(f, p, method::FORM{<:RIA})
4343
n = size(p)[1]
4444
addvar!(innerOptModel, fill(-Inf, n + 1), fill(Inf, n + 1))
4545
add_eq_constraint!(innerOptModel, constr)
46-
result = optimize(
47-
innerOptModel,
48-
alg,
49-
[mean(p); 0.0],
50-
options = options,
51-
)
46+
result = optimize(innerOptModel, alg, [mean(p); 0.0], options = options)
5247
return vcat(result.minimizer, result.problem.mult_g[1])
5348
end
5449
return forward
@@ -59,11 +54,7 @@ function get_conditions(f, ::FORM{<:RIA})
5954
p = pcmult[1:end-2]
6055
c = pcmult[end-1]
6156
mult = pcmult[end]
62-
return vcat(
63-
2 * p + vec(_jacobian(f, x, p)) * mult,
64-
2c - mult,
65-
f(x, p) .- c,
66-
)
57+
return vcat(2 * p + vec(_jacobian(f, x, p)) * mult, 2c - mult, f(x, p) .- c)
6758
end
6859
end
6960

@@ -111,8 +102,13 @@ function (f::RandomFunction)(x)
111102
end
112103

113104
# necessary type piracy FiniteDifferences._estimate_magnitudes uses this constructor which Zygote struggles to differentiate on its own
114-
function ChainRulesCore.rrule(::typeof(StaticArraysCore.SVector{3}), x1::T, x2::T, x3::T) where {T}
105+
function ChainRulesCore.rrule(
106+
::typeof(StaticArraysCore.SVector{3}),
107+
x1::T,
108+
x2::T,
109+
x3::T,
110+
) where {T}
115111
StaticArraysCore.SVector{3}(x1, x2, x3), Δ -> (NoTangent(), Δ[1], Δ[2], Δ[3])
116112
end
117113

118-
end
114+
end

test/example.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ReliabilityOptimization, Test, NonconvexTOBS, ChainRulesCore, TopOpt, Zygote, FiniteDifferences
1+
using ReliabilityOptimization,
2+
Test, NonconvexTOBS, ChainRulesCore, TopOpt, Zygote, FiniteDifferences
23

34
const densities = [0.0, 0.5, 1.0] # for mass calculation
45
const nmats = 3 # number of materials
@@ -22,11 +23,11 @@ logEs = MvNormal(log.(avgEs), Matrix(Diagonal(0.1 .* abs.(log.(avgEs)))))
2223
# 'Original' function. At least one input is random.
2324
# In this example, Es is the random input.
2425
function uncertainComp(x, logEs)
25-
Es = exp.(logEs)
26-
# interpolation of properties between materials
27-
interp = MaterialInterpolation(Es, penalty)
28-
MultiMaterialVariables(x, nmats) |> interp |> filter |> comp
29-
# return sum(x) + sum(Es)
26+
Es = exp.(logEs)
27+
# interpolation of properties between materials
28+
interp = MaterialInterpolation(Es, penalty)
29+
MultiMaterialVariables(x, nmats) |> interp |> filter |> comp
30+
# return sum(x) + sum(Es)
3031
end
3132
# wrap original function in RandomFunction struct
3233
rf = RandomFunction(uncertainComp, logEs, FORM(RIA()))
@@ -41,8 +42,8 @@ constr = x -> begin
4142
return sum(element_densities(ρs, densities)) / ncells - 0.3 # unit element volume
4243
end
4344
function obj(x) # objective for TO problem
44-
dist = rf(x)
45-
mean(dist)[1] + 2 * sqrt(cov(dist)[1, 1])
45+
dist = rf(x)
46+
mean(dist)[1] + 2 * sqrt(cov(dist)[1, 1])
4647
end
4748
obj(x0)
4849
Zygote.gradient(obj, x0)
@@ -51,4 +52,4 @@ FiniteDifferences.grad(central_fdm(5, 1), obj, x0)[1]
5152
m = Model(obj) # create optimization model
5253
addvar!(m, zeros(length(x0)), ones(length(x0))) # setup optimization variables
5354
Nonconvex.add_ineq_constraint!(m, constr) # setup volume inequality constraint
54-
@time r = Nonconvex.optimize(m, TOBSAlg(), x0; options = TOBSOptions())
55+
@time r = Nonconvex.optimize(m, TOBSAlg(), x0; options = TOBSOptions())

test/runtests.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ using ReliabilityOptimization, Test, FiniteDifferences, Zygote
22

33
@testset "ReliabilityOptimization.jl" begin
44
# test function - y is random
5-
for pol in [
6-
(x, y) -> [norm(x + y)^2],
7-
(x, y) -> norm(x + y)^2
8-
]
5+
for pol in [(x, y) -> [norm(x + y)^2], (x, y) -> norm(x + y)^2]
96
# input value to be used in example
107
x = [2.0, 3.0, 6.0]
118
# wrap original function in RandomFunction struct

0 commit comments

Comments
 (0)