I’ve run into a number of issues with quadratic objectives in JuMP. A simple example is:
m = Model(solver = CplexSolver())
@variable(m, w[1:10])
@expression(m, dotproduct, sum(w[i]*w[i] for i = 1:10))
If I try to make the dotproduct squared the objective with @objective(m, Min, dotproduct^2)
I get an error
ERROR: *(::QuadExpr,::QuadExpr) is not defined. Are you trying to build a nonlinear problem? Make sure you use @NLconstraint/@NLobjective.
Stacktrace:
[1] power_by_squaring(::JuMP.GenericQuadExpr{Float64,JuMP.Variable}, ::Int64) at ./intfuncs.jl:169
[2] literal_pow(::Base.#^, ::JuMP.GenericQuadExpr{Float64,JuMP.Variable}, ::Type{Val{2}}) at ./intfuncs.jl:208
[3] macro expansion at ./REPL.jl:97 [inlined]
[4] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73
If I try it with @NLobjective(m, Min, dotproduct^2)
, I get the error
ERROR: MethodError: no method matching parseNLExpr_runtime(::JuMP.Model, ::JuMP.GenericQuadExpr{Float64,JuMP.Variable}, ::Array{ReverseDiffSparse.NodeData,1}, ::Int64, ::Array{Float64,1})
Closest candidates are:
parseNLExpr_runtime(::JuMP.Model, !Matched::Number, ::Any, ::Any, ::Any) at /home/paul/.julia/v0.6/JuMP/src/parsenlp.jl:196
parseNLExpr_runtime(::JuMP.Model, !Matched::JuMP.Variable, ::Any, ::Any, ::Any) at /home/paul/.julia/v0.6/JuMP/src/parsenlp.jl:202
parseNLExpr_runtime(::JuMP.Model, !Matched::JuMP.NonlinearExpression, ::Any, ::Any, ::Any) at /home/paul/.julia/v0.6/JuMP/src/parsenlp.jl:208
...
Stacktrace:
[1] macro expansion at ./REPL.jl:97 [inlined]
[2] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73
Aren’t those the only two ways to add the objectives?