# Defining the objective using function

**URL:** <https://discourse.julialang.org/t/defining-the-objective-using-function/27633>\
**Category:** Optimization (Mathematical)\
**Created:** [August 16, 2019, 10:28pm UTC](https://discourse.julialang.org/t/defining-the-objective-using-function/27633 "2019-08-16T22:28:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [August 16, 2019, 10:28pm UTC](https://discourse.julialang.org/t/defining-the-objective-using-function/27633/1 "2019-08-16T22:28:07Z")

</div>

Dear users,  
I need to minimize this nonlinear function: f(x1,x2) = 1 + x2^2 - x - sin(x1) with box constraints.  
How can I do this by using a function, named E-restrito, providing as argument f?  
My code is:

```julia
function E_restrito( f(x1,x2) )
    optimizer = Juniper.Optimizer
    params = Dict{Symbol,Any}()
    params[:nl_solver] = with_optimizer(Ipopt.Optimizer, print_level=0)
    modelo = Model(with_optimizer(optimizer, params))

    @variable(modelo, l[i] <= x[i in 1:2] <= u[i], start = 1)

    @objective(modelo, Min, f(x1,x2))

    optimize!(modelo)

end

```

---

<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 16, 2019, 11:19pm UTC](https://discourse.julialang.org/t/defining-the-objective-using-function/27633/2 "2019-08-16T23:19:05Z")

</div>

Hi there. Have you read [Nonlinear Modeling · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.2/nlp/)?

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [August 16, 2019, 11:21pm UTC](https://discourse.julialang.org/t/defining-the-objective-using-function/27633/3 "2019-08-16T23:21:02Z")

</div>

Yes, but no solved my problem. I want to define a given objective-function and call it by using a function.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [August 17, 2019, 7:59am UTC](https://discourse.julialang.org/t/defining-the-objective-using-function/27633/4 "2019-08-17T07:59:47Z")

</div>

You should use User defined functions as explained here:  
[http://www.juliaopt.org/JuMP.jl/v0.19.2/nlp/#User-defined-Functions-1](http://www.juliaopt.org/JuMP.jl/v0.19.2/nlp/#User-defined-Functions-1)  
The signature of `E_restrito` should be `function E_restrito(f::Function)`

---

<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 17, 2019, 2:07pm UTC](https://discourse.julialang.org/t/defining-the-objective-using-function/27633/5 "2019-08-17T14:07:38Z")

</div>

To flesh out @blegat’s suggestion:

```nohighlight
using JuMP, Ipopt

function E_restrito(f)
    model = Model(with_optimizer(Ipopt.Optimizer))
    @variable(model, x[1:2] >= 0)
    JuMP.register(model, :f, 2, f, autodiff=true)
    @NLobjective(model, Min, f(x[1], x[2]))
    optimize!(model)
    return value.(x)
end    

foo(x, y) = 1 + y^2 - x - sin(x)
E_restrito(foo)

```

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [August 17, 2019, 2:09pm UTC](https://discourse.julialang.org/t/defining-the-objective-using-function/27633/6 "2019-08-17T14:09:55Z")

</div>

Wonderfull!  
Works very well.  
Thank you so much for the complete response.
