4
4
Structure for holding an optimization variable with a sparse structure with extra indexing
5
5
"""
6
6
struct IndexedVarArray{N,T} <: AbstractSparseArray{VariableRef,N}
7
- model:: Model
8
- name:: Any
7
+ f:: Function
9
8
data:: Dictionary{T,VariableRef}
10
9
index_names:: NamedTuple
11
10
index_cache:: Vector{Dictionary}
@@ -22,8 +21,7 @@ function IndexedVarArray(
22
21
N = length (fieldtypes (Ts))
23
22
dict = Dictionary {T,VariableRef} ()
24
23
return model[Symbol (name)] = IndexedVarArray {N,T} (
25
- model,
26
- name,
24
+ (ix... ) -> createvar (model, name, ix; lower_bound, kw_args... ),
27
25
dict,
28
26
index_names,
29
27
Vector {Dictionary} (undef, 2 ^ N),
@@ -46,8 +44,7 @@ function IndexedVarArray(
46
44
(createvar (model, name, k; lower_bound, kw_args... ) for k in indices),
47
45
)
48
46
return model[Symbol (name)] = IndexedVarArray {N,T} (
49
- model,
50
- name,
47
+ (ix... ) -> createvar (model, name, ix; lower_bound, kw_args... ),
51
48
dict,
52
49
index_names,
53
50
Vector {Dictionary} (undef, 2 ^ N),
@@ -109,7 +106,7 @@ function insertvar!(
109
106
! valid_index (var, index) && throw (BoundsError (var, index))# "Not a valid index for $(var.name): $index"g
110
107
already_defined (var, index) && error (" $(var. name) : $index already defined" )
111
108
112
- var[index] = createvar ( var. model, var . name, index; lower_bound, kw_args ... )
109
+ var[index] = var. f ( index... )
113
110
114
111
clear_cache! (var)
115
112
return var[index]
@@ -127,19 +124,7 @@ function unsafe_insertvar!(
127
124
lower_bound = 0 ,
128
125
kw_args... ,
129
126
) where {N,T}
130
- return var[index] =
131
- createvar (var. model, var. name, index; lower_bound, kw_args... )
132
-
133
- # TODO : Reactivate this later
134
- # If active caches, update with new variable
135
- # cache = _getcache(var.sa, index)
136
- # for ind in keys(cache)
137
- # vred = Tuple(val for (i, val) in enumerate(index) if i in ind)
138
- # if !(vred in keys(var.index_cache[ind]))
139
- # var.index_cache[ind][vred] = []
140
- # end
141
- # push!(var.index_cache[ind][vred], index)
142
- # end
127
+ return var[index] = var. f (index... )
143
128
end
144
129
145
130
joinex (ex1, ex2) = :($ ex1... , $ ex2... )
@@ -251,3 +236,27 @@ function _getcache(sa::IndexedVarArray{N,T}, pat::P) where {N,T,P}
251
236
end
252
237
return sa. index_cache[t]
253
238
end
239
+
240
+ # Extension for standard JuMP macros
241
+ function Containers. container (
242
+ f:: Function ,
243
+ indices,
244
+ D:: Type{IndexedVarArray} ,
245
+ names,
246
+ )
247
+ iva_names = NamedTuple {tuple(names...)} (indices. prod. iterators)
248
+ T = Tuple{eltype .(indices. prod. iterators)... }
249
+ N = length (names)
250
+ return IndexedVarArray {N,T} (
251
+ f,
252
+ Dictionary {T,VariableRef} (),
253
+ iva_names,
254
+ Vector {Dictionary} (undef, 2 ^ N),
255
+ )
256
+ end
257
+
258
+ # Fallback when no names are provided
259
+ function Containers. container (f:: Function , indices, D:: Type{IndexedVarArray} )
260
+ index_vars = Symbol .(" i$i " for i in 1 : length (indices. prod. iterators))
261
+ return Containers. container (f, indices, D, index_vars)
262
+ end
0 commit comments