Skip to content

Commit 812a073

Browse files
authored
[docs] improve docstrings of write_to_file and read_from_file (#3956)
1 parent 5587a72 commit 812a073

File tree

1 file changed

+199
-4
lines changed

1 file changed

+199
-4
lines changed

src/file_formats.jl

+199-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,89 @@ end
3838
3939
Write the JuMP model `model` to `filename` in the format `format`.
4040
41-
If the filename ends in `.gz`, it will be compressed using GZip.
42-
If the filename ends in `.bz2`, it will be compressed using BZip2.
41+
See [`MOI.FileFormats.FileFormat`](@ref) for a list of supported formats.
42+
43+
## Compression
44+
45+
If the filename ends in `.gz`, the file will be compressed using GZip.
46+
47+
If the filename ends in `.bz2`, the file will be compressed using BZip2.
48+
49+
## Keyword arguments
4350
4451
Other `kwargs` are passed to the `Model` constructor of the chosen format.
52+
53+
For details, see the docstring each file format's `Model` constructor. For
54+
example, [`MOI.FileFormats.MPS.Model`](@ref).
55+
56+
## Example
57+
58+
```jldoctest
59+
julia> model = Model();
60+
61+
julia> @variable(model, x >= 0);
62+
63+
julia> @objective(model, Min, 2 * x + 1);
64+
65+
julia> filename = joinpath(mktempdir(), "model.mps");
66+
67+
julia> write_to_file(model, filename; generic_names = true)
68+
69+
julia> print(read(filename, String))
70+
NAME
71+
ROWS
72+
N OBJ
73+
COLUMNS
74+
C1 OBJ 2
75+
RHS
76+
rhs OBJ -1
77+
RANGES
78+
BOUNDS
79+
LO bounds C1 0
80+
PL bounds C1
81+
ENDATA
82+
```
83+
84+
## Solver-specific formats
85+
86+
[`write_to_file`](@ref) calls a Julia function from the MathOptInterface package
87+
that is independent of the choice of solver. That is, it does not call a
88+
solver's internal API to write a model to disk.
89+
90+
For MPS files in particular, [`write_to_file`](@ref) may not support the full
91+
range of features that a solver's internal API supports. This is because some
92+
solvers have defined solver-specific extensions to the MPS format, whereas our
93+
Julia implementation supports only features which are standardized across
94+
multiple solvers.
95+
96+
To write a file to disk using the solver's internal API, use
97+
[`direct_model`](@ref) and call the solver's C API. For example:
98+
```jldoctest
99+
julia> import HiGHS
100+
101+
julia> model = direct_model(HiGHS.Optimizer());
102+
103+
julia> set_silent(model)
104+
105+
julia> @variable(model, x >= 0);
106+
107+
julia> @objective(model, Min, 2 * x + 1);
108+
109+
julia> filename = joinpath(mktempdir(), "model.mps");
110+
111+
julia> HiGHS.Highs_writeModel(backend(model), filename);
112+
113+
julia> print(read(filename, String))
114+
NAME
115+
ROWS
116+
N Obj
117+
COLUMNS
118+
c0 Obj 2
119+
RHS
120+
RHS_V Obj -1
121+
ENDATA
122+
```
123+
45124
"""
46125
function write_to_file(
47126
model::GenericModel,
@@ -70,7 +149,46 @@ end
70149
71150
Write the JuMP model `model` to `io` in the format `format`.
72151
152+
See [`MOI.FileFormats.FileFormat`](@ref) for a list of supported formats.
153+
154+
Other `kwargs` are passed to the `Model` constructor of the chosen format.
155+
156+
## Keyword arguments
157+
73158
Other `kwargs` are passed to the `Model` constructor of the chosen format.
159+
160+
For details, see the docstring each file format's `Model` constructor. For
161+
example, [`MOI.FileFormats.MPS.Model`](@ref).
162+
163+
## Example
164+
165+
```jldoctest
166+
julia> model = Model();
167+
168+
julia> @variable(model, x >= 0);
169+
170+
julia> @objective(model, Min, 2 * x + 1);
171+
172+
julia> io = IOBuffer();
173+
174+
julia> write(io, model; format = MOI.FileFormats.FORMAT_MPS);
175+
176+
julia> seekstart(io);
177+
178+
julia> print(read(io, String))
179+
NAME
180+
ROWS
181+
N OBJ
182+
COLUMNS
183+
x OBJ 2
184+
RHS
185+
rhs OBJ -1
186+
RANGES
187+
BOUNDS
188+
LO bounds x 0
189+
PL bounds x
190+
ENDATA
191+
```
74192
"""
75193
function Base.write(
76194
io::IO,
@@ -105,10 +223,49 @@ _value_type(::MOI.ModelLike) = Float64
105223
106224
Return a JuMP model read from `filename` in the format `format`.
107225
108-
If the filename ends in `.gz`, it will be uncompressed using GZip.
109-
If the filename ends in `.bz2`, it will be uncompressed using BZip2.
226+
See [`MOI.FileFormats.FileFormat`](@ref) for a list of supported formats.
227+
228+
## Compression
229+
230+
If the filename ends in `.gz`, the file will be uncompressed using GZip.
231+
232+
If the filename ends in `.bz2`, the file will be uncompressed using BZip2.
233+
234+
## Keyword arguments
110235
111236
Other `kwargs` are passed to the `Model` constructor of the chosen format.
237+
238+
For details, see the docstring each file format's `Model` constructor. For
239+
example, [`MOI.FileFormats.MPS.Model`](@ref).
240+
241+
## Example
242+
243+
```jldoctest
244+
julia> model = Model();
245+
246+
julia> @variable(model, x >= 0);
247+
248+
julia> @objective(model, Min, 2 * x + 1);
249+
250+
julia> filename = joinpath(mktempdir(), "model.mps");
251+
252+
julia> write_to_file(model, filename; generic_names = true)
253+
254+
julia> new_model = read_from_file(filename)
255+
A JuMP Model
256+
├ solver: none
257+
├ objective_sense: MIN_SENSE
258+
│ └ objective_function_type: AffExpr
259+
├ num_variables: 1
260+
├ num_constraints: 1
261+
│ └ VariableRef in MOI.GreaterThan{Float64}: 1
262+
└ Names registered in the model: none
263+
264+
julia> print(new_model)
265+
Min 2 C1 + 1
266+
Subject to
267+
C1 ≥ 0
268+
```
112269
"""
113270
function read_from_file(
114271
filename::String;
@@ -132,7 +289,45 @@ end
132289
133290
Return a JuMP model read from `io` in the format `format`.
134291
292+
See [`MOI.FileFormats.FileFormat`](@ref) for a list of supported formats.
293+
294+
## Keyword arguments
295+
135296
Other `kwargs` are passed to the `Model` constructor of the chosen format.
297+
298+
For details, see the docstring each file format's `Model` constructor. For
299+
example, [`MOI.FileFormats.MPS.Model`](@ref).
300+
301+
## Example
302+
303+
```jldoctest
304+
julia> model = Model();
305+
306+
julia> @variable(model, x >= 0);
307+
308+
julia> @objective(model, Min, 2 * x + 1);
309+
310+
julia> io = IOBuffer();
311+
312+
julia> write(io, model; format = MOI.FileFormats.FORMAT_MPS);
313+
314+
julia> seekstart(io);
315+
316+
julia> new_model = read(io, Model; format = MOI.FileFormats.FORMAT_MPS)
317+
A JuMP Model
318+
├ solver: none
319+
├ objective_sense: MIN_SENSE
320+
│ └ objective_function_type: AffExpr
321+
├ num_variables: 1
322+
├ num_constraints: 1
323+
│ └ VariableRef in MOI.GreaterThan{Float64}: 1
324+
└ Names registered in the model: none
325+
326+
julia> print(new_model)
327+
Min 2 x + 1
328+
Subject to
329+
x ≥ 0
330+
```
136331
"""
137332
function Base.read(
138333
io::IO,

0 commit comments

Comments
 (0)