Skip to content

Commit

Permalink
support variable-constrain and parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Oct 30, 2024
1 parent 13f130b commit 3b7d691
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 86 deletions.
6 changes: 5 additions & 1 deletion src/Plasmo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export OptiGraph,
OptiNode,
OptiEdge,
NodeVariableRef,
EdgeConstraintRef,
direct_moi_graph,
graph_backend,
graph_index,
Expand Down Expand Up @@ -108,7 +109,8 @@ export OptiGraph,

# other functions

set_jump_model
set_jump_model,
extract_variables

include("core_types.jl")

Expand Down Expand Up @@ -138,6 +140,8 @@ include("graph_functions/topology.jl")

include("graph_functions/partition.jl")

include("utilities.jl")

# extensions
function __init__()
@require KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880" include(
Expand Down
49 changes: 41 additions & 8 deletions src/backends/moi_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -528,22 +528,54 @@ end
# MOI variables and constraints
#

function MOI.add_variable(graph_backend::GraphMOIBackend, vref::NodeVariableRef)
function MOI.add_variable(backend::GraphMOIBackend, vref::NodeVariableRef)
# return if variable already exists in backend
vref in keys(graph_backend.element_to_graph_map.var_map) && return nothing
vref in keys(backend.element_to_graph_map.var_map) && return nothing

# add the variable
graph_var_index = MOI.add_variable(graph_backend.moi_backend)
graph_var_index = MOI.add_variable(backend.moi_backend)

# map reference to index
graph_backend.element_to_graph_map[vref] = graph_var_index
graph_backend.graph_to_element_map[graph_var_index] = vref
backend.element_to_graph_map[vref] = graph_var_index
backend.graph_to_element_map[graph_var_index] = vref

# create key for node if necessary
if !haskey(graph_backend.node_variables, vref.node)
graph_backend.node_variables[vref.node] = MOI.VariableIndex[]
if !haskey(backend.node_variables, vref.node)
backend.node_variables[vref.node] = MOI.VariableIndex[]
end
push!(graph_backend.node_variables[vref.node], graph_var_index)
push!(backend.node_variables[vref.node], graph_var_index)
return graph_var_index
end

function MOI.add_constrained_variable(
backend::GraphMOIBackend,
vref::NodeVariableRef,
cref::NodeConstraintRef,
set::MOI.AbstractScalarSet,
)
# return if variable already exists in backend
vref in keys(backend.element_to_graph_map.var_map) && return nothing

# add the variable and parameter constraint
graph_var_index, graph_con_index = MOI.add_constrained_variable(
backend.moi_backend, set
)

# map reference to index
backend.element_to_graph_map[vref] = graph_var_index
backend.graph_to_element_map[graph_var_index] = vref
backend.element_to_graph_map[cref] = graph_con_index
backend.graph_to_element_map[graph_con_index] = cref

# create key for node if necessary
if !haskey(backend.node_variables, vref.node)
backend.node_variables[vref.node] = MOI.VariableIndex[]
end
if !haskey(backend.element_constraints, vref.node)
graph_backend.element_constraints[vref.node] = MOI.ConstraintIndex[]
end
push!(backend.node_variables[vref.node], graph_var_index)
push!(backend.element_constraints[vref.node], graph_con_index)
return graph_var_index
end

Expand Down Expand Up @@ -879,6 +911,7 @@ function _copy_node_variables(

# map existing variables in the index_map
# existing variables may come from linking constraints added between graphs
# TODO: could be slow...
existing_vars = intersect(node_variables, keys(dest.element_to_graph_map.var_map))
for var in existing_vars
src_graph_index = graph_index(var)
Expand Down
2 changes: 0 additions & 2 deletions src/core_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ struct ElementData{GT<:AbstractOptiGraph}
# track constraint indices
last_constraint_index::OrderedDict{OptiElement,Int}
end

# default is OptiGraph
function ElementData(GT::Type{<:AbstractOptiGraph})
return ElementData{GT}(
OrderedDict{OptiNode{GT},Vector{GT}}(),
Expand Down
Loading

0 comments on commit 3b7d691

Please sign in to comment.