# ERROR: The objective function (erf) is not supported by JuMP

**URL:** https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [October 13, 2020, 2:19pm UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302 "2020-10-13T14:19:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![BuRanran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buranran/32/24283_2.png) [@BuRanran](https://discourse.julialang.org/u/BuRanran)
#### Post date: [October 13, 2020, 2:19pm UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302/1 "2020-10-13T14:19:28Z")

</div>

I have a non-linear optimization problem to be solved with JuMP and IPOPT solver. The objective function is a probability distribution function of a normal distribution. It can be optimized by IPOPT directly, but not supported by JuMP. Is there any method to solve this problem? As the constrains are complicated, I hope to write constrains with JuMP. Thanks a lot !  
The codes with error are as follows.

```julia
func1(x)=1/(2*3.1415926)^0.5/20*exp(-(x-100)^2/2/20^2)
gp = Model(with_optimizer(Ipopt.Optimizer))
@variable(gp, Q)
@objective(gp, Max, integrate(func1)(Q))
optimize!(gp)

```

ERROR: LoadError: The objective function `0.199471141902022*sqrt(2)*sqrt(pi)*erf(sqrt(2)*(x - 100)/40)` is not supported by JuMP.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 13, 2020, 9:59pm UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302/2 "2020-10-13T21:59:10Z")

</div>

Here are the docs: [https://jump.dev/JuMP.jl/stable/nlp/](https://jump.dev/JuMP.jl/stable/nlp/)  
You need to use `@NLobjective` and `@NLconstraint`.

You will also need to register the `integrate` function as a user-defined function, so something like:

```nohighlight
using JuMP
using Ipopt

gp = Model(Ipopt.Optimizer)
@variable(gp, Q)
func1 = integrate(1/(2*3.1415926)^0.5/20*exp(-(x-100)^2/2/20^2))
register(gp, :func1, 1, func1; autodiff=true)
@NLobjective(gp, Max, func1(Q))
optimize!(gp)

```

Note that I haven’t tested this because I don’t know where `integrate` comes from.

---

<div class="post-metadata">

### Author: ![BuRanran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buranran/32/24283_2.png) [@BuRanran](https://discourse.julialang.org/u/BuRanran)
#### Post date: [October 14, 2020, 2:43am UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302/3 "2020-10-14T02:43:34Z")

</div>

Thanks for your reply. I correct the @objective to @NLobjective, it really helps. But there are still errors.  
Ps. The `integrate()` is a integral function in package “SymPy”

```julia
using JuMP
using Ipopt
using SymPy

gp = Model(Ipopt.Optimizer)
@variable(gp, Q)
func1(x)=1/(2*3.1415926)^0.5/20*exp(-(x-100)^2/2/20^2)
func2(y)=integrate(func1, -Inf, y)
register(gp, :func2, 1, func2; autodiff=true)
@NLobjective(gp, Max, func2(Q))
optimize!(gp)

```

ERROR: LoadError: Expected return type of Float64 from a user-defined function, but got Sym.

That the type of `func2(constant)` is `Sym`, for example:

```julia
julia> func2(100)
0.199471141902022⋅√2⋅√π

julia> typeof(func2(100))
Sym

```

I want to transfer `Sym` to `float` by using function `float(func2(Q))`, but function `float()` can not be used in the objective. How can I fix this problem?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 14, 2020, 5:32am UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302/5 "2020-10-14T05:32:49Z")

</div>

You want something like

```nohighlight
using JuMP
using Ipopt
using SymPy

func1(x)=1/(2*3.1415926)^0.5/20*exp(-(x-100)^2/2/20^2)
func2 = lambdify(integrate(func1))
func3(y) = convert(Real, func2(y))

gp = Model(Ipopt.Optimizer)
@variable(gp, Q)
register(gp, :func3, 1, func3; autodiff=true)
@NLobjective(gp, Max, func3(Q))
optimize!(gp)

```

---

<div class="post-metadata">

### Author: ![BuRanran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buranran/32/24283_2.png) [@BuRanran](https://discourse.julialang.org/u/BuRanran)
#### Post date: [October 15, 2020, 7:01am UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302/6 "2020-10-15T07:01:45Z")

</div>

It works. Thanks so much!

---

<div class="post-metadata">

### Author: ![anand.kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anand.kumar/32/206982_2.png) [@anand.kumar](https://discourse.julialang.org/u/anand.kumar)
#### Post date: [August 13, 2024, 12:45pm UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302/7 "2024-08-13T12:45:39Z")

</div>

Have something similar and was trying to replicate this. However, it seems syntax has updated for SymPy. Got it working, but `convert` doesn’t seem to work.

 ![Screenshot 2024-08-13 at 6.16.30 PM](https://global.discourse-cdn.com/julialang/original/3X/2/9/2907063778b8e07da89151dec16e743ca02bf688.png)

update: realised func(z) returns float64. Thus, I may not need convert (maybe?). However, adding this in objective function yields back `erf` in the expression and I’m back to square -

 ![Screenshot 2024-08-13 at 6.29.18 PM](https://global.discourse-cdn.com/julialang/original/3X/1/e/1e2b1ea3f99bd0e1d118a59b6be114a81dd9f1fa.png)

In my case, objective function involves `f(u,w) + g(u)*erf(u/w)`. Since, `erf` isn’t defined in Julia natively, I’m hoping, resorting to your approach could work for my case too. I’m using standard solver e.g. Gurobi/Knitro. Any other insights would be helpful.

Thank You.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [August 13, 2024, 10:17pm UTC](https://discourse.julialang.org/t/error-the-objective-function-erf-is-not-supported-by-jump/48302/8 "2024-08-13T22:17:46Z")

</div>

Hi @anand.kumar,

`erf` is supported as part of SpecialFunctions.jl

See the JuMP documentation section: [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nonlinear/#Supported-operators)

If you’re still stuck, please start a new thread and we can discusss there.
