java.lang.Object
io.openems.edge.controller.ess.timeofusetariff.optimizer.Utils

public final class Utils extends Object
Utils for TimeOfUseTariffController.

All energy values are in [Wh] and positive, unless stated differently.

  • Field Details

    • ESS_MAX_SOC

      public static final float ESS_MAX_SOC
      Keep some buffer to avoid scheduling errors because of bad predictions.
      See Also:
    • ESS_CHARGE_C_RATE

      public static final float ESS_CHARGE_C_RATE
      C-Rate (capacity divided by time) during StateMachine.CHARGE_GRID. With a C-Rate of 0.5 the battery gets fully charged within 2 hours.
      See Also:
    • SUM_GRID

      protected static final ChannelAddress SUM_GRID
    • SUM_PRODUCTION

      protected static final ChannelAddress SUM_PRODUCTION
    • SUM_UNMANAGED_CONSUMPTION

      protected static final ChannelAddress SUM_UNMANAGED_CONSUMPTION
    • SUM_CONSUMPTION

      protected static final ChannelAddress SUM_CONSUMPTION
    • SUM_ESS_DISCHARGE_POWER

      protected static final ChannelAddress SUM_ESS_DISCHARGE_POWER
    • SUM_ESS_SOC

      protected static final ChannelAddress SUM_ESS_SOC
    • EXECUTION_LIMIT_SECONDS_BUFFER

      protected static final long EXECUTION_LIMIT_SECONDS_BUFFER
      See Also:
    • EXECUTION_LIMIT_SECONDS_MINIMUM

      protected static final long EXECUTION_LIMIT_SECONDS_MINIMUM
      See Also:
  • Method Details

    • initializeRandomRegistryForProduction

      public static void initializeRandomRegistryForProduction()
      Initializes the Jenetics RandomRegistry for production.
    • initializeRandomRegistryForUnitTest

      public static void initializeRandomRegistryForUnitTest()
      Initializes the Jenetics RandomRegistry for JUnit tests.
    • createSimulatorParams

      public static Params createSimulatorParams(Context context, TreeMap<ZonedDateTime,Period> existingSchedule) throws InvalidValueException
      Create Params for Simulator.
      Parameters:
      context - the Context object
      existingSchedule - the existing schedule, i.e. result of previous optimization
      Returns:
      Params
      Throws:
      InvalidValueException - on error
    • generateProductionPrediction

      protected static Integer[] generateProductionPrediction(Integer[] prediction, int minLength)
      Postprocesses production prediction; makes sure length is at least the same as consumption prediction - filling up with zeroes.
      Parameters:
      prediction - the production prediction
      minLength - the min length (= consumption prediction length)
      Returns:
      new production prediction
    • joinConsumptionPredictions

      protected static Integer[] joinConsumptionPredictions(int splitAfterIndex, Integer[] totalConsumption, Integer[] unmanagedConsumption)
    • paramsAreValid

      protected static boolean paramsAreValid(Params p)
    • getEssMinSocEnergy

      protected static int getEssMinSocEnergy(Context context, int essCapacity)
      Returns the amount of energy that is not available for scheduling because of a configured minimum SoC.
      Parameters:
      context - the Context
      essCapacity - net SymmetricEss.ChannelId.CAPACITY
      Returns:
      the value in [Wh]
    • getEssMinSocPercentage

      public static int getEssMinSocPercentage(List<ControllerEssLimitTotalDischarge> ctrlLimitTotalDischarges, List<ControllerEssEmergencyCapacityReserve> ctrlEmergencyCapacityReserves)
      Returns the configured minimum SoC, or zero.
      Parameters:
      ctrlLimitTotalDischarges - the list of ControllerEssLimitTotalDischarge
      ctrlEmergencyCapacityReserves - the list of ControllerEssEmergencyCapacityReserve
      Returns:
      the value in [%]
    • interpolateArray

      protected static double[] interpolateArray(Double[] values)
      Interpolate an Array of Doubles.

      Replaces nulls with previous value. If first entry is null, it is set to first available value. If all values are null, all are set to 0.

      Parameters:
      values - the values
      Returns:
      values without nulls
    • interpolateArray

      protected static int[] interpolateArray(Integer[] values)
      Interpolate an Array of Integers.

      Replaces nulls with previous value. If first entry is null, it is set to first available value. If all values are null, all are set to 0.

      Parameters:
      values - the values
      Returns:
      values without nulls
    • calculateMaxChargeEnergy

      protected static int calculateMaxChargeEnergy(int essMaxSocEnergy, int essMaxEnergyPerPeriod, int essInitial)
      Calculates the ESS max charge energy for a period.
      Parameters:
      essMaxSocEnergy - ESS energy below a configured maximium SoC [Wh]
      essMaxEnergyPerPeriod - ESS max charge/discharge energy per period [Wh]
      essInitial - ESS initially available energy (SoC in [Wh]) of the given period
      Returns:
      the value in [Wh]
    • calculateMaxDischargeEnergy

      protected static int calculateMaxDischargeEnergy(int essMinSocEnergy, int essMaxEnergyPerPeriod, int essInitial)
      Calculates the ESS max discharge energy for a period.
      Parameters:
      essMinSocEnergy - ESS energy below a configured minimum SoC [Wh]
      essMaxEnergyPerPeriod - ESS max charge/discharge energy per period [Wh]
      essInitial - ESS initially available energy (SoC in [Wh]) of the given period
      Returns:
      the value in [Wh]
    • calculateBalancingEnergy

      protected static int calculateBalancingEnergy(int essMaxCharge, int essMaxDischarge, int production, int consumption)
      Calculates the ESS charge (negative) or discharge (positive) energy for a period in StateMachine.BALANCING.
      Parameters:
      essMaxCharge - ESS max charge energy
      essMaxDischarge - ESS max discharge energy
      production - Production prediction
      consumption - Consumption prediction
      Returns:
      the value in [Wh]
    • calculateParamsChargeEnergyInChargeGrid

      protected static int calculateParamsChargeEnergyInChargeGrid(int essMinSocEnergy, int essMaxSocEnergy, int[] productions, int[] consumptions, double[] prices)
      Calculates the default ESS charge energy per period in StateMachine.CHARGE_GRID.

      Applies ESS_CHARGE_C_RATE with the minimum of usable ESS energy or predicted consumption energy that cannot be supplied from production.

      Parameters:
      essMinSocEnergy - ESS energy below a configured minimum SoC [Wh]
      essMaxSocEnergy - ESS energy below a configured maximium SoC [Wh]
      productions - Production predictions per period
      consumptions - Consumption predictions per period
      prices - Prices per period
      Returns:
      the value in [Wh]
    • findFirstPeakIndex

      protected static int findFirstPeakIndex(int fromIndex, double[] values)
    • findFirstValleyIndex

      protected static int findFirstValleyIndex(int fromIndex, double[] values)
    • calculateChargeGridEnergy

      protected static int calculateChargeGridEnergy(int essMaxCharge, int essChargeInState, int maxBuyFromGrid, int production, int consumption)
      Calculates the ESS charge energy for one period in StateMachine.CHARGE_GRID.
      Parameters:
      essMaxCharge - ESS max charge energy [Wh]
      essChargeInState - ESS target charge energy in CHARGE_GRID [Wh]
      maxBuyFromGrid - Max buy-from-grid energy [Wh]
      production - Production prediction
      consumption - Consumption prediction
      Returns:
      the value in [Wh]
    • handleGetScheduleRequest

      public static GetScheduleResponse handleGetScheduleRequest(Optimizer optimizer, UUID requestId, Timedata timedata, String componentId, ZonedDateTime now) throws OpenemsError.OpenemsNamedException
      Utilizes the previous three hours' data and computes the next 21 hours data from the Optimizer provided, then concatenates them to generate a 24-hour GetScheduleResponse.
      Parameters:
      optimizer - the Optimizer
      requestId - the JSON-RPC request-id
      timedata - theTimedata
      componentId - the Component-ID
      now - the current ZonedDateTime (will get rounded down to 15 minutes)
      Returns:
      the GetScheduleResponse
      Throws:
      OpenemsError.OpenemsNamedException - on error
    • createSchedule

      public static com.google.gson.JsonArray createSchedule(List<Utils.ScheduleData> datas, ZonedDateTime timestamp)
      Generates a 24-hour schedule as a JsonArray. The resulting schedule includes timestamped entries of predicted 'state', 'price', 'production', 'consumption', 'soc' for each 15-minute interval.
      Parameters:
      datas - the list of Utils.ScheduleDatas
      timestamp - the timestamp of the first entry in the schedule
      Returns:
      the schedule data as a JsonArray
    • calculateExecutionLimitSeconds

      public static long calculateExecutionLimitSeconds(Clock clock)
      Calculates the ExecutionLimitSeconds for the Optimizer.
      Parameters:
      clock - a clock
      Returns:
      execution limit in [s]
    • postprocessSimulatorState

      public static StateMachine postprocessSimulatorState(Params p, int essChargeDischarge, int essInitialEnergy, int balancingChargeDischarge, StateMachine state)
      Post-Process a state of a Period during Simulation, i.e. replace with 'better' state with the same behaviour.

      NOTE: heavy computation is ok here, because this method is called only at the end with the best Schedule.

      Parameters:
      p - the Params
      essChargeDischarge - the scheduled ESS charge/discharge energy for this period
      essInitialEnergy - the initial ESS energy in this period
      balancingChargeDischarge - the ESS energy that would be required for BALANCING
      state - the initial state
      Returns:
      the new state
    • postprocessRunState

      public static StateMachine postprocessRunState(int minSoc, Integer soc, int production, StateMachine state)
      Post-Process a state during Controller.run(), i.e. replace with 'better' state if appropriate.

      NOTE: this can be useful, if live operation deviates from predicted operation, e.g. because predictions were wrong.

      Parameters:
      minSoc - the configured Minimum-SoC, or zero
      soc - the current SymmetricEss.ChannelId.SOC
      production - the current Sum.ChannelId.PRODUCTION_ACTIVE_POWER, or zero
      state - the initial state
      Returns:
      the new state
    • calculateEssChargeInChargeGridPowerFromParams

      protected static int calculateEssChargeInChargeGridPowerFromParams(Params params, ManagedSymmetricEss ess)
    • calculateChargeGridPower

      public static Integer calculateChargeGridPower(Params params, ManagedSymmetricEss ess, Sum sum, int maxChargePowerFromGrid)
      Calculates the Max-ActivePower constraint for StateMachine.CHARGE_GRID.
      Parameters:
      params - the Params
      ess - the ManagedSymmetricEss
      sum - the Sum
      maxChargePowerFromGrid - the configured max charge from grid power
      Returns:
      the set-point or null
    • calculateMaxChargeProductionPower

      public static Integer calculateMaxChargeProductionPower(Sum sum)
      Calculates the Max-ActivePower constraint for
      invalid reference
      StateMachine#CHARGE_PRODUCTION
      .
      Parameters:
      sum - the Sum
      Returns:
      the set-point
    • calculateDelayDischargePower

      public static Integer calculateDelayDischargePower(ManagedSymmetricEss ess)
      Calculates the ActivePower constraint for StateMachine.DELAY_DISCHARGE.
      Parameters:
      ess - the ManagedSymmetricEss
      Returns:
      the set-point
    • toEnergy

      protected static int toEnergy(int power)
      Converts power [W] to energy [Wh/15 min].
      Parameters:
      power - the power value
      Returns:
      the energy value
    • toPower

      public static Integer toPower(Integer energy)
      Converts energy [Wh/15 min] to power [W].
      Parameters:
      energy - the energy value
      Returns:
      the power value
    • logSchedule

      protected static void logSchedule(Params params, TreeMap<ZonedDateTime,Period> periods)
      Prints the Schedule to System.out.

      NOTE: The output format is suitable as input for "RunOptimizerFromLogApp". This is useful to re-run a simulation.

      Parameters:
      params - the Params
      periods - the map of Periods