# Defining Non Linear Vector Constraints in JuMP

**URL:** <https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 8, 2017, 10:13pm UTC](https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742 "2017-07-08T22:13:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![acauligi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acauligi/32/4956_2.png) [@acauligi](https://discourse.julialang.org/u/acauligi)\
**Post date:** [July 8, 2017, 10:13pm UTC](https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742/1 "2017-07-08T22:13:55Z")

</div>

I’m trying to implement pseudospectral methods using JuMP & Ipopt. The state is n-dimensional and control m-dimensional and the values are evaluated at N+1 collocation points; in this particular problem, n=3, m=3, and N = 120.

For pseudospectral methods, one constraint is that the numerical approximation of the derivative 2/T_D_X = f\_all(X), where X is the stacked state [x1;x2…;x\_{N+1}] and f\_all = [f(x1); f(x2);…;f(x\_{N+1})].

```julia
@NLconstraint(mod, f_all(dyn, state, N, n, m) - 2/Tp*D*state[1:n*(N+1)] == zeros(n*(N+1)))

```

dyn is the non-linear dynamics function and f\_all returns a stacked vector of dyn() evaluated for each x.

```julia
dyn(p,w) = 0.25*((1-norm(p)^2)*w - 2*skew(w)*p + 2*dot(w,p)*p);
JuMP.register(mod, :dyn, 2, dyn, autodiff=true);

```

I keep running into numerous errors regardless of how I try to define the nonlinear functions:

```julia
ERROR: Cannot multiply a quadratic expression by a variable

```

I also tried breaking apart the n-D derivative into n different @NLexpression:

```julia
@NLexpression(mod, dyn1_expr[1:n], [0.25*(p[1]^2-p[2]^2-p[3]^2+1)*w[1] + 0.5*(p[1]*p[2]-p[3])*w[2] + 0.5*(p[2]-p[1]*p[3])*w[3]; 0.5*(p[1]*p[2]+p[3])*w[1] + 0.25*(p[2]^2 - p[1]^2-p[3]^2)*w[2] + 0.5*(p[2]*p[3]-p[1])*w[3]; 0.5*(p[1]*p[3]-p[2])*w[1] + 0.5*(p[1]+p[2]*p[3])*w[2] + 0.25*(p[3]^2-p[2]^2-p[1]^2+1)*w[3]])

```

and received:

```julia
ERROR: Unexpected vector JuMP.GenericQuadExpr{Float64,JuMP.Variable}[1.736231682755747e-5 state[1]² + 0.00033454250210412234 state[1]*state[2] - 1.736231682755747e-5 state[2]² - 0.0006879201991741926 state[1]*state[3] - 1.736231682755747e-5 state[3]² - 0.00033454250210412234 state[3] + 0.0006879201991741926 state[2] + 1.736231682755747e-5,-0.00016727125105206117 state[1]² + 3.472463365511494e-5 state[1]*state[2] + 0.00016727125105206117 state[2]² + 0.0006879201991741926 state[2]*state[3] - 0.00016727125105206117 state[3]² + 3.472463365511494e-5 state[3] - 0.0006879201991741926 state[1],-0.0003439600995870963 state[1]² - 0.0003439600995870963 state[2]² + 3.472463365511494e-5 state[1]*state[3] + 0.00033454250210412234 state[2]*state[3] + 0.0003439600995870963 state[3]² - 3.472463365511494e-5 state[2] + 0.00033454250210412234 state[1] + 0.0003439600995870963] in nonlinear expression. Nonlinear expressions may contain only scalar expressions.

```

Any idea how I could go about defining an n\*(N+1) dimensional constraint by evaluating the dynamics f(x) N+1 times?

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [July 10, 2017, 5:09am UTC](https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742/2 "2017-07-10T05:09:32Z")

</div>

JuMP’s syntax and automatic differentiation utilities were not designed to handle this case. There’s a workaround for multidimensional input ([Julia+JuMP: variable number of arguments to function - Stack Overflow](https://stackoverflow.com/questions/44710900/juliajump-variable-number-of-arguments-to-function)) but not for multidimensional output. I’d recommend looking into tools like [CasADi](https://github.com/casadi/casadi), or you can use [ReverseDiff](https://github.com/JuliaDiff/ReverseDiff.jl) to compute derivatives and manually communicate them to Ipopt via [MathProgBase](https://github.com/JuliaOpt/MathProgBase.jl).

---

<div class="post-metadata">

**Author:** ![acauligi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acauligi/32/4956_2.png) [@acauligi](https://discourse.julialang.org/u/acauligi)\
**Post date:** [July 10, 2017, 7:25am UTC](https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742/3 "2017-07-10T07:25:18Z")

</div>

If I were to break apart the dynamics function _f_ into _n_ separate functions, would that circumvent the issue? Since I know the matrix D from D\*X, I could grab the i’th row and call a function f\_i that also returns the i’th output of f(x). It seems inefficient, but that would allow me to pass in an n dimensional input and return one output at a time.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [July 10, 2017, 2:24pm UTC](https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742/4 "2017-07-10T14:24:11Z")

</div>

Yes, you can do that. I won’t claim that it’s pretty though.

---

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [September 4, 2017, 9:05pm UTC](https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742/5 "2017-09-04T21:05:11Z")

</div>

Is it still the case that matrix-vector products cannot be used in nonlinear constraints?

---

<div class="post-metadata">

**Author:** ![Gummala\_Navneeth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gummala_navneeth/32/35311_2.png) [@Gummala\_Navneeth](https://discourse.julialang.org/u/Gummala_Navneeth)\
**Post date:** [April 13, 2022, 3:49pm UTC](https://discourse.julialang.org/t/defining-non-linear-vector-constraints-in-jump/4742/6 "2022-04-13T15:49:54Z")

</div>

@acauligi, If you don’t mind . If you got any working solution to this problem, kindly provide it here. Thank you.
