Skip to content

Commit

Permalink
start adding edge copy
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Jan 12, 2024
1 parent 2d8f2d5 commit 92a16a5
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 97 deletions.
24 changes: 12 additions & 12 deletions src/jump_interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function JuMP.GenericAffExpr{C,NodeVariableRef}(
) where {C}
aff = GenericAffExpr{C,NodeVariableRef}(f.constant)
for t in f.terms
node_var_index = graph_backend(node).graph_to_node_map[t.variable].index
node_var_index = graph_backend(node).graph_to_element_map[t.variable].index
JuMP.add_to_expression!(
aff,
t.coefficient,
Expand All @@ -86,7 +86,7 @@ function JuMP.GenericAffExpr{C,NodeVariableRef}(
aff = GenericAffExpr{C,NodeVariableRef}(f.constant)
# build JuMP Affine Expression over edge variables
for t in f.terms
node_var = graph_backend(edge).graph_to_node_map[t.variable]
node_var = graph_backend(edge).graph_to_element_map[t.variable]
node = node_var.node
node_index = node_var.index
JuMP.add_to_expression!(
Expand Down Expand Up @@ -114,7 +114,7 @@ function JuMP.GenericAffExpr{C,NodeVariableRef}(
# build JuMP Affine Expression over func variables
for t in f.terms
gb = graph_backend(graph)
node_var = gb.graph_to_node_map[t.variable]
node_var = gb.graph_to_element_map[t.variable]
node = node_var.node
node_index = node_var.index
JuMP.add_to_expression!(
Expand Down Expand Up @@ -175,8 +175,8 @@ function JuMP.GenericQuadExpr{C,NodeVariableRef}(
),
)
for t in f.quadratic_terms
v1 = graph_backend(node).graph_to_node_map[t.variable_1].index
v2 = graph_backend(node).graph_to_node_map[t.variable_2].index
v1 = graph_backend(node).graph_to_element_map[t.variable_1].index
v2 = graph_backend(node).graph_to_element_map[t.variable_2].index
coef = t.coefficient
if v1 == v2
coef /= 2
Expand Down Expand Up @@ -223,12 +223,12 @@ function JuMP.GenericQuadExpr{C,NodeVariableRef}(
end

# variable index 1
node_var_1 = graph_backend(edge).graph_to_node_map[v1]
node_var_1 = graph_backend(edge).graph_to_element_map[v1]
node1 = node_var_1.node
var_index_1 = node_var_1.index

# variable index 2
node_var_2 = graph_backend(edge).graph_to_node_map[v2]
node_var_2 = graph_backend(edge).graph_to_element_map[v2]
node2 = node_var_2.node
var_index_2 = node_var_2.index

Expand Down Expand Up @@ -275,12 +275,12 @@ function JuMP.GenericQuadExpr{C,NodeVariableRef}(
end

# variable index 1
node_var_1 = gb.graph_to_node_map[v1]
node_var_1 = gb.graph_to_element_map[v1]
node1 = node_var_1.node
var_index_1 = node_var_1.index

# variable index 2
node_var_2 = gb.graph_to_node_map[v2]
node_var_2 = gb.graph_to_element_map[v2]
node2 = node_var_2.node
var_index_2 = node_var_2.index

Expand All @@ -305,7 +305,7 @@ JuMP.jump_function(::OptiNode, x::Number) = convert(Float64, x)

function JuMP.jump_function(node::OptiNode, vidx::MOI.VariableIndex)
gb = graph_backend(node)
node_var = gb.graph_to_node_map[vidx]
node_var = gb.graph_to_element_map[vidx]
return NodeVariableRef(node, node_var.index)
end

Expand Down Expand Up @@ -337,7 +337,7 @@ JuMP.variable_ref_type(::Type{OptiEdge{OptiGraph}}) = NodeVariableRef
JuMP.jump_function(::OptiEdge, x::Number) = convert(Float64, x)

function JuMP.jump_function(edge::OptiEdge, vidx::MOI.VariableIndex)
node_var = graph_backend(edge).graph_to_node_map[vidx]
node_var = graph_backend(edge).graph_to_element_map[vidx]
node = node_var.node
node_idx = node_var.index
return NodeVariableRef(node, node_idx)
Expand Down Expand Up @@ -371,7 +371,7 @@ JuMP.variable_ref_type(::Type{OptiGraph}) = NodeVariableRef
JuMP.jump_function(::OptiGraph, x::Number) = convert(Float64, x)

function JuMP.jump_function(graph::OptiGraph, vidx::MOI.VariableIndex)
node_var = graph_backend(graph).graph_to_node_map[vidx]
node_var = graph_backend(graph).graph_to_element_map[vidx]
node = node_var.node
node_idx = node_var.index
return NodeVariableRef(node, node_idx)
Expand Down
41 changes: 31 additions & 10 deletions src/moi_aggregate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ Aggregate the moi backends from each subgraph within `graph` to create a single
"""
function aggregate_backends!(graph::OptiGraph)
for subgraph in get_subgraphs(graph)
_aggregate_subgraph_nodes!(subgraph)
_aggregate_subgraph_edges!(subgraph)
_aggregate_subgraph_nodes!(graph, subgraph)
# _aggregate_subgraph_edges!(subgraph)
end
end

function _aggregate_subgraph_nodes!(graph::OptiGraph)
for node in all_nodes(graph)
function _aggregate_subgraph_nodes!(graph::OptiGraph, subgraph::OptiGraph)
for node in all_nodes(subgraph)
_append_node_to_backend!(graph, node)
end
end

function _aggregate_subgraph_edges!(graph::OptiGraph, subgraph::OptiGraph)
for edges in all_edges(subgraph)
_append_edge_to_backend!(graph, node)
end
end

function _append_node_to_backend!(graph::OptiGraph, node::OptiNode)
src = graph_backend(node)
dest = graph_backend(graph)
Expand Down Expand Up @@ -50,7 +56,7 @@ function _append_node_to_backend!(graph::OptiGraph, node::OptiNode)
nonvariable_constraint_types
)

# TODO: pass non-objective attributes (use MOI Filter?)
# TODO: pass non-objective graph attributes (use an MOI Filter?)

return
end
Expand All @@ -64,15 +70,15 @@ function _copy_node_variables(
node_variables = all_variables(node)

# map existing variables in index_map
existing_vars = intersect(node_variables, keys(dest.node_to_graph_map.var_map))
existing_vars = intersect(node_variables, keys(dest.element_to_graph_map.var_map))
for var in existing_vars
src_graph_index = graph_index(var)
dest_graph_index = dest.node_to_graph_map[var]
dest_graph_index = dest.element_to_graph_map[var]
index_map[src_graph_index] = dest_graph_index
end

# create and add new variables
vars_to_add = setdiff(node_variables, keys(dest.node_to_graph_map.var_map))
vars_to_add = setdiff(node_variables, keys(dest.element_to_graph_map.var_map))
for var in vars_to_add
src_graph_index = graph_index(var)
dest_graph_index = _add_variable_to_backend(dest, var)
Expand All @@ -85,7 +91,6 @@ function _copy_node_variables(
return
end

# TODO: update graph backend mappings
function _copy_node_constraints(
dest::GraphMOIBackend,
node::OptiNode,
Expand Down Expand Up @@ -129,7 +134,8 @@ function _copy_node_constraints(
for ci in cis_src
f = MOI.get(src.moi_backend, MOI.ConstraintFunction(), ci)
s = MOI.get(src.moi_backend, MOI.ConstraintSet(), ci)
cref = src.graph_to_node_map[ci]
cref = src.graph_to_element_map[ci]
cref in keys(dest.element_to_graph_map.con_map) && return
dest_index = _add_node_constraint_to_backend(
dest,
cref,
Expand All @@ -139,4 +145,19 @@ function _copy_node_constraints(
index_map_FS[ci] = dest_index
end
return
end

function _append_edge_to_backend!(graph::OptiGraph, edge::OptiEdge)
src = graph_backend(edge)
dest = graph_backend(graph)
index_map = MOIU.IndexMap()

constraint_types = MOI.get(edge, MOI.ListOfConstraintTypesPresent())
_copy_edge_constraints(
dest,
edge,
index_map,
constraint_types
)
return
end
Loading

0 comments on commit 92a16a5

Please sign in to comment.