Skip to content

Commit

Permalink
add topology tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Jun 30, 2024
1 parent 56550af commit 849d297
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/Plasmo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export OptiGraph, NodeVariableRef,

graph_backend, graph_index,

add_node, add_edge, add_subgraph, has_edge, get_edge,
add_node, get_node, add_edge, add_subgraph, has_edge, get_edge, get_edge_by_index,

collect_nodes, local_nodes, all_nodes, local_edges, all_edges,

Expand Down
4 changes: 2 additions & 2 deletions src/aggregate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ end

function GraphReferenceMap()
return GraphReferenceMap(
Dict{NodeVariableRef,NodeVariableRef}(),
Dict{JuMP.ConstraintRef,JuMP.ConstraintRef}(),
OrderedDict{NodeVariableRef,NodeVariableRef}(),
OrderedDict{JuMP.ConstraintRef,JuMP.ConstraintRef}(),
)
end
function Base.merge!(ref_map1::GraphReferenceMap, ref_map2::GraphReferenceMap)
Expand Down
7 changes: 2 additions & 5 deletions src/backends/moi_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,13 @@ Aggregate the moi backends from each subgraph within `graph` to create a single
# function _copy_subgraph_backends!(graph::OptiGraph)
function _copy_subgraph_backends!(backend::GraphMOIBackend)
graph = backend.optigraph
for subgraph in get_subgraphs(graph)
for subgraph in local_subgraphs(graph)
_copy_subgraph_nodes!(backend, subgraph)
_copy_subgraph_edges!(backend, subgraph)
# TODO: pass non-objective graph attributes (use an MOI Filter?)
# TODO: pass non-objective graph attributes we may need (use an MOI Filter?)
end
end

#function _copy_subgraph_nodes!(graph::OptiGraph, subgraph::OptiGraph)
function _copy_subgraph_nodes!(backend::GraphMOIBackend, subgraph::OptiGraph)
graph = backend.optigraph
for node in all_nodes(subgraph) # NOTE: hits ALL NODES in the subgraph.
Expand All @@ -619,7 +618,6 @@ function _copy_subgraph_nodes!(backend::GraphMOIBackend, subgraph::OptiGraph)
end
end

# function _copy_subgraph_edges!(graph::OptiGraph, subgraph::OptiGraph)
function _copy_subgraph_edges!(backend::GraphMOIBackend, subgraph::OptiGraph)
graph = backend.optigraph
for edge in all_edges(subgraph)
Expand All @@ -630,7 +628,6 @@ function _copy_subgraph_edges!(backend::GraphMOIBackend, subgraph::OptiGraph)
end
end

# function _append_node_to_backend!(graph::OptiGraph, node::OptiNode)
function _append_node_to_backend!(backend::GraphMOIBackend, node::OptiNode)
graph = backend.optigraph
_add_node(backend, node)
Expand Down
12 changes: 8 additions & 4 deletions src/graph_functions/topology.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function incident_edges(hyper::HyperGraphProjection, nodes::Vector{OptiNode})
hypernodes = Base.getindex.(Ref(hyper), nodes)
#hypernodes = get_mapped_elements(hyper, nodes)
inc_edges = GOI.incident_edges(hyper.projected_graph, hypernodes)
return get_mapped_elements(hyper, inc_edges) #getindex.(Ref(hyper), incidentedges))
return get_mapped_elements(hyper, inc_edges)
end

function incident_edges(hyper::HyperGraphProjection, node::OptiNode)
Expand All @@ -51,7 +51,7 @@ Retrieve induced edges to a set of optinodes.
function induced_edges(hyper::HyperGraphProjection, nodes::Vector{OptiNode})
hypernodes = get_mapped_elements(hyper, nodes)
induced = GOI.induced_edges(hyper.projected_graph, hypernodes)
optiedges = get_mapped_elements(hyper, induced)
optiedges = convert(Vector{OptiEdge}, get_mapped_elements(hyper, induced))
return optiedges
end

Expand Down Expand Up @@ -104,7 +104,7 @@ end
Return the optinodes within `distance` of the given `nodes` in the optigraph `graph`.
"""
function neighborhood(hyper::HyperGraphProjection, nodes::Vector{OptiNode}, distance::Int64)
function Graphs.neighborhood(hyper::HyperGraphProjection, nodes::Vector{OptiNode}, distance::Int64)
vertices = get_mapped_elements(hyper, nodes)
new_nodes = GOI.neighborhood(hyper.projected_graph, vertices, distance)
return get_mapped_elements(hyper, new_nodes) #getindex.(Ref(hyper), new_nodes)
Expand All @@ -118,7 +118,11 @@ The returned subgraph contains the expanded neighborhood within `distance` of th
"""
function expand(hyper::HyperGraphProjection, subgraph::OptiGraph, distance::Int64)
nodes = all_nodes(subgraph)
new_optinodes = neighborhood(hyper, nodes, distance)
return expand(hyper, nodes, distance)
end

function expand(hyper::HyperGraphProjection, nodes::Vector{OptiNode}, distance::Int64)
new_optinodes = Graphs.neighborhood(hyper, nodes, distance)
new_optiedges = induced_edges(hyper, new_optinodes)
expanded_subgraph = assemble_optigraph(new_optinodes, new_optiedges)
return expanded_subgraph
Expand Down
24 changes: 10 additions & 14 deletions src/optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Base.print(io::IO, graph::OptiGraph) = Base.print(io, Base.string(graph))
Base.show(io::IO, graph::OptiGraph) = Base.print(io, graph)

function Base.getindex(graph::OptiGraph, idx::Int)
return graph.optinodes[idx]
return collect(graph.optinodes)[idx]
end

Base.broadcastable(graph::OptiGraph) = Ref(graph)
Expand Down Expand Up @@ -134,7 +134,7 @@ function add_node(graph::OptiGraph, node::OptiNode)
end

function get_node(graph::OptiGraph, idx::Int)
return graph.optinodes[idx]
return collect(graph.optinodes)[idx]
end

"""
Expand Down Expand Up @@ -234,6 +234,14 @@ function get_edge(graph::OptiGraph, nodes::Set{OptiNode})
return graph.optiedge_map[nodes]
end

function get_edge(graph::OptiGraph, nodes::OptiNode...)
return get_edge(graph, Set(nodes))
end

function get_edge_by_index(graph::OptiGraph, idx::Int64)
return collect(graph.optiedges)[idx]
end

function local_edges(graph::OptiGraph)
return collect(graph.optiedges)
end
Expand Down Expand Up @@ -527,18 +535,6 @@ function JuMP.value(graph::OptiGraph, expr::GenericNonlinearExpr; result::Int =
end
end

### Expression values

# function JuMP.value(var_value::Function, ex::GenericAffExpr{T,V}) where {T,V}
# S = Base.promote_op(var_value, V)
# U = Base.promote_op(*, T, S)
# ret = convert(U, ex.constant)
# for (var, coef) in ex.terms
# ret += coef * var_value(var)
# end
# return ret
# end

### Constraints

function JuMP.add_constraint(
Expand Down
11 changes: 6 additions & 5 deletions test/test_aggregate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ function test_aggregate_solution()
graph = _create_test_optigraph()
agg_node, ref_map = aggregate(graph)

set_optimizer(graph, Ipopt.Optimizer)
set_optimizer(graph, optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))
optimize!(graph)

agg_graph = set_optimizer(agg_node, Ipopt.Optimizer)
agg_graph = set_optimizer(agg_node, optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))
optimize!(agg_graph)

@test objective_value(agg_graph) == objective_value(graph)
Expand All @@ -51,11 +51,12 @@ function test_set_model()
n1 = add_node(graph)
set_jump_model(n1, m)

set_optimizer(m, Ipopt.Optimizer)
set_optimizer(m, optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))
optimize!(m)

set_optimizer(n1, Ipopt.Optimizer)
optimize!(n1)
set_optimizer(graph, optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))
set_to_node_objectives(graph)
optimize!(graph)

@test objective_value(m) == objective_value(graph, n1)
@test value.(all_variables(m)) == value.(graph, all_variables(n1))
Expand Down
17 changes: 9 additions & 8 deletions test/test_optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module TestOptiGraph
using Plasmo
using Ipopt
using HiGHS
using Suppressor
using Test

function test_simple_graph()
Expand All @@ -15,7 +16,7 @@ function test_simple_graph()
@objective(graph, Max, nodes[1][:x] + 2*nodes[2][:x])

set_optimizer(graph, HiGHS.Optimizer)
optimize!(graph)
@suppress optimize!(graph)

@test objective_value(graph) == 7.0
@test value(nodes[1][:x]) == 1.0
Expand Down Expand Up @@ -173,8 +174,8 @@ function test_subgraphs()

sg1 = _create_test_nonlinear_optigraph()
sg2 = _create_test_nonlinear_optigraph()
add_subgraph!(graph, sg1)
add_subgraph!(graph, sg2)
add_subgraph(graph, sg1)
add_subgraph(graph, sg2)

n11,n12,n13,n14 = all_nodes(sg1)
n21,n22,n23,n24 = all_nodes(sg2)
Expand Down Expand Up @@ -285,25 +286,25 @@ function test_variable_constraints()

# fix variables
JuMP.fix(n1[:x], 1; force=true)
optimize!(graph)
@suppress optimize!(graph)
@test value(n1[:x]) == 1

JuMP.fix(n1[:x], 2)
optimize!(graph)
@suppress optimize!(graph)
@test value(n1[:x]) == 2

JuMP.fix(n1[:x], 0)
optimize!(graph)
@suppress optimize!(graph)
@test value(n1[:x]) == 0

# integer and binary
set_binary(n1[:x])
@test is_binary(n1[:x]) == true
optimize!(graph)
@suppress optimize!(graph)

set_integer(n2[:x])
@test is_integer(n2[:x]) == true
optimize!(graph)
@suppress optimize!(graph)
end

function test_nonlinear_operators()
Expand Down
Loading

0 comments on commit 849d297

Please sign in to comment.