Use function as objective of JuMP

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:

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"

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.

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

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 instead. There is also GitHub - lanl-ansi/Juniper.jl: A JuMP-based Nonlinear Integer Program Solver, but it hasn’t been ported to the new JuMP yet, it is a work-in-progress.

Are there any example of using Alpine with JuMP?

Oops. It seems like Alpine is also not updated.

You can use SCIP: GitHub - scipopt/SCIP.jl: Julia interface to SCIP solver

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

cc @leethargo re SCIP installation

Other options

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

GLPK doesn’t support nonlinear constraints.

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.

Does JuMP with Gurobi offer MINLP functionality?

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

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?

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

I did not have Juno.

It could also be Revise or some such package?

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