Search code examples
pythonoptimizationpyomo

Optimization of a battery storage with pyomo


I'm currently trying to optimize the CO2-Emissions of a househould based on the electricity consumption. It also includes a battery storage. But for some reason the state of energy calculation doesnt work. I keep getting this error:

WARNING: Implicitly replacing the Component attribute soe (type=<class
    'pyomo.core.base.var.IndexedVar'>) on block unknown with a new Component
    (type=<class 'pyomo.core.base.constraint.IndexedConstraint'>). This is
    usually indicative of a modelling error. To avoid this warning, use
    block.del_component() and block.add_component().

Restarting kernel...

This is my code so far:

model = ConcreteModel()

n = 30
model.t = RangeSet(1, n)

model.consumption = Param(model.t, initialize = df['Consumption'])
model.pv = Param(model.t, initialize = df['PV'])
model.emissionen = Param(model.t, initialize = df['CO2-Emissions'])
model.heatpump = Param(model.t, initialize = df['Heatpump'])

in_out_leistung = bt.iloc[1]['Values']
in_out_efficiency = bt.iloc[2]['Values']
battery_capacity = bt.iloc[0]['Values'] 
soe_start = 0
elec_import_max = 200

model.soe = Var(model.t, initialize = 0, within = NonNegativeReals)
model.charge = Var(model.t, within = NonNegativeReals, initialize = 0)
model.discharge = Var(model.t, within = NonNegativeReals, initialize = 0)
model.elec_grid = Var(model.t, bounds = (0, elec_import_max), within = NonNegativeReals)

def discharge_capacity_rule(model, t):
    return model.discharge[t] <= in_out_leistung
model.discharge_capacity_rule = Constraint(model.t, rule = discharge_capacity_rule)

def charge_capacity_rule(model, t):
    return model.charge[t] <= in_out_leistung
model.charge_capacity_rule = Constraint(model.t, rule = charge_capacity_rule)
    
def max_capacity_rule(model, t):
    return model.soe[t] <= battery_capacity
model.max_capacity_rule = Constraint(model.t, rule = max_capacity_rule)
    
def soe_start_rule(model):
    return model.soe[1] == soe_start 
model.soe_start_rule = Constraint(rule = soe_start_rule)

def soe(model, t):
    if t == 1:
        return model.soe[t] == soe_start
    else:
        return model.soe[t] == model.soe[t-1] + (model.charge[t] * in_out_efficiency) - model.discharge[t] / in_out_efficiency
model.soe = Constraint(model.t, rule = soe)

def soe_end_rule(model):
    return model.soe[n] == model.soe[1]
model.soe_end_rule = Constraint(rule = soe_end_rule)

def demand(model, t):
    return model.demand[t] == model.heatpump[t] + model.consumption[t] + model.charge[t]
model.demand = Constraint(model.t, rule = demand)

def lastdeckung(model, t):
    return model.pv[t] + model.elec_grid[t] + model.discharge[t] == model.demand[t]
model.lastdeckung = Constraint(model.t, rule = lastdeckung)

def emissionsreduzierung(model, t):
    return sum(model.elec_grid[t] * model.emissionen[t] for t in model.n)
model.emissionsreduzierung = Objective(rule = emissionsreduzierung, sense = minimize)

The storage ist supposed to be empty at the beginning and at the end aswell.


Solution

  • So I had a few mistakes here and there. For example in the objective function where I forgot to change model.n to model.t because I renamed that one in the process of writing the code. The biggest mistake thought was forgetting to create a variable for the demand.

    model.demand = Var(model.t, within = NonNegativeReals)
    
    def demand_rule(model, t):
        return model.demand[t] == model.heatpump[t] + model.consumption[t] + model.charge[t]
    model.demand_rule = Constraint(model.t, rule = demand_rule)
    

    Now it actually works.