# JuMP: AffExpr error

**URL:** <https://discourse.julialang.org/t/jump-affexpr-error/14921>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [September 13, 2018, 9:23pm UTC](https://discourse.julialang.org/t/jump-affexpr-error/14921 "2018-09-13T21:23:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [September 13, 2018, 9:23pm UTC](https://discourse.julialang.org/t/jump-affexpr-error/14921/1 "2018-09-13T21:23:37Z")

</div>

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

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

```julia
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](https://jump.readthedocs.io/en/latest/refexpr.html) but am confused about what I’m doing wrong. I tried with the following adjustment:

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

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

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [September 13, 2018, 10:10pm UTC](https://discourse.julialang.org/t/jump-affexpr-error/14921/2 "2018-09-13T22:10:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 13, 2018, 11:21pm UTC](https://discourse.julialang.org/t/jump-affexpr-error/14921/3 "2018-09-13T23:21:28Z")

</div>

See the [syntax notes for nonlinear modelling](http://www.juliaopt.org/JuMP.jl/0.18/nlp.html#syntax-notes) 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))
