Skip to content

Commit 5d31266

Browse files
authored
Tidy docs of FileFormats (jump-dev#1271)
1 parent efa7b88 commit 5d31266

File tree

1 file changed

+159
-30
lines changed

1 file changed

+159
-30
lines changed

docs/src/submodules/FileFormats/overview.md

Lines changed: 159 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,158 @@ end
77
DocTestFilters = [r"MathOptInterface|MOI"]
88
```
99

10-
# The `FileFormats` submodule
10+
# The FileFormats submodule
1111

1212
The `FileFormats` module provides functionality for reading and writing MOI
1313
models using [`write_to_file`](@ref) and [`read_from_file`](@ref).
1414

15-
To write a model `src` to a MathOptFormat file, use:
16-
```julia
17-
src = # ...
18-
dest = FileFormats.Model(format = FileFormats.FORMAT_MOF)
19-
MOI.copy_to(dest, src)
20-
MOI.write_to_file(dest, "file.mof.json")
15+
## Supported file types
16+
17+
You must read and write files to a [`FileFormats.Model`](@ref) object. Specifc
18+
the file-type by passing a [`FileFormats.FileFormat`](@ref) enum. For example:
19+
20+
**The Conic Benchmark Format**
21+
22+
```jldoctest
23+
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF)
24+
A Conic Benchmark Format (CBF) model
25+
```
26+
27+
**The LP file format**
28+
29+
```jldoctest
30+
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_LP)
31+
A .LP-file model
32+
```
33+
34+
**The MathOptFormat file format**
35+
36+
```jldoctest
37+
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF)
38+
A MathOptFormat Model
39+
```
40+
41+
**The MPS file format**
42+
43+
```jldoctest
44+
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS)
45+
A Mathematical Programming System (MPS) model
46+
```
47+
48+
**The SDPA file format**
49+
50+
```jldoctest
51+
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_SDPA)
52+
A SemiDefinite Programming Algorithm Format (SDPA) model
53+
```
54+
55+
## Write to file
56+
57+
To write a model `src` to a [MathOptFormat file](https://jump.dev/MathOptFormat/),
58+
use:
59+
```jldoctest fileformats
60+
julia> src = MOI.Utilities.Model{Float64}()
61+
MOIU.Model{Float64}
62+
63+
julia> MOI.add_variable(src)
64+
MathOptInterface.VariableIndex(1)
65+
66+
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF)
67+
A MathOptFormat Model
68+
69+
julia> MOI.copy_to(dest, src)
70+
MathOptInterface.Utilities.IndexMap with 1 entry:
71+
VariableIndex(1) => VariableIndex(1)
72+
73+
julia> MOI.write_to_file(dest, "file.mof.json")
74+
75+
julia> print(read("file.mof.json", String))
76+
{
77+
"name": "MathOptFormat Model",
78+
"version": {
79+
"major": 0,
80+
"minor": 5
81+
},
82+
"variables": [
83+
{
84+
"name": "x1"
85+
}
86+
],
87+
"objective": {
88+
"sense": "feasibility"
89+
},
90+
"constraints": []
91+
}
92+
```
93+
94+
## Read from file
95+
96+
To read a MathOptFormat file, use:
97+
```jldoctest fileformats
98+
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF)
99+
A MathOptFormat Model
100+
101+
julia> MOI.read_from_file(dest, "file.mof.json")
102+
103+
julia> MOI.get(dest, MOI.ListOfVariableIndices())
104+
1-element Array{MathOptInterface.VariableIndex,1}:
105+
MathOptInterface.VariableIndex(1)
106+
107+
julia> rm("file.mof.json") # Clean up after ourselves.
21108
```
22-
The list of supported formats is given by the [`FileFormats.FileFormat`](@ref)
23-
enum.
109+
110+
## Detecing the filetype automatically
24111

25112
Instead of the `format` keyword, you can also use the `filename` keyword
26113
argument to [`FileFormats.Model`](@ref). This will attempt to automatically
27114
guess the format from the file extension. For example:
28-
```julia
29-
src = # ...
30-
filename = "my_model.cbf.gz"
31-
dest = FileFormats.Model(filename = filename)
32-
MOI.copy_to(dest, src)
33-
MOI.write_to_file(dest, filename)
34-
35-
src_2 = FileFormats.Model(filename = filename)
36-
MOI.read_from_file(src_2, filename)
115+
116+
```jldoctest fileformats
117+
julia> src = MOI.Utilities.Model{Float64}()
118+
MOIU.Model{Float64}
119+
120+
julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz")
121+
A Conic Benchmark Format (CBF) model
122+
123+
julia> MOI.copy_to(dest, src)
124+
MathOptInterface.Utilities.IndexMap with 0 entries
125+
126+
julia> MOI.write_to_file(dest, "file.cbf.gz")
127+
128+
julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz")
129+
A Conic Benchmark Format (CBF) model
130+
131+
julia> src = MOI.Utilities.Model{Float64}()
132+
MOIU.Model{Float64}
133+
134+
julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz")
135+
A Conic Benchmark Format (CBF) model
136+
137+
julia> MOI.copy_to(dest, src)
138+
MathOptInterface.Utilities.IndexMap with 0 entries
139+
140+
julia> MOI.write_to_file(dest, "file.cbf.gz")
141+
142+
julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz")
143+
A Conic Benchmark Format (CBF) model
144+
145+
julia> MOI.read_from_file(src_2, "file.cbf.gz")
146+
147+
julia> rm("file.cbf.gz") # Clean up after ourselves.
37148
```
38149
Note how the compression format (GZip) is also automatically detected from the
39150
filename.
40151

152+
## Unsupported constraints
153+
41154
In some cases `src` may contain constraints that are not supported by the file
42155
format (e.g., the CBF format supports integer variables but not binary). If so,
43156
you should copy `src` to a bridged model using [`Bridges.full_bridge_optimizer`](@ref):
44157
```julia
45-
src = # ... conic model ...
46-
dest = FileFormats.Model(format = FileFormats.FORMAT_CBF)
158+
src = MOI.Utilities.Model{Float64}()
159+
x = MOI.add_variable(model)
160+
MOI.add_constraint(model, MOI.SingleVariable(x), MOI.ZeroOne())
161+
dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF)
47162
bridged = MOI.Bridges.full_bridge_optimizer(dest, Float64)
48163
MOI.copy_to(bridged, src)
49164
MOI.write_to_file(dest, "my_model.cbf")
@@ -52,16 +167,30 @@ You should also note that even after bridging, it may still not be possible to
52167
write the model to file because of unsupported constraints (e.g., PSD variables
53168
in the LP file format).
54169

170+
## Read and write to `io`
171+
55172
In addition to [`write_to_file`](@ref) and [`read_from_file`](@ref), you can
56173
read and write directly from `IO` streams using `Base.write` and `Base.read!`:
57-
```julia
58-
src = # ...
59-
io = IOBuffer()
60-
dest = FileFormats.Model(format = FileFormats.FORMAT_MPS)
61-
MOI.copy_to(dest, src)
62-
write(io, dest)
63-
64-
seekstart(io)
65-
src_2 = FileFormats.Model(format = FileFormats.FORMAT_MPS)
66-
read!(io, src_2)
174+
```jldoctest
175+
julia> src = MOI.Utilities.Model{Float64}()
176+
MOIU.Model{Float64}
177+
178+
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS)
179+
A Mathematical Programming System (MPS) model
180+
181+
julia> MOI.copy_to(dest, src)
182+
MathOptInterface.Utilities.IndexMap with 0 entries
183+
184+
julia> io = IOBuffer()
185+
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)
186+
187+
julia> write(io, dest)
188+
189+
julia> seekstart(io)
190+
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=61, maxsize=Inf, ptr=1, mark=-1)
191+
192+
julia> src_2 = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS)
193+
A Mathematical Programming System (MPS) model
194+
195+
julia> read!(io, src_2)
67196
```

0 commit comments

Comments
 (0)