Skip to content

Commit

Permalink
Merge pull request #19 from JuliaNonconvex/mt/fix_multimaterial
Browse files Browse the repository at this point in the history
fix and test multi-material example
  • Loading branch information
LucasMSpereira authored Feb 3, 2024
2 parents 4e58ac4 + 66c9e7b commit b386c71
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -27,14 +27,18 @@ FiniteDifferences = "0.12"
ImplicitDifferentiation = "0.2"
JuliaFormatter = "1"
NonconvexIpopt = "0.4"
NonconvexTOBS = "0.2"
Reexport = "1"
StaticArraysCore = "1"
TopOpt = "0.8"
UnPack = "1"
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"]
test = ["NonconvexTOBS", "Test", "TopOpt"]
14 changes: 6 additions & 8 deletions src/ReliabilityOptimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ 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])
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
20 changes: 15 additions & 5 deletions test/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit b386c71

@mohamed82008
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/100180

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" b386c71af552db2ef74db45c85ffb6c1d5113a92
git push origin v0.3.0

Please sign in to comment.