Skip to content
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

[DNMY] Streamline Dispatch Variable Reference Structure #247

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/general_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,23 @@ julia> index(vref)
FiniteVariableIndex(1)
```
"""
function JuMP.index(vref::GeneralVariableRef)::AbstractInfOptIndex
index_type = _index_type(vref)
if index_type == DependentParameterIndex
return index_type(DependentParametersIndex(_raw_index(vref)),
_param_index(vref))
else
return index_type(_raw_index(vref))
function JuMP.index(vref::GeneralVariableRef)
return JuMP.index(vref.index_type, vref)
end

# Implement dispatch functions
for idx in (:IndependentParameterIndex, :FiniteParameterIndex,
:InfiniteVariableIndex, :SemiInfiniteVariableIndex,
:PointVariableIndex, :MeasureIndex, :DerivativeIndex,
:FiniteVariableIndex, :ParameterFunctionIndex)
@eval begin
JuMP.index(::Type{$idx}, vref::GeneralVariableRef) = $idx(vref.raw_index)
end
end
function JuMP.index(::Type{DependentParameterIndex}, vref::GeneralVariableRef)
return DependentParameterIndex(DependentParametersIndex(vref.raw_index),
vref.param_index)
end

"""
JuMP.index(vref::DispatchVariableRef)::AbstractInfOptIndex
Expand Down
2 changes: 2 additions & 0 deletions test/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct TestVariableRef <: DispatchVariableRef
index::TestIndex
end
InfiniteOpt.dispatch_variable_ref(m::InfiniteModel, i::TestIndex) = TestVariableRef(m, i)
JuMP.index(::Type{TestIndex}, v::GeneralVariableRef) = TestIndex(v.raw_index)

struct TestIndex2 <: ObjectIndex
value::Int
Expand All @@ -53,6 +54,7 @@ struct TestVariableRef2 <: DispatchVariableRef
index::TestIndex2
end
InfiniteOpt.dispatch_variable_ref(m::InfiniteModel, i::TestIndex2) = TestVariableRef2(m, i)
JuMP.index(::Type{TestIndex2}, v::GeneralVariableRef) = TestIndex2(v.raw_index)
JuMP.name(::TestVariableRef2) = "test"

InfiniteOpt.support_label(::TestGenMethod) = InternalLabel
Expand Down