Use function as objective of JuMP

question
jump
#1

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.Integerconstraints is not supported by the model.

My code is as below:

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 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"
0 Likes

#2

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 OBJ, and use @NLobjective for it.
0 Likes

#3

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

0 Likes

#4

The error message is telling you that integer variables are not supported by Ipopt. You will need to use an MINLP solver like Alpine https://github.com/lanl-ansi/Alpine.jl instead. There is also https://github.com/lanl-ansi/juniper.jl, but it hasn’t been ported to the new JuMP yet, it is a work-in-progress.

0 Likes

#5

Are there any example of using Alpine with JuMP?

0 Likes

#6

Oops. It seems like Alpine is also not updated.

You can use SCIP: https://github.com/SCIP-Interfaces/SCIP.jl

0 Likes

#7

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

0 Likes

#8

cc @leethargo re SCIP installation

Other options

0 Likes

#9

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

0 Likes

#10

GLPK doesn’t support nonlinear constraints.

0 Likes

#11

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

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.

0 Likes

#12

Does JuMP with Gurobi offer MINLP functionality?

0 Likes

#13

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 for my installation notes.

0 Likes

#14

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?

0 Likes

#15

ASTInterpreter2 is not a dependency of either package. This is probably related to Juno. See ERROR: Unsatisfiable requirements detected for package ASTInterpreter2 [e6d88f4b].

0 Likes

#16

I did not have Juno.

0 Likes

#17

It could also be Revise or some such package?

0 Likes

#18

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

0 Likes