Skip to content

Commit 3f4c220

Browse files
authored
Merge pull request #969 from JuliaOpt/od/mof
Add MathOptFormat.jl as FileFormats submodule
2 parents 633bb7c + 3beed00 commit 3f4c220

83 files changed

Lines changed: 7195 additions & 23 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4+
.vscode/*
45
docs/build/
56
docs/site/
6-
Manifest.toml
77
test/Benchmarks/*.json
8+
Manifest.toml

LICENSE.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ The MathOptInterface.jl package is licensed under the MIT "Expat" License:
22

33
> Copyright (c) 2017: Miles Lubin and contributors
44
> Copyright (c) 2017: Google Inc.
5-
>
5+
>
66
> Permission is hereby granted, free of charge, to any person obtaining a copy
77
> of this software and associated documentation files (the "Software"), to deal
88
> in the Software without restriction, including without limitation the rights
99
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
> copies of the Software, and to permit persons to whom the Software is
1111
> furnished to do so, subject to the following conditions:
12-
>
12+
>
1313
> The above copyright notice and this permission notice shall be included in all
1414
> copies or substantial portions of the Software.
15-
>
15+
>
1616
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1919
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2020
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
> SOFTWARE.
23-
>
23+
>

Project.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ version = "0.9.8"
44

55
[deps]
66
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
7+
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
8+
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
9+
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
10+
JSONSchema = "7d188eb4-7ad8-530c-ae41-71a32a6d4692"
711
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
812
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
913
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
@@ -13,6 +17,10 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
1317

1418
[compat]
1519
BenchmarkTools = "0.4"
20+
CodecBzip2 = "~0.6"
21+
CodecZlib = "~0.6"
22+
JSON = "~0.21"
23+
JSONSchema = "0.2"
1624
MutableArithmetics = "0.2"
1725
OrderedCollections = "1"
1826
julia = "1"

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
44

55
[compat]
6-
Documenter = "~0.22"
6+
Documenter = "0.24"

docs/src/apimanual.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,63 @@ becomes ``A = [3.0, 4.0]^\top`` as follows:
792792
modify(m, c, MultirowChange(x, [3.0, 4.0]))
793793
```
794794

795+
## File formats
796+
797+
The `FileFormats` module provides functionality for reading and writing MOI models using
798+
[`write_to_file`](@ref) and [`read_from_file`](@ref).
799+
800+
To write a model `src` to a MathOptFormat file, use:
801+
```julia
802+
src = # ...
803+
dest = FileFormats.Model(format = FileFormats.FORMAT_MOF)
804+
MOI.copy_to(dest, src)
805+
MOI.write_to_file(dest, "file.mof.json")
806+
```
807+
The list of supported formats is given by the [`FileFormats.FileFormat`](@ref) enum.
808+
809+
Instead of the `format` keyword, you can also use the `filename` keyword argument to
810+
[`FileFormats.Model`](@ref). This will attempt to automatically guess the format from the
811+
file extension. For example:
812+
```julia
813+
src = # ...
814+
filename = "my_model.cbf.gz"
815+
dest = FileFormats.Model(filename = filename)
816+
MOI.copy_to(dest, src)
817+
MOI.write_to_file(dest, filename)
818+
819+
src_2 = FileFormats.Model(filename = filename)
820+
MOI.read_from_file(src_2, filename)
821+
```
822+
Note how the compression format (GZip) is also automatically detected from the filename.
823+
824+
In some cases `src` may contain constraints that are not supported by the file format (e.g.,
825+
the CBF format supports integer variables but not binary). If so, you should copy `src` to a
826+
bridged model using [`Bridges.full_bridge_optimizer`](@ref):
827+
```julia
828+
src = # ... conic model ...
829+
dest = FileFormats.Model(format = FileFormats.FORMAT_CBF)
830+
bridged = MOI.Bridges.full_bridge_optimizer(dest, Float64)
831+
MOI.copy_to(bridged, src)
832+
MOI.write_to_file(dest, "my_model.cbf")
833+
```
834+
You should also note that even after bridging, it may still not be possible to write the
835+
model to file because of unsupported constraints (e.g., PSD variables in the LP file
836+
format).
837+
838+
In addition to [`write_to_file`](@ref) and [`read_from_file`](@ref), you can read and write
839+
directly from `IO` streams using `Base.write` and `Base.read!`:
840+
```julia
841+
src = # ...
842+
io = IOBuffer()
843+
dest = FileFormats.Model(format = FileFormats.FORMAT_MPS)
844+
MOI.copy_to(dest, src)
845+
write(io, dest)
846+
847+
seekstart(io)
848+
src_2 = FileFormats.Model(format = FileFormats.FORMAT_MPS)
849+
read!(io, src_2)
850+
```
851+
795852
## Advanced
796853

797854
### Duals

docs/src/apireference.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,13 @@ Benchmarks.suite
10181018
Benchmarks.create_baseline
10191019
Benchmarks.compare_against_baseline
10201020
```
1021+
1022+
## File Formats
1023+
1024+
Functions to help read and write MOI models to/from various file formats. See
1025+
[File Formats](@ref) for more details.
1026+
1027+
```@docs
1028+
FileFormats.Model
1029+
FileFormats.FileFormat
1030+
```

0 commit comments

Comments
 (0)