Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed82008 committed Nov 16, 2023
1 parent 1350a49 commit 4e58ac4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
24 changes: 10 additions & 14 deletions src/ReliabilityOptimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end
struct FORM{M}
method::M
end
struct RIA{A, O}
struct RIA{A,O}
optim_alg::A
optim_options::O
end
Expand All @@ -43,12 +43,7 @@ function get_forward(f, p, method::FORM{<:RIA})
n = size(p)[1]
addvar!(innerOptModel, fill(-Inf, n + 1), fill(Inf, n + 1))
add_eq_constraint!(innerOptModel, constr)
result = optimize(
innerOptModel,
alg,
[mean(p); 0.0],
options = options,
)
result = optimize(innerOptModel, alg, [mean(p); 0.0], options = options)
return vcat(result.minimizer, result.problem.mult_g[1])
end
return forward
Expand All @@ -59,11 +54,7 @@ function get_conditions(f, ::FORM{<:RIA})
p = pcmult[1:end-2]
c = pcmult[end-1]
mult = pcmult[end]
return vcat(
2 * p + vec(_jacobian(f, x, p)) * mult,
2c - mult,
f(x, p) .- c,
)
return vcat(2 * p + vec(_jacobian(f, x, p)) * mult, 2c - mult, f(x, p) .- c)
end
end

Expand Down Expand Up @@ -111,8 +102,13 @@ function (f::RandomFunction)(x)
end

# necessary type piracy FiniteDifferences._estimate_magnitudes uses this constructor which Zygote struggles to differentiate on its own
function ChainRulesCore.rrule(::typeof(StaticArraysCore.SVector{3}), x1::T, x2::T, x3::T) where {T}
function ChainRulesCore.rrule(
::typeof(StaticArraysCore.SVector{3}),
x1::T,
x2::T,
x3::T,
) where {T}
StaticArraysCore.SVector{3}(x1, x2, x3), Δ -> (NoTangent(), Δ[1], Δ[2], Δ[3])
end

end
end
19 changes: 10 additions & 9 deletions test/example.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ReliabilityOptimization, Test, NonconvexTOBS, ChainRulesCore, TopOpt, Zygote, FiniteDifferences
using ReliabilityOptimization,
Test, NonconvexTOBS, ChainRulesCore, TopOpt, Zygote, FiniteDifferences

const densities = [0.0, 0.5, 1.0] # for mass calculation
const nmats = 3 # number of materials
Expand All @@ -22,11 +23,11 @@ logEs = MvNormal(log.(avgEs), Matrix(Diagonal(0.1 .* abs.(log.(avgEs)))))
# 'Original' function. At least one input is random.
# In this example, Es is the random input.
function uncertainComp(x, logEs)
Es = exp.(logEs)
# interpolation of properties between materials
interp = MaterialInterpolation(Es, penalty)
MultiMaterialVariables(x, nmats) |> interp |> filter |> comp
# return sum(x) + sum(Es)
Es = exp.(logEs)
# interpolation of properties between materials
interp = MaterialInterpolation(Es, penalty)
MultiMaterialVariables(x, nmats) |> interp |> filter |> comp
# return sum(x) + sum(Es)
end
# wrap original function in RandomFunction struct
rf = RandomFunction(uncertainComp, logEs, FORM(RIA()))
Expand All @@ -41,8 +42,8 @@ constr = x -> begin
return sum(element_densities(ρs, densities)) / ncells - 0.3 # unit element volume
end
function obj(x) # objective for TO problem
dist = rf(x)
mean(dist)[1] + 2 * sqrt(cov(dist)[1, 1])
dist = rf(x)
mean(dist)[1] + 2 * sqrt(cov(dist)[1, 1])
end
obj(x0)
Zygote.gradient(obj, x0)
Expand All @@ -51,4 +52,4 @@ FiniteDifferences.grad(central_fdm(5, 1), obj, x0)[1]
m = Model(obj) # create optimization model
addvar!(m, zeros(length(x0)), ones(length(x0))) # setup optimization variables
Nonconvex.add_ineq_constraint!(m, constr) # setup volume inequality constraint
@time r = Nonconvex.optimize(m, TOBSAlg(), x0; options = TOBSOptions())
@time r = Nonconvex.optimize(m, TOBSAlg(), x0; options = TOBSOptions())
5 changes: 1 addition & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ using ReliabilityOptimization, Test, FiniteDifferences, Zygote

@testset "ReliabilityOptimization.jl" begin
# test function - y is random
for pol in [
(x, y) -> [norm(x + y)^2],
(x, y) -> norm(x + y)^2
]
for pol in [(x, y) -> [norm(x + y)^2], (x, y) -> norm(x + y)^2]
# input value to be used in example
x = [2.0, 3.0, 6.0]
# wrap original function in RandomFunction struct
Expand Down

0 comments on commit 4e58ac4

Please sign in to comment.