joaquimg / MathOptFormat.jl

A file-format for Mathematical Optimization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MathOptFormat.jl

Build Status Coverage
Build Status Codecov branch

Read and write MathOptInterface models to a variety of mathematical optimization file formats.

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.

Write to file

To write user_model to file filename, follow these steps.

  1. Create a MathOptFormat model mathoptformat_model
    • mathoptformat_model = MathOptFormat.MOF.Model()
  2. Copy user_model into mathoptformat_model
    • MOI.copy_to(mathoptformat_model, user_model)
  3. Write mathoptformat_model to filename
    • 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.

Example

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")

Read from file

To read a file filename into user_model, steps 2 and 3 are reversed:

  1. Create a MathOptFormat model mathoptformat_model
    • mathoptformat_model = MathOptFormat.MOF.Model()
  2. Read filename into mathoptformat_model
    • MOI.read_from_file(mathoptformat_moodel, filename)
  3. Copy mathoptformat_model into user_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.

Example

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")

Supported file formats

File-formats supported are

  • Conic benchmark format (.cbf):
    • Use MathOptFormat.CBF.Model
  • Linear programming format (.lp):
    • Use MathOptFormat.LP.Model
  • MathOptFormat (.mof.json):
    • Use MathOptFormat.MOF.Model
  • Mathematical programming system (.mps):
    • Use MathOptFormat.MPS.Model

Notes

  • 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!

GZipped files

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")

About

A file-format for Mathematical Optimization

License:Other


Languages

Language:Julia 100.0%