forked from plasmo-dev/Plasmo.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1 @@ | ||
""" | ||
JuMP.constraint_ref_with_index( | ||
element::OptiElement, | ||
idx::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction, <:MOI.AbstractScalarSet} | ||
) | ||
|
||
Return a `ConstraintRef` given an optigraph element and `MOI.ConstraintIndex`. | ||
Note that the index is the index corresponding to the graph backend, not the element index. | ||
""" | ||
function JuMP.constraint_ref_with_index( | ||
element::OptiElement, | ||
idx::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction, <:MOI.AbstractScalarSet} | ||
) | ||
return JuMP.constraint_ref_with_index(graph_backend(element), idx) | ||
end | ||
|
||
function JuMP.constraint_ref_with_index(element::OptiElement, idx::MOI.VariableIndex) | ||
return JuMP.constraint_ref_with_index(graph_backend(element), idx) | ||
end | ||
|
||
function JuMP.list_of_constraint_types(element::OptiElement)::Vector{Tuple{Type,Type}} | ||
# NOTE from JuMP: | ||
# We include an annotated return type here because Julia fails terribly at | ||
# inferring it, even though we annotate the type of the return vector. | ||
return Tuple{Type,Type}[ | ||
(JuMP.jump_function_type(element, F), S) for | ||
(F, S) in MOI.get(element, MOI.ListOfConstraintTypesPresent()) | ||
] | ||
end | ||
|
||
function JuMP.list_of_constraint_types(graph::OptiGraph)::Vector{Tuple{Type,Type}} | ||
all_constraint_types = JuMP.list_of_constraint_types.(all_elements(graph)) | ||
return unique(vcat(all_constraint_types...)) | ||
end | ||
|
||
### num_constraints | ||
|
||
""" | ||
JuMP.num_constraints( | ||
element::OptiElement, | ||
function_type::Type{ | ||
<:Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}}, | ||
},set_type::Type{<:MOI.AbstractSet})::Int64 | ||
Return the total number of constraints on an element. | ||
""" | ||
function JuMP.num_constraints( | ||
element::OptiElement, | ||
function_type::Type{ | ||
<:Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}}, | ||
}, | ||
set_type::Type{<:MOI.AbstractSet} | ||
)::Int64 | ||
F = JuMP.moi_function_type(function_type) | ||
return MOI.get(element, MOI.NumberOfConstraints{F,set_type}()) | ||
end | ||
|
||
function JuMP.num_constraints(graph::OptiGraph) | ||
all_num_constraints = JuMP.num_constraints.(all_elements(graph)) | ||
return sum(all_num_constraints) | ||
end | ||
|
||
### all_constraints | ||
|
||
function JuMP.all_constraints( | ||
element::OptiElement, | ||
func_type::Type{ | ||
<:Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}}, | ||
}, | ||
set_type::Type{<:MOI.AbstractSet}, | ||
) | ||
F = JuMP.moi_function_type(func_type) | ||
if set_type <: MOI.AbstractScalarSet | ||
constraint_ref_type = JuMP.ConstraintRef{ | ||
typeof(element), | ||
MOI.ConstraintIndex{F,set_type}, | ||
ScalarShape, | ||
} | ||
else | ||
constraint_ref_type = | ||
ConstraintRef{typeof(element),MOI.ConstraintIndex{F,set_type}} | ||
end | ||
result = constraint_ref_type[] | ||
for idx in MOI.get(element, MOI.ListOfConstraintIndices{F,set_type}()) | ||
push!(result, JuMP.constraint_ref_with_index(element, idx)) | ||
end | ||
return result | ||
end | ||
|
||
# return all of the constraints, including constraints in subgraphs | ||
function JuMP.all_constraints( | ||
graph::OptiGraph, | ||
func_type::Type{ | ||
<:Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}}, | ||
}, | ||
set_type::Type{<:MOI.AbstractSet}) | ||
|
||
all_graph_constraints = JuMP.all_constraints.( | ||
all_elements(graph), | ||
Ref(func_type), | ||
Ref(set_type) | ||
) | ||
return vcat(all_graph_constraints...) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# JuMP methods that dispatch on both optinodes and optiedges | ||
|
||
""" | ||
JuMP.constraint_ref_with_index( | ||
element::OptiElement, | ||
idx::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction, <:MOI.AbstractScalarSet} | ||
) | ||
Return a `ConstraintRef` given an optigraph element and `MOI.ConstraintIndex`. | ||
Note that the index is the index corresponding to the graph backend, not the element index. | ||
""" | ||
function JuMP.constraint_ref_with_index( | ||
element::OptiElement, | ||
idx::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction, <:MOI.AbstractScalarSet} | ||
) | ||
return JuMP.constraint_ref_with_index(graph_backend(element), idx) | ||
end | ||
|
||
function JuMP.constraint_ref_with_index(element::OptiElement, idx::MOI.VariableIndex) | ||
return JuMP.constraint_ref_with_index(graph_backend(element), idx) | ||
end | ||
|
||
function JuMP.list_of_constraint_types(element::OptiElement)::Vector{Tuple{Type,Type}} | ||
# NOTE from JuMP: | ||
# We include an annotated return type here because Julia fails terribly at | ||
# inferring it, even though we annotate the type of the return vector. | ||
return Tuple{Type,Type}[ | ||
(JuMP.jump_function_type(element, F), S) for | ||
(F, S) in MOI.get(element, MOI.ListOfConstraintTypesPresent()) | ||
] | ||
end | ||
|
||
""" | ||
JuMP.num_constraints( | ||
element::OptiElement, | ||
function_type::Type{ | ||
<:Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}}, | ||
},set_type::Type{<:MOI.AbstractSet})::Int64 | ||
Return the total number of constraints on an element. | ||
""" | ||
function JuMP.num_constraints( | ||
element::OptiElement, | ||
function_type::Type{ | ||
<:Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}}, | ||
}, | ||
set_type::Type{<:MOI.AbstractSet} | ||
)::Int64 | ||
F = JuMP.moi_function_type(function_type) | ||
return MOI.get(element, MOI.NumberOfConstraints{F,set_type}()) | ||
end | ||
|
||
function JuMP.all_constraints( | ||
element::OptiElement, | ||
func_type::Type{ | ||
<:Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}}, | ||
}, | ||
set_type::Type{<:MOI.AbstractSet}, | ||
) | ||
F = JuMP.moi_function_type(func_type) | ||
if set_type <: MOI.AbstractScalarSet | ||
constraint_ref_type = JuMP.ConstraintRef{ | ||
typeof(element), | ||
MOI.ConstraintIndex{F,set_type}, | ||
ScalarShape, | ||
} | ||
else | ||
constraint_ref_type = | ||
ConstraintRef{typeof(element),MOI.ConstraintIndex{F,set_type}} | ||
end | ||
result = constraint_ref_type[] | ||
for idx in MOI.get(element, MOI.ListOfConstraintIndices{F,set_type}()) | ||
push!(result, JuMP.constraint_ref_with_index(element, idx)) | ||
end | ||
return result | ||
end |
Oops, something went wrong.