@@ -792,6 +792,63 @@ becomes ``A = [3.0, 4.0]^\top`` as follows:
792792modify (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
0 commit comments