# MINLP optimization with user defined objective function

**URL:** <https://discourse.julialang.org/t/minlp-optimization-with-user-defined-objective-function/33627>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, first-steps\
**Created:** [January 21, 2020, 4:06pm UTC](https://discourse.julialang.org/t/minlp-optimization-with-user-defined-objective-function/33627 "2020-01-21T16:06:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martina/32/202266_2.png) [@martina](https://discourse.julialang.org/u/martina)\
**Post date:** [January 21, 2020, 4:06pm UTC](https://discourse.julialang.org/t/minlp-optimization-with-user-defined-objective-function/33627/1 "2020-01-21T16:06:34Z")

</div>

Hello,  
I’m still very new with Julia, and I’m having troubles with my MINLP optimization problem that I used to solve with MATLAB.  
I can’t find a way to provide an user-defined objective function to the @NLobjective macro.  
The problem is that my objective function cannot be defined (like in most examples) in an analytic way as a function of my variables.  
Here’s a MWE (actually, in this particular case, I could have expressed myfunction in a row, but what I need is to do that with an “outer” function, as I did):

```julia
using JuMP,Ipopt,Cbc,Juniper
optimizer = Juniper.Optimizer
params = Dict{Symbol,Any}()
params[:nl_solver] = with_optimizer(Ipopt.Optimizer, print_level=0)
m = Model(with_optimizer(optimizer, params))

@variable(m, 0 <= x[1:4] <= 6, Int)

function myfunction(x1,x2,x3,x4)
    xx=[x1,x2,x3,x4]
    return sum(xx[i] for i = 1:4)
end
register(m, :myfunction, 4, myfunction; autodiff = true)

@NLobjective(m,Max,myfunction(x[1],x[2],x[3],x[4]))

print(m)
optimize!(m)

```

The problem that arises is:

> KeyError: key :myfunction not found

Any help is really appreciated. Thanks a lot!

---

<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:** [January 21, 2020, 4:33pm UTC](https://discourse.julialang.org/t/minlp-optimization-with-user-defined-objective-function/33627/2 "2020-01-21T16:33:57Z")

</div>

I haven’t tested this, so there might be a typo, but you need to tell Juniper that you are registering the functions.

```nohighlight
using JuMP
using Ipopt
using Juniper

function myfunction(x1, x2, x3, x4)
    xx=[x1, x2, x3, x4]
    return sum(xx[i] for i = 1:4)
end

model = Model(
    with_optimizer(
        Juniper.Optimizer;
            nl_solver = with_optimizer(Ipopt.Optimizer, print_level = 0),
            registered_functions = [
                Juniper.register(:myfunction, 4, myfunction; autodiff = true)
            ]
        )
    )
)
@variable(model, 0 <= x[1:4] <= 6, Int)
register(model, :myfunction, 4, myfunction; autodiff = true)
@NLobjective(model, Max, myfunction(x[1], x[2], x[3], x[4]))

```

cc @Wikunia

(Edit: I opened an issue [https://github.com/lanl-ansi/Juniper.jl/issues/173](https://github.com/lanl-ansi/Juniper.jl/issues/173))

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [January 21, 2020, 4:39pm UTC](https://discourse.julialang.org/t/minlp-optimization-with-user-defined-objective-function/33627/3 "2020-01-21T16:39:01Z")

</div>

Thanks for linking me. Yes currently this is the way to go even though it is ugly.

---

<div class="post-metadata">

**Author:** ![martina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martina/32/202266_2.png) [@martina](https://discourse.julialang.org/u/martina)\
**Post date:** [January 21, 2020, 4:45pm UTC](https://discourse.julialang.org/t/minlp-optimization-with-user-defined-objective-function/33627/4 "2020-01-21T16:45:37Z")

</div>

Thanks a lot. This is exactly what I needed 😉
