Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JuMP.delete extension for link constraints #118

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/optiedge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make this mirror the node method here? you will need to define. It would be more consistent to delete the constraint from the edge. you till need to define _set_dirty(edge) as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also say don't delete the edge. it is okay to have edges with no constraints if the user wants to model that way. (we also don't have delete methods for nodes and edges yet anyways)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hesitant to allow deleting from a graph because more than one graph may contain the same edge. If we require users to specify the edge, it is more apparent (at least to me), that the constraint is deleted from all containing graphs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jalving I just pushed a change where I based it off of optielements now. I removed the JuMP.delete function I added for graphs and I removed the one for nodes and just replaced it with a general function that applies to OptiElements and added a _set_dirty method for edges. I think this function should do the same thing as the old JuMP.delete function that was specific for nodes, but now it works for edges too, which, if I understand correctly, is what you were suggesting above.


function JuMP.unregister(graph::OptiGraph, key::Symbol)
delete!(object_dictionary(graph), key)
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
4 changes: 4 additions & 0 deletions src/optinode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ function JuMP.delete(node::OptiNode, cref::ConstraintRef)
return nothing
end

function JuMP.unregister(node::OptiNode, key::Symbol)
delete!(object_dictionary(node), (node, key))
end

### Duals

"""
Expand Down
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
Loading