# Use function as objective of JuMP

**URL:** <https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332>\
**Category:** General Usage\
**Tags:** question, jump\
**Created:** [March 26, 2019, 3:07am UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332 "2019-03-26T03:07:41Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 26, 2019, 3:07am UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/1 "2019-03-26T03:07:41Z")

</div>

I am trying to optimize a function in JuMP, but got the error message:

`ERROR: MathOptInterface.UnsupportedConstraint{MathOptInterface.SingleVariable,MathOptInterface.Integer}: `MathOptInterface.SingleVariable`-in-`MathOptInterface.Integer` constraints is not supported by the model.`

My code is as below:

```julia
using LinearAlgebra, Random, JuMP, Ipopt

model = Model(with_optimizer(Ipopt.Optimizer))
@variable(model, 1 <= Share[1:10], Int)

function OBJ(Share...)
    N_Asset = 10
    Price = [i*5 for i in 1:N_Asset]
    CovMat = [10 -4 -3 1 4 6 2 7 6 8;
                    -4 9 -4 10 2 8 10 8 3 3;
                    -3 -4 9 -8 3 2 2 1 10 3;
                    1 10 -8 4 -1 3 5 9 4 7;
                    4 2 3 -1 3 -4 1 6 5 1;
                    6 8 2 3 -4 9 -9 1 6 7;
                    2 10 2 5 1 -9 6 -2 3 9;
                    7 8 1 9 6 1 -2 1 6 -10;
                    6 3 10 4 5 6 3 6 -10 -4;
                    8 3 3 7 1 7 9 10 -4 7]
   
    
    return dot(collect(Share), Price) - collect(Share)' * CovMat * collect(Share) * 0.001

    
end

register(model, :OBJ, 10, OBJ; autodiff=true)
@NLobjective(model, Max, OBJ(Share...))

function CONS(Share...)
    #Share = [i for i in 1:10]
    N_Asset = 10
    Price = [i*5 for i in 1:N_Asset]
    return dot(collect(Share), Price)
end

register(model, :CONS, 10, CONS, autodiff=true)

@constraint(model, con, CONS(Share...) <= 10000)

optimize!(model)

objective_value(model)

```

version:

```julia
Julia Version 1.0.3
Commit 099e826241 (2018-12-18 01:34 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, broadwell)
Environment:
  JULIA_EDITOR = "/usr/share/code/code"

```

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [March 26, 2019, 8:25am UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/2 "2019-03-26T08:25:23Z")

</div>

I don’t know to which line that error message belongs, but two things are suspicious here:

- You use `Price` in the `@constraint`, but it seems to be defined only in the function scope of `OBJ`.
- You might need to register your [user-defined function](http://www.juliaopt.org/JuMP.jl/v0.19.0/nlp/#User-defined-Functions-1) `OBJ`, and use `@NLobjective` for it.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 26, 2019, 3:45pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/3 "2019-03-26T15:45:18Z")

</div>

I have updated my code, but I still get the error message.

---

<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:** [March 26, 2019, 3:57pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/4 "2019-03-26T15:57:24Z")

</div>

The error message is telling you that integer variables are not supported by Ipopt. You will need to use an MINLP solver like Alpine [GitHub - lanl-ansi/Alpine.jl: A Julia/JuMP-based Global Optimization Solver for Non-convex Programs](https://github.com/lanl-ansi/Alpine.jl) instead. There is also [GitHub - lanl-ansi/Juniper.jl: A JuMP-based Nonlinear Integer Program Solver](https://github.com/lanl-ansi/juniper.jl), but it hasn’t been ported to the new JuMP yet, it is a work-in-progress.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 26, 2019, 4:03pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/5 "2019-03-26T16:03:15Z")

</div>

Are there any example of using Alpine with 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:** [March 26, 2019, 4:10pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/6 "2019-03-26T16:10:21Z")

</div>

Oops. It seems like Alpine is also not updated.

You can use SCIP: [GitHub - scipopt/SCIP.jl: Julia interface to SCIP solver](https://github.com/SCIP-Interfaces/SCIP.jl)

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 26, 2019, 9:06pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/7 "2019-03-26T21:06:32Z")

</div>

I could not install scipoptsuite on my laptop. Is there any other options?

---

<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:** [March 26, 2019, 9:25pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/8 "2019-03-26T21:25:31Z")

</div>

cc @leethargo re SCIP installation

Other options

- [GitHub - jump-dev/AmplNLWriter.jl: Julia interface to AMPL-enabled solvers](https://github.com/JuliaOpt/AmplNLWriter.jl) + Bonmin or Couenne from [Open Source Solvers - AMPLAMPL](https://ampl.com/products/solvers/open-source/)

- [https://github.com/JuliaOpt/KNITRO.jl](https://github.com/JuliaOpt/KNITRO.jl)

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 26, 2019, 11:21pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/9 "2019-03-26T23:21:21Z")

</div>

I tried GLPK but got the error message:  
`ERROR: MathOptInterface.UnsupportedAttribute{MathOptInterface.NLPBlock}: Attribute MathOptInterface.NLPBlock() is not supported by the model.`

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 27, 2019, 1:10am UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/10 "2019-03-27T01:10:48Z")

</div>

GLPK doesn’t support nonlinear constraints.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [March 27, 2019, 8:19am UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/11 "2019-03-27T08:19:28Z")

</div>

> [@odow](#):
>
> You can use SCIP

SCIP can not support user-defined functions, as it uses the expression-graph interface, not AD.

> [@odow](#):
>
> SCIP installation

Feel free to open an issue if you encounter problems with the installation. Note that only Linux systems are supported, currently, though in principle also OSX and Windows could be supported.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 27, 2019, 4:37pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/12 "2019-03-27T16:37:46Z")

</div>

Does JuMP with Gurobi offer MINLP functionality?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 27, 2019, 6:29pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/13 "2019-03-27T18:29:21Z")

</div>

No. Again, that’s not a JuMP limitation, Gurobi just doesn’t do MINLP. FWIW, SCIP has been working fine for me on Linux. See [Use of MINLP in Julia - #10 by tkoolen](https://discourse.julialang.org/t/use-of-minlp-in-julia/22161/10) for my installation notes.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 27, 2019, 10:51pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/14 "2019-03-27T22:51:30Z")

</div>

I tried to use Juniper and Alpine on Julia 1.1.0 and 1.0.1, but both failed. On Julia 1.1.0, when I load the packages the error message said failed to precompile them while on Julia 1.0.1 I cannot install them because unsatisfiable requirements detected for package ASTInterpreter2.

I have tried both linux and windows, but still not working. Do I need to go back to Julia 0.7?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 27, 2019, 11:01pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/15 "2019-03-27T23:01:37Z")

</div>

> [@odow](#):
>
> There is also [https://github.com/lanl-ansi/juniper.jl](https://github.com/lanl-ansi/juniper.jl), but it hasn’t been ported to the new JuMP yet, it is a work-in-progress.

> [@odow](#):
>
> It seems like Alpine is also not updated.

ASTInterpreter2 is not a dependency of either package. This is probably related to Juno. See [ERROR: Unsatisfiable requirements detected for package ASTInterpreter2 [e6d88f4b]](https://discourse.julialang.org/t/error-unsatisfiable-requirements-detected-for-package-astinterpreter2-e6d88f4b/22224).

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 27, 2019, 11:10pm UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/16 "2019-03-27T23:10:50Z")

</div>

I did not have Juno.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [March 28, 2019, 8:23am UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/17 "2019-03-28T08:23:30Z")

</div>

> [@tkoolen](#):
>
> ASTInterpreter2 is not a dependency of either package. This is probably related to Juno.

It could also be Revise or some such package?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 28, 2019, 8:35am UTC](https://discourse.julialang.org/t/use-function-as-objective-of-jump/22332/18 "2019-03-28T08:35:25Z")

</div>

> [@Yifan\_Liu](#):
>
> I have tried both linux and windows, but still not working. Do I need to go back to Julia 0.7?

`update` should fix it, or `rm -m ASTInterpreter2` in the Pkg REPL mode.
