Skip to content

Commit

Permalink
Added delete extension for link constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcole3 committed Aug 21, 2024
1 parent c970324 commit cb2e92b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/optiedge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ 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",
)
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

### Edge Constraints

# NOTE: could use one method for node and edge
Expand Down
4 changes: 4 additions & 0 deletions src/optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1423,3 +1423,7 @@ function _set_objective_coefficient(
)
return nothing
end

function _set_dirty(graph::OptiGraph)
graph.is_model_dirty = true
end
18 changes: 18 additions & 0 deletions test/test_optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,24 @@ function test_nlp_exceptions()
@test_throws Exception @NLconstraint(graph, graph[1][:x]^3 >= 0)
end

function test_delete_extensions()
graph = OptiGraph()
@optinode(graph, nodes[1:2])

@variable(nodes[1], x >= 1)
@variable(nodes[2], x >= 2)
@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)
@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)
@test !(l_con in all_link_constraints(graph))
end

function run_tests()
for name in names(@__MODULE__; all=true)
if !startswith("$(name)", "test_")
Expand Down

0 comments on commit cb2e92b

Please sign in to comment.