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

[Utilities] add support for VariableIndex in ModelFilter #2315

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions src/Test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,42 @@ function test_model_ModelFilter_ListOfConstraintIndices(
return
end

"""
test_model_ModelFilter_ListOfVariableIndices(
src::MOI.ModelLike,
::Config{T},
) where {T}

Tests `Utilities.ModelFilter` of `ListOfVariableIndices`.
"""
function test_model_ModelFilter_ListOfVariableIndices(
src::MOI.ModelLike,
::Config{T},
) where {T}
F, S = MOI.VariableIndex, MOI.GreaterThan{T}
@requires MOI.supports_constraint(src, F, S)
src = MOI.Utilities.Model{Float64}()
x = MOI.add_variables(src, 4)
MOI.add_constraint.(src, x, MOI.GreaterThan(T(0)))
dest = MOI.Utilities.Model{T}()
index_map = MOI.copy_to(dest, MOI.Utilities.ModelFilter(src) do item
if item isa MOI.VariableIndex
return isodd(item.value)
elseif item isa MOI.ConstraintIndex{MOI.VariableIndex}
return isodd(item.value)
end
return true
end)
@test MOI.get(dest, MOI.NumberOfVariables()) == 2
@test MOI.get(dest, MOI.NumberOfConstraints{F,S}()) == 2
for xi in x
@test haskey(index_map, xi) == isodd(xi.value)
ci = MOI.ConstraintIndex{F,S}(xi.value)
@test haskey(index_map, ci) == isodd(ci.value)
end
return
end

"""
test_model_ModelFilter_ListOfConstraintTypesPresent(
src::MOI.ModelLike,
Expand Down
25 changes: 23 additions & 2 deletions src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@
* `MOI.ListOfConstraintTypesPresent`
* Individual constraints via:
* `MOI.ListOfConstraintIndices{F,S}`
* Individual variables via:
* `MOI.ListOfVariableIndices`
* Specific attributes via:
* `MOI.ListOfModelAttributesSet`
* `MOI.ListOfConstraintAttributesSet`
Expand Down Expand Up @@ -574,6 +576,20 @@
filtered_src = MOI.Utilities.ModelFilter(my_filter, src)
MOI.copy_to(dest, filtered_src)
```

## Example: copy model excluding some variables

!!! warning
If exclude a variable, you must also exclude any constraints and attributes
which include the variable. It may be simpler in practice to copy the full
model and then delete variables from the copied model.

```julia
my_filter(::Any) = true # Note the generic fallback!
my_filter(x::MOI.VariableIndex) = isodd(x.value)
filtered_src = MOI.Utilities.ModelFilter(my_filter, src)
MOI.copy_to(dest, filtered_src)
```
"""
struct ModelFilter{T,F} <: MOI.ModelLike
inner::T
Expand All @@ -591,6 +607,7 @@
MOI.ListOfConstraintTypesPresent,
MOI.ListOfModelAttributesSet,
MOI.ListOfVariableAttributesSet,
MOI.ListOfVariableIndices,
},
)
return filter(model.filter, MOI.get(model.inner, attr))
Expand All @@ -601,13 +618,17 @@
end

# !!! warning
# Slow implementations, but we need to report the number of constraints in
# the filtered model, not in the `.inner`.
# Slow implementations, but we need to report the number of constraints and
# variables in the filtered model, not in the `.inner`.

function MOI.get(model::ModelFilter, ::MOI.NumberOfConstraints{F,S}) where {F,S}
return length(MOI.get(model, MOI.ListOfConstraintIndices{F,S}()))
end

function MOI.get(model::ModelFilter, ::MOI.NumberOfVariables)
return length(MOI.get(model, MOI.ListOfVariableIndices()))

Check warning on line 629 in src/Utilities/copy.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/copy.jl#L628-L629

Added lines #L628 - L629 were not covered by tests
end

# These just forward the attributes into the inner model.

function MOI.get(
Expand Down
Loading