From 9b3b7dcd5e0e71909df80397968d20d5cae5e4c3 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Sat, 3 Feb 2024 05:11:54 +1100 Subject: [PATCH 1/6] fix and test multimaterial example --- Project.toml | 4 +++- test/example.jl | 20 +++++++++++++++----- test/runtests.jl | 4 ++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 221cab7..ba750d6 100644 --- a/Project.toml +++ b/Project.toml @@ -29,12 +29,14 @@ JuliaFormatter = "1" NonconvexIpopt = "0.4" Reexport = "1" StaticArraysCore = "1" +TopOpt = "0.8" UnPack = "1" Zygote = "0.6" julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TopOpt = "53a1e1a5-51bb-58a9-8a02-02056cc81109" [targets] -test = ["Test"] +test = ["Test", "TopOpt"] diff --git a/test/example.jl b/test/example.jl index 19ed83f..4e10cd0 100644 --- a/test/example.jl +++ b/test/example.jl @@ -26,7 +26,7 @@ function uncertainComp(x, logEs) Es = exp.(logEs) # interpolation of properties between materials interp = MaterialInterpolation(Es, penalty) - MultiMaterialVariables(x, nmats) |> interp |> filter |> comp + MultiMaterialVariables(x, nmats) |> tounit |> filter |> interp |> comp # return sum(x) + sum(Es) end # wrap original function in RandomFunction struct @@ -37,19 +37,29 @@ x0 = fill(M, ncells * (length(logEs) - 1)) # (Returns propability distribution of the objective for current point) d = rf(x0) # mass constraint +constr_penalty = TopOpt.PowerPenalty(1.0) +constr_interp = MaterialInterpolation(densities, constr_penalty) constr = x -> begin - ρs = PseudoDensities(MultiMaterialVariables(x, nmats)) - return sum(element_densities(ρs, densities)) / ncells - 0.3 # unit element volume + ρs = constr_interp(MultiMaterialVariables(x, nmats)) + return sum(ρs.x) / ncells - 0.3 # elements have unit volumes end function obj(x) # objective for TO problem dist = rf(x) mean(dist)[1] + 2 * sqrt(cov(dist)[1, 1]) end obj(x0) -Zygote.gradient(obj, x0) -FiniteDifferences.grad(central_fdm(5, 1), obj, x0)[1] +g1 = Zygote.gradient(obj, x0)[1] +g2 = FiniteDifferences.grad(central_fdm(5, 1), obj, x0)[1] +norm(g1 - g2) + +cg1 = Zygote.gradient(constr, x0)[1] +cg2 = FiniteDifferences.grad(central_fdm(5, 1), constr, x0)[1] +norm(cg1 - cg2) 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, IpoptAlg(), x0; options = IpoptOptions()) +# @time r = Nonconvex.optimize(m, MMA87(), x0; options = MMAOptions()) diff --git a/test/runtests.jl b/test/runtests.jl index 305134f..a7862ea 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,3 +21,7 @@ using ReliabilityOptimization, Test, FiniteDifferences, Zygote @test norm(g1 - g2) < 1e-7 end end + +@testset "Multi-material TopOpt example" begin + include("example.jl") +end From 0ad50709d5f8b4d53171103a90dbca3ac9082bc4 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Sat, 3 Feb 2024 09:42:25 +1100 Subject: [PATCH 2/6] little type piracy --- src/ReliabilityOptimization.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ReliabilityOptimization.jl b/src/ReliabilityOptimization.jl index 4188f63..25355b5 100644 --- a/src/ReliabilityOptimization.jl +++ b/src/ReliabilityOptimization.jl @@ -111,4 +111,8 @@ function ChainRulesCore.rrule( StaticArraysCore.SVector{3}(x1, x2, x3), Δ -> (NoTangent(), Δ[1], Δ[2], Δ[3]) end +function ChainRulesCore._eltype_projectto(::Type{T}) where {T<:AbstractVector{<:Number}} + return ChainRulesCore.ProjectTo(zero(T)) +end + end From 3d2c06612a248657316400e8909c276ab27b886b Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Sat, 3 Feb 2024 10:04:00 +1100 Subject: [PATCH 3/6] remove all type piracy --- src/ReliabilityOptimization.jl | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/ReliabilityOptimization.jl b/src/ReliabilityOptimization.jl index 25355b5..0078b26 100644 --- a/src/ReliabilityOptimization.jl +++ b/src/ReliabilityOptimization.jl @@ -101,18 +101,4 @@ function (f::RandomFunction)(x) return MvNormal(muf, covf) 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} - StaticArraysCore.SVector{3}(x1, x2, x3), Δ -> (NoTangent(), Δ[1], Δ[2], Δ[3]) -end - -function ChainRulesCore._eltype_projectto(::Type{T}) where {T<:AbstractVector{<:Number}} - return ChainRulesCore.ProjectTo(zero(T)) -end - end From a0ccad5d709f94263627b51bcf729052019ce47f Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Sat, 3 Feb 2024 10:35:15 +1100 Subject: [PATCH 4/6] define ProjectTo --- src/ReliabilityOptimization.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ReliabilityOptimization.jl b/src/ReliabilityOptimization.jl index 0078b26..ce249df 100644 --- a/src/ReliabilityOptimization.jl +++ b/src/ReliabilityOptimization.jl @@ -101,4 +101,12 @@ function (f::RandomFunction)(x) return MvNormal(muf, covf) end +function ChainRulesCore.ProjectTo(x::StaticArraysCore.SVector{N, Vector{Float64}}) where {N} + return ChainRulesCore.ProjectTo{SArray}(; + element = ChainRulesCore.ProjectTo(zero(first(x))), + axes = axes(x), + size = Size(x), + ) +end + end From 8ff0449378d14a0d75ec6dd0426faa9b39b31e4a Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Sat, 3 Feb 2024 10:43:17 +1100 Subject: [PATCH 5/6] add NonconvexTOBS as a test dep --- Project.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ba750d6..9bb327f 100644 --- a/Project.toml +++ b/Project.toml @@ -27,6 +27,7 @@ FiniteDifferences = "0.12" ImplicitDifferentiation = "0.2" JuliaFormatter = "1" NonconvexIpopt = "0.4" +NonconvexTOBS = "0.2" Reexport = "1" StaticArraysCore = "1" TopOpt = "0.8" @@ -35,8 +36,9 @@ Zygote = "0.6" julia = "1" [extras] +NonconvexTOBS = "6c0b5230-d4c9-466e-bfd4-b31e6272ab65" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TopOpt = "53a1e1a5-51bb-58a9-8a02-02056cc81109" [targets] -test = ["Test", "TopOpt"] +test = ["NonconvexTOBS", "Test", "TopOpt"] From 66c9e7b5ec496b6013449705e1c2b79c9225360c Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Sat, 3 Feb 2024 10:52:27 +1100 Subject: [PATCH 6/6] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9bb327f..d29fb2a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ReliabilityOptimization" uuid = "55eddd50-7f45-4398-96cf-6a37e2b16f80" authors = ["Lucas Pereira and Mohamed Tarek"] -version = "0.2.0" +version = "0.3.0" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"