# Examodels: Dealing with empty constraints and data

**URL:** <https://discourse.julialang.org/t/examodels-dealing-with-empty-constraints-and-data/118669>\
**Category:** Optimization (Mathematical)\
**Created:** [August 27, 2024, 2:46pm UTC](https://discourse.julialang.org/t/examodels-dealing-with-empty-constraints-and-data/118669 "2024-08-27T14:46:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [August 27, 2024, 2:46pm UTC](https://discourse.julialang.org/t/examodels-dealing-with-empty-constraints-and-data/118669/1 "2024-08-27T14:46:57Z")

</div>

@sshin23 , @frapac

I am solving AC OPF with extended modeled features. In particular my issue is that I may have actively controlled transformers in data, but also there could be no actively controlled transformers and the code needs to accept both cases.

For simplicity lets consider the following bus balance constraint as it has less variables and parameters:  
`con_Pbusbalance1 = constraint(w, -v.pd - v.gs * V[v.i]^2 for v in [])`

Error message:

> ERROR: type ParSource has no field pd

On the other hand, variable declaration seems to support variables of 0 size (R^{0}).

Another issue is that once the code is prepared for CUDA, CuArray does not support CuArray([]) with error message:

> ERROR: CuArray only supports element types that are allocated inline.  
> Any is not allocated inline

What is your take on this, what is elegant way to approach this?

---

<div class="post-metadata">

**Author:** ![sshin23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sshin23/32/205225_2.png) [@sshin23](https://discourse.julialang.org/u/sshin23)\
**Post date:** [August 27, 2024, 5:16pm UTC](https://discourse.julialang.org/t/examodels-dealing-with-empty-constraints-and-data/118669/2 "2024-08-27T17:16:28Z")

</div>

Curious why is 0-length constraint necessary. Have you looked at `constraint!`? This allows you to modify the constraint by adding additional terms

> <https://github.com/exanauts/ExaModels.jl/blob/main/src/nlp.jl#L514-L539>

Sorry–I realized that this part of the code is not well documented:

---

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [August 27, 2024, 8:14pm UTC](https://discourse.julialang.org/t/examodels-dealing-with-empty-constraints-and-data/118669/3 "2024-08-27T20:14:31Z")

</div>

I have been using constraint!. It works fine and that is not my issue.

Zero length constraints serve as place holders so that code works regardless if there is such element in the network or not.

For example JuMP code will work:

```julia
using JuMP, Ipopt
model=Model(optimizer_with_attributes(Ipopt.Optimizer))
@variable(model,Pgen[g in []])
@constraint(model,pg[g in []], Pgen[g] <= 1 )

```

Similar will work in Ampl and GAMS too. I am just trying to find elegant way to have general model that will work regardless if there is element category present in the network or not (such as active transformer, active shut or HVDC present).

---

<div class="post-metadata">

**Author:** ![sshin23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sshin23/32/205225_2.png) [@sshin23](https://discourse.julialang.org/u/sshin23)\
**Post date:** [August 27, 2024, 8:26pm UTC](https://discourse.julialang.org/t/examodels-dealing-with-empty-constraints-and-data/118669/4 "2024-08-27T20:26:18Z")

</div>

Aha, I see.

ExaModels infer the structure of data from the type of the array. So, the array that is passed to constraint should have an explicit type of the data.

It’s a bit silly, but you can do:

```julia
data = [(pd = 0., gs = 0., i = 1) for i=1:0] # just to set eltype(data). still an empty array
con_Pbusbalance1 = constraint(w, -v.pd - v.gs * V[v.i]^2 for v in data)

```

This will also resolve the issue in creating `CuArray`

---

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [August 31, 2024, 9:25pm UTC](https://discourse.julialang.org/t/examodels-dealing-with-empty-constraints-and-data/118669/5 "2024-08-31T21:25:59Z")

</div>

Thank you for the reply. I did not get email notice that you wrote answer so I did not see it before.

I like your work around. Meantime I made my workaround too. Before using convert\_array, I deleted empty named tupple key using custom function (function for that was merged recently in Julia master, but is not in the release version) so that CuArray does not try to make CuArray([]). Then using if and haskey as switch for creating variables and constraints. The code is not very tidy, but it works.
