# User defined objective function to run MICP

**URL:** https://discourse.julialang.org/t/user-defined-objective-function-to-run-micp/16992
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [October 31, 2018, 3:19am UTC](https://discourse.julialang.org/t/user-defined-objective-function-to-run-micp/16992 "2018-10-31T03:19:00Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![coiacy](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@coiacy](https://discourse.julialang.org/u/coiacy)
#### Post date: [October 31, 2018, 6:27pm UTC](https://discourse.julialang.org/t/user-defined-objective-function-to-run-micp/16992/5 "2018-10-31T18:27:56Z")

</div>

Thank you for suggesting Bonmin in C++, but I would like to try that later if this does not work in Julia. Indeed Pavito can use IpoptSolver as cont-solver and I have tried it as follows, but Julia returned an error.

```julia
using JuMP, Pavito
nx = 100
c = 10
F = rand(nx, nx)

function my_obj(w...)
    M = eye(nx)
    for i = 1 : nx
        M += w[i] * F[:, i]' * F[:, i]
    end
    return trace(inv(M))
end

mip_solver_drives = true
rel_gap = 1e-5

using CPLEX
mip_solver = CplexSolver(
    CPX_PARAM_SCRIND = (mip_solver_drives ? 1 : 0),
    CPX_PARAM_EPINT = 1e-8,
    CPX_PARAM_EPRHS = 1e-7,
    CPX_PARAM_EPGAP=(mip_solver_drives ? 1e-5 : 1e-9)
)

using Ipopt
cont_solver = IpoptSolver(print_level = 0)

solver = PavitoSolver(
    mip_solver_drives = mip_solver_drives,
    log_level = 1,
    rel_gap = rel_gap,
    mip_solver = mip_solver,
    cont_solver = cont_solver,
)

model = Model(solver = solver)
JuMP.register(model, :my_obj, nx, my_obj, autodiff = true)
w = @variable(model, [j = 1 : nx], Bin, lowerbound = 0, upperbound = 1)
@constraint(model, sum(w) <= c)
@NLobjective(model, Min, my_obj(w))

```

Error message: LoadError: Incorrect number of arguments for “my\_obj” in nonlinear expression. This is also related to a previous question: [https://discourse.julialang.org/t/passing-an-array-of-variables-to-a-user-defined-non-linear-function/4132](https://discourse.julialang.org/t/passing-an-array-of-variables-to-a-user-defined-non-linear-function/4132).

Currently I use auto-diff in the code, but I will provide gradient after the code can run for the simpler case. Could you give some advice on how I should define the function ?

---

_[View the full topic](https://discourse.julialang.org/t/user-defined-objective-function-to-run-micp/16992)._
