# Automatic differentiation and \`abs()\` (or comparison operators in general) issues with JuMP/Ipopt

**URL:** https://discourse.julialang.org/t/automatic-differentiation-and-abs-or-comparison-operators-in-general-issues-with-jump-ipopt/29333
**Category:** General Usage
**Created:** [September 30, 2019, 4:40pm UTC](https://discourse.julialang.org/t/automatic-differentiation-and-abs-or-comparison-operators-in-general-issues-with-jump-ipopt/29333 "2019-09-30T16:40:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [September 30, 2019, 4:40pm UTC](https://discourse.julialang.org/t/automatic-differentiation-and-abs-or-comparison-operators-in-general-issues-with-jump-ipopt/29333/1 "2019-09-30T16:40:17Z")

</div>

I just scratched my head for a while because my super fast reconstruction algorithm suddenly took 100 times longer than usual after playing around with the quality function. Then I realised that the `abs()` function inside the function which is being minimised is causing issues. Now, the only reason I can think of is that probably some dispatch goes wrong and the automatic differentiation is causing type inference issues.

Before I start to dive in and create an MWE: is this a known issue?

Edit: ehm, actually the following workaround is almost as slow as `abs()`, so I guess the `<` operator is the problem 😕

```julia
a = a < 0 ? -a : a

```

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [September 30, 2019, 6:09pm UTC](https://discourse.julialang.org/t/automatic-differentiation-and-abs-or-comparison-operators-in-general-issues-with-jump-ipopt/29333/2 "2019-09-30T18:09:35Z")

</div>

have you tried changing `0` for `zero(T)`? maybe it’s a dispatch problem  
what’s the speed of `sqrt(a^2)` ?

---

<div class="post-metadata">

### Author: ![sebastianpech](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastianpech/32/6715_2.png) [@sebastianpech](https://discourse.julialang.org/u/sebastianpech)
#### Post date: [September 30, 2019, 7:30pm UTC](https://discourse.julialang.org/t/automatic-differentiation-and-abs-or-comparison-operators-in-general-issues-with-jump-ipopt/29333/3 "2019-09-30T19:30:04Z")

</div>

What auto diff package are you using? I recently had issues with ForwardDiff returning `Any`.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [September 30, 2019, 7:36pm UTC](https://discourse.julialang.org/t/automatic-differentiation-and-abs-or-comparison-operators-in-general-issues-with-jump-ipopt/29333/4 "2019-09-30T19:36:31Z")

</div>

@longemen3000 I’ll try with `sort`, `zero(T)` has no effect as I am using regular floats.

@sebastianpech I am using `JuMP` and no idea what’s used, I guess it’s `ForwardDiff.jl` but I have to look it up…

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [October 1, 2019, 7:13am UTC](https://discourse.julialang.org/t/automatic-differentiation-and-abs-or-comparison-operators-in-general-issues-with-jump-ipopt/29333/5 "2019-10-01T07:13:00Z")

</div>

I cannot reproduce it… This is a MNWE which should only contain the relevant parts. I did everything the same way, as far as I can tell but the following snippet takes the same time with or without the `<`-code block.

```julia
using JuMP
using Ipopt
using BenchmarkTools
using StaticArrays
using ProgressMeter

struct Bar{T} <: FieldVector{2, T}
    s::T
    t::T
end

dosomethingwithbar(bar::Bar) = bar.s + bar.t

struct FooMinimiser <: Function
    x::Vector{Float64}
end

function (f::FooMinimiser)(a)
    bar = Bar(f.x[1], f.x[2])
    z = dosomethingwithbar(bar)
    # This is the block which in my case causes a ~200x slowdown, where both `z` and `a` are `Float64`
    # if a < z
    # a .+ 0.01
    # end
    sum((f.x .- a).^2)
end

function loop(;n=10000)
    results = Vector{Float64}()
    @showprogress 1 for _ in 1:n
        x = rand(Float64, 1000)
        foo = FooMinimiser(x)
        model = Model(with_optimizer(Ipopt.Optimizer, print_level=0))
        register(model, :foo, 1, foo, autodiff=true)

        @variable(model, 0 <= a <= 1)

        @NLobjective(model, Min, foo(a))

        optimize!(model)

        push!(results, value(a))
    end
    results
end

loop()

```
