Skip to content

Commit

Permalink
refactored JuMP.delete methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcole3 committed Aug 22, 2024
1 parent e5af7d5 commit adc32a8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 35 deletions.
23 changes: 4 additions & 19 deletions src/optiedge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,11 @@ function JuMP.all_variables(edge::OptiEdge)
return unique(vars)
end

function JuMP.delete(graph::OptiGraph, cref::ConstraintRef)
if typeof(JuMP.owner_model(cref)) == OptiNode{OptiGraph}
error(
"You have passed a node constraint but specified an OptiGraph." *
"Use `JuMP.delete(::OptiNode, ::ConstraintRef)` instead",
)
end
if graph !== source_graph(JuMP.owner_model(cref))
error(
"The constraint reference you are trying to delete does not" *
"belong to the specified graph",
)
function _set_dirty(edge::OptiEdge)
for graph in containing_optigraphs(edge)
graph.is_model_dirty = true
end
_set_dirty(graph)
MOI.delete(JuMP.owner_model(cref), cref)
#TODO: Probably need to delete the edge altogether if it is the only constraint on the edge
end

function JuMP.unregister(graph::OptiGraph, key::Symbol)
delete!(object_dictionary(graph), key)
return nothing
end

### Edge Constraints
Expand Down
12 changes: 12 additions & 0 deletions src/optielement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ function JuMP.all_constraints(
end
return constraints
end

function JuMP.delete(element::OptiElement, cref::ConstraintRef)
if element !== JuMP.owner_model(cref)
error(
"The constraint reference you are trying to delete does not " *
"belong to the model.",
)
end
_set_dirty(element)
MOI.delete(element, cref)
return nothing
end
4 changes: 2 additions & 2 deletions src/optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,6 @@ function _set_objective_coefficient(
return nothing
end

function _set_dirty(graph::OptiGraph)
graph.is_model_dirty = true
function JuMP.unregister(graph::OptiGraph, key::Symbol)
delete!(object_dictionary(graph), key)
end
12 changes: 0 additions & 12 deletions src/optinode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,6 @@ function JuMP.all_variables(node::OptiNode)
return NodeVariableRef.(Ref(node), var_inds)
end

function JuMP.delete(node::OptiNode, cref::ConstraintRef)
if node !== JuMP.owner_model(cref)
error(
"The constraint reference you are trying to delete does not " *
"belong to the model.",
)
end
_set_dirty(node)
MOI.delete(node, cref)
return nothing
end

function JuMP.unregister(node::OptiNode, key::Symbol)
delete!(object_dictionary(node), (node, key))
end
Expand Down
6 changes: 4 additions & 2 deletions test/test_optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,14 @@ function test_delete_extensions()
@constraint(nodes[1], n_con, nodes[1][:x]^2 >= 1)
@linkconstraint(graph, l_con, nodes[1][:x] + nodes[2][:x] == 4)

@test_throws ErrorException JuMP.delete(graph, n_con)
edge = JuMP.owner_model(l_con)

@test_throws ErrorException JuMP.delete(edge, n_con)
@test_throws ErrorException JuMP.delete(nodes[1], l_con)

JuMP.delete(nodes[1], n_con)
@test !(n_con in all_constraints(nodes[1]))
JuMP.delete(graph, l_con)
JuMP.delete(edge, l_con)
@test !(l_con in all_link_constraints(graph))
end

Expand Down

0 comments on commit adc32a8

Please sign in to comment.