How do you export an MPS file from ortools?

Viewed 166

Take the example SCIP code for ortools:

from ortools.linear_solver import pywraplp
solver = pywraplp.Solver.CreateSolver('SCIP')

infinity = solver.infinity()
# x and y are integer non-negative variables.
x = solver.IntVar(0.0, infinity, 'x')
y = solver.IntVar(0.0, infinity, 'y')

print('Number of variables =', solver.NumVariables())

# x + 7 * y <= 17.5.
solver.Add(x + 7 * y <= 17.5)

# x <= 3.5.
solver.Add(x <= 3.5)

print('Number of constraints =', solver.NumConstraints())

# Maximize x + 10 * y.
solver.Maximize(x + 10 * y)

I would like to export this as an MPS file. I can see

solver.ExportModelAsMpsFormat

exists but the only documentation I can find says

Signature: solver.ExportModelAsMpsFormat(fixed_format: 'bool', obfuscated: 'bool') -> 'std::string'
Docstring: <no docstring>
File:      c:\users\csxrc\anaconda3\lib\site-packages\ortools\linear_solver\pywraplp.py
Type:      method

I don't understand how to use it. For example how to specify the file name.

If I just wanted to save the model as an MPS file called "test.mps", say, what should I do?

Update

@Krerkkiat Chusap suggests calling the function to create a string that can be written separately. If I do

output_string = solver.ExportModelAsMpsFormat()

I get

TypeError: ExportModelAsMpsFormat() missing 2 required positional arguments: 'fixed_format' and 'obfuscated'

It seems both those arguments are Booleans. What do they signify?

1 Answers

From C++ implementation the fixed_format does not seem to be used, so passing False or True should be okay.

For obfuscated, it usually mean make something hard to read, so make sense to keep it as False.

with open("test.mps", "w") as out_f:
    mps_text = solver.ExportModelAsMpsFormat(fixed_format=False, obfuscated=False)
    out_f.write(mps_text)
Related