I am currently trying to optimize a battery storage. For the case that there is a overproduction of pv electricity I am trying to come up with a overproduction constraint.
I first tried this version which doesnt work:
def pv_overproduction(model, t):
if model.demand[t] <= model.pv[t]:
return model.excess_pv[t] == model.pv[t] - model.demand[t]
else:
model.excess_pv[t] == 0
model.pv_overproduction = Constraint(model.t, rule = pv_overproduction)
As far as I understood this one doesnt work because I cant use a Variable in the if statement. But I dont have a way to work around that one.
This is the load coverage function in order to reduce the pv electricity input:
def load_coverage(model, t):
return (model.pv[t] - model.excess_pv[t]) + model.elec_grid[t] + model.discharge[t] == model.demand[t]
model.load_coverage = Constraint(model.t, rule = load_coverage)
This was my second attempt which sadly doesnt work either.
def pv_overproduction(model, t):
return model.excess_pv[t] == model.pv[t] - model.demand[t]
model.pv_overproduction = Constraint(model.t, rule = pv_overproduction)
My second attempt doenst work due to the model.excess_pv[t] being negative most of the time which in general makes sense. But I also dont need the negative values because that obviously means there is no overproduction...
Any help to work around the mentioned problems would be appreciated.
I think your first attempt is very close. The if
statement is not necessary if you use inequality constraints correctly...
constraint to capture the overage:
excess >= supply - demand
capture only the excess when it is non-negative:
excess >= 0 (or alternatively set the domain to non-negative reals, which is equivalent)
put a small (or large) penalty in your objective, assuming that your problem is a minimization
obj = ... + penalty * excess
Plug in a couple test values to ensure you believe it! :)