在类

时间:2017-10-22 12:01:24

标签: python optimization gurobi pyomo

在课堂上:

    class House(object):

我已经提供了一个工作的opzimization模型。但是,在类中实现模型时,我收到一条错误消息。 代码:

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'

我无法弄清问题是什么。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

最后一行说:AttributeError: 'House' object has no attribute 'Constraint'所以您的班级House的对象没有属性Constraint。所以看起来问题位于此代码之外。作为第一个猜测 - 您已将此声明为类静态字段,并将其称为对象的字段。 Link到文档...

答案 1 :(得分:1)

而不是

  

返回self.Constraint.Skip

你应该写吗

  

返回Constraint.Skip