Skip to content

MOI.modify version for multiple changes at once #1800

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 6 commits into from
Apr 29, 2022
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
75 changes: 75 additions & 0 deletions src/modifications.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,58 @@ objectives is not supported by the model `model`.
```julia
modify(model, ObjectiveFunction{ScalarAffineFunction{Float64}}(), ScalarConstantChange(10.0))
```

## Multiple modifications in Constraint Functions

modify(
model::ModelLike,
cis::AbstractVector{<:ConstraintIndex},
changes::AbstractVector{<:AbstractFunctionModification},
)

Apply multiple modifications specified by `changes` to the functions of constraints `cis`.

An [`ModifyConstraintNotAllowed`](@ref) error is thrown if modifying
constraints is not supported by the model `model`.

### Examples

```julia
modify(
model,
[ci, ci],
[
ScalarCoefficientChange{Float64}(VariableIndex(1), 1.0),
ScalarCoefficientChange{Float64}(VariableIndex(2), 0.5),
],
)
```

## Multiple modifications in the Objective Function

modify(
model::ModelLike,
attr::ObjectiveFunction,
changes::AbstractVector{<:AbstractFunctionModification},
)

Apply multiple modifications specified by `changes` to the functions of constraints `cis`.

An [`ModifyConstraintNotAllowed`](@ref) error is thrown if modifying
constraints is not supported by the model `model`.

### Examples

```julia
modify(
model,
ObjectiveFunction{ScalarAffineFunction{Float64}}(),
[
ScalarCoefficientChange{Float64}(VariableIndex(1), 1.0),
ScalarCoefficientChange{Float64}(VariableIndex(2), 0.5),
],
)
```
"""
function modify end

Expand All @@ -100,10 +152,33 @@ function modify(
return throw_modify_not_allowed(ci, change)
end

function modify(
model::ModelLike,
cis::AbstractVector{<:ConstraintIndex},
changes::AbstractVector{<:AbstractFunctionModification},
)
@assert length(cis) == length(changes)
for (ci, change) in zip(cis, changes)
modify(model, ci, change)
end
return
end

function modify(
model::ModelLike,
attr::ObjectiveFunction,
change::AbstractFunctionModification,
)
return throw_modify_not_allowed(attr, change)
end

function modify(
model::ModelLike,
attr::ObjectiveFunction,
changes::AbstractVector{<:AbstractFunctionModification},
)
for change in changes
modify(model, attr, change)
end
return
end
52 changes: 52 additions & 0 deletions test/Utilities/cachingoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,58 @@ function test_final_touch_optimize()
@test model.model_cache.index_map === nothing
end

function test_multiple_modifications()
m = MOIU.CachingOptimizer(MOIU.Model{Float64}(), MOIU.AUTOMATIC)

x = MOI.add_variables(m, 3)

saf = MOI.ScalarAffineFunction(
[
MOI.ScalarAffineTerm(1.0, x[1]),
MOI.ScalarAffineTerm(1.0, x[2]),
MOI.ScalarAffineTerm(1.0, x[3]),
],
0.0,
)
ci1 = MOI.add_constraint(m, saf, MOI.LessThan(1.0))
ci2 = MOI.add_constraint(m, saf, MOI.LessThan(2.0))

MOI.set(m, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(), saf)

fc1 = MOI.get(m, MOI.ConstraintFunction(), ci1)
@test MOI.coefficient.(fc1.terms) == [1.0, 1.0, 1.0]
fc2 = MOI.get(m, MOI.ConstraintFunction(), ci2)
@test MOI.coefficient.(fc2.terms) == [1.0, 1.0, 1.0]
obj = MOI.get(m, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}())
@test MOI.coefficient.(obj.terms) == [1.0, 1.0, 1.0]

changes_cis = [
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 0.5)
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
]
MOI.modify(m, [ci1, ci2, ci2], changes_cis)

fc1 = MOI.get(m, MOI.ConstraintFunction(), ci1)
@test MOI.coefficient.(fc1.terms) == [4.0, 1.0, 1.0]
fc2 = MOI.get(m, MOI.ConstraintFunction(), ci2)
@test MOI.coefficient.(fc2.terms) == [0.5, 1.0, 2.0]

changes_obj = [
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
MOI.ScalarCoefficientChange(MOI.VariableIndex(2), 10.0)
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
]
MOI.modify(
m,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
changes_obj,
)

obj = MOI.get(m, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}())
@test MOI.coefficient.(obj.terms) == [4.0, 10.0, 2.0]
end

end # module

TestCachingOptimizer.runtests()