How to save (pickle) a model instance in pyomo

纵饮孤独 提交于 2019-11-30 06:09:44

问题


I want to create a model instance and then save it so I can load and solve at a later time (the initialization takes quite long compared to the solving). When I tried this it gave me the following error.

with open('model.pickle', 'w') as f:
    pickle.dump(instance, f)

AttributeError: Can't pickle local object 'Euphemia.init..obj_expression

The objective function is this:

    def obj_expression(model):
        curve = sum(model.x[area, hour, Type, index] * model.Q[area, hour, Type, index] * 
                    ( model.P1[area, hour, Type, index] + model.P0[area, hour, Type, index] ) / 2  
                        for (area, hour, Type, index) in model.Curve )
        bids = sum(model.y[area, index] * model.PB[area, index] * 
                       sum( model.QB[area, index, hour] for (hour) in model.Hours ) 
                               for (area, index) in model.Bids  )
        return curve + bids
    self.model.OBJ = pe.Objective(rule = obj_expression, sense = pe.maximize)

does anybody know how to save a concrete model?


回答1:


The solution is the cloudpickle module, regular pickle has problems pickling functions. An example:

import cloudpickle

with open('test.pkl', mode='wb') as file:
   cloudpickle.dump(instance, file)


with open('test.pkl', mode='rb') as file:
   instance = cloudpickle.load(file)



回答2:


It looks like pickle (and also cloudpickle) will not work when DerivativeVar is used.

from pyomo.environ import *
from pyomo.dae import *
import pickle
import dill
model = ConcreteModel()
model.x = ContinuousSet(initialize=(0., 1.))
model.y = Var(model.x, initialize=1.)
with open('model1.pickle', 'wb') as f:
    pickle.dump(model, f)

model.dydx = DerivativeVar(model.y, wrt=model.x)
with open('model2.pickle', 'wb') as f:
    try:
        pickle.dump(model, f)
    except TypeError:
        dill.dump(model, f)

dill appears to work instead for pickling a weakref: Pickling weakref in Python



来源:https://stackoverflow.com/questions/50834258/how-to-save-pickle-a-model-instance-in-pyomo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!