其他分享
首页 > 其他分享> > 如何在pyomo中保存(pickle)模型实例

如何在pyomo中保存(pickle)模型实例

作者:互联网

我想创建一个模型实例,然后保存它,以便我可以在以后加载和解决(与解决相比,初始化需要很长时间).
当我尝试这个时它给了我以下错误.

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

AttributeError的:
不能腌制本地对象’Euphemia.init..obj_expression

目标函数是这样的:

    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)

有人知道如何保存具体模型吗?

解决方法:

解决方案是cloudpickle模块,常规泡菜有酸洗功能问题.一个例子:

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)

标签:python,pyomo
来源: https://codeday.me/bug/20191003/1848563.html