Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ksmax and friends complex-step safe #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FLOWMath"
uuid = "6cb5d3fb-0fe8-4cc2-bd89-9fe0b19a99d3"
authors = ["Andrew Ning <[email protected]>"]
version = "0.4.1"
version = "0.4.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
6 changes: 3 additions & 3 deletions src/smooth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ goes to infinity the maximum function is returned. Is mathematically guaranteed
overestimate the maximum function, i.e. `maximum(x) <= ksmax(x, hardness)`.
"""
function ksmax(x, hardness=50)
k = maximum(x)
k = maximum(real(x))
return 1.0 / hardness * log(sum(exp.(hardness * (x .- k)))) .+ k
end

Expand Down Expand Up @@ -83,7 +83,7 @@ Computes the derivative of the Kreisselmeier–Steinhauser constraint aggregatio
function with respect to `hardness`.
"""
function ksmax_h(x, hardness)
k = maximum(x)
k = maximum(real(x))
tmp1 = exp.(hardness * (x .- k))
tmp2 = sum((x .- k) .* tmp1)
tmp3 = sum(tmp1)
Expand All @@ -98,7 +98,7 @@ Computes the second derivative of the Kreisselmeier–Steinhauser constraint agg
function with respect to `hardness`.
"""
function ksmax_hh(x, hardness)
k = maximum(x)
k = maximum(real(x))
tmp1 = exp.(hardness * (x .- k))
tmp2 = sum((x .- k) .* tmp1)
tmp2_h = sum((x .- k) .^ 2 .* tmp1)
Expand Down
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ hardness = 100.0
x_max_smooth = ksmin(x, hardness)
@test isapprox(x_max_smooth, -0.006931471805599453)

# Test we can diff through ksmax and ksmin:
ksmax_wrapper(x) = sin(ksmax(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmax_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmax_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

ksmin_wrapper(x) = sin(ksmin(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmin_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmin_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

# -------------------------

# ------ ksmax_adaptive ---------
Expand Down Expand Up @@ -348,6 +361,19 @@ smoothing_fraction = 0.2
x_max_smooth = ksmin_adaptive(x, smoothing_fraction=smoothing_fraction)
@test isapprox(x_max_smooth, -5.2856933329025475e-6)

# Test we can diff through ksmax_adaptive and ksmin_adaptive:
ksmax_adaptive_wrapper(x) = sin(ksmax_adaptive(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmax_adaptive_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmax_adaptive_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

ksmin_adaptive_wrapper(x) = sin(ksmin_adaptive(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmin_adaptive_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmin_adaptive_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

# -------------------------

# ------ sigmoid ---------
Expand Down