Prescribed Idiom for Saving and Restoring Pyomo solutions

405 views
Skip to first unread message

Qi Chen

unread,
Jul 10, 2016, 1:37:28 PM7/10/16
to Pyomo Forum
Hi -
I notice that PyomoModel has a couple of functions for save/restore, but I would like to know more about the prescribed idiom for their use, since saving seems to require a results object as an argument, rather than just returning some object with variable values. My intended workflow is:
Build model -> solve model -> get result A -> solve with different objective -> change model slightly -> restore values from result A

Siirola, John D

unread,
Jul 17, 2016, 11:35:26 PM7/17/16
to pyomo...@googlegroups.com

Qi,

 

As with many things in Python (and transitively, Pyomo), there is no One True Way.  That said, here are some of (my) thoughts…

 

from pyomo.environ import *

from pyomo.opt import SolverResults

 

# Get your instance…  I am assuming

#    from my_model_stuff import instance as inst

# but, importing a model and

#    inst = model.create_instance(…) works just as well

               

                # Get the solver…

                solver = SolverFactory(“ipopt”)

 

                # Solve the model

                results = solver.solve(inst)

 

Note that at this point, the solution is already loaded back into the model.  This is A Good Thing (tm), because it is both more efficient to load things directly back into the model and because the model is a far more intuitive interface for you to access the model data when doing your subsequent model modifications.  If you want to save the results for later, you can either re-use the results object you got from solve():

 

                inst.solutions.store_to(results)

 

or you can create a new results object and store things there:

 

                myResultsFromStep1 = SolverResults()

                inst.solutions.store_to(myResultsFromStep1)

 

At this point, you can do anything you want with the results object, including pickling it (recommended) or writing out a JSON or YAML representation using native methods.  Now, go ahead and muck with the model and resolve:

 

                # muck with inst objective….

                inst.obj1.deactivate()

                inst.obj2.activate()

                results2 = solver.solve(inst)

 

Now muck with the instance…  then re-load the solution from the first step:

 

                # Modify inst however you want…  and then

                inst.solutions.load_from(myResultsFromStep1)

 

Now for the caveats:

1)      In the past, we treated the SolverResults object as something that the user would print out and look at (in fact, the results printed by the pyomo command’s –summary option was just the string representation of the results object).  This really isn’t the case anymore – you should consider the results object as an opaque thing that we may (and will likely need to) change.   This is mostly due to the development of hierarchical (block-oriented) modeling in more recent Pyomo releases: It turns out that the string representations in the Solutions section of the results object are not fully compatible with hierarchical models (they mostly work, but I know of several edge cases that will break things – fortunately loudly).  As a result, you should expect the keys in the Solutions section to change from strings to something more akin to ComponentUIDs in the relatively near future.

2)      Gabe will point out that you can pass an option (load_solutions=False) to solver.solve() that will prevent the solution from being automatically loaded into the model.  In that case, solver.solve() puts the solution directly into the results object (although not necessarily using any meaningful string names).  I don’t recommend this, as you will likely want the results in the model anyways to support your model modification routine.  It also ends up polluting the results object with a SymbolMap (an internal data structure we use to manage the mapping between Pyomo components and the strings that identify them in the solver interfaces).  If you then pickle the results object, it will also save the SymbolMap … significantly increasing the size of the pickle.

3)      Finally, depending on how much you modify the model, you may not be able to re-load the original results back into the model without getting an error.  If you run across that problem, let me know and we can work through it to get things working.

 

john

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages