Skip to content

Commit 7db04da

Browse files
authored
[docs] mention use_nlp_block when reading nonlinear file formats (#3958)
1 parent 02fabbf commit 7db04da

File tree

2 files changed

+68
-18
lines changed

2 files changed

+68
-18
lines changed

docs/src/manual/models.md

+33
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,39 @@ A JuMP Model
344344
[`variable_by_name`](@ref) or [`constraint_by_name`](@ref) to access
345345
specific variables or constraints.
346346

347+
### Nonlinear file formats
348+
349+
To maintain backwards compatibility, nonlinear models in `.mof.json` and `.nl`
350+
files are parsed into a [`MOI.NLPBlock`](@ref). To parse as
351+
[`MOI.ScalarNonlinearFunction`](@ref), pass the keyword `use_nlp_block = false`:
352+
353+
```jldoctest
354+
julia> model = Model();
355+
356+
julia> @variable(model, x >= 0);
357+
358+
julia> @objective(model, Min, log(x));
359+
360+
julia> filename = joinpath(mktempdir(), "model.mof.json");
361+
362+
julia> write_to_file(model, filename)
363+
364+
julia> new_model = read_from_file(filename; use_nlp_block = false)
365+
A JuMP Model
366+
├ solver: none
367+
├ objective_sense: MIN_SENSE
368+
│ └ objective_function_type: NonlinearExpr
369+
├ num_variables: 1
370+
├ num_constraints: 1
371+
│ └ VariableRef in MOI.GreaterThan{Float64}: 1
372+
└ Names registered in the model: none
373+
374+
julia> print(new_model)
375+
Min log(x)
376+
Subject to
377+
x ≥ 0
378+
```
379+
347380
## Relax integrality
348381

349382
Use [`relax_integrality`](@ref) to remove any integrality constraints from the

src/file_formats.jl

+35-18
Original file line numberDiff line numberDiff line change
@@ -225,46 +225,52 @@ Return a JuMP model read from `filename` in the format `format`.
225225
226226
See [`MOI.FileFormats.FileFormat`](@ref) for a list of supported formats.
227227
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-
234228
## Keyword arguments
235229
236230
Other `kwargs` are passed to the `Model` constructor of the chosen format.
237231
238232
For details, see the docstring each file format's `Model` constructor. For
239233
example, [`MOI.FileFormats.MPS.Model`](@ref).
240234
235+
## Nonlinear models
236+
237+
To maintain backwards compatibility, nonlinear models in `.mof.json` and `.nl`
238+
files are parsed into a [`MOI.NLPBlock`](@ref). To parse as [`MOI.ScalarNonlinearFunction`](@ref),
239+
pass the keyword `use_nlp_block = false`.
240+
241+
## Compression
242+
243+
If the filename ends in `.gz`, the file will be uncompressed using GZip.
244+
245+
If the filename ends in `.bz2`, the file will be uncompressed using BZip2.
246+
241247
## Example
242248
243249
```jldoctest
244250
julia> model = Model();
245251
246252
julia> @variable(model, x >= 0);
247253
248-
julia> @objective(model, Min, 2 * x + 1);
254+
julia> @objective(model, Min, log(x));
249255
250-
julia> filename = joinpath(mktempdir(), "model.mps");
256+
julia> filename = joinpath(mktempdir(), "model.mof.json");
251257
252-
julia> write_to_file(model, filename; generic_names = true)
258+
julia> write_to_file(model, filename)
253259
254-
julia> new_model = read_from_file(filename)
260+
julia> new_model = read_from_file(filename; use_nlp_block = false)
255261
A JuMP Model
256262
├ solver: none
257263
├ objective_sense: MIN_SENSE
258-
│ └ objective_function_type: AffExpr
264+
│ └ objective_function_type: NonlinearExpr
259265
├ num_variables: 1
260266
├ num_constraints: 1
261267
│ └ VariableRef in MOI.GreaterThan{Float64}: 1
262268
└ Names registered in the model: none
263269
264270
julia> print(new_model)
265-
Min 2 C1 + 1
271+
Min log(x)
266272
Subject to
267-
C1 ≥ 0
273+
x ≥ 0
268274
```
269275
"""
270276
function read_from_file(
@@ -298,33 +304,44 @@ Other `kwargs` are passed to the `Model` constructor of the chosen format.
298304
For details, see the docstring each file format's `Model` constructor. For
299305
example, [`MOI.FileFormats.MPS.Model`](@ref).
300306
307+
## Nonlinear models
308+
309+
To maintain backwards compatibility, nonlinear models in `.mof.json` and `.nl`
310+
files are parsed into a [`MOI.NLPBlock`](@ref). To parse as [`MOI.ScalarNonlinearFunction`](@ref),
311+
pass the keyword `use_nlp_block = false`.
312+
301313
## Example
302314
303315
```jldoctest
304316
julia> model = Model();
305317
306318
julia> @variable(model, x >= 0);
307319
308-
julia> @objective(model, Min, 2 * x + 1);
320+
julia> @objective(model, Min, log(x));
309321
310322
julia> io = IOBuffer();
311323
312-
julia> write(io, model; format = MOI.FileFormats.FORMAT_MPS);
324+
julia> write(io, model; format = MOI.FileFormats.FORMAT_MOF);
313325
314326
julia> seekstart(io);
315327
316-
julia> new_model = read(io, Model; format = MOI.FileFormats.FORMAT_MPS)
328+
julia> new_model = read(
329+
io,
330+
Model;
331+
format = MOI.FileFormats.FORMAT_MOF,
332+
use_nlp_block = false,
333+
)
317334
A JuMP Model
318335
├ solver: none
319336
├ objective_sense: MIN_SENSE
320-
│ └ objective_function_type: AffExpr
337+
│ └ objective_function_type: NonlinearExpr
321338
├ num_variables: 1
322339
├ num_constraints: 1
323340
│ └ VariableRef in MOI.GreaterThan{Float64}: 1
324341
└ Names registered in the model: none
325342
326343
julia> print(new_model)
327-
Min 2 x + 1
344+
Min log(x)
328345
Subject to
329346
x ≥ 0
330347
```

0 commit comments

Comments
 (0)