Skip to content

Commit b933423

Browse files
authored
Improve documentation of IndexMap (#1530)
1 parent f08c78c commit b933423

File tree

3 files changed

+85
-30
lines changed

3 files changed

+85
-30
lines changed

docs/src/reference/models.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ write_to_file
3131
read_from_file
3232
supports_incremental_interface
3333
copy_to
34+
IndexMap
3435
```
3536

3637
## Model attributes

src/MathOptInterface.jl

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,41 @@ This is used in two places to determine whether to add a cache:
124124
supports_incremental_interface(::ModelLike, ::Bool) = false
125125

126126
"""
127-
copy_to(dest::ModelLike, src::ModelLike; copy_names=true, warn_attributes=true)
128-
129-
Copy the model from `src` into `dest`. The target `dest` is emptied, and all
130-
previous indices to variables or constraints in `dest` are invalidated. Returns
131-
a dictionary-like object that translates variable and constraint indices from
132-
the `src` model to the corresponding indices in the `dest` model.
133-
134-
If `copy_names` is `false`, the `Name`, `VariableName` and `ConstraintName`
135-
attributes are not copied even if they are set in `src`. If a constraint that
136-
is copied from `src` is not supported by `dest` then an
137-
[`UnsupportedConstraint`](@ref) error is thrown. Similarly, if a model, variable
138-
or constraint attribute that is copied from `src` is not supported by `dest`
139-
then an [`UnsupportedAttribute`](@ref) error is thrown. Unsupported *optimizer*
140-
attributes are treated differently:
141-
142-
* If `warn_attributes` is `true`, a warning is displayed, otherwise,
143-
* the attribute is silently ignored.
127+
copy_to(
128+
dest::ModelLike,
129+
src::ModelLike;
130+
copy_names::Bool = true,
131+
warn_attributes::Bool = true,
132+
)::IndexMap
133+
134+
Copy the model from `src` into `dest`.
135+
136+
The target `dest` is emptied, and all previous indices to variables and
137+
constraints in `dest` are invalidated.
138+
139+
Returns an [`IndexMap`](@ref) object that translates variable and constraint
140+
indices from the `src` model to the corresponding indices in the `dest` model.
141+
142+
## Notes
143+
144+
* If `copy_names == false`, the [`Name`](@ref), [`VariableName`](@ref) and
145+
[`ConstraintName`](@ref) attributes are not copied, even if they are set in
146+
`src`.
147+
* If a constraint that in `src` is not supported by `dest`, then an
148+
[`UnsupportedConstraint`](@ref) error is thrown.
149+
* If an [`AbstractModelAttribute`](@ref), [`AbstractVariableAttribute`](@ref),
150+
or [`AbstractConstraintAttribute`](@ref) is set in `src` but not supported by
151+
`dest`, then an [`UnsupportedAttribute`](@ref) error is thrown.
152+
* Unsupported [`AbstractOptimizerAttribute`](@ref)s are treated differently:
153+
* If `warn_attributes == true`, a warning is displayed, otherwise, the
154+
attribute is silently ignored.
155+
156+
## IndexMap
157+
158+
Implementations of `copy_to` must return an [`IndexMap`](@ref). For technical
159+
reasons, this type is defined in the Utilties submodule as
160+
`MOI.Utilities.IndexMap`. However, since it is an integral part of the MOI API,
161+
we provide `MOI.IndexMap` as an alias.
144162
145163
### Example
146164
@@ -160,16 +178,26 @@ is_valid(dest, index_map[x]) # true
160178
function copy_to end
161179

162180
"""
163-
copy_to_and_optimize!(dest::ModelLike, src::ModelLike; copy_names=true, warn_attributes=true)
181+
copy_to_and_optimize!(
182+
dest::AbstractOptimizer,
183+
src::ModelLike;
184+
kwargs...
185+
)::IndexMap
164186
165-
Same as [`copy_to`](@ref) followed [`optimize!`](@ref). An optimizer
166-
can decide to implement this function and not implement [`copy_to`](@ref) and
167-
[`optimize!`](@ref).
187+
A single call equivalent to calling [`copy_to`](@ref) followed by
188+
[`optimize!`](@ref). Like [`copy_to`](@ref), it returns an [`IndexMap`].
168189
169-
**WARNING** This is an experimental new feature of MOI v0.10 that may break in MOI v1.0.
190+
Keyword arguments are passed to [`copy_to`](@ref).
191+
192+
An optimizer can decide to implement this function instead of implementing
193+
[`copy_to`](@ref) and [`optimize!`](@ref) individually.
194+
195+
!!! warning
196+
This is an experimental new feature of MOI v0.10 that may break in MOI v1.0.
170197
"""
171-
function copy_to_and_optimize!(dest, src; kws...)
172-
index_map = copy_to(dest, src; kws...)
198+
function copy_to_and_optimize!(dest, src; kwargs...)
199+
# The arguments above are untyped to avoid ambiguities.
200+
index_map = copy_to(dest, src; kwargs...)
173201
optimize!(dest)
174202
return index_map
175203
end
@@ -199,4 +227,21 @@ include("instantiate.jl")
199227
include("deprecate.jl")
200228
include("DeprecatedTest/DeprecatedTest.jl")
201229

230+
"""
231+
IndexMap(number_of_variables::Int = 0)
232+
233+
The dictionary-like object returned by [`copy_to`](@ref).
234+
235+
If known in advance, pass `number_of_variables` to preallocate the necessary
236+
space for the variables.
237+
238+
## IndexMap
239+
240+
Implementations of [`copy_to`](@ref) must return an [`IndexMap`](@ref). For
241+
technical reasons, the `IndexMap` type is defined in the Utilties submodule as
242+
`MOI.Utilities.IndexMap`. However, since it is an integral part of the MOI API,
243+
we provide this `MOI.IndexMap` as an alias.
244+
"""
245+
const IndexMap = Utilities.IndexMap
246+
202247
end

src/Utilities/copy/index_map.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# IndexMap is defined here because there is a boostrapping problem.
2+
# * IndexMap requires `Utilities.CleverDicts` and `Utilities.DoubleDicts`, so
3+
# if it were to be defined in MOI proper, it must be included after
4+
# Utilities.
5+
# * However, Utilities requires IndexMap, so it must be defined before
6+
# Utilities.jl is included.
7+
# To work around this issue, we define `IndexMap` here.
8+
19
struct IndexMap <: AbstractDict{MOI.Index,MOI.Index}
210
var_map::CleverDicts.CleverDict{
311
MOI.VariableIndex,
@@ -9,17 +17,18 @@ struct IndexMap <: AbstractDict{MOI.Index,MOI.Index}
917
end
1018

1119
"""
12-
IndexMap(n::Int = 0)
20+
IndexMap(number_of_variables::Int = 0)
21+
22+
The dictionary-like object returned by [`MathOptInterface.copy_to`](@ref).
1323
14-
Dictionary-like object returned by [`MathOptInterface.copy_to`](@ref) that
15-
contains the mapping between variable indices in `.var_map` and between
16-
constraint indices in `.con_map`.
24+
If known in advance, pass `number_of_variables` to preallocate the necessary
25+
space for the variables.
1726
"""
18-
function IndexMap(n::Int = 0)
27+
function IndexMap(number_of_variables::Int = 0)
1928
var_map = CleverDicts.CleverDict{MOI.VariableIndex,MOI.VariableIndex}(
2029
CleverDicts.key_to_index,
2130
CleverDicts.index_to_key,
22-
n,
31+
number_of_variables,
2332
)
2433
con_map = DoubleDicts.IndexDoubleDict()
2534
return IndexMap(var_map, con_map)

0 commit comments

Comments
 (0)