# How to add a constraint with neural network in a JuMP (PowerModels) model

**URL:** <https://discourse.julialang.org/t/how-to-add-a-constraint-with-neural-network-in-a-jump-powermodels-model/64593>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [July 13, 2021, 4:48pm UTC](https://discourse.julialang.org/t/how-to-add-a-constraint-with-neural-network-in-a-jump-powermodels-model/64593 "2021-07-13T16:48:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oli1119](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oli1119/32/27102_2.png) [@oli1119](https://discourse.julialang.org/u/oli1119)\
**Post date:** [July 13, 2021, 4:48pm UTC](https://discourse.julialang.org/t/how-to-add-a-constraint-with-neural-network-in-a-jump-powermodels-model/64593/1 "2021-07-13T16:48:28Z")

</div>

Hello,

I want to create a constraint: a variable _var1_ is equal to the output of a torch artificial neural network whose input is another variable _var2_.

------- This following function is my ann prediction (the ANN has been imported) -----

```julia
function get_y_pred(ann, x, x_scl, y_scl) 
# x_scl and y_scl are scaler to modify the input x and output y.
    x_std = x_scl.transform(x)
    x_std = V.Variable(torch.from_numpy(x_std).float())
    y_std = ann(x_std).data.numpy()
    y_pred = y_scl.inverse_transform(y_std)
    return y_pred
end 

```

The input x should be a matrix containing _var2_ and some constants, e.g.,

```julia
x = [1.0, 0.6, var2, 0.8] # Vector
x = reshape(x, 1, length(x)) # Vector to Matrix

```

now I try to add a constraint for _var1_, i.e., during the optimization _var1_ should be adaptively changed by the ANN.

```julia
@addNLconstraint(model, var1 == get_y_pred(ann, x, x_scl, y_scl))

```

but it does not work, because I can not create a constraint by using a self-defined function or expression ( **get\_y\_pred** ) with ANN.

Can anyone help me with finding a way to solve this problem? Thank you very much!

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [July 13, 2021, 6:14pm UTC](https://discourse.julialang.org/t/how-to-add-a-constraint-with-neural-network-in-a-jump-powermodels-model/64593/2 "2021-07-13T18:14:16Z")

</div>

Have you tried defining an expression whose value is the output of your `get_y_pred` function and then using your expression in the `NLconstraint`?

---

<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:** [July 13, 2021, 10:11pm UTC](https://discourse.julialang.org/t/how-to-add-a-constraint-with-neural-network-in-a-jump-powermodels-model/64593/3 "2021-07-13T22:11:48Z")

</div>

Hi there, since it’s your first post, please read [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). It’s easier to help if you provide a complete minimal working example.

However, there’s some scary stuff in your example that are going to cause issues.

- I don’t think your `get_y_pred` is going to support automatic differentiation. That means you can’t use JuMP to solve this problem. Please read: [https://jump.dev/JuMP.jl/stable/background/should\_i\_use/#Black-box,-derivative-free,-or-unconstrained-optimization](https://jump.dev/JuMP.jl/stable/background/should_i_use/#Black-box,-derivative-free,-or-unconstrained-optimization)
- `@addNLconstraint` is very old syntax. Are you running the latest version of Julia and JuMP?

---

<div class="post-metadata">

**Author:** ![oli1119](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oli1119/32/27102_2.png) [@oli1119](https://discourse.julialang.org/u/oli1119)\
**Post date:** [July 14, 2021, 8:12am UTC](https://discourse.julialang.org/t/how-to-add-a-constraint-with-neural-network-in-a-jump-powermodels-model/64593/4 "2021-07-14T08:12:38Z")

</div>

hi,  
yes, I tried, it also doesn’t work.  
I think, if I use the function `get_y_pred`, I have to give `x` a specific matrix, not a matrix containing the JuMP variable `var2`. Because there is an ANN in the function `get_y_pred`. Its input has to be actual values.

The `x` containing `var2` is a 1x4 Martix{ **AffExpr** }. The input the function `get_y_pred` really needs should be a 1x4 Martix{ **Float64** }.

---

<div class="post-metadata">

**Author:** ![oli1119](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oli1119/32/27102_2.png) [@oli1119](https://discourse.julialang.org/u/oli1119)\
**Post date:** [July 14, 2021, 8:14am UTC](https://discourse.julialang.org/t/how-to-add-a-constraint-with-neural-network-in-a-jump-powermodels-model/64593/5 "2021-07-14T08:14:16Z")

</div>

Thank you for the suggestions.
