在 class 中构建优化模型
building an optimization model inside a class
在我的 class 里面:
class House(object):
我已经实施了一个有效的优化模型。但是,在 class 中实现模型时,我收到一条错误消息。
代码:
def optimization_model(self):
# Define the model: -------------------------------------------------
self.model = ConcreteModel()
# Define time set: --------------------------------------------------
self.model.T = Set(ordered=True, initialize=house_1.index)
# Define parameters: ------------------------------------------------
self.model.price = Param(self.model.T, within=NonNegativeReals, mutable=True)
self.model.R_EV_ch = Param(within=NonNegativeReals, mutable=True)
self.model.R_EV_dis = Param(within=NonNegativeReals, mutable=True)
self.model.DE_EV = Param(within=NonNegativeReals, mutable=True)
self.model.CE_EV = Param(within=NonNegativeReals, mutable=True)
self.model.SOE_EV_ini = Param(within=NonNegativeReals, mutable=True)
self.model.SOE_EV_min = Param(within=NonNegativeReals, mutable=True)
self.model.SOE_EV_max = Param(within=NonNegativeReals, mutable=True)
self.model.delta_T = Param(within=NonNegativeReals, mutable=True)
# Initialize parameters: --------------------------------------------
for t in self.model.T:
self.model.price[t] = price2.loc[t, 'Day-ahead Price [EUR/MWh]']
self.model.R_EV_ch = 7.0
self.model.DE_EV = 1.0 # discharging efficiency, 100% for now
self.model.CE_EV = 1.0 # charging efficiency, 100% for now
self.model.R_EV_dis = 5.0 # discharging rate, not correct value
self.model.SOE_EV_ini = 12.0
self.model.SOE_EV_min = 8.0 # minimum battery capacity?
self.model.SOE_EV_max = 24.0
self.model.delta_T = 0.0833
self.model.T_a = pd.Timestamp('09-01-2017 00:00')
self.model.T_d = pd.Timestamp('09-01-2017 07:00')
# Define decision variables: ----------------------------------------
self.model.P_EV_ch = Var(self.model.T, within=NonNegativeReals)
self.model.P_EV_dis = Var(self.model.T, within=NonNegativeReals)
self.model.SOE_EV = Var(self.model.T, within=NonNegativeReals)
self.model.P_EV_used = Var(self.model.T, within=NonNegativeReals)
self.model.P_EV_sold = Var(self.model.T, within=NonNegativeReals)
self.model.u_EV = Var(self.model.T, within=Binary)
为了简单起见,我只添加了前几个约束:
def cost_rule(model):
return sum(self.model.P_EV_ch[t] * self.model.price[t] * self.model.delta_T for t in self.model.T)
def constraint_0(model, t):
return self.model.P_EV_used[t] + self.model.P_EV_sold[t] == self.model.DE_EV * self.model.P_EV_dis[t]
def constraint_1(model, t):
if t >= self.model.T_a and t <= self.model.T_d:
return self.model.P_EV_ch[t] <= self.model.R_EV_ch * self.model.u_EV[t] # Do you need binary value?
else:
return self.Constraint.Skip
self.model.cost = Objective(rule=cost_rule, sense=1)
self.model.constraint_0 = Constraint(self.model.T, rule=constraint_0)
self.model.constraint_1 = Constraint(self.model.T, rule=constraint_1)
opt = SolverFactory('gurobi')
results = opt.solve(self.model)
我收到以下错误消息:
Traceback (most recent call last):
File "C:/Users/s134927/Documents/TU/Year
5/BEP/programming/import_houses.py", line 236, in <module>
new_house1.optimization_model()
File "C:/Users/s134927/Documents/TU/Year
5/BEP/programming/import_houses.py", line 187, in optimization_model
self.model.constraint_1 = Constraint(self.model.T, rule=constraint_1)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\block.py", line 542, in __setattr__
self.add_component(name, val)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\block.py", line 969, in add_component
val.construct(data)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\constraint.py", line 761, in construct
ndx)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\misc.py", line 61, in apply_indexed_rule
return rule(model, index)
File "C:/Users/s134927/Documents/TU/Year
5/BEP/programming/import_houses.py", line 151, in constraint_1
return self.Constraint.Skip
AttributeError: 'House' object has no attribute 'Constraint'
ERROR: Rule failed when generating expression for constraint constraint_1
with index 2017-09-01 07:05:00:
AttributeError: 'House' object has no attribute 'Constraint'
ERROR: Constructing component 'constraint_1' from data=None failed:
AttributeError: 'House' object has no attribute 'Constraint'
我不知道是什么问题。有没有人可以帮助我?
最后一行说:AttributeError: 'House' object has no attribute 'Constraint'
所以你 class House
的对象没有属性 Constraint
。所以看起来问题出在这段代码之外。作为第一个猜测 - 您已将其声明为 class 静态字段,并作为对象的字段调用。 Link 到文档...
而不是
return self.Constraint.Skip
你应该写
return Constraint.Skip
?
在我的 class 里面:
class House(object):
我已经实施了一个有效的优化模型。但是,在 class 中实现模型时,我收到一条错误消息。 代码:
def optimization_model(self):
# Define the model: -------------------------------------------------
self.model = ConcreteModel()
# Define time set: --------------------------------------------------
self.model.T = Set(ordered=True, initialize=house_1.index)
# Define parameters: ------------------------------------------------
self.model.price = Param(self.model.T, within=NonNegativeReals, mutable=True)
self.model.R_EV_ch = Param(within=NonNegativeReals, mutable=True)
self.model.R_EV_dis = Param(within=NonNegativeReals, mutable=True)
self.model.DE_EV = Param(within=NonNegativeReals, mutable=True)
self.model.CE_EV = Param(within=NonNegativeReals, mutable=True)
self.model.SOE_EV_ini = Param(within=NonNegativeReals, mutable=True)
self.model.SOE_EV_min = Param(within=NonNegativeReals, mutable=True)
self.model.SOE_EV_max = Param(within=NonNegativeReals, mutable=True)
self.model.delta_T = Param(within=NonNegativeReals, mutable=True)
# Initialize parameters: --------------------------------------------
for t in self.model.T:
self.model.price[t] = price2.loc[t, 'Day-ahead Price [EUR/MWh]']
self.model.R_EV_ch = 7.0
self.model.DE_EV = 1.0 # discharging efficiency, 100% for now
self.model.CE_EV = 1.0 # charging efficiency, 100% for now
self.model.R_EV_dis = 5.0 # discharging rate, not correct value
self.model.SOE_EV_ini = 12.0
self.model.SOE_EV_min = 8.0 # minimum battery capacity?
self.model.SOE_EV_max = 24.0
self.model.delta_T = 0.0833
self.model.T_a = pd.Timestamp('09-01-2017 00:00')
self.model.T_d = pd.Timestamp('09-01-2017 07:00')
# Define decision variables: ----------------------------------------
self.model.P_EV_ch = Var(self.model.T, within=NonNegativeReals)
self.model.P_EV_dis = Var(self.model.T, within=NonNegativeReals)
self.model.SOE_EV = Var(self.model.T, within=NonNegativeReals)
self.model.P_EV_used = Var(self.model.T, within=NonNegativeReals)
self.model.P_EV_sold = Var(self.model.T, within=NonNegativeReals)
self.model.u_EV = Var(self.model.T, within=Binary)
为了简单起见,我只添加了前几个约束:
def cost_rule(model):
return sum(self.model.P_EV_ch[t] * self.model.price[t] * self.model.delta_T for t in self.model.T)
def constraint_0(model, t):
return self.model.P_EV_used[t] + self.model.P_EV_sold[t] == self.model.DE_EV * self.model.P_EV_dis[t]
def constraint_1(model, t):
if t >= self.model.T_a and t <= self.model.T_d:
return self.model.P_EV_ch[t] <= self.model.R_EV_ch * self.model.u_EV[t] # Do you need binary value?
else:
return self.Constraint.Skip
self.model.cost = Objective(rule=cost_rule, sense=1)
self.model.constraint_0 = Constraint(self.model.T, rule=constraint_0)
self.model.constraint_1 = Constraint(self.model.T, rule=constraint_1)
opt = SolverFactory('gurobi')
results = opt.solve(self.model)
我收到以下错误消息:
Traceback (most recent call last):
File "C:/Users/s134927/Documents/TU/Year
5/BEP/programming/import_houses.py", line 236, in <module>
new_house1.optimization_model()
File "C:/Users/s134927/Documents/TU/Year
5/BEP/programming/import_houses.py", line 187, in optimization_model
self.model.constraint_1 = Constraint(self.model.T, rule=constraint_1)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\block.py", line 542, in __setattr__
self.add_component(name, val)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\block.py", line 969, in add_component
val.construct(data)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\constraint.py", line 761, in construct
ndx)
File "C:\Users\s134927\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pyomo\core\base\misc.py", line 61, in apply_indexed_rule
return rule(model, index)
File "C:/Users/s134927/Documents/TU/Year
5/BEP/programming/import_houses.py", line 151, in constraint_1
return self.Constraint.Skip
AttributeError: 'House' object has no attribute 'Constraint'
ERROR: Rule failed when generating expression for constraint constraint_1
with index 2017-09-01 07:05:00:
AttributeError: 'House' object has no attribute 'Constraint'
ERROR: Constructing component 'constraint_1' from data=None failed:
AttributeError: 'House' object has no attribute 'Constraint'
我不知道是什么问题。有没有人可以帮助我?
最后一行说:AttributeError: 'House' object has no attribute 'Constraint'
所以你 class House
的对象没有属性 Constraint
。所以看起来问题出在这段代码之外。作为第一个猜测 - 您已将其声明为 class 静态字段,并作为对象的字段调用。 Link 到文档...
而不是
return self.Constraint.Skip
你应该写
return Constraint.Skip
?