Skip to content

Commit b386c71

Browse files
Merge pull request #19 from JuliaNonconvex/mt/fix_multimaterial
fix and test multi-material example
2 parents 4e58ac4 + 66c9e7b commit b386c71

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

Project.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ReliabilityOptimization"
22
uuid = "55eddd50-7f45-4398-96cf-6a37e2b16f80"
33
authors = ["Lucas Pereira and Mohamed Tarek"]
4-
version = "0.2.0"
4+
version = "0.3.0"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
@@ -27,14 +27,18 @@ FiniteDifferences = "0.12"
2727
ImplicitDifferentiation = "0.2"
2828
JuliaFormatter = "1"
2929
NonconvexIpopt = "0.4"
30+
NonconvexTOBS = "0.2"
3031
Reexport = "1"
3132
StaticArraysCore = "1"
33+
TopOpt = "0.8"
3234
UnPack = "1"
3335
Zygote = "0.6"
3436
julia = "1"
3537

3638
[extras]
39+
NonconvexTOBS = "6c0b5230-d4c9-466e-bfd4-b31e6272ab65"
3740
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
41+
TopOpt = "53a1e1a5-51bb-58a9-8a02-02056cc81109"
3842

3943
[targets]
40-
test = ["Test"]
44+
test = ["NonconvexTOBS", "Test", "TopOpt"]

src/ReliabilityOptimization.jl

+6-8
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,12 @@ function (f::RandomFunction)(x)
101101
return MvNormal(muf, covf)
102102
end
103103

104-
# necessary type piracy FiniteDifferences._estimate_magnitudes uses this constructor which Zygote struggles to differentiate on its own
105-
function ChainRulesCore.rrule(
106-
::typeof(StaticArraysCore.SVector{3}),
107-
x1::T,
108-
x2::T,
109-
x3::T,
110-
) where {T}
111-
StaticArraysCore.SVector{3}(x1, x2, x3), Δ -> (NoTangent(), Δ[1], Δ[2], Δ[3])
104+
function ChainRulesCore.ProjectTo(x::StaticArraysCore.SVector{N, Vector{Float64}}) where {N}
105+
return ChainRulesCore.ProjectTo{SArray}(;
106+
element = ChainRulesCore.ProjectTo(zero(first(x))),
107+
axes = axes(x),
108+
size = Size(x),
109+
)
112110
end
113111

114112
end

test/example.jl

+15-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function uncertainComp(x, logEs)
2626
Es = exp.(logEs)
2727
# interpolation of properties between materials
2828
interp = MaterialInterpolation(Es, penalty)
29-
MultiMaterialVariables(x, nmats) |> interp |> filter |> comp
29+
MultiMaterialVariables(x, nmats) |> tounit |> filter |> interp |> comp
3030
# return sum(x) + sum(Es)
3131
end
3232
# wrap original function in RandomFunction struct
@@ -37,19 +37,29 @@ x0 = fill(M, ncells * (length(logEs) - 1))
3737
# (Returns propability distribution of the objective for current point)
3838
d = rf(x0)
3939
# mass constraint
40+
constr_penalty = TopOpt.PowerPenalty(1.0)
41+
constr_interp = MaterialInterpolation(densities, constr_penalty)
4042
constr = x -> begin
41-
ρs = PseudoDensities(MultiMaterialVariables(x, nmats))
42-
return sum(element_densities(ρs, densities)) / ncells - 0.3 # unit element volume
43+
ρs = constr_interp(MultiMaterialVariables(x, nmats))
44+
return sum(ρs.x) / ncells - 0.3 # elements have unit volumes
4345
end
4446
function obj(x) # objective for TO problem
4547
dist = rf(x)
4648
mean(dist)[1] + 2 * sqrt(cov(dist)[1, 1])
4749
end
4850
obj(x0)
49-
Zygote.gradient(obj, x0)
50-
FiniteDifferences.grad(central_fdm(5, 1), obj, x0)[1]
51+
g1 = Zygote.gradient(obj, x0)[1]
52+
g2 = FiniteDifferences.grad(central_fdm(5, 1), obj, x0)[1]
53+
norm(g1 - g2)
54+
55+
cg1 = Zygote.gradient(constr, x0)[1]
56+
cg2 = FiniteDifferences.grad(central_fdm(5, 1), constr, x0)[1]
57+
norm(cg1 - cg2)
5158

5259
m = Model(obj) # create optimization model
5360
addvar!(m, zeros(length(x0)), ones(length(x0))) # setup optimization variables
5461
Nonconvex.add_ineq_constraint!(m, constr) # setup volume inequality constraint
62+
5563
@time r = Nonconvex.optimize(m, TOBSAlg(), x0; options = TOBSOptions())
64+
# @time r = Nonconvex.optimize(m, IpoptAlg(), x0; options = IpoptOptions())
65+
# @time r = Nonconvex.optimize(m, MMA87(), x0; options = MMAOptions())

test/runtests.jl

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ using ReliabilityOptimization, Test, FiniteDifferences, Zygote
2121
@test norm(g1 - g2) < 1e-7
2222
end
2323
end
24+
25+
@testset "Multi-material TopOpt example" begin
26+
include("example.jl")
27+
end

0 commit comments

Comments
 (0)