从Pyomo块继承的类
所以,我正在开发一个具体的能量模型,我想利用Pyomo中的块特征。特别是,我想创建一个类DieselGenerator,它是Block的子类。我希望在DieselGenerator中具有与Block相同的功能,例如索引等,但也为类提供一些特定于模型的函数。下面是生成类的代码。调用超级函数会出现以下错误: “TypeError:无法将集合运算符应用于索引块组件 (柴油发电机) 提前感谢您的帮助:)从Pyomo块继承的类,pyomo,Pyomo,所以,我正在开发一个具体的能量模型,我想利用Pyomo中的块特征。特别是,我想创建一个类DieselGenerator,它是Block的子类。我希望在DieselGenerator中具有与Block相同的功能,例如索引等,但也为类提供一些特定于模型的函数。下面是生成类的代码。调用超级函数会出现以下错误: “TypeError:无法将集合运算符应用于索引块组件 (柴油发电机) 提前感谢您的帮助:) 欢迎来到SO。请在您的帖子中包含完整的回溯。查看您的导入内容会非常有帮助,不清楚Block来自何处。
欢迎来到SO。请在您的帖子中包含完整的回溯。查看您的导入内容会非常有帮助,不清楚
Block
来自何处。Pyomo有两个主要接口:environ
和kernel
<代码>环境组件的设计不便于子类化。如果您想这样做,那么您应该看看内核
:
Class DieselGenerator(Block):
def __init__(self,d,t):
super().__init__(self,d,t)
def construct(self, name, d,file="BlockGen.yaml"):
try:
f = open(file)
generators_dict = yaml.load(f)
return generators_dict[d][name]
except:
print("Generator file not found. Verify name and directory path")
def power_output_rule(self):
return self.fuel_cost == self.fuel_cost* self.status
def fuel_consumption_rule(self):
power_min = self.rated_power* self.status
return self.output== power_min
self.d = d
self.t = t
self.rated_power = Param(initialize = construct(self, "rated_power",self.d))
self.output = Var(within=NonNegativeReals)
self.status = Var(within=Boolean)
self.fuel_cost = Param(within=NonNegativeReals, initialize= construct(self,"fuel_cost",self.d))
self.fuel_consumption = Constraint(rule = fuel_consumption_rule)
self.power_output = Constraint(rule = power_output_rule)`