Examodels: Dealing with empty constraints and data

@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?

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

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

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:

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).

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:

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

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.