# The objective function is not supported by JuMP

**URL:** https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652
**Category:** Optimization (Mathematical)
**Created:** [December 11, 2020, 6:59am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652 "2020-12-11T06:59:57Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![prit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prit/32/20187_2.png) [@prit](https://discourse.julialang.org/u/prit)
#### Post date: [December 11, 2020, 6:59am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/1 "2020-12-11T06:59:57Z")

</div>

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

I have the following constraint optimization problem in jump:

```julia
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\_mat_p, where p is a vector of N+1. But I’m getting the following error:

```julia
ERROR: LoadError: The objective function `GenericQuadExpr{Float64,VariableRef}[0 0 0; 0 0 0; 0 0 0]` is not supported by JuMP.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] set_objective_function(::Model, ::Array{GenericQuadExpr{Float64,VariableRef},2}) at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/objective.jl:123
 [3] set_objective(::Model, ::MathOptInterface.OptimizationSense, ::Array{GenericQuadExpr{Float64,VariableRef},2}) at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/objective.jl:128
 [4] top-level scope at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/macros.jl:831
 [5] top-level scope at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:107
 [6] include(::Function, ::Module, ::String) at ./Base.jl:380
 [7] include(::Module, ::String) at ./Base.jl:368
 [8] exec_options(::Base.JLOptions) at ./client.jl:296
 [9] _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?

---

<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: [December 11, 2020, 8:31am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/2 "2020-12-11T08:31:58Z")

</div>

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

---

<div class="post-metadata">

### Author: ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)
#### Post date: [December 11, 2020, 9:00am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/3 "2020-12-11T09:00:16Z")

</div>

You should write

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

```

otherwise `p` is a scalar variable.

---

<div class="post-metadata">

### Author: ![prit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prit/32/20187_2.png) [@prit](https://discourse.julialang.org/u/prit)
#### Post date: [December 11, 2020, 9:11am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/4 "2020-12-11T09:11:44Z")

</div>

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:  
[1] error(::string) at ./error.jl:33  
[2] set\_objective\_function(::model, ::array{genericaffexpr{float64,variableref},1}) at /users/prikshetsharma/.julia/packages/jump/qhovb/src/objective.jl:123  
[3] set\_objective(::model, ::mathoptinterface.optimizationsense, ::array{genericaffexpr{float64,variableref},1}) at /users/prikshetsharma/.julia/packages/jump/qhovb/src/objective.jl:128  
[4] top-level scope at /users/prikshetsharma/.julia/packages/jump/qhovb/src/macros.jl:831  
[5] top-level scope at /users/prikshetsharma/documents/clotorch/src/clotorch/flight/trajectory.jl:105  
[6] include(::function, ::module, ::string) at ./base.jl:380  
[7] include(::module, ::string) at ./base.jl:368  
[8] exec\_options(::base.jloptions) at ./client.jl:296  
[9] \_start() at ./client.jl:506  
in expression starting at /users/prikshetsharma/documents/clotorch/src/clotorch/flight/trajectory.jl:105

---

<div class="post-metadata">

### Author: ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)
#### Post date: [December 11, 2020, 9:12am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/5 "2020-12-11T09:12:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![prit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prit/32/20187_2.png) [@prit](https://discourse.julialang.org/u/prit)
#### Post date: [December 11, 2020, 9:27am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/6 "2020-12-11T09:27:59Z")

</div>

```julia
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)

```

---

<div class="post-metadata">

### Author: ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)
#### Post date: [December 11, 2020, 9:40am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/7 "2020-12-11T09:40:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![prit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prit/32/20187_2.png) [@prit](https://discourse.julialang.org/u/prit)
#### Post date: [December 11, 2020, 9:51am UTC](https://discourse.julialang.org/t/the-objective-function-is-not-supported-by-jump/51652/8 "2020-12-11T09:51:37Z")

</div>

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