Warm start in PuLP

275 views
Skip to first unread message

Tilda Lundgren

unread,
Feb 14, 2020, 2:22:03 AM2/14/20
to pulp-or-discuss
Hi!

I am new to PuLP and was wondering whether it's possible to save a previous solution and then reuse it as a warm start the next time I run the model.

In DoCPLEX, I would use the following commands:

WRITE COMMAND >> sol.export_as_mst(path="./mst/",basename="filename.mst")
READ COMMAND >> mdl.get_cplex().MIP_starts.read("./mst/filename.mst")

What would the equivalent be in PuLP?

Sen Zhan

unread,
Feb 14, 2020, 3:17:57 AM2/14/20
to pulp-or...@googlegroups.com
Hi if you have access to gurobi solver I can share my experience with you

--
New posters to this group are moderated which can take up to 48 hours, so please be patient if your first post takes a while to turn up.
---
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pulp-or-discuss/967af1f2-a1bd-4091-9834-302dccaece7a%40googlegroups.com.

Tilda Lundgren

unread,
Feb 14, 2020, 6:57:45 AM2/14/20
to pulp-or-discuss
Hi Sen! Yes, please share your experience with doing warm starts in Gurobi!

Den fredag 14 februari 2020 kl. 09:17:57 UTC+1 skrev Sen Zhan:
Hi if you have access to gurobi solver I can share my experience with you

On Fri, Feb 14, 2020 at 08:22 Tilda Lundgren <tilda.v...@gmail.com> wrote:
Hi!

I am new to PuLP and was wondering whether it's possible to save a previous solution and then reuse it as a warm start the next time I run the model.

In DoCPLEX, I would use the following commands:

WRITE COMMAND >> sol.export_as_mst(path="./mst/",basename="filename.mst")
READ COMMAND >> mdl.get_cplex().MIP_starts.read("./mst/filename.mst")

What would the equivalent be in PuLP?

--
New posters to this group are moderated which can take up to 48 hours, so please be patient if your first post takes a while to turn up.
---
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or...@googlegroups.com.

Sen Zhan

unread,
Feb 14, 2020, 7:57:30 AM2/14/20
to pulp-or...@googlegroups.com
Hi Tilda,
After you define all the variables and constraints with PuLP package you add following code: note that prob in the () is your problem name you specified
solver = pulp.GUROBI() 
prob.setSolver(solver)
prob.solver.buildSolverModel(prob)
variables = prob.solverModel.getVars() 
for var in variables:
    var.start = init_dict.get(var.varname) #here the init_dict is a python dict where you store initial values
prob.solver.callSolver(prob) #here you solve the problem
result_dict= {}
for var in variables:
    result_dict[var.varname] = var.x   #you store your new results in this new python dict
obj = prob.solverModel.getObjective().getValue() # you get objective 

It works in my case. Hope it helps for you as well.

Tilda Lundgren <tilda.v.k...@gmail.com> 于2020年2月14日周五 下午12:57写道:
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pulp-or-discuss/de63f905-3769-4815-b671-05f50a668430%40googlegroups.com.

Tilda Lundgren

unread,
Feb 14, 2020, 8:13:43 AM2/14/20
to pulp-or-discuss
Thanks, I'll give it a try!


Den fredag 14 februari 2020 kl. 13:57:30 UTC+1 skrev Sen Zhan:
Hi Tilda,
After you define all the variables and constraints with PuLP package you add following code: note that prob in the () is your problem name you specified
solver = pulp.GUROBI() 
prob.setSolver(solver)
prob.solver.buildSolverModel(prob)
variables = prob.solverModel.getVars() 
for var in variables:
    var.start = init_dict.get(var.varname) #here the init_dict is a python dict where you store initial values
prob.solver.callSolver(prob) #here you solve the problem
result_dict= {}
for var in variables:
    result_dict[var.varname] = var.x   #you store your new results in this new python dict
obj = prob.solverModel.getObjective().getValue() # you get objective 

It works in my case. Hope it helps for you as well.

Tilda Lundgren <tilda.v...@gmail.com> 于2020年2月14日周五 下午12:57写道:
Hi Sen! Yes, please share your experience with doing warm starts in Gurobi!

Den fredag 14 februari 2020 kl. 09:17:57 UTC+1 skrev Sen Zhan:
Hi if you have access to gurobi solver I can share my experience with you

On Fri, Feb 14, 2020 at 08:22 Tilda Lundgren <tilda.v...@gmail.com> wrote:
Hi!

I am new to PuLP and was wondering whether it's possible to save a previous solution and then reuse it as a warm start the next time I run the model.

In DoCPLEX, I would use the following commands:

WRITE COMMAND >> sol.export_as_mst(path="./mst/",basename="filename.mst")
READ COMMAND >> mdl.get_cplex().MIP_starts.read("./mst/filename.mst")

What would the equivalent be in PuLP?

--
New posters to this group are moderated which can take up to 48 hours, so please be patient if your first post takes a while to turn up.
---
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pulp-or-discuss/967af1f2-a1bd-4091-9834-302dccaece7a%40googlegroups.com.

--
New posters to this group are moderated which can take up to 48 hours, so please be patient if your first post takes a while to turn up.
---
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or...@googlegroups.com.

Franco Peschiera

unread,
Feb 14, 2020, 12:26:16 PM2/14/20
to pulp-or-discuss
Thanks Sen,

We have not added support for the `GUROBI` API in the mip-start functionality in pulp but you are tight you can already access the solver to do that. I'm going to copy your example to make it available soon, I hope.

Alternatively, pulp already offers a solver-agnostic way of doing mip-start for some solver APIs (GUROBI_CMD, CPLEX_CMD and CBC_CMD). I've just updated the pulp docs with a guide on doing mip-start in pulp: http://coin-or.github.io/pulp/guides/how_to_mip_start.html

The other "how to" I also added (a relatively common question in the forums) is the configuration of the solver so PuLP can find it: http://coin-or.github.io/pulp/guides/how_to_configure_solvers.html

Cheers,

Franco

Tilda Lundgren

unread,
Feb 17, 2020, 6:04:37 AM2/17/20
to pulp-or-discuss
I tried to follow the instructions posted in http://coin-or.github.io/pulp/guides/how_to_mip_start.html, but got the following error:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-11-35506d6cc784> in <module>
      1 for variable, value in initial_solution.items():
----> 2     variable.setInitialValue(value)

/anaconda3/lib/python3.7/site-packages/pulp/pulp.py in setInitialValue(self, val)
    496         may of may not be supported by the solver
    497         """
--> 498         raise NotImplementedError
    499 
    500 

NotImplementedError: 


Any idea why?

Sen Zhan

unread,
Feb 17, 2020, 6:08:16 AM2/17/20
to pulp-or...@googlegroups.com
I tried before setInitialValue, but it didnt work as well. Alternatively you can go with Julia where you can use JuMP package to build models.

Tilda Lundgren <tilda.v.k...@gmail.com> 于2020年2月17日周一 下午12:04写道:
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pulp-or-discuss/1432dbd9-6280-443c-b3f6-4006bbf945d3%40googlegroups.com.

Franco Peschiera

unread,
Feb 17, 2020, 7:37:54 AM2/17/20
to pulp-or-discuss

Hello Sen and Tilda,


This functionality is somewhat new (that’s why there was no documentation of it).
To be able to use it, you’d need to reinstall pulp to its newer version (2.0 as of now):


pip install -U pulp


F.


El lunes, 17 de febrero de 2020, 12:08:16 (UTC+1), Sen Zhan escribió:

I tried before setInitialValue, but it didnt work as well. Alternatively you can go with Julia where you can use JuMP package to build models.

To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.

Tilda Lundgren

unread,
Feb 17, 2020, 8:35:23 AM2/17/20
to pulp-or-discuss
Thanks for your quick reply Franco!

I updated pulp to 2.0, but I'm still getting an error:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-21-35506d6cc784> in <module>

      1 for variable, value in initial_solution.items():
----> 2     variable.setInitialValue(value)

/anaconda3/lib/python3.7/site-packages/pulp/pulp.py in setInitialValue(self, val)
    496         for constraint, coeff in e.items():
    497             constraint.addVariable(self,coeff)
--> 498 
    499     def setInitialValue(self, val, check=True):
    500         """sets the initial value of the Variable to val

NotImplementedError: 


Franco Peschiera

unread,
Feb 18, 2020, 8:42:43 AM2/18/20
to pulp-or-discuss

Hello Tilda,

Sorry to hear that it’s not working. Are you sure that you’ve installed pulp 2.0 in your python 3.7 anacoda3 installation though?

I sometimes make the mistake of mixing all the several python environments installed in my pc…

You can check the PuLP version with the method here: https://stackoverflow.com/a/32965521

or by doing:

    import pulp
    print(pulp.__version__)

F.

Reply all
Reply to author
Forward
0 new messages