Skip to content

Commit bba59d2

Browse files
authored
Improve the docstring of write_to_file and read_from_file and support kwargs (#2771)
1 parent 9bccb51 commit bba59d2

File tree

1 file changed

+71
-11
lines changed

1 file changed

+71
-11
lines changed

src/MathOptInterface.jl

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,39 +151,99 @@ function compute_conflict!(optimizer::AbstractOptimizer)
151151
end
152152

153153
"""
154-
write_to_file(model::ModelLike, filename::String)
154+
write_to_file(model::ModelLike, filename::String; kwargs...)
155155
156156
Write the current model to the file at `filename`.
157157
158158
Supported file types depend on the model type.
159+
160+
Additional keyword arguments are passed to the `Model` constructor of the
161+
relevant file format. See, for example, [`FileFormats.LP.Model`](@ref) and
162+
[`FileFormats.MPS.Model`](@ref).
163+
164+
## Example
165+
166+
```jldoctest
167+
julia> model = MOI.Utilities.Model{Int}();
168+
169+
julia> x, _ = MOI.add_constrained_variable(model, MOI.Interval(2, 3));
170+
171+
julia> MOI.set(model, MOI.VariableName(), x, "x");
172+
173+
julia> filename = joinpath(tempdir(), "model.lp");
174+
175+
julia> MOI.write_to_file(model, filename; coefficient_type = Int);
176+
177+
julia> print(read(filename, String))
178+
minimize
179+
obj:
180+
subject to
181+
Bounds
182+
2 <= x <= 3
183+
End
184+
```
159185
"""
160-
function write_to_file(model::ModelLike, filename::String)
161-
dest = FileFormats.Model(; filename = filename)
186+
function write_to_file(model::ModelLike, filename::String; kwargs...)
187+
dest = FileFormats.Model(; filename, kwargs...)
162188
copy_to(dest, model)
163189
write_to_file(dest, filename)
164190
return
165191
end
166192

167193
"""
168-
read_from_file(model::ModelLike, filename::String)
194+
read_from_file(model::ModelLike, filename::String; kwargs...)
169195
170196
Read the file `filename` into the model `model`. If `model` is non-empty, this
171197
may throw an error.
172198
173199
Supported file types depend on the model type.
174200
201+
Additional keyword arguments are passed to the `Model` constructor of the
202+
relevant file format. See, for example, [`FileFormats.LP.Model`](@ref) and
203+
[`FileFormats.MPS.Model`](@ref).
204+
175205
### Note
176206
177-
Once the contents of the file are loaded into the model, users can query the
178-
variables via `get(model, ListOfVariableIndices())`. However, some filetypes,
179-
such as LP files, do not maintain an explicit ordering of the variables.
180-
Therefore, the returned list may be in an arbitrary order.
207+
Once the contents of the file are loaded into the model, you can query the
208+
variables via `MOI.get(model, MOI.ListOfVariableIndices())`. However, some
209+
filetypes, such as LP files, do not maintain an explicit ordering of the
210+
variables. Therefore, the returned list may be in an arbitrary order.
181211
182212
To avoid depending on the order of the indices, look up each variable index by
183-
name using `get(model, VariableIndex, "name")`.
213+
name using `MOI.get(model, MOI.VariableIndex, "name")`.
214+
215+
## Example
216+
217+
```jldoctest
218+
julia> model = MOI.Utilities.Model{Int}();
219+
220+
julia> x, _ = MOI.add_constrained_variable(model, MOI.GreaterThan(2));
221+
222+
julia> MOI.set(model, MOI.VariableName(), x, "x");
223+
224+
julia> filename = joinpath(tempdir(), "model.lp");
225+
226+
julia> MOI.write_to_file(model, filename; coefficient_type = Int);
227+
228+
julia> new_model = MOI.Utilities.Model{Int}();
229+
230+
julia> MOI.read_from_file(new_model, filename; coefficient_type = Int);
231+
232+
julia> print(new_model)
233+
Minimize ScalarAffineFunction{Int64}:
234+
(0)
235+
236+
Subject to:
237+
238+
VariableIndex-in-GreaterThan{Int64}
239+
x >= (2)
240+
241+
julia> MOI.get(new_model, MOI.VariableIndex, "x")
242+
MOI.VariableIndex(1)
243+
```
184244
"""
185-
function read_from_file(model::ModelLike, filename::String)
186-
src = FileFormats.Model(; filename = filename)
245+
function read_from_file(model::ModelLike, filename::String; kwargs...)
246+
src = FileFormats.Model(; filename, kwargs...)
187247
read_from_file(src, filename)
188248
copy_to(model, src)
189249
return

0 commit comments

Comments
 (0)