Conditional Objective Function with Python Pyomo

210 Views Asked by At

as part of my master thesis about load shifting of households I would like to extend my current linear objective function (cost minimization) with a non-linear, conditional part.

Right now, my objective function wants to minimize the overall energy costs of a household, thus it looks similar to this:

    Min Cost = Sum(Energy bought from the grid[t])*p_buying[t] - Sum(Energy sold to the grid[t])*p_selling[t]. 

Now I would like to extend the objective function by integrating a utility term which depends on whether the optimized SOC of the battery is higher or lower than the original, non-optimized SOC (which is a fixed value):

                u(x)= (SOC_Bat-SOC_Bat')^alpha        if SOC_Bat >= SOC_Bat'
                    = -delta*(SOC_Bat'-SOC_Bat)^beta  if SOC_Bat<SOC_Bat'

So that in the end the objective function will be (in a simplified way):

       Min C = Energy Costs - Energy Profits +/- Utility of Household k 

However, as I am super new to Python and Pyomo, I don't know how to include such a conditional term into my pyomo objective function.

As I cannot simply integrate an if-clause into my objective function, I thought about implementing the utility-part as piecewise function, however I have no idea how to best do that or if this is even possible - especially as the "SOC_Bat" in my utlity part is a Variable which will be initialized & optimized within the optimization run, so I have no "hard" breakpoints to define for the piecewise function?

I'm using the Ipopt-Solver, so the non-linearity of my problem shouldn't be a problem.

I'm grateful for any help - thanks a lot in advance!

[Update] I tried to use the max() function within my objective function to intergate this problem in the following way:

   def obj_rule(model):
      return sum(p_buying[t]*model.P_spot_Bat[t]
               - p_selling[t]*model.P_Bat_spot[t]
               - p_selling[t]*model.P_pv_spot[t]
               -max(0, (model.SOC_Bat[t] - SOC_Bat_org[24*day+t]))**alpha
               + max(0,(SOC_Bat_org[24*day+t] - model.SOC_Bat[t]))**beta*gamma
               for t in model.Hours)

model.obj = pyo.Objective(rule=obj_rule, sense=pyo.minimize)

However, with this approach, I receive the following error message:

pyomo.common.errors.PyomoException: 
Cannot convert non-constant Pyomo expression (0  <  SOC_Bat[0]) to bool.
This error is usually caused by using a Var, unit, or mutable Param in a
   Boolean context such as an "if" statement, or when checking container membership or equality. 
For example,
>>> m.x = Var()
>>> if m.x >= 1:
...     pass
and
>>> m.y = Var()
>>> if m.y in [m.x, m.y]:
...     pass
would both cause this exception.
0

There are 0 best solutions below