-
Notifications
You must be signed in to change notification settings - Fork 87
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
Universal constraint fallback #415
Conversation
Ready for review :) |
Codecov Report
@@ Coverage Diff @@
## master #415 +/- ##
==========================================
+ Coverage 95.55% 95.78% +0.22%
==========================================
Files 40 40
Lines 5088 5315 +227
==========================================
+ Hits 4862 5091 +229
+ Misses 226 224 -2
Continue to review full report at Codecov.
|
src/Utilities/model.jl
Outdated
removevariable(f::MOI.AbstractFunction, s::MOI.AbstractSet, vi::MOI.VariableIndex) | ||
|
||
Return a tuple `(g, t)` representing the constraint `f`-in-`s` with the | ||
variable of index `vi` removed. |
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.
Unclear what it means to remove a variable from a constraint. Could you clarify?
src/Utilities/model.jl
Outdated
@@ -137,16 +142,48 @@ end | |||
MOI.canget(model::AbstractModel, ::MOI.Name) = true | |||
MOI.get(model::AbstractModel, ::MOI.Name) = model.name | |||
|
|||
""" | |||
newname(model::MOI.ModelLike, IdxT::Type{<:MOI.Index}, idx::MOI.Index, name::String) |
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: IdxT
could be IndexType
. Another question for the style guide is whether we prefer idx
or index
.
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.
Indeed, it would be nice to have a convention for that
src/Utilities/model.jl
Outdated
end | ||
|
||
""" | ||
setname(idxnames::Dict{<:MOI.Index, String}, namesidx::Dict{String, <:MOI.Index}, idx::MOI.Index, name::String, idxtype::Symbol) |
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.
Naming suggestion: index_to_name
and name_to_index
src/Utilities/model.jl
Outdated
setname(idxnames::Dict{<:MOI.Index, String}, namesidx::Dict{String, <:MOI.Index}, idx::MOI.Index, name::String, idxtype::Symbol) | ||
|
||
Sets the name of the index `idx` to the name `name` in the maps `idxnames` and `namesidx`. | ||
If the name is already taken by an index different from `idx` then an error is thrown usin g `idxtype` to create a custom 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.
Typo: "usin g" -> "using"
src/Utilities/model.jl
Outdated
end | ||
function _removevar!(constrs::Vector, vi::VI) | ||
for i in eachindex(constrs) | ||
constrs[i] = _removevar(constrs[i]..., vi) | ||
ci, f, s = constrs[i] | ||
constrs[i] = (ci, removevariable(f, s, vi)...) | ||
end | ||
[] |
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: since you're touching this function, please add a return statement.
src/Utilities/model.jl
Outdated
""" | ||
newname(model::MOI.ModelLike, IndexType::Type{<:MOI.Index}, idx::MOI.Index, name::String) | ||
|
||
Return a `Bool` indicating whether `name` is not already used by an index of |
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 negative is confusing in this description. Maybe say "Returns a Bool
indicating whether name
is available to be used by an index of type IndexType
(i.e., it's not already used)."
src/Utilities/model.jl
Outdated
@@ -137,16 +145,48 @@ end | |||
MOI.canget(model::AbstractModel, ::MOI.Name) = true | |||
MOI.get(model::AbstractModel, ::MOI.Name) = model.name | |||
|
|||
""" | |||
newname(model::MOI.ModelLike, IndexType::Type{<:MOI.Index}, idx::MOI.Index, name::String) |
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.
check_can_assign_name
maybe? It's a bit confusing why this method should throw an error. What about returning 1 of 3 possible statuses: NameUnassigned
, NameAssignedAndMatches
, and NameMismatch
?
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.
If it does not throw an error, there won't be much code to share between the three places where this is used since we will have to branch on these three statuses. If we make typos in these branches we will introduce bugs and inconsistencies between the three places
src/Utilities/model.jl
Outdated
setname(index_to_names::Dict{<:MOI.Index, String}, names_to_index::Dict{String, <:MOI.Index}, idx::MOI.Index, name::String, idxtype::Symbol) | ||
|
||
Sets the name of the index `idx` to the name `name` in the maps `index_to_names` and `names_to_index`. | ||
If the name is already taken by an index different from `idx` then an error is thrown using `idxtype` to create a custom 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.
I'm looking at the code and don't see where the error is thrown.
src/Utilities/universalfallback.jl
Outdated
critical constraints and attributes while still supporting other attributes | ||
with a small performance penalty. Note that `model` is unaware of constraints | ||
and attributes stored by `UniversalFallback` so this is not appropriate if | ||
`model` is an optimizer (for this reason, `optimize!` have not been |
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: optimize!
has not been implemented
src/Utilities/universalfallback.jl
Outdated
@@ -76,6 +127,29 @@ function MOI.get(uf::UniversalFallback, attr::Union{MOI.AbstractVariableAttribut | |||
end | |||
end | |||
|
|||
function MOI.get(uf::UniversalFallback, attr::MOI.NumberOfConstraints{F, S}) where {F, S} | |||
if MOI.supportsconstraint(uf.model, F, S) | |||
MOI.get(uf.model, attr) |
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: use return
statements in these functions
src/Utilities/universalfallback.jl
Outdated
end | ||
function MOI.get(uf::UniversalFallback, ::Type{CI}, name::String) | ||
if MOI.canget(uf.model, CI, name) | ||
MOI.get(uf.model, CI, name) |
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 statements
src/Utilities/universalfallback.jl
Outdated
MOI.canset(uf.model, attr, C) | ||
function MOI.canaddconstraint(uf::UniversalFallback, ::Type{F}, ::Type{S}) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet} | ||
if MOI.supportsconstraint(uf.model, F, S) | ||
MOI.canaddconstraint(uf.model, F, S) |
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.
Return statements
@@ -64,8 +65,12 @@ end | |||
|
|||
struct UnknownOptimizerAttribute <: MOI.AbstractOptimizerAttribute end | |||
|
|||
# A few constraint types are supported to test both the fallback and the |
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: "A few" -> "Few"
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.
To my Kiwi English, it seems weird to say "Few" here. To me, "A few" reads as "we've only implemented a couple of constraint types to test the functionality" whereas "Few" has a negative connotation and means "Not enough constraint types are supported".
SO suggests the following example:
- I have few friends.
- I have a few friends.
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 suggested "few" because I understood that the comment was trying to say that the model supports fewer constraint types than usual so that it can test the fallback.
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 meant "A few" in opposition of none otherwise only the fallback is tested
Closes #397
Closes jump-dev/JuMP.jl#1152