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

Add ListOfVariablesWithAttributeSet and ListOfConstraintsWithAttributeSet #2331

Merged
merged 6 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions docs/src/manual/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ The following attributes are available:
* [`ListOfConstraintAttributesSet`](@ref)
* [`ListOfConstraintIndices`](@ref)
* [`ListOfConstraintTypesPresent`](@ref)
* [`ListOfConstraintsWithAttributeSet`](@ref)
* [`ListOfModelAttributesSet`](@ref)
* [`ListOfVariableAttributesSet`](@ref)
* [`ListOfVariableIndices`](@ref)
* [`ListOfVariablesWithAttributeSet`](@ref)
* [`NumberOfConstraints`](@ref)
* [`NumberOfVariables`](@ref)
* [`Name`](@ref)
Expand Down
2 changes: 2 additions & 0 deletions docs/src/reference/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ ListOfConstraintIndices
ListOfOptimizerAttributesSet
ListOfModelAttributesSet
ListOfVariableAttributesSet
ListOfVariablesWithAttributeSet
ListOfConstraintAttributesSet
ListOfConstraintsWithAttributeSet
UserDefinedFunction
ListOfSupportedNonlinearOperators
```
Expand Down
2 changes: 2 additions & 0 deletions docs/src/tutorials/implementing.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ Variable-related attributes:
| Attribute | [`get`](@ref) | [`set`](@ref) | [`supports`](@ref) |
| ---------------------- | --------------| ------------- | ------------------ |
| [`ListOfVariableAttributesSet`](@ref) | Yes | No | No |
| [`ListOfVariablesWithAttributeSet`](@ref) | Yes | No | No |
| [`NumberOfVariables`](@ref) | Yes | No | No |
| [`ListOfVariableIndices`](@ref) | Yes | No | No |

Expand All @@ -487,6 +488,7 @@ Constraint-related attributes:
| Attribute | [`get`](@ref) | [`set`](@ref) | [`supports`](@ref) |
| ---------------------- | --------------| ------------- | ------------------ |
| [`ListOfConstraintAttributesSet`](@ref) | Yes | No | No |
| [`ListOfConstraintsWithAttributeSet`](@ref) | Yes | No | No |
| [`NumberOfConstraints`](@ref) | Yes | No | No |
| [`ListOfConstraintTypesPresent`](@ref) | Yes | No | No |
| [`ConstraintFunction`](@ref) | Yes | Yes | No |
Expand Down
27 changes: 27 additions & 0 deletions src/Test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1194,3 +1194,30 @@ function test_model_ModelFilter_ListOfConstraintTypesPresent(
@test MOI.get(dest, attr) == 4
return
end

function test_model_ListOfVariablesWithAttributeSet(
model::MOI.ModelLike,
::Config,
)
attr = MOI.VariableName()
@requires MOI.supports(model, attr, MOI.VariableIndex)
x = MOI.add_variables(model, 2)
MOI.set(model, attr, x[2], "y")
@test x[2] in MOI.get(model, MOI.ListOfVariablesWithAttributeSet(attr))
return
end

function test_model_ListOfConstraintsWithAttributeSet(
model::MOI.ModelLike,
::Config{T},
) where {T}
F, S = MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}
attr = MOI.ConstraintName()
@requires MOI.supports(model, attr, MOI.ConstraintIndex{F,S})
x = MOI.add_variables(model, 2)
c = MOI.add_constraint.(model, one(T) .* x, MOI.GreaterThan(zero(T)))
MOI.set(model, attr, c[2], "y")
ret = MOI.get(model, MOI.ListOfConstraintsWithAttributeSet{F,S}(attr))
@test c[2] in ret
return
end
35 changes: 35 additions & 0 deletions src/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,38 @@
MOI.add_variable(uf::UniversalFallback) = MOI.add_variable(uf.model)

MOI.add_variables(uf::UniversalFallback, n) = MOI.add_variables(uf.model, n)

function MOI.get(
uf::UniversalFallback,
attr::MOI.ListOfVariablesWithAttributeSet,
)
odow marked this conversation as resolved.
Show resolved Hide resolved
dict = get(uf.varattr, attr.attr, nothing)
if dict === nothing
return MOI.get(uf.model, attr)
end
return collect(keys(dict))

Check warning on line 951 in src/Utilities/universalfallback.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/universalfallback.jl#L951

Added line #L951 was not covered by tests
end

function MOI.get(

Check warning on line 954 in src/Utilities/universalfallback.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/universalfallback.jl#L954

Added line #L954 was not covered by tests
uf::UniversalFallback,
attr::MOI.ListOfConstraintsWithAttributeSet{F,S},
) where {F,S}
dict = get(uf.conattr, attr.attr, nothing)
if dict === nothing
return MOI.get(uf.model, attr)

Check warning on line 960 in src/Utilities/universalfallback.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/universalfallback.jl#L958-L960

Added lines #L958 - L960 were not covered by tests
end
return filter(Base.Fix2(isa, MOI.ConstraintIndex{F,S}), keys(dict))

Check warning on line 962 in src/Utilities/universalfallback.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/universalfallback.jl#L962

Added line #L962 was not covered by tests
end

function MOI.get(
uf::UniversalFallback,
attr::MOI.ListOfConstraintsWithAttributeSet{F,S,MOI.ConstraintName},
) where {F,S}
if MOI.supports_constraint(uf.model, F, S)
return MOI.get(uf.model, attr)
end
return filter(
Base.Fix2(isa, MOI.ConstraintIndex{F,S}),
keys(uf.con_to_name),
)
end
55 changes: 55 additions & 0 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,31 @@ attribute was set to variables.
"""
struct ListOfVariableAttributesSet <: AbstractModelAttribute end

"""
ListOfVariablesWithAttributeSet(attr::AbstractVariableAttribute)

A model attribute for the `Vector{VariableIndex}` of all variables with the
attribute `attr` set.

The returned list may not be minimal, so some elements may have their default
value set.

## Note

This is an optional attribute to implement. The default fallback is to get
[`ListOfVariableIndices`](@ref).
"""
struct ListOfVariablesWithAttributeSet{A} <: AbstractModelAttribute
attr::A
function ListOfVariablesWithAttributeSet(attr::AbstractVariableAttribute)
return new{typeof(attr)}(attr)
end
end

function get_fallback(model::ModelLike, ::ListOfVariablesWithAttributeSet)
return get(model, ListOfVariableIndices())
end

"""
VariableName()

Expand Down Expand Up @@ -1581,6 +1606,36 @@ not be included in the list even if then have been set with [`set`](@ref).
"""
struct ListOfConstraintAttributesSet{F,S} <: AbstractModelAttribute end

"""
ListOfConstraintsWithAttributeSet{F,S}(attr:AbstractConstraintAttribute)

A model attribute for the `Vector{ConstraintIndex{F,S}}` of all constraints with
the attribute `attr` set.

The returned list may not be minimal, so some elements may have their default
value set.

## Note

This is an optional attribute to implement. The default fallback is to get
[`ListOfConstraintIndices`](@ref).
"""
struct ListOfConstraintsWithAttributeSet{F,S,A} <: AbstractModelAttribute
attr::A
function ListOfConstraintsWithAttributeSet{F,S}(
attr::AbstractConstraintAttribute,
) where {F,S}
return new{F,S,typeof(attr)}(attr)
end
end

function get_fallback(
model::ModelLike,
::ListOfConstraintsWithAttributeSet{F,S},
) where {F,S}
return get(model, ListOfConstraintIndices{F,S}())
end

"""
ConstraintName()

Expand Down
Loading