JuMP: AffExpr error

I’m using Julia v0.6.4 on Ubuntu (14.04) and am getting an affine expression error in the following toy model:

using JuMP
using Ipopt

model = Model(solver=IpoptSolver())

## params
N = 5
idx1 = [1, 2]
idx2 = [5, 4]

## variables
@variable(model, x_r[1:N])
@variable(model, x_i[1:N])

## constraints
for i = 1:length(idx1)
    j = idx1[i]
    k = idx2[i]
    ## below I want: 2 * (x_r[j] - x_r[k])^2 +  2 * (x_i[j] - x_i[k])^2 - 1 >= 0.0
    ae_r = AffExpr([x_r[j], x_r[k]], [1.0, -1.0])  ## <<-- ERROR
    ae_i = AffExpr([x_i[j], x_i[k]], [1.0, -1.0])
    @NLexpression(model, nle[i], 2.0 * (ae_r^2 + ae_i^2))
    @NLconstraint(model, nle[i] - 1.0 >= 0.0)
end

the error is:

ERROR: MethodError: no method matching JuMP.GenericAffExpr{Float64,JuMP.Variable}(::Array{JuMP.Variable,1}, ::Array{Float64,1})
Stacktrace:
 [1] macro expansion at ./REPL[145]:5 [inlined]
 [2] anonymous at ./<missing>:?

I looked at the docs but am confused about what I’m doing wrong. I tried with the following adjustment:

ae_r = AffExpr([x_r[j], x_r[k]], [1.0, -1.0], 0.0)
ae_i = AffExpr([x_i[j], x_i[k]], [1.0, -1.0], 0.0)

and received the error:

ERROR: MethodError: no method matching parseNLExpr_runtime(::JuMP.Model, ::JuMP.GenericAffExpr{Float64,JuMP.Variable}, ::Array{ReverseDiffSparse.NodeData,1}, ::Int64, ::Array{Float64,1})
Closest candidates are:
  parseNLExpr_runtime(::JuMP.Model, ::Number, ::Any, ::Any, ::Any) at /homes/jmroth/.julia/v0.6/JuMP/src/parsenlp.jl:196
  parseNLExpr_runtime(::JuMP.Model, ::JuMP.Variable, ::Any, ::Any, ::Any) at /homes/jmroth/.julia/v0.6/JuMP/src/parsenlp.jl:202
  parseNLExpr_runtime(::JuMP.Model, ::JuMP.NonlinearExpression, ::Any, ::Any, ::Any) at /homes/jmroth/.julia/v0.6/JuMP/src/parsenlp.jl:208
  ...
Stacktrace:
 [1] macro expansion at /homes/USER/.julia/v0.6/JuMP/src/parsenlp.jl:226 [inlined]
 [2] macro expansion at /homes/USER/.julia/v0.6/JuMP/src/macros.jl:1264 [inlined]
 [3] macro expansion at ./REPL[154]:7 [inlined]
 [4] anonymous at ./<missing>:?

Any help would be appreciated! Thanks!

I think I had an error with using @NL. Will update later tonight.

Update: Can’t build a QuadExpr from AffExpr. Should have just used QuadExpr from the beginning.

See the syntax notes for nonlinear modelling in the JuMP docs.

AffExpr and QuadExpr objects cannot currently be used inside nonlinear expressions. Instead,
introduce auxiliary variables, e.g.:

myexpr = dot(c,x) + 3y # where x and y are variables
@variable(m, aux)
@constraint(m, aux == myexpr)
@NLobjective(m, Min, sin(aux))

1 Like