Skip to content

Commit

Permalink
Allow optionally forcing power requests
Browse files Browse the repository at this point in the history
A power request might need to be forced to implement
safety mechanisms, even when some components might be
seemingly failing (i.e. when there is not proper consumption
information, the user wants to slowly discharge batteries to
prevent potential peak breaches).

Signed-off-by: Daniel Zullo <[email protected]>
  • Loading branch information
daniel-zullo-frequenz committed May 25, 2023
1 parent 022cfac commit 24a08eb
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 16 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ This release drops support for Python versions older than 3.11.

* Now `frequenz.sdk.timeseries.Sample` uses a more sensible comparison. Before this release `Sample`s were compared only based on the `timestamp`. This was due to a limitation in Python versions earlier than 3.10. Now that the minimum supported version is 3.11 this hack is not needed anymore and `Sample`s are compared using both `timestamp` and `value` as most people probably expects.

* A power request can now be forced by setting the `force` attribute. This is especially helpful as a safety measure when components appear to be failing, such as when battery metrics are unavailable. Note that applications previously relying on automatic fallback to all batteries when none of them was working will now require the force attribute to be explicitly set in the request.

## New Features

<!-- Here goes the main new features and examples or instructions on how to use them -->
Expand Down
130 changes: 114 additions & 16 deletions src/frequenz/sdk/actor/power_distributing/power_distributing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging
from asyncio.tasks import ALL_COMPLETED
from dataclasses import dataclass, replace
from datetime import datetime, timedelta, timezone
from math import isnan
from typing import ( # pylint: disable=unused-import
Any,
Expand Down Expand Up @@ -62,6 +63,34 @@ class _User:
"""The bidirectional channel to communicate with the user."""


@dataclass
class _CacheEntry:
"""Represents an entry in the cache with expiry time."""

inv_bat_pair: InvBatPair
"""The inverter and adjacent battery data pair."""

def __init__(
self, inv_bat_pair: InvBatPair, duration: timedelta = timedelta(hours=2.5)
) -> None:
"""Initialize a CacheEntry instance.
Args:
inv_bat_pair: the inverter and adjacent battery data pair to cache.
duration: the duration of the cache entry.
"""
self.inv_bat_pair = inv_bat_pair
self._expiry_time = datetime.now(tz=timezone.utc) + duration

def is_expired(self) -> bool:
"""Check whether the cache entry has expired.
Returns:
whether the cache entry has expired.
"""
return datetime.now(tz=timezone.utc) >= self._expiry_time


@actor
class PowerDistributingActor:
# pylint: disable=too-many-instance-attributes
Expand Down Expand Up @@ -211,6 +240,10 @@ def __init__(
max_data_age_sec=10.0,
)

self._cached_metrics: dict[int, _CacheEntry | None] = {
bat_id: None for bat_id, _ in self._bat_inv_map.items()
}

def _create_users_tasks(self) -> List[asyncio.Task[None]]:
"""For each user create a task to wait for request.
Expand All @@ -224,37 +257,43 @@ def _create_users_tasks(self) -> List[asyncio.Task[None]]:
)
return tasks

def _get_upper_bound(self, batteries: Set[int]) -> float:
def _get_upper_bound(self, batteries: Set[int], force_use_all: bool) -> float:
"""Get total upper bound of power to be set for given batteries.
Note, output of that function doesn't guarantee that this bound will be
the same when the request is processed.
Args:
batteries: List of batteries
force_use_all: whether all batteries in the power request must be used.
Returns:
Upper bound for `set_power` operation.
"""
pairs_data: List[InvBatPair] = self._get_components_data(batteries)
pairs_data: List[InvBatPair] = self._get_components_data(
batteries, force_use_all
)
return sum(
min(battery.power_upper_bound, inverter.active_power_upper_bound)
for battery, inverter in pairs_data
)

def _get_lower_bound(self, batteries: Set[int]) -> float:
def _get_lower_bound(self, batteries: Set[int], force_use_all: bool) -> float:
"""Get total lower bound of power to be set for given batteries.
Note, output of that function doesn't guarantee that this bound will be
the same when the request is processed.
Args:
batteries: List of batteries
force_use_all: whether all batteries in the power request must be used.
Returns:
Lower bound for `set_power` operation.
"""
pairs_data: List[InvBatPair] = self._get_components_data(batteries)
pairs_data: List[InvBatPair] = self._get_components_data(
batteries, force_use_all
)
return sum(
max(battery.power_lower_bound, inverter.active_power_lower_bound)
for battery, inverter in pairs_data
Expand Down Expand Up @@ -282,21 +321,19 @@ async def run(self) -> None:

try:
pairs_data: List[InvBatPair] = self._get_components_data(
request.batteries
request.batteries, request.force
)
except KeyError as err:
await user.channel.send(Error(request=request, msg=str(err)))
continue

if len(pairs_data) == 0:
if not pairs_data and not request.force:
error_msg = f"No data for the given batteries {str(request.batteries)}"
await user.channel.send(Error(request=request, msg=str(error_msg)))
continue

try:
distribution = self.distribution_algorithm.distribute_power(
request.power, pairs_data
)
distribution = self._get_power_distribution(request, pairs_data)
except ValueError as err:
error_msg = f"Couldn't distribute power, error: {str(err)}"
await user.channel.send(Error(request=request, msg=str(error_msg)))
Expand Down Expand Up @@ -379,6 +416,52 @@ async def _set_distributed_power(

return self._parse_result(tasks, distribution.distribution, timeout_sec)

def _get_power_distribution(
self, request: Request, inv_bat_pairs: List[InvBatPair]
) -> DistributionResult:
"""Get power distribution result for the batteries in the request.
Args:
request: the power request to process.
inv_bat_pairs: the battery and adjacent inverter data pairs.
Returns:
the power distribution result.
"""

def distribute_power_equally(
power: float, batteries: set[int]
) -> DistributionResult:
power_per_battery = power / len(batteries)
return DistributionResult(
distribution={
self._bat_inv_map[battery_id]: power_per_battery
for battery_id in batteries
},
remaining_power=0.0,
)

if request.force and not inv_bat_pairs:
return distribute_power_equally(request.power, request.batteries)

available_bat_ids = {battery.component_id for battery, _ in inv_bat_pairs}
unavailable_bat_ids = request.batteries - available_bat_ids

result = self.distribution_algorithm.distribute_power(
request.power, inv_bat_pairs
)

if request.force and unavailable_bat_ids:
additional_result = distribute_power_equally(
result.remaining_power, unavailable_bat_ids
)

for inv_id, power in additional_result.distribution.items():
result.distribution[inv_id] = power
result.remaining_power = 0.0

return result

def _check_request(self, request: Request) -> Optional[Result]:
"""Check whether the given request if correct.
Expand All @@ -388,7 +471,7 @@ def _check_request(self, request: Request) -> Optional[Result]:
Returns:
Result for the user if the request is wrong, None otherwise.
"""
if len(request.batteries) == 0:
if not request.batteries:
return Error(request=request, msg="Empty battery IDs in the request")

for battery in request.batteries:
Expand All @@ -401,11 +484,11 @@ def _check_request(self, request: Request) -> Optional[Result]:

if not request.adjust_power:
if request.power < 0:
bound = self._get_lower_bound(request.batteries)
bound = self._get_lower_bound(request.batteries, request.force)
if request.power < bound:
return OutOfBound(request=request, bound=bound)
else:
bound = self._get_upper_bound(request.batteries)
bound = self._get_upper_bound(request.batteries, request.force)
if request.power > bound:
return OutOfBound(request=request, bound=bound)

Expand Down Expand Up @@ -554,11 +637,14 @@ def _get_components_pairs(

return bat_inv_map, inv_bat_map

def _get_components_data(self, batteries: Set[int]) -> List[InvBatPair]:
def _get_components_data(
self, batteries: Set[int], force_use_all: bool
) -> List[InvBatPair]:
"""Get data for the given batteries and adjacent inverters.
Args:
batteries: Batteries that needs data.
force_use_all: whether all batteries in the power request must be used.
Raises:
KeyError: If any battery in the given list doesn't exists in microgrid.
Expand All @@ -568,7 +654,9 @@ def _get_components_data(self, batteries: Set[int]) -> List[InvBatPair]:
"""
pairs_data: List[InvBatPair] = []
working_batteries = (
self._all_battery_status.get_working_batteries(batteries) or batteries
batteries
if force_use_all
else self._all_battery_status.get_working_batteries(batteries)
)

for battery_id in working_batteries:
Expand All @@ -581,6 +669,12 @@ def _get_components_data(self, batteries: Set[int]) -> List[InvBatPair]:
inverter_id: int = self._bat_inv_map[battery_id]

data = self._get_battery_inverter_data(battery_id, inverter_id)
if not data and force_use_all:
cached_entry = self._cached_metrics[battery_id]
if cached_entry and not cached_entry.is_expired():
data = cached_entry.inv_bat_pair
else:
data = None
if data is None:
_logger.warning(
"Skipping battery %d because its message isn't correct.",
Expand Down Expand Up @@ -648,7 +742,9 @@ def _get_battery_inverter_data(

# If all values are ok then return them.
if not any(map(isnan, replaceable_metrics)):
return InvBatPair(battery_data, inverter_data)
inv_bat_pair = InvBatPair(battery_data, inverter_data)
self._cached_metrics[battery_id] = _CacheEntry(inv_bat_pair)
return inv_bat_pair

# Replace NaN with the corresponding value in the adjacent component.
# If both metrics are None, return None to ignore this battery.
Expand All @@ -670,10 +766,12 @@ def _get_battery_inverter_data(
elif isnan(inv_bound):
inverter_new_metrics[inv_attr] = bat_bound

return InvBatPair(
inv_bat_pair = InvBatPair(
replace(battery_data, **battery_new_metrics),
replace(inverter_data, **inverter_new_metrics),
)
self._cached_metrics[battery_id] = _CacheEntry(inv_bat_pair)
return inv_bat_pair

async def _create_channels(self) -> None:
"""Create channels to get data of components in microgrid."""
Expand Down
3 changes: 3 additions & 0 deletions src/frequenz/sdk/actor/power_distributing/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ class Request:
If `False` and the power is outside the batteries' bounds, the request will
fail and be replied to with an `OutOfBound` result.
"""

force: bool = False
"""Whether to force the power request regardless the status of components."""
Loading

0 comments on commit 24a08eb

Please sign in to comment.