How can I save a problem?

65 views
Skip to first unread message

Bertie Wooster

unread,
Sep 2, 2018, 10:28:07 PM9/2/18
to pulp-or-discuss
Hello,

I have an optimal allocation problem which takes much longer to create than solve. Each subsequent time I solve it the problem has only changed slightly, so I decided to start maintaining statefulness of the problem rather than recreating it from scratch each time I run the program.

I noticed we have the pulp.LpProblem.writeLP() function. But it is not clear to me how to load it back up again as a pulp.LpProblem object since there seems to be no method to create one from file.

Please advise on how to go about this, and any other tips/advice for the best way to go about saving/serialising a problem then reloading it later. 

Thanks,
Matt

Franco Peschiera

unread,
Sep 3, 2018, 4:35:27 PM9/3/18
to pulp-or-discuss

Hello Bertie,

As far as I know there is no import implemented for lp or mps files.

First of all, is your model code shareable? I say this because from my experience there can be a lot of variation in speed depending on the way the model is constructed in python.

Do you know where the time is consumed? in the model construction? preprocessingM in the writing of the file? depending on this, different solutions may exist. For example, writing the file into a memory disk.

Although I have not do it myself with pulp, I believe you can edit the model object directly. At least it was my impression you could do it with the gurobi and cplex libraries. For example take a look here.

hope that helps.

F.

SUPPLY CHAIN CUBE

unread,
Sep 20, 2018, 12:33:59 AM9/20/18
to pulp-or-discuss
Hi Bertie,

You may save the problem using Shelve, below is the sample code to save LpProblem:

Here prob is your LpProblem object
To save the problem:

import shelve
with shelve.open('TestProblem.db') as shelf:
shelf['prob'] = prob

#To load problem from text file:
with shelve.open('TestProblem.db') as shelf:
prob = shelf['prob']

juan pineros

unread,
May 6, 2020, 3:02:37 AM5/6/20
to pulp-or-discuss
Hi Supply chain cube.

Do you have any examples of the code structure   ?


Because I dont know if after ou before the send problem to solve

prob.solve()

and I load after the finish the first time of solve problem


......
......
prob.solve()

with shelve.open('TestProblem.db') as shelf:
    prob = shelf['prob']

# fix value of some  variavel
------------------------------

# solve by the second time without need to recreate the problem
prob.solve()
Reply all
Reply to author
Forward
0 new messages