-
Notifications
You must be signed in to change notification settings - Fork 94
Add CanonicalConstraintFunction attribute #1118
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
17ad3db
Add CanonicalConstraintFunction attribute
blegat e80c11b
Canonicalize function in MOIU.Model
blegat 4ed307e
Readd attribute
blegat fac2a7a
Add tests
blegat 5688b17
Fix CanonicalConstraintFunction with UF
blegat e47cce3
Move the implementation to get_fallback
blegat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1102,6 +1102,44 @@ struct ConstraintBasisStatus <: AbstractConstraintAttribute | |
end | ||
ConstraintBasisStatus() = ConstraintBasisStatus(1) | ||
|
||
""" | ||
CanonicalConstraintFunction() | ||
|
||
A constraint attribute for a canonical representation of the | ||
[`AbstractFunction`](@ref) object used to define the constraint. | ||
Getting this attribute is guaranteed to return a function that is equivalent but | ||
not necessarily identical to the function provided by the user. | ||
|
||
By default, `MOI.get(model, MOI.CanonicalConstraintFunction(), ci)` fallbacks to | ||
`MOI.Utilities.canonical(MOI.get(model, MOI.ConstraintFunction(), ci))`. | ||
However, if `model` knows that the constraint function is canonical then it can | ||
implement a specialized method that directly return the function without calling | ||
[`Utilities.canonical`](@ref). Therefore, the value returned **cannot** be | ||
assumed to be a copy of the function stored in `model`. | ||
Moreover, [`Utilities.Model`](@ref) checks with [`Utilities.is_canonical`](@ref) | ||
whether the function stored internally is already canonical and if it's the case, | ||
then it returns the function stored internally instead of a copy. | ||
""" | ||
struct CanonicalConstraintFunction <: AbstractConstraintAttribute end | ||
|
||
function get_fallback(model::ModelLike, ::CanonicalConstraintFunction, ci::ConstraintIndex) | ||
func = get(model, ConstraintFunction(), ci) | ||
# In `Utilities.AbstractModel` and `Utilities.UniversalFallback`, | ||
# the function is canonicalized in `add_constraint` so it might already | ||
# be canonical. In other models, the constraint might have been copied from | ||
# from one of these two model so there is in fact a good chance of the | ||
# function being canonical in any model type. | ||
# As `is_canonical` is quite cheap compared to `canonical` which | ||
# requires a copy and sorting the terms, it is worth checking. | ||
if Utilities.is_canonical(func) | ||
return func | ||
else | ||
return Utilities.canonical(func) | ||
end | ||
|
||
return Utilities.canonical(get(model, ConstraintFunction(), ci)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done :) |
||
end | ||
|
||
""" | ||
ConstraintFunction() | ||
|
||
|
@@ -1351,13 +1389,13 @@ _result_index_field(attr::DualStatus) = attr.N | |
|
||
|
||
# Cost of bridging constrained variable in S | ||
struct VariableBridgingCost{S <: AbstractSet} <: AbstractModelAttribute | ||
struct VariableBridgingCost{S <: AbstractSet} <: AbstractModelAttribute | ||
end | ||
get_fallback(model::ModelLike, ::VariableBridgingCost{S}) where {S<:AbstractScalarSet} = supports_add_constrained_variable(model, S) ? 0.0 : Inf | ||
get_fallback(model::ModelLike, ::VariableBridgingCost{S}) where {S<:AbstractVectorSet} = supports_add_constrained_variables(model, S) ? 0.0 : Inf | ||
|
||
# Cost of bridging F-in-S constraints | ||
struct ConstraintBridgingCost{F <: AbstractFunction, S <: AbstractSet} <: AbstractModelAttribute | ||
struct ConstraintBridgingCost{F <: AbstractFunction, S <: AbstractSet} <: AbstractModelAttribute | ||
end | ||
get_fallback(model::ModelLike, ::ConstraintBridgingCost{F, S}) where {F<:AbstractFunction, S<:AbstractSet} = supports_constraint(model, F, S) ? 0.0 : Inf | ||
|
||
|
@@ -1421,8 +1459,9 @@ method should be defined for attributes which are copied indirectly during | |
* [`ObjectiveFunctionType`](@ref): this attribute is set indirectly when setting | ||
the [`ObjectiveFunction`](@ref) attribute. | ||
* [`NumberOfConstraints`](@ref), [`ListOfConstraintIndices`](@ref), | ||
[`ListOfConstraints`](@ref), [`ConstraintFunction`](@ref) and | ||
[`ConstraintSet`](@ref): these attributes are set indirectly by | ||
[`ListOfConstraints`](@ref), [`CanonicalConstraintFunction`](@ref), | ||
[`ConstraintFunction`](@ref) and [`ConstraintSet`](@ref): | ||
these attributes are set indirectly by | ||
[`add_constraint`](@ref) and [`add_constraints`](@ref). | ||
""" | ||
function is_copyable(attr::AnyAttribute) | ||
|
@@ -1440,6 +1479,7 @@ function is_copyable(::Union{ListOfOptimizerAttributesSet, | |
ObjectiveFunctionType, | ||
ListOfConstraintIndices, | ||
ListOfConstraints, | ||
CanonicalConstraintFunction, | ||
ConstraintFunction, | ||
ConstraintSet, | ||
VariableBridgingCost, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.