Hoekieee
Hoekieee

Reputation: 391

building an optimization model inside a class

Inside my class:

    class House(object):

I have inplemented a working opzimization model. However, when implementing the model inside the class I get an error message. Code:

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)

For simplicity I only add the first few constraints:

    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)

I get the following error message:

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'

I could not figure out what the problem is. Is there someone who can help me out?

Upvotes: 0

Views: 539

Answers (2)

silvado
silvado

Reputation: 18197

Instead of

return self.Constraint.Skip

should you maybe write

return Constraint.Skip

?

Upvotes: 1

Michał Zaborowski
Michał Zaborowski

Reputation: 4387

Last line says: AttributeError: 'House' object has no attribute 'Constraint' so your object of class House has no attribute Constraint. So looks like problem is located outside this code. As a first guess - you've declared this as class static field, and calling as object's field. Link to the docs...

Upvotes: 1

Related Questions