-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable creation for Data
types
#11
Comments
I think this is a promising approach.
Could dispatching on the type itself for generating the (empty) containers using SparseVariables be a solution? That would allow someone writing an extension package to only exploit knowledge about which subtypes use the same variables/constraints. We would still need to check that the variable has not already been added, as multiple subtypes may define the same variables. I can't think of a way to avoid it completely, but at least we only need to do it once for each type. See stylized example below: # Simplified node structure for illustration
abstract type GenericNode end
struct ANode <: GenericNode
id
end
struct BNode <: GenericNode
id
val
end
# Methods to create (empty) containers based on the node (or link) types
function variables_extension(m, nt::Type{<:GenericNode}, N, T, P, modeltype)
return "add variable containers with SparseVariables"
end
function variables_extension(m, nt::Type{BNode}, N, T, P, modeltype)
return "add variable containers for `BNode` with SparseVariables"
end
# Methods to create variables for actual nodes
function variables_extension(m, n::GenericNode, N, T, P, modeltype)
return "create variables for generic nodes"
end
function variables_extension(m, n::BNode, N, T, P, modeltype)
return "create variables for `BNode`s"
end
# Simplified test with two node types
N = [ANode(1), ANode(2), BNode(3,9), BNode(4,3)]
m = nothing
T = 1:2; P = 1:2; modeltype = nothing
# First create containers
for nt ∈ unique(typeof.(N))
variables_extension(m, nt, N, T, P, modeltype)
end
# Then create actual variables where needed
for n ∈ N
variables_extension(m, n, N, T, P, modeltype)
end If instead of adding the variables (containers) directly, we had methods reporting what variables they would need for each type and then only generating unique ones could be a way, or would that be too complicated? E.g. report the name it wants to add for the type as a symbol along with the desired indices and then create them together at a later point? |
Regarding your two points:
However, your comment brings up a problem in my initial code I did not consider too much. Consider the following case in which we have an intermediate supertype: # Intermediate supertype
abstract type ExampleData <: Data end
# First composite type
struct ExampleDataA <: ExampleData
end
# Second composite type
struct ExampleDataB <: ExampleData
end If we want to define variables for all
Personally, I would prefer the second approach. This way, we reduce one point of error for users using the functionality as they do not have to consider the logic whether a variable name is already registered. |
So far, we did not have a functionality for adding variables for
Data
types except through dispatching on the model type. That may lead to issues with respect to method ambiguities. Hence, a new method is sought after.Introducing variables for
Data
types should follow the following three standards:Point 2 is the reason we cannot use the same approach as applied for
Node
types as we would in this case index over the data, and not the node.A potential solution for this problem, still utilizing the core structure developed for
Node
types, is outlined below. It requires the introduction of a functionvariables_data(m, 𝒩, 𝒯, 𝒫, modeltype)
to the functioncreate_model
given by:In addition, we have to create two additional functions:
The first function is used to create empty variable containers through the application of
SparseVariables
, while the second would only inserts the variables for the nodes that have the corresponding data.Another required change is to define the following function.
This function may be in general dangerous as it requires that if one wants to define a new composite subtype of
Availability
, one has to define as well the function for this type. However, the throught process is thatAvailability
nodes should not include thedata
field.The code runs with these functions added, but I have not tested it for additional variable creation. The function for looping through the types is however working. I have not added
SparseVariables
either, as it is not yet registered.The text was updated successfully, but these errors were encountered: