I am seeking to better understand JuMP’s indexing functionality. As I understand it, when a set of decision variables or constraints in a container is created, JuMP automatically uses the elements of the set as the indexing key for these variables or constraints.
However, I am wondering if we could instruct JuMP to use a derived key instead. To illustrate, consider the following set up:
model = Model()
my_set = Set([MyStruct(1), MyStruct(2)])
@variable(model, x[my_set]) # here, elements of `my_set` serve as indices
What I would like to explore is the possibility of using an element’s internal id (or similar property) as an index:
JuMP.derived_key(o::MyStruct) = o.internal_id
@variable(model, y[my_set]) # here, we'd like indices to be based on internal id's
It would nevertheless be possible to do something like
derived_key(x) = x
# Collect iterators with unknown size so they can be used as axes.
_collect(::Base.SizeUnknown, x) = collect(derived_key.(x))
_collect(::Any, x) = derived_key.(x)