Skip to contents

Functions optimize_demand, add_battery_optimization or smart_charging make use of Quadratic programming in order to obtain the optimal power load given certain conditions. The Quadratic programming problem can be formulated according to multiple objectives. Currently, flextools package allows to optimize a time-series power load considering multiple objectives:

  • Minimize the power exchanged with the grid (net power)
  • Minimize the energy cost

In this article, we’ll cover the optimization problem for the first objective, net power minimization, for both the flexible demand (e.g. heatpumps, electric vehicles, etc.) and the battery.

Demand optimization

To minimize the energy exchanged with the distribution grid while maximizing the use of local generation, using the flexibility from a power demand profile, the objective function of the optimization problem has been raised in the following way:

mint=1T(LSt+OtGt)2+λ(OtLFt)2 min \sum_{t=1}^{T} (LS_t + O_t - G_t)^2 + \lambda (O_t-LF_t)^{2}

Where:

  • TT : Number of time intervals within the optimization window
  • GtG_t : Local power generation time-series vector
  • LStLS_t : Non-flexible load time-series vector
  • LFtLF_t : Flexible load time-series vector (if not optimized)
  • OtO_t : Optimal flexible load time-series vector
  • λ\lambda : penalty on change for the flexible load

Moreover, the squared term penalizes high values of net power, so the optimization always results in the most flat as possible net power profile (peak shaving).

This optimization problem has the following constraints:

  • The energy consumed by the flexible load must remain the same than the expected behavior:

t=1TOtΔt=t=1TLFtΔt \sum_{t=1}^T O_t \Delta t = \sum_{t=1}^T LF_t \Delta t

  • Optimal flexible load must be lower than a certain maximum power LFmaxLF_{max}:

0OtLFmaxttT 0 \le O_t \le LFmax_t \quad t \in T

  • The energy balance between generation and loads must be lower than the grid import capacity ICtIC_t:

Ot+LStGtICttT O_t + LS_t - G_t \le IC_t \quad t \in T

At the same time, we can optimize the flexible energy demand with two opposite approaches:

  1. Postpone the consumption to later time-slots (shift the energy forward)
  2. Consume now (store) the energy that will be consumed later (shift the energy backward)

We consider both approaches for different objectives or applications. The energy shift is always done within a maximum time horizon (hh) to consider realistic scenarios. For example, the time horizon for the energy demand of a water boiler could have a time horizon of 6 hours, because it wouldn’t make sense to heat the water more than 6 hours before the final consumption. Each one of these approaches brings extra constraints to the optimization problem:

If the energy can only be shifted forward:

  • The cumulative sum of the optimal load OO must be higher than the cumulative sum of the original flexible load LFLF except the last hh time slots, and lower than the total cumulative sum of the original flexible load LFLF (energy can only be shifted forwards):

t=1uhLFtt=1uOtt=1uLFtu=1T \sum_{t = 1}^{u-h} LF_t \le \sum_{t=1}^u O_t \le \sum_{t=1}^u LF_t \quad u = 1 \dots T

  • The maximum values for the optimal demand OtO_t will depend on the time horizon hh:

Out=uhuLFtu=1T O_u \le \sum_{t = u-h}^u LF_t \quad u = 1 \dots T

If the energy can only be shifted backward:

  • The cumulative sum of the optimal demand OO must be higher than the total cumulative sum of the original demand LFLF (energy can only be shifted backwards), and lower than the cumulative sum of the original demand LFLF except the following hh time slots:

t=1uLFtt=1uOtt=1u+hLFtu=1T \sum_{t=1}^u LF_t \le \sum_{t=1}^u O_t \le \sum_{t=1}^{u+h} LF_t \quad u = 1 \dots T

  • The maximum values for the optimal demand OtO_t will depend on the time horizon hh:

Out=uu+hLFtu=1T O_u \le \sum_{t = u}^{u+h} LF_t \quad u = 1 \dots T

Battery optimization

To minimize the energy exchanged with the distribution grid while maximizing the use of local generation, using the flexibility from a battery, the objective function of the optimization problem has been raised in the following way:

mint=1T(Lt+BtGt)2+λt=1T1(Bt+1Bt)2 min \sum_{t=1}^{T} (L_t + B_t - G_t)^2 + \lambda \sum_{t=1}^{T-1}(B_{t+1} - B_t)^{2}

Where:

  • TT : Number of time intervals within the optimization window
  • GtG_t : Local power generation time-series vector
  • LtL_t : Power load time-series vector
  • BtB_t : Optimal battery power time-series vector. Positive charging, negative discharging.
  • λ\lambda : penalty on change for the flexible load

Additionally, this optimization problem also counts with the following parameters used in the constraints below:

  • BcapB_{cap} : Battery capacity
  • BcB_c : Maximum charging power
  • BdB_d : Maximum discharging power
  • SOCminSOC_{min} : Minimum state of charge of the battery
  • SOCmaxSOC_{max} : Maximum state of charge of the battery
  • SOCiniSOC_{ini} : state of charge at the beginning/end of the optimization window

Optimization constraints:

  • Battery power (positive = charging, BcB_c, negative = discharging, BdB_d) limits:

BdBtBctT -B_d \le B_t \le B_c \quad t \in T

  • State of charge limits:

SOCminSOCini+t=1TBtΔtBcapSOCmaxtT SOC_{min} \le SOC_{ini} + \frac{\sum_{t=1}^T B_t \Delta t}{B_{cap}} \le SOC_{max} \quad t \in T

  • The balance of charged/discharge energy must be 0 at the end of the optimization window to have the same initial state of charge at the beginning of every optimization window:

t=1TBtΔt=0 \sum_{t=1}^T B_t \Delta t = 0

  • The energy balance of generation and loads must be between the import capacity ICtIC_t and export capacity ECtEC_t:

ECtBt+LtGtICttT -EC_t \le B_t + L_t - G_t \le IC_t \quad t \in T