-
-
Notifications
You must be signed in to change notification settings - Fork 390
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
Expose JuMP.delete and JuMP.is_valid methods #1445
Conversation
MOI.delete! and MOI.isvalid for JuMP variables and constraints.
Codecov Report
@@ Coverage Diff @@
## master #1445 +/- ##
==========================================
+ Coverage 88.86% 89.13% +0.26%
==========================================
Files 25 25
Lines 3692 3700 +8
==========================================
+ Hits 3281 3298 +17
+ Misses 411 402 -9
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff has only 50% coverage, which isn't great. I think the delete_*
and unset_*
methods might not be tested?
docs/src/variables.md
Outdated
@@ -470,7 +470,21 @@ Dict{Symbol,Symmetric{JuMP.VariableRef,Array{JuMP.VariableRef,2}}} with 2 entrie | |||
|
|||
## Deleting variables | |||
|
|||
**TODO(@odow):** explain how to delete variables. | |||
Finally, JuMP supports the deletion of optimization variables. To delete |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Finally" won't age well if we move text around or add new sections.
docs/src/variables.md
Outdated
@@ -470,7 +470,21 @@ Dict{Symbol,Symmetric{JuMP.VariableRef,Array{JuMP.VariableRef,2}}} with 2 entrie | |||
|
|||
## Deleting variables | |||
|
|||
**TODO(@odow):** explain how to delete variables. | |||
Finally, JuMP supports the deletion of optimization variables. To delete | |||
variables, we can use the `JuMP.delete` method. We can also check whether `x` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when the variable that's deleted already appears in constraints?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Urgh. Solver-specific? At least in direct mode. What does the caching optimizer do? @blegat?
src/JuMP.jl
Outdated
Delete the constraint associated with `constraint_ref` from the model `model`. | ||
""" | ||
function delete(model::Model, constraint_ref::ConstraintRef{Model}) | ||
# TODO: should model be a parameter in the constraint_ref? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Model
is already a parameter in the constraint ref. Not sure what this TODO means, maybe just delete?
src/variables.jl
Outdated
Delete the variable associated with `variable_ref` from the model `model`. | ||
""" | ||
function delete(model::Model, variable_ref::VariableRef) | ||
@assert model === variable_ref.m |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer error messages over asserts for validating user-provided arguments.
src/variables.jl
Outdated
function delete_lower_bound(variable_ref::VariableRef) | ||
JuMP.delete(variable_ref.m, LowerBoundRef(variable_ref)) | ||
delete!(variable_ref.m.variable_to_lower_bound, index(variable_ref)) | ||
return nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: return
instead of return nothing
(also below).
I see the issue with
Can we plausibly say that this applies to deleting a variable from a JuMP model? Also |
Codecov Report
@@ Coverage Diff @@
## master #1445 +/- ##
==========================================
+ Coverage 88.86% 88.87% +<.01%
==========================================
Files 25 25
Lines 3692 3694 +2
==========================================
+ Hits 3281 3283 +2
Misses 411 411
Continue to review full report at Codecov.
|
Yes, but for consistency, I would prefer that the |
The decision we take for JuMP should be the same than for MOI. I don't see any different between the JuMP model/references and MOI model/indices cases. Except that we care more about user-friendliness with JuMP so that might be an argument for |
Okay, this diff coverage is now 100% so we're good to merge from my point of view. |
We're missing a style guide point on when it's appropriate to add methods to existing functions. I would say that it should be one concept per function, and you shouldn't just add a method because of the name. One way to check this is "can the new method be described by the existing docstring?" Deleting a variable from a model is arguably not the same as removing an item from a collection (models aren't iterable), so that's an argument for not using |
delete!(m.variables, vref.idx) | ||
delete!(m.varnames, vref.idx) | ||
function JuMP.delete(model::MyModel, variable_ref::MyVariableRef) | ||
@assert JuMP.is_valid(model, variable_ref) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Error message instead of assert
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I just made it an assert because it is a test for an extension. Should this be a style thing to never use asserts and always have an informative error message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using errors in JuMPExtension is maybe overkill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I got confused. It's fine to use this in a test.
As suggested by Miles.
I have prefixed
JuMP.delete
in the code to disambiguateBase.delete!
as it was slightly confusing distinguishing betweendelete
anddelete!
.Maybe this is a good candidate to break from the style guide and extend a Base method?
There will be some conflicts with #1442