Minh Trần
Minh Trần

Reputation: 1

How can I read an LP/MPS file in pulp?

I have a LP/MPS file like this:

Maximize
3x + 2y

Subject To
Constraint1: x + 2y <= 5
Constraint2: 2x - y >= 3

Bounds
0 <= x <= 10
y >= 0

General
x, y

End

Can I read it in pulp or I have to read by myself? I've tried to read it by myself, and it's very complicated. I also searched on the internet for how to read an LP/MPS file, but I didn't find any instructions. Thanks

Upvotes: 0

Views: 271

Answers (1)

Marcus Reaiche
Marcus Reaiche

Reputation: 31

One can use pulp.LpProblem.fromMPS to read a MPS file.

The function signature is:

def fromMPS(cls, filename, sense=const.LpMinimize, **kwargs)

and it returns a tuple where the:

  • first item is problem dictionary of variables;
  • second item is the model instance (pulp.LpProblem instance).

One important detail is that one needs to pass the sense to this function. In general, one should not expect a MPS file to have the sense information and, even if it has, this information is not parsed by this function.

The following code snippet should do the trick assuming that your MPS file path is stored in the file_path variable. In this example, we set the sense as "Maximize" which is not the default behaviour.

import pulp


file_path = "model.mps"
variables_dict, model = pulp.LpProblem.fromMPS(file_path, sense=pulp.LpMaximize)

If you are dealing with a minimization problem, then you don't need to set the sense or set it as pulp.LpMinimize if you want to be explicit.

Upvotes: 1

Related Questions