我正在尝试在PYOMO中解决一个抽象模型,但是在将数据导入模型中创建实例时遇到了麻烦。
我一直在网上寻找导入数据的文档/示例,但是我看不出我的文档与在网上找到的有效示例有什么不同。 ''' ##致电PYOMO 从pyomo.environ导入*
## Define Model
model = AbstractModel()
# Define index sets
# I= set if calculation steps where investments are possible
model.I = Set(dimen=1)
model.I.construct()
# D = set of DERs
model.D = Set(dimen=1)
model.D.construct()
## Define Parameters
a = 0.5
model.NFix = Param(model.D, model.I, within=PositiveReals)
model.NVar = Param(model.D, model.I, within=PositiveReals)
model.RFix = Param(model.D, model.I, within=PositiveReals)
model.RVar = Param(model.D, model.I, within=PositiveReals)
model.y = Param(model.I, within=PositiveReals)
## Define variables
model.n = Var(model.D, model.I, within = Binary)
model.r = Var(model.D, model.I, within = Binary)
model.NCap = Var(model.D, model.I, within = PositiveReals)
model.RCap = Var(model.D, model.I, within = PositiveReals)
## Define objective value
model.cost = Objective(sum((sum(model.n[d,i]*model.NFix[d,i]+ model.NCap[d,i]*model.NVar[d,i] +
model.r[d,i]*model.RFix[d,i] +model.RCap[d,i]*model.RVar[d,i] for d in model.D))/(1+a)**y[i]) for i in model.I)
instance = model.create_instance('data.dat')
opt = pyo.SolverFactory('glpk')
opt.solve(instance)
对于data.dat文件,我具有以下内容:
set I := i1 i2 i3;
set D := d1 d2;
param NFix : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param NVar : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param RFix : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param RVar : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param y := 1 2 ;
我收到以下错误消息:
错误:从data = {('d1','i1'):1,('d2','i1')构造组件'NFix': 1,('d1','i2'):1,('d2','i2'):3,('d1','i3'):3,('d2','i3'):5} 失败: RuntimeError:无法为param = NFix,index =('d1','i1'), 值= 1。 源错误消息=“索引'('d1','i1')'对于索引无效 组件“ NFix””
答案 0 :(得分:1)
我在您的模型文件和dat文件中发现了几个小错误。
在模型文件中,永远不要手动调用Pyomo组件上的construct
方法。在目标函数中,括号放置不正确,并且在使用Abstract模型时,需要确保使用规则定义目标函数。我还注意到您在引用model.
之前忘记了y
。该文件的更正版本如下:
## Define Model
model = AbstractModel()
# Define index sets
# I= set if calculation steps where investments are possible
model.I = Set(dimen=1)
# D = set of DERs
model.D = Set(dimen=1)
## Define Parameters
a = 0.5
model.NFix = Param(model.D, model.I, within=PositiveReals)
model.NVar = Param(model.D, model.I, within=PositiveReals)
model.RFix = Param(model.D, model.I, within=PositiveReals)
model.RVar = Param(model.D, model.I, within=PositiveReals)
model.y = Param(model.I, within=PositiveReals)
## Define variables
model.n = Var(model.D, model.I, within = Binary)
model.r = Var(model.D, model.I, within = Binary)
model.NCap = Var(model.D, model.I, within = PositiveReals)
model.RCap = Var(model.D, model.I, within = PositiveReals)
## Define objective value
def _obj(model):
return sum((sum(model.n[d,i]*model.NFix[d,i]+ model.NCap[d,i]*model.NVar[d,i] + \
model.r[d,i]*model.RFix[d,i] +model.RCap[d,i]*model.RVar[d,i] for d in model.D)) \
/(1+a)**model.y[i] for i in model.I)
model.cost = Objective(rule=_obj)
instance = model.create_instance('abstract.dat')
opt = SolverFactory('glpk')
opt.solve(instance, tee=True)
您的dat文件中唯一的问题是您没有正确定义y
参数。在模型中,看来y
被I
索引了,这意味着您需要定义如下参数:
param y :=
i1 1
i2 2
i3 3 ;