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

Expose JuMP.delete and JuMP.is_valid methods #1445

Merged
merged 5 commits into from
Aug 29, 2018
Merged

Expose JuMP.delete and JuMP.is_valid methods #1445

merged 5 commits into from
Aug 29, 2018

Conversation

odow
Copy link
Member

@odow odow commented Aug 27, 2018

As suggested by Miles.

I have prefixed JuMP.delete in the code to disambiguate Base.delete! as it was slightly confusing distinguishing between delete and delete!.

Maybe this is a good candidate to break from the style guide and extend a Base method?

There will be some conflicts with #1442

odow and others added 2 commits August 27, 2018 18:17
MOI.delete! and MOI.isvalid for JuMP variables and constraints.
@codecov
Copy link

codecov bot commented Aug 27, 2018

Codecov Report

Merging #1445 into master will increase coverage by 0.26%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/variables.jl 93.85% <100%> (+5.48%) ⬆️
src/JuMP.jl 77.37% <100%> (+0.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9835373...4495f2a. Read the comment docs.

Copy link
Member

@mlubin mlubin left a 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?

@@ -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
Copy link
Member

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.

@@ -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`
Copy link
Member

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?

Copy link
Member Author

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?
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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).

@mlubin
Copy link
Member

mlubin commented Aug 28, 2018

I see the issue with JuMP.delete vs. Base.delete!.
The docstring for Base.delete! says:

delete!(collection, key)

  Delete the mapping for the given key in a collection, and return the collection.

Can we plausibly say that this applies to deleting a variable from a JuMP model? Also and return the collection means that we should return the model?

@codecov
Copy link

codecov bot commented Aug 28, 2018

Codecov Report

Merging #1445 into master will increase coverage by <.01%.
The diff coverage is 57.14%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1445      +/-   ##
==========================================
+ Coverage   88.86%   88.87%   +<.01%     
==========================================
  Files          25       25              
  Lines        3692     3694       +2     
==========================================
+ Hits         3281     3283       +2     
  Misses        411      411
Impacted Files Coverage Δ
src/JuMP.jl 77.2% <100%> (ø) ⬆️
src/variables.jl 88.5% <50%> (+0.13%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9835373...c7e7e24. Read the comment docs.

@odow
Copy link
Member Author

odow commented Aug 28, 2018

Can we plausibly say that this applies to deleting a variable from a JuMP model?

Yes, but for consistency, I would prefer that the JuMP. prefix is for every user-facing JuMP function. It is a simple rule to remember.

@blegat
Copy link
Member

blegat commented Aug 28, 2018

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 Base.delete!

@odow
Copy link
Member Author

odow commented Aug 28, 2018

Okay, this diff coverage is now 100% so we're good to merge from my point of view.

@mlubin
Copy link
Member

mlubin commented Aug 29, 2018

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 Base.delete!. I agree we should be consistent between MOI and JuMP now that we have style guidelines.

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)
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Member

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.

@mlubin mlubin merged commit f5a9fe9 into master Aug 29, 2018
@mlubin mlubin deleted the od/delete branch August 29, 2018 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants