Reputation: 1
I have a MPS file like this:
* ENCODING=ISO-8859-1
NAME test.lp
ROWS
N obj1
L Constraint1
G Constraint2
COLUMNS
x obj1 -3
x Constraint1 1
x Constraint2 2
MARK0000 'MARKER' 'INTORG'
y obj1 -2
y Constraint1 2
y Constraint2 -1
MARK0001 'MARKER' 'INTEND'
RHS
rhs Constraint1 5
rhs Constraint2 3
BOUNDS
UP bnd x 10
LI bnd y 0
ENDATA
Can I generate a pulp problem with it? It can generate automatic or I have to read each line?
I've search on the internet and I can export it like this:
var1, prob1 = LpProblem.fromMPS("test.mps")
I am thinking to generate the problem by take each variable in var1 and so on. Have any way to do it automatically? Thanks
Upvotes: 0
Views: 245
Reputation: 2263
Yes, you can generate a pulp problem from an mps
file.
Please note that the method pulp.LpProblem.fromMPS("test.mps")
that you've found imports a problem from an MPS file, rather than exporting it.
You can find more information on how to import and export models using PuLP here: How to import and export models in PuLP
Having said that, the mps
file you've included in your example is missing the information about the problem's "sense" (whether to minimize or maximize your objective value). When you import your problem using pulp.LpProblem.fromMPS("test.mps")
, PuLP will interpret it as being a minimization problem, when no sense is included inside the mps
file. If you want to maximize the objective value, you can include the parameter sense
to .fromMPS()
method like so:
import pulp
mps_filename = "test.mps"
# Include:
# sense = -1, to maximize the objective function
# sense = 1 (default), to minimize the objective function
lpvars, model = pulp.LpProblem.fromMPS(mps_filename, sense=-1)
status = model.solve(pulp.PULP_CBC_CMD(msg=False))
status_str = pulp.LpStatus[status]
print(status_str)
# Prints: 'Optimal'
The mps format is an industry standard. But it is not very flexible with some information not being stored. It stores only variables and constraints. It does not store the values of variables. In other words, after importing the model, you need to call prob1.solve()
to re-generate the variables optimal values.
Upvotes: 0