Skip to content

ForwardDiff with min, max, and clamp #640

Open
@smkatz12

Description

@smkatz12

Hi! We are working on a textbook chapter on reachability analysis and are interested in support for intervals when using ForwardDiff.jl to compute gradients and hessians of functions that use the min, max, and clamp. The current behavior with these functions does not cause an error but is incorrect. We have written the functions below to correct for this. These functions could be added to IntervalArithmeticForwardDiffExt.jl.

function Base.max(x::Dual{T,V,N}, y::AbstractFloat) where {T,V<:Interval,N}
    if value(x).hi < y
        return Dual{T,V,N}(y..y, (0..0) * partials(x))
    elseif value(x).lo > y
        return Dual{T,V,N}(value(x), (1..1) * partials(x))
    else
        return Dual{T,V,N}(y..value(x).hi, (0..1) * partials(x))
    end
end

function Base.max(x::Dual{T,Dual{T2,V2,N2},N}, y::AbstractFloat) where {T,T2,V2<:Interval,N2,N}
    if value(value(x)).hi < y
        return Dual{T,Dual{T2,V2,N2},N}(Dual{T2,V2,N2}(y..y), (0..0) * partials(x))
    elseif value(value(x)).lo > y
        return Dual{T,Dual{T2,V2,N2},N}(value(x), (1..1) * partials(x))
    else
        return Dual{T,Dual{T2,V2,N2},N}(Dual{T2,V2,N2}(y..value(value(x)).hi, partials(value(x))), (0..1) * partials(x))
    end
end

function Base.min(x::Dual{T,V,N}, y::AbstractFloat) where {T,V<:Interval,N}
    if value(x).lo > y
        return Dual{T,V,N}(y..y, (0..0) * partials(x))
    elseif value(x).hi < y
        return Dual{T,V,N}(value(x), (1..1) * partials(x))
    else
        return Dual{T,V,N}(value(x).lo..y, (0..1) * partials(x))
    end
end

function Base.min(x::Dual{T,Dual{T2,V2,N2},N}, y::AbstractFloat) where {T,T2,V2<:Interval,N2,N}
    if value(value(x)).lo > y
        return Dual{T,Dual{T2,V2,N2},N}(Dual{T2,V2,N2}(y..y), (0..0) * partials(x))
    elseif value(value(x)).hi < y
        return Dual{T,Dual{T2,V2,N2},N}(value(x), (1..1) * partials(x))
    else
        return Dual{T,Dual{T2,V2,N2},N}(Dual{T2,V2,N2}(value(value(x)).lo..y, partials(value(x))), (0..1) * partials(x))
    end
end

function Base.clamp(i::Dual{T,V,N}, lo::AbstractFloat, hi::AbstractFloat) where {T,V<:Interval,N}
    return min(max(i, lo), hi)
end

function Base.clamp(i::Dual{T,Dual{T2,V2,N2},N}, lo::AbstractFloat, hi::AbstractFloat) where {T,T2,V2<:Interval,N2,N}
    return min(max(i, lo), hi)
end

Example:

function f(x)
    return x[1]^2 + clamp(x[2], 1.5, 2.5)^3
end

ForwardDiff.hessian(f, [2..3, 1..2])

Current output:

2×2 Matrix{Interval{Float64}}:
 [2, 2]   [0, 0]
 [0, 0]  [6, 12]

Correct output using code above:

2×2 Matrix{Interval{Float64}}:
 [2, 2]   [0, 0]
 [0, 0]  [0, 12]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions