# Quadratic and non-linear expressions cannot be mixed up!

**URL:** <https://discourse.julialang.org/t/quadratic-and-non-linear-expressions-cannot-be-mixed-up/45120>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [August 17, 2020, 5:08pm UTC](https://discourse.julialang.org/t/quadratic-and-non-linear-expressions-cannot-be-mixed-up/45120 "2020-08-17T17:08:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![musman](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@musman](https://discourse.julialang.org/u/musman)\
**Post date:** [August 17, 2020, 5:08pm UTC](https://discourse.julialang.org/t/quadratic-and-non-linear-expressions-cannot-be-mixed-up/45120/1 "2020-08-17T17:08:37Z")

</div>

Hi, I am setting up a non-linear objective which contains both quadratic and non-linear terms. However, I am getting the following error

```julia
Unexpected quadratic expression 378.5346047250809 e[1]² + 378.5346047250809 f[1]² + 378.5346047250809 e[2]² + 378.5346047250809 f[2]² - 757.0692094501618 e[1]*e[2] - 757.0692094501618 f[1]*f[2] in nonlinear expression. 
Quadratic expressions (e.g., created using @expression) and nonlinear expressions cannot be mixed.

```

Now, the thing is I am setting non-linear constraints as well which contains both quadratic and non-linear expressions using @NLconstraint macro but I am not getting the above mentioned error. For the objective function, I need the summation of non-linear expression, therefore, I set up a loop and store all non-linear expressions in an array, and then I am using the ‘sum’ function in order to get the final expression. However, when I give this final expression to @NLobjective macro, I am getting the above-mentioned error. Why is that? and what would be the possible solutions to overcome this error?

The code for objective function is as follows:

```julia
ploss = []
for i in 1:nLines
    rline = nw_lines[i].line_r
    gij_line = real(yij_line[i,1])
    bij_line = imag(yij_line[i,1])
    lcrnt = (gij_line^2+bij_line^2)*((e[nw_lines[i].line_from]^2+f[nw_lines[i].line_from]^2)+(e[nw_lines[i].line_to]^2+f[nw_lines[i].line_to]^2)
                                    -2(e[nw_lines[i].line_from]*e[nw_lines[i].line_to]+f[nw_lines[i].line_from]*f[nw_lines[i].line_to]))
    push!(ploss,lcrnt)
end
loss_expr = sum(ploss[j] for j in 1:size(ploss,1))
@NLobjective(acopf,Min,loss_expr)

```

---

<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:** [August 18, 2020, 3:16am UTC](https://discourse.julialang.org/t/quadratic-and-non-linear-expressions-cannot-be-mixed-up/45120/2 "2020-08-18T03:16:24Z")

</div>

Use `@objective` instead of `@NLobjective`. `@objective` can be used with linear and quadratic terms.

---

<div class="post-metadata">

**Author:** ![musman](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@musman](https://discourse.julialang.org/u/musman)\
**Post date:** [August 18, 2020, 7:42am UTC](https://discourse.julialang.org/t/quadratic-and-non-linear-expressions-cannot-be-mixed-up/45120/3 "2020-08-18T07:42:14Z")

</div>

But my objective also contains the product of two variables and I cannot set it using @objective macro. So, I have a combination of quadratic terms

`e[i]^2+f[i]^2`

and bilinear terms

```julia
e[i]*e[j]+f[i]f[j]

```

and I cannot set it using @objective macro.

---

<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:** [August 18, 2020, 7:57am UTC](https://discourse.julialang.org/t/quadratic-and-non-linear-expressions-cannot-be-mixed-up/45120/4 "2020-08-18T07:57:03Z")

</div>

Please try it; you can add bilinear terms with `@objective`.

---

<div class="post-metadata">

**Author:** ![musman](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@musman](https://discourse.julialang.org/u/musman)\
**Post date:** [August 18, 2020, 8:02am UTC](https://discourse.julialang.org/t/quadratic-and-non-linear-expressions-cannot-be-mixed-up/45120/5 "2020-08-18T08:02:12Z")

</div>

Well, it worked. But honestly speaking, I was not expecting it.  
I have a lot of constraints which are the mixture of quadratic and non-linear expressions and I have set them through @NLconstraint macro. I was expecting the same for the objective (containing both quad and NL expressions); however, it seems that I was wrong. Although I have also looked into non-linear modelling page of JuMP and still, I believe that I was setting the objective right way.  
Anyway, thanks a lot, man!
