Saving Pyomo results from solver as the model runs

54 views
Skip to first unread message

omowu...@gmail.com

unread,
Oct 12, 2021, 6:59:24 AM10/12/21
to Pyomo Forum
Hi,

Please I have a question regarding saving Pyomo results while my model runs. I recently started to run my model on an HPC platform (linux system). Since my model is complex and could take days to run, I am unsure of the walltime to assign to it. I would like an opportunity to create checkpoints by saving the results from the solver as the model runs. When the model stops running due to the walltime limit I want to be able to get some results from the model, even if it isn't optimal. This will enable me use the last results to warmstart the model when I re-run. Please is this possible? Any suggestions will help

Thank you

Ola

Michael Bynum

unread,
Oct 12, 2021, 9:19:21 AM10/12/21
to Pyomo Forum
This depends on the solver. What solver are you using?

Michael

omowu...@gmail.com

unread,
Oct 12, 2021, 1:56:34 PM10/12/21
to Pyomo Forum
Thank you for your response Michael. I am using Gurobi. 

Michael Bynum

unread,
Oct 12, 2021, 2:05:50 PM10/12/21
to Pyomo Forum
This is possible with the GurobiPersistent interface. You can use a callback to save feasible solutions. See the set_callback method (https://pyomo.readthedocs.io/en/stable/library_reference/solvers/gurobi_persistent.html#pyomo.solvers.plugins.solvers.gurobi_persistent.GurobiPersistent.set_callback) of the GurobiPersistent interface (https://pyomo.readthedocs.io/en/stable/advanced_topics/persistent_solvers.html). You will also have to be familiar with the Gurobi Python API (see https://www.gurobi.com/documentation/9.1/refman/cb_codes.html#sec:CallbackCodes). 

Michael

omowu...@gmail.com

unread,
Oct 12, 2021, 9:01:19 PM10/12/21
to Pyomo Forum
Thank you. I will do this.

Ola

omowu...@gmail.com

unread,
Oct 13, 2021, 4:15:45 AM10/13/21
to Pyomo Forum
Hi Michael / Pyomo team, 
Thank you for your last advice. I tried to come up with the following callback function and I am wondering if this will work. I ran this but I didn't find the solutions saved

opt = pe.SolverFactory('gurobi_persistent') 
opt.set_instance(m) 
opt.set_gurobi_param('PreCrush', 1)

def mycallback(cb_m, cb_opt, cb_where):
    if cb_where == GRB.Callback.MIPSOL:
        obj = cb_opt.cbGet(GRB.Callback.MIPSOL_OBJ)
        nodecnt = int(cb_opt.cbGet(GRB.Callback.MIPSOL_NODCNT))
        for v in model.component_objects(Var, active=True):
            varobject = get_var_attr(model, str(v))
            for index in varobject:
                x = cb_opt.cbGetSolution(vars = [varobject[index]] )
                print('**** New solution at node %d, obj %g, sol %d, ' 'x[0] = %g ****' % (nodecnt, obj, x[0]))

opt.set_callback(mycallback) 
opt.solve()

Please can you correct where I may be getting it wrong?

I also noticed Gurobi v9.1.2 has a parameter 'SolFiles' which should save incumbent solution. I tried it in my Pyomo model but I didn't see any files saved. Please does this work in Pyomo?

Thank you for your help.

Michael Bynum

unread,
Oct 13, 2021, 10:05:44 AM10/13/21
to Pyomo Forum
I think the problem is the use of get_var_attr and cbGetSolution. Note that cb_opt.cbGetSolution takes a list of Pyomo variables. I think this should look something like:

def mycallback(cb_m, cb_opt, cb_where):
    if cb_where == GRB.Callback.MIPSOL:
        obj = cb_opt.cbGet(GRB.Callback.MIPSOL_OBJ)
        nodecnt = int(cb_opt.cbGet(GRB.Callback.MIPSOL_NODCNT))
        var_list = [v for v in model.component_data_objects(Var, descend_into=True)]
        var_sol = cb_opt.cbGetSolution(vars=var_list)
        for v, v_sol in zip(var_list, var_sol):
            print(...)

On Wednesday, October 13, 2021 at 2:15:45 AM UTC-6  wrote:
Hi Michael / Pyomo team, 
Thank you for your last advice. I tried to come up with the following callback function and I am wondering if this will work. I ran this but I didn't find the solutions saved

opt = pe.SolverFactory('gurobi_persistent') 
opt.set_instance(m) 
opt.set_gurobi_param('PreCrush', 1)

def mycallback(cb_m, cb_opt, cb_where):
    if cb_where == GRB.Callback.MIPSOL:
        obj = cb_opt.cbGet(GRB.Callback.MIPSOL_OBJ)
        nodecnt = int(cb_opt.cbGet(GRB.Callback.MIPSOL_NODCNT))
        for v in model.component_objects(Var, active=True):
            varobject = get_var_attr(model, str(v))
            for index in varobject:
                x = cb_opt.cbGetSolution(vars = [varobject[index]] )
                print('**** New solution at node %d, obj %g, sol %d, ' 'x[0] = %g ****' % (nodecnt, obj, x[0]))

opt.set_callback(mycallback) 
opt.solve()

Please can you correct where I may be getting it wrong?

I also noticed Gurobi v9.1.2 has a parameter 'SolFiles' which should save incumbent solution. I tried it in my Pyomo model but I didn't see any files saved. Please does this work in Pyomo?

Thank you for your help.

omowunmiayo

unread,
Oct 13, 2021, 10:30:11 AM10/13/21
to pyomo...@googlegroups.com
Thank you so much Michael. This is very helpful.

Best Regards

Ola

--
You received this message because you are subscribed to a topic in the Google Groups "Pyomo Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyomo-forum/M4h8Wk96W-Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/249bd8ef-906f-4708-bab3-4d4d7fb033f5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages