Skip to content

Commit db7b687

Browse files
committed
Refactor modifyobjective! to modify!(m, ObjectiveFunction, change).
1 parent 0c25804 commit db7b687

File tree

12 files changed

+86
-82
lines changed

12 files changed

+86
-82
lines changed

docs/src/apimanual.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ See [`PositiveSemidefiniteConeTriangle`](@ref MathOptInterface.PositiveSemidefin
531531

532532
### Modifying a model
533533

534-
[Explain `modify!` and `modifyobjective!`.]
534+
[Explain `modify!`.]
535535

536536
### Constraint bridges
537537

docs/src/apireference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ dimension
255255
Functions for modifying objective functions. Use `ObjectiveFunction` and `ObjectiveSense` to set and query the objective function.
256256

257257
```@docs
258-
modifyobjective!
259-
canmodifyobjective
258+
modify!
259+
canmodify
260260
```
261261

262262
## Nonlinear programming (NLP)

src/Bridges/bridgeoptimizer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ function MOI.set!(b::AbstractBridgeOptimizer, ::MOI.ConstraintFunction, constrai
266266
end
267267

268268
# Objective
269-
MOI.canmodifyobjective(b::AbstractBridgeOptimizer, ::Type{M}) where M<:MOI.AbstractFunctionModification = MOI.canmodifyobjective(b.model, M)
270-
MOI.modifyobjective!(b::AbstractBridgeOptimizer, change::MOI.AbstractFunctionModification) = MOI.modifyobjective!(b.model, change)
269+
MOI.canmodify(b::AbstractBridgeOptimizer, obj::MOI.ObjectiveFunction, ::Type{M}) where M<:MOI.AbstractFunctionModification = MOI.canmodify(b.model, obj, M)
270+
MOI.modify!(b::AbstractBridgeOptimizer, obj::MOI.ObjectiveFunction, change::MOI.AbstractFunctionModification) = MOI.modify!(b.model, obj, change)
271271

272272
# Variables
273273
MOI.canaddvariable(b::AbstractBridgeOptimizer) = MOI.canaddvariable(b.model)

src/MathOptInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ include("functions.jl")
152152
include("sets.jl")
153153
include("attributes.jl")
154154
include("constraints.jl")
155-
include("objectives.jl")
155+
include("modifications.jl")
156156
include("variables.jl")
157157
include("nlp.jl")
158158

src/Test/contlinear.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,14 @@ function linear1test(model::MOI.ModelLike, config::TestConfig)
182182
c = MOI.addconstraint!(model, cf, MOI.LessThan(1.0))
183183
end
184184

185-
@test MOI.canmodifyobjective(model, MOI.ScalarCoefficientChange{Float64})
186-
MOI.modifyobjective!(model, MOI.ScalarCoefficientChange{Float64}(z, 2.0))
185+
@test MOI.canmodify(model,
186+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
187+
MOI.ScalarCoefficientChange{Float64}
188+
)
189+
MOI.modify!(model,
190+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
191+
MOI.ScalarCoefficientChange{Float64}(z, 2.0)
192+
)
187193

188194
@test MOI.get(model, MOI.NumberOfConstraints{MOI.ScalarAffineFunction{Float64},MOI.LessThan{Float64}}()) == 1
189195
@test MOI.get(model, MOI.NumberOfConstraints{MOI.SingleVariable,MOI.GreaterThan{Float64}}()) == 3

src/Utilities/cachingoptimizer.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,22 @@ function MOI.set!(m::CachingOptimizer, ::MOI.ConstraintFunction, cindex::CI{F,S}
295295
return
296296
end
297297

298-
function MOI.canmodifyobjective(m::CachingOptimizer, change)
299-
MOI.canmodifyobjective(m.model_cache, change) || return false
298+
function MOI.canmodify(m::CachingOptimizer, obj::MOI.ObjectiveFunction, change)
299+
MOI.canmodify(m.model_cache, obj, change) || return false
300300
if m.state == AttachedOptimizer && m.mode == Manual
301-
MOI.canmodifyobjective(m.optimizer, change) || return false
301+
MOI.canmodify(m.optimizer, obj, change) || return false
302302
end
303303
return true
304304
end
305305

306-
function MOI.modifyobjective!(m::CachingOptimizer, change::MOI.AbstractFunctionModification)
307-
if m.mode == Automatic && m.state == AttachedOptimizer && !MOI.canmodifyobjective(m.optimizer, typeof(change))
306+
function MOI.modify!(m::CachingOptimizer, obj::MOI.ObjectiveFunction, change::MOI.AbstractFunctionModification)
307+
if m.mode == Automatic && m.state == AttachedOptimizer && !MOI.canmodify(m.optimizer, obj, typeof(change))
308308
resetoptimizer!(m)
309309
end
310-
@assert MOI.canmodifyobjective(m, typeof(change))
311-
MOI.modifyobjective!(m.model_cache, change)
310+
@assert MOI.canmodify(m, obj, typeof(change))
311+
MOI.modify!(m.model_cache, obj, change)
312312
if m.state == AttachedOptimizer
313-
MOI.modifyobjective!(m.optimizer, mapvariables(m.model_to_optimizer_map,change))
313+
MOI.modify!(m.optimizer, obj, mapvariables(m.model_to_optimizer_map,change))
314314
end
315315
return
316316
end

src/Utilities/mockoptimizer.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ function MOI.set!(mock::MockOptimizer, ::MOI.ConstraintFunction, c::CI{F,S}, fun
219219
MOI.set!(mock.inner_model, MOI.ConstraintFunction(), xor_index(c), func)
220220
end
221221

222-
function MOI.canmodifyobjective(mock::MockOptimizer, change)
223-
MOI.canmodifyobjective(mock.inner_model, change)
222+
function MOI.canmodify(mock::MockOptimizer, obj::MOI.ObjectiveFunction, change)
223+
MOI.canmodify(mock.inner_model, obj, change)
224224
end
225225

226-
function MOI.modifyobjective!(mock::MockOptimizer, change::MOI.AbstractFunctionModification)
227-
MOI.modifyobjective!(mock.inner_model, xor_variables(change))
226+
function MOI.modify!(mock::MockOptimizer, obj::MOI.ObjectiveFunction, change::MOI.AbstractFunctionModification)
227+
MOI.modify!(mock.inner_model, obj, xor_variables(change))
228228
end
229229

230230
# TODO: transformconstraint and cantransformconstraint

src/Utilities/model.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ function MOI.set!(model::AbstractModel, ::MOI.ObjectiveFunction, f::MOI.Abstract
203203
model.objective = deepcopy(f)
204204
end
205205

206-
MOI.canmodifyobjective(::AbstractModel, ::Type{<:MOI.AbstractFunctionModification}) = true
207-
function MOI.modifyobjective!(model::AbstractModel, change::MOI.AbstractFunctionModification)
206+
MOI.canmodify(::AbstractModel, ::MOI.ObjectiveFunction, ::Type{<:MOI.AbstractFunctionModification}) = true
207+
function MOI.modify!(model::AbstractModel, obj::MOI.ObjectiveFunction, change::MOI.AbstractFunctionModification)
208208
model.objective = modifyfunction(model.objective, change)
209209
end
210210

src/Utilities/universalfallback.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ function MOI.set!(uf::UniversalFallback, ::MOI.ConstraintFunction, ci::CI{F,S},
170170
end
171171

172172
# Objective
173-
MOI.canmodifyobjective(uf::UniversalFallback, ::Type{M}) where M<:MOI.AbstractFunctionModification = MOI.canmodifyobjective(uf.model, M)
174-
MOI.modifyobjective!(uf::UniversalFallback, change::MOI.AbstractFunctionModification) = MOI.modifyobjective!(uf.model, change)
173+
MOI.canmodify(uf::UniversalFallback, obj::MOI.ObjectiveFunction, ::Type{M}) where M<:MOI.AbstractFunctionModification = MOI.canmodify(uf.model, obj, M)
174+
MOI.modify!(uf::UniversalFallback, obj::MOI.ObjectiveFunction, change::MOI.AbstractFunctionModification) = MOI.modify!(uf.model, obj, change)
175175

176176
# Variables
177177
MOI.canaddvariable(uf::UniversalFallback) = MOI.canaddvariable(uf.model)

src/constraints.jl

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,6 @@ function set!(model::ModelLike, ::ConstraintFunction, constraint_index, func)
115115
throw(MethodError(set!, (model, ConstraintFunction(), constraint_index, func)))
116116
end
117117

118-
"""
119-
canmodify(model::ModelLike, ::Type{CI}, ::Type{M})::Bool where CI<:ConstraintIndex where M<:AbstractFunctionModification
120-
121-
Return a `Bool` indicating whether it is possible to apply a modification of
122-
type `M` to the function of constraint of type `CI`.
123-
124-
### Examples
125-
126-
```julia
127-
canmodify(model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}}, ScalarConstantChange{Float64})
128-
```
129-
"""
130-
function canmodify end
131-
canmodify(model::ModelLike, constraint_index, change) = false
132-
133-
"""
134-
modify!(model::ModelLike, c::ConstraintIndex, change::AbstractFunctionModification)
135-
136-
Apply the modification specified by `change` to the function of constraint `c`.
137-
138-
### Examples
139-
140-
```julia
141-
modify!(model, c, ScalarConstantChange(10.0))
142-
```
143-
"""
144-
function modify! end
145-
146-
147118
"""
148119
## Transform Constraint Set
149120

src/modifications.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
## Constraint Function
3+
canmodify(model::ModelLike, ::Type{CI}, ::Type{M})::Bool where CI<:ConstraintIndex where M<:AbstractFunctionModification
4+
5+
Return a `Bool` indicating whether it is possible to apply a modification of
6+
type `M` to the function of constraint of type `CI`.
7+
8+
### Examples
9+
10+
```julia
11+
canmodify(model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}}, ScalarConstantChange{Float64})
12+
```
13+
14+
## Objective Function
15+
16+
canmodify(model::ModelLike, ::ObjectiveFunction, ::Type{M})::Bool where M<:AbstractFunctionModification
17+
18+
Return a `Bool` indicating whether it is possible to apply a modification of
19+
type `M` to the objective function of model `model`.
20+
21+
### Examples
22+
23+
```julia
24+
canmodify(model, ObjectiveFunction{ScalarAffineFunction{Float64}}(), ScalarConstantChange{Float64})
25+
```
26+
"""
27+
function canmodify end
28+
canmodify(model::ModelLike, ref, change) = false
29+
30+
"""
31+
## Constraint Function
32+
33+
modify!(model::ModelLike, c::ConstraintIndex, change::AbstractFunctionModification)
34+
35+
Apply the modification specified by `change` to the function of constraint `c`.
36+
37+
### Examples
38+
39+
```julia
40+
modify!(model, c, ScalarConstantChange(10.0))
41+
```
42+
43+
## Objective Function
44+
45+
modify!(model::ModelLike, ::ObjectiveFunction, change::AbstractFunctionModification)
46+
47+
Apply the modification specified by `change` to the objective function of
48+
`model`. To change the function completely, call `set!` instead.
49+
50+
### Examples
51+
52+
```julia
53+
modify!(model, ObjectiveFunction{ScalarAffineFunction{Float64}}(), ScalarConstantChange(10.0))
54+
```
55+
"""
56+
function modify! end

src/objectives.jl

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)