# The objective function is not supported by JuMP

How to optimize a function in jump that contains an array in the objective function?

I have the following constraint optimization problem in jump:

``````m = Model(optimizer_with_attributes(Mosek.Optimizer, "QUIET" => false, "INTPNT_CO_TOL_DFEAS" => 1e-7))
N = 2

function Q_r(i::Number, l::Number, r::Number, tau::Float64)
if i >= r && l >= r
return 2 * fac1(r, i, l) * fac2(r, i, l, tau)
else
return 0.0
end
end

function Q(i::Number, l::Number, tau::Number)
elem = 0
for r in 0:N
println(penalties[r + 1], Q_r(i, l, r, tau))
elem += penalties[r + 1] * Q_r(i, l, r, tau)
end
return elem
end

Q_mat = Array{Float64, 2}(undef, N+1, N+1)
for i in 1:N+1
for j in 1:N+1
Q_mat[i, j] = Q(i, j, 0.0)
end
end

@variable(m, p)

@objective(m, Min, p'*Q_mat*p)

``````

I want to minimize p’Q_matp, where p is a vector of N+1. But I’m getting the following error:

``````ERROR: LoadError: The objective function `GenericQuadExpr{Float64,VariableRef}[0 0 0; 0 0 0; 0 0 0]` is not supported by JuMP.
Stacktrace:
 error(::String) at ./error.jl:33
 set_objective(::Model, ::MathOptInterface.OptimizationSense, ::Array{GenericQuadExpr{Float64,VariableRef},2}) at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/objective.jl:128
 top-level scope at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/macros.jl:831
 top-level scope at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:107
 include(::Function, ::Module, ::String) at ./Base.jl:380
 include(::Module, ::String) at ./Base.jl:368
 exec_options(::Base.JLOptions) at ./client.jl:296
 _start() at ./client.jl:506
in expression starting at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:107

``````

How to do the optimization if this objective function isn’t supported?

I can’t help with the specific problem, but I’m sure this issue will get more attention when moved to the `Optimization` category.

2 Likes

You should write

``````@variable(m, p[1:N+1])
``````

otherwise `p` is a scalar variable.

3 Likes

I changed it to p[1:N+1], but now I’m getting the error:

0.0error: loaderror: the objective function `genericaffexpr{float64,variableref}[0, 0, 0]` is not supported by jump.
stacktrace:
 error(::string) at ./error.jl:33
 set_objective_function(::model, ::array{genericaffexpr{float64,variableref},1}) at /users/prikshetsharma/.julia/packages/jump/qhovb/src/objective.jl:123
 set_objective(::model, ::mathoptinterface.optimizationsense, ::array{genericaffexpr{float64,variableref},1}) at /users/prikshetsharma/.julia/packages/jump/qhovb/src/objective.jl:128
 top-level scope at /users/prikshetsharma/.julia/packages/jump/qhovb/src/macros.jl:831
 top-level scope at /users/prikshetsharma/documents/clotorch/src/clotorch/flight/trajectory.jl:105
 include(::function, ::module, ::string) at ./base.jl:380
 include(::module, ::string) at ./base.jl:368
 exec_options(::base.jloptions) at ./client.jl:296
 _start() at ./client.jl:506
in expression starting at /users/prikshetsharma/documents/clotorch/src/clotorch/flight/trajectory.jl:105

Please post a runnable code snippet to help others reproduce the issue.

2 Likes
``````using JuMP
using MosekTools
m = Model(optimizer_with_attributes(Mosek.Optimizer, "QUIET" => false, "INTPNT_CO_TOL_DFEAS" => 1e-7))

K = 3
N = 2
penalties = [1.0, 3.9, 8.7]
function fac1(r::Number, i::Number, l::Number)
fac1 = 1.0
for m in 0:r-1
fac1 *= (i-m)*(l-m)
end
return fac1
end

function fac2(r::Number, i::Number, l::Number, tau::Float64)
return tau ^ (i + l - 2r + 1)/(i + l - 2r + 1)
end

function Q_r(i::Number, l::Number, r::Number, tau::Float64)
if i >= r && l >= r
return 2 * fac1(r, i, l) * fac2(r, i, l, tau)
else
return 0.0
end
end

function Q(i::Number, l::Number, tau::Number)
elem = 0
for r in 0:N
elem += penalties[r + 1] * Q_r(i, l, r, tau)
end
return elem
end

Q_mat = Array{Float64, 2}(undef, N+1, N+1)
for i in 1:N+1
for j in 1:N+1
Q_mat[i, j] = Q(i, j, 0.0)
end
end

@variable(m, p[1:N+1])

@objective(m, Min, p'*Q_mat*p)

``````

Tested on JuMP@0.21.5 and MosekTools@0.9.4 on Windows 10 and no error is thrown.

2 Likes

I’m running the same versions of both. Error was thrown. Now it works magically.