Build Status | Coverage |
---|---|
Read and write MathOptInterface models to a variety of mathematical optimization file formats.
Using JuMP? Read the JuMP integration section
In order to read or write a mathematical optimization problem to/from a file, first create a MathOptInterface model. This can be done using JuMP or via one of the solver packages, for example, GLPK.jl.
In the rest of this documentation, we assume that this model is a Julia variable
named user_model
.
To write user_model
to file filename
, follow these steps.
- Create a MathOptFormat model
mathoptformat_model
mathoptformat_model = MathOptFormat.MOF.Model()
- Copy
user_model
intomathoptformat_model
MOI.copy_to(mathoptformat_model, user_model)
- Write
mathoptformat_model
tofilename
MOI.write_to_file(mathoptformat_model, filename)
Step 1) assumes that you want to write a MathOptFormat file (.mof.json). For
other formats, replace MathOptFormat.MOF.Model
with an appropriate model. See
Supported file formats for details.
using MathOptInterface, MathOptFormat, GLPK
const MOI = MathOptInterface
user_model = GLPK.Optimizer()
x = MOI.add_variable(user_model)
MOI.set(user_model, MOI.VariableName(), x, "x")
MOI.add_constraint(user_model, MOI.SingleVariable(x), MOI.GreaterThan(0.0))
mathoptformat_model = MathOptFormat.MPS.Model()
MOI.copy_to(mathoptformat_model, user_model)
MOI.write_to_file(mathoptformat_model, "my_model.mps")
To read a file filename
into user_model
, steps 2 and 3 are reversed:
- Create a MathOptFormat model
mathoptformat_model
mathoptformat_model = MathOptFormat.MOF.Model()
- Read
filename
intomathoptformat_model
MOI.read_from_file(mathoptformat_moodel, filename)
- Copy
mathoptformat_model
intouser_model
MOI.copy_to(user_model, mathoptformat_model)
Step 1) assumes that you want to read a MathOptFormat file (.mof.json). For
other formats, replace MathOptFormat.MOF.Model
with an appropriate model. See
Supported file formats for details.
using MathOptInterface, MathOptFormat, GLPK
const MOI = MathOptInterface
mathoptformat_model = MathOptFormat.MPS.Model()
MOI.read_from_file(mathoptformat_model, "my_model.mps")
user_model = GLPK.Optimizer()
MOI.copy_to(user_model, mathoptformat_model)
x = MOI.get(user_model, MOI.VariableIndex, "x")
File-formats supported are
- Conic benchmark format (.cbf):
- Use
MathOptFormat.CBF.Model
- Use
- Linear programming format (.lp):
- Use
MathOptFormat.LP.Model
- Use
- MathOptFormat (.mof.json):
- Use
MathOptFormat.MOF.Model
- Use
- Mathematical programming system (.mps):
- Use
MathOptFormat.MPS.Model
- Use
- The LP file format does not (yet) support
MOI.read_from_file
. - The MathOptFormat file format (.mof.json) is under active development. No backward compatibility yet!
To read and write GZip'ed files, append .gz
to the filename. For example:
# Uncompressed version:
MOI.write_to_file(model, "my_model.mps")
# Compressed version:
MOI.write_to_file(model, "my_model.mps.gz")
To write a JuMP model to a file, pass backend(jump_model)
to MOI.copy_to
instead of user_model
:
using JuMP, MathOptFormat
jump_model = Model()
@variable(jump_model, x, Int)
@constraint(jump_model, my_con, 2x + 1 <= 2)
@objective(jump_model, Max, x)
mps_model = MathOptFormat.MPS.Model()
MOI.copy_to(mps_model, backend(jump_model))
MOI.write_to_file(mps_model, "my_model.mps")