Or-tools: Python:Read MPS for MIP

Created on 7 Nov 2017  Â·  9Comments  Â·  Source: google/or-tools

Is there a method exposed in python to read a .mps file

Feature Request Help Needed Python Linear Solver

Most helpful comment

Ok, I see.

Fair point, won't be in 7.0, but I will look at it.
Laurent Perron | Operations Research | [email protected] | (33) 1 42 68 53
00

Le mer. 23 janv. 2019 à 07:47, Simon notifications@github.com a écrit :

To be clear, is this what you mean with "solve() binary"?:

(1) Download binary (https://developers.google.com/optimization/install/)
& extract it somewhere
(2) Go to directory where it was extracted
(3) Run make build SOURCE=examples/cpp/solve.cc which generates it as
bin/solve
(4) Open the Python file where you initially wanted to solve a .mps file,
and add:

import subprocess
result = subprocess.run(
[
"/path/to/bin/solve", # Might have to be sure to run with working directory as or-tools to make sure the dynamic libraries are linked, but just to simplify
"--input",
lp_mps_filename,
"--output_csv",
csv_filename
],
stdout=subprocess.PIPE
)# ... now parse the csv file etc. etc.

I'm not saying the above pipeline does not work, but isn't it more
convenient to just do:

(1) pip install ortools
(2) Add to the code:

import ortools
solution = ortools.linear_solver.pywraplp.solve(lp_mps_filename) # Something in this trend

Which also avoids the need to do all the "solve() binary" steps on each
platform on which the Python project is deployed.

I can't seem to find the ability to import an LP formatted file.. I do see
the ability to export [1], which states to adhere to the lpsolve 5.1 LP
format [2].

[1]
https://developers.google.com/optimization/reference/lp_data/lp_data/LinearProgram/
[2] http://lpsolve.sourceforge.net/5.1/lp-format.htm

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/google/or-tools/issues/523#issuecomment-456852310,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AKj17XBWWQsdR90Nz_ZSil1_qsu2Ufgfks5vGIQDgaJpZM4QUEiD
.

All 9 comments

We do in C++. It is a bit convoluted :-)

See https://github.com/google/or-tools/blob/master/examples/cpp/solve.cc

I do not see the need in Python. MPS is a file format for executable, you load a problem, solve it, output the solution to file or to screen,

In Python, just create the model using the Python linear API.

I actually think this is a good feature to have. Gurobi, CPLEX, FICO all have this feature built-in, even SAS/OR has it. This feature comes extremely handy when evaluating the solver capabilities.

As it is Moved from ToDo to Done in v6.9, and v6.10 is now in production: is there a way in Python to import a MPS formatted file? The version available via pip is currently v6.10.6025, but the lp_data module is missing.

As discussed on this thread, I belive the main feature is to run a solver
on it. This is done by the examples/cpp/solve.cc exe.

Now, importing a model in a python interpreter does not give you a handle
on the object. so you cannot inspect the model, can inspect the solution.

Do you need it for something else?
Laurent Perron | Operations Research | [email protected] | (33) 1 42 68 53
00

Le lun. 21 janv. 2019 à 17:29, Simon notifications@github.com a écrit :

As it is Moved from ToDo to Done in v6.9, and v6.10 is now in production:
is there a way in Python to import a MPS formatted file? The version
available via pip is currently v6.10.6025, but the lp_data module is
missing.

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/google/or-tools/issues/523#issuecomment-456131222,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AKj17RmJJA4dj9dk8938EzZGOOngkPZ6ks5vFer4gaJpZM4QUEiD
.

I would argue however that a significant portion of developers have some program which already writes a linear program to file, which is subsequently fed to a linear solver. This could be for a multitude of reasons: either they do not want to be tied to a specific solver, or want to use a beefier external machine to solve it for them, or just because it is perceived to be simpler to write to a file in a specific format than having to link and learn another library.

The OR-tools linear solver might not be the fastest out there (I assume, couldn't easily find a fair comparison to other solvers), but it has a one-line cross-platform install procedure (i.e. pip install ortools) for a decent solver. It could become the ideal candidate for fast prototyping software which depends on linear program solutions, and can be used until the problem size is so large it requires a specialized optimized (commercial) solver. A common file format instead of specific API calls is preferable in this transition.

Yes, but the solve() binary can read MPS and LP files.

So what is the workflow?
Laurent Perron | Operations Research | [email protected] | (33) 1 42 68 53
00

Le mar. 22 janv. 2019 à 13:28, Simon notifications@github.com a écrit :

I would argue however that a significant portion of developers have some
program which already writes a linear program to file, which is
subsequently fed to a linear solver. This could be for a multitude of
reasons: either they do not want to be tied to a specific solver, or want
to use a beefier external machine to solve it for them, or just because it
is perceived to be simpler to write to a file in a specific format than
having to link and learn another library.

The OR-tools linear solver might not be the fastest out there (I assume,
couldn't easily find a fair comparison to other solvers), but it has a
one-line cross-platform install procedure (i.e. pip install ortools) for
a decent solver. It could become the ideal candidate for fast prototyping
software which depends on linear program solutions, and can be used until
the problem size is so large it requires a specialized optimized
(commercial) solver. A common file format instead of specific API calls is
preferable in this transition.

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/google/or-tools/issues/523#issuecomment-456382098,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AKj17TcO9pevfY6dfrGUqqRadNCIWf9Lks5vFwPPgaJpZM4QUEiD
.

To be clear, is this what you mean with "solve() binary"?:

(1) Download binary (https://developers.google.com/optimization/install/) & extract it somewhere
(2) Go to directory where it was extracted
(3) Run make build SOURCE=examples/cpp/solve.cc which generates it as bin/solve
(4) Open the Python file where you initially wanted to solve a .mps file, and add:

import subprocess
result = subprocess.run(
        [
            "/path/to/bin/solve",  # Might have to be sure to run with working directory as or-tools to make sure the dynamic libraries are linked, but just to simplify
            "--input",
            lp_mps_filename,
            "--output_csv",
            csv_filename
        ],
        stdout=subprocess.PIPE
    )
# ... now parse the csv file etc. etc.

I'm not saying the above pipeline does not work, but isn't it more convenient to just do:

(1) pip install ortools
(2) Add to the code:

import ortools
solution = ortools.linear_solver.pywraplp.solve(lp_mps_filename)  # Something in this trend

Which also avoids the need to do all the "solve() binary" steps on each platform on which the Python project is deployed.

I can't seem to find the ability to import an LP formatted file.. I do see the ability to export [1], which states to adhere to the lpsolve 5.1 LP format [2]. EDIT: I also can't see which arguments in the "solve() binary" I should give for it to parse and solve an LP formatted file.

[1] https://developers.google.com/optimization/reference/lp_data/lp_data/LinearProgram/
[2] http://lpsolve.sourceforge.net/5.1/lp-format.htm

Ok, I see.

Fair point, won't be in 7.0, but I will look at it.
Laurent Perron | Operations Research | [email protected] | (33) 1 42 68 53
00

Le mer. 23 janv. 2019 à 07:47, Simon notifications@github.com a écrit :

To be clear, is this what you mean with "solve() binary"?:

(1) Download binary (https://developers.google.com/optimization/install/)
& extract it somewhere
(2) Go to directory where it was extracted
(3) Run make build SOURCE=examples/cpp/solve.cc which generates it as
bin/solve
(4) Open the Python file where you initially wanted to solve a .mps file,
and add:

import subprocess
result = subprocess.run(
[
"/path/to/bin/solve", # Might have to be sure to run with working directory as or-tools to make sure the dynamic libraries are linked, but just to simplify
"--input",
lp_mps_filename,
"--output_csv",
csv_filename
],
stdout=subprocess.PIPE
)# ... now parse the csv file etc. etc.

I'm not saying the above pipeline does not work, but isn't it more
convenient to just do:

(1) pip install ortools
(2) Add to the code:

import ortools
solution = ortools.linear_solver.pywraplp.solve(lp_mps_filename) # Something in this trend

Which also avoids the need to do all the "solve() binary" steps on each
platform on which the Python project is deployed.

I can't seem to find the ability to import an LP formatted file.. I do see
the ability to export [1], which states to adhere to the lpsolve 5.1 LP
format [2].

[1]
https://developers.google.com/optimization/reference/lp_data/lp_data/LinearProgram/
[2] http://lpsolve.sourceforge.net/5.1/lp-format.htm

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/google/or-tools/issues/523#issuecomment-456852310,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AKj17XBWWQsdR90Nz_ZSil1_qsu2Ufgfks5vGIQDgaJpZM4QUEiD
.

Happy to see it got some traction, I really appreciate the interaction -- I think it's great that there is a one-line import for an LP solver in Python! :+1: I couldn't unfortunately find which function in the v7.4 release enables the import of LP or MPS file?
http://google.github.io/or-tools/python/ortools/linear_solver/pywraplp.html

I've in the meanwhile a small Python module to do the .lp file parsing -- it's not perfect nor production quality but performed reasonably well so far for my use cases. If you have any feedback, that'd be greatly appreciated!
https://github.com/snkas/python-ortools-lp-parser

Was this page helpful?
0 / 5 - 0 ratings

Related issues

husamrahmanh2o picture husamrahmanh2o  Â·  4Comments

uioplmn picture uioplmn  Â·  5Comments

mlk621 picture mlk621  Â·  3Comments

frodrigo picture frodrigo  Â·  5Comments

partumamet picture partumamet  Â·  4Comments