diff --git a/src/ReliabilityOptimization.jl b/src/ReliabilityOptimization.jl index 336b472..4188f63 100644 --- a/src/ReliabilityOptimization.jl +++ b/src/ReliabilityOptimization.jl @@ -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 @@ -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 @@ -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 @@ -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 \ No newline at end of file +end diff --git a/test/example.jl b/test/example.jl index 14442ed..19ed83f 100644 --- a/test/example.jl +++ b/test/example.jl @@ -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 @@ -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())) @@ -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) @@ -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()) diff --git a/test/runtests.jl b/test/runtests.jl index d218be9..305134f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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