|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Utilities for checking and clamping bounds and power values to exclusion bounds.""" |
| 5 | + |
| 6 | +from ...timeseries import Bounds, Power |
| 7 | + |
| 8 | + |
| 9 | +def check_exclusion_bounds_overlap( |
| 10 | + lower_bound: Power, |
| 11 | + upper_bound: Power, |
| 12 | + exclusion_bounds: Bounds[Power] | None, |
| 13 | +) -> tuple[bool, bool]: |
| 14 | + """Check if the given bounds overlap with the given exclusion bounds. |
| 15 | +
|
| 16 | + Example: |
| 17 | +
|
| 18 | + ``` |
| 19 | + lower upper |
| 20 | + .----- exclusion zone -----. |
| 21 | + -----|✓✓✓✓✓✓✓✓✓✓✓✓|xxxxxxxxxxxxxxx|----------|---- |
| 22 | + `-- usable --'-- exclusion --´ |
| 23 | + | overlap | |
| 24 | + | | |
| 25 | + lower upper |
| 26 | + bound bound |
| 27 | + (inside the exclusion zone) |
| 28 | + ``` |
| 29 | +
|
| 30 | + Resulting in `(False, True)` because only the upper bound is inside the |
| 31 | + exclusion zone. |
| 32 | +
|
| 33 | + Args: |
| 34 | + lower_bound: The lower bound to check. |
| 35 | + upper_bound: The upper bound to check. |
| 36 | + exclusion_bounds: The exclusion bounds to check against. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + A tuple containing a boolean indicating if the lower bound is bounded by the |
| 40 | + exclusion bounds, and a boolean indicating if the upper bound is bounded by |
| 41 | + the exclusion bounds. |
| 42 | + """ |
| 43 | + if exclusion_bounds is None: |
| 44 | + return False, False |
| 45 | + |
| 46 | + bounded_lower = False |
| 47 | + bounded_upper = False |
| 48 | + |
| 49 | + if exclusion_bounds.lower < lower_bound < exclusion_bounds.upper: |
| 50 | + bounded_lower = True |
| 51 | + if exclusion_bounds.lower < upper_bound < exclusion_bounds.upper: |
| 52 | + bounded_upper = True |
| 53 | + |
| 54 | + return bounded_lower, bounded_upper |
| 55 | + |
| 56 | + |
| 57 | +def adjust_exclusion_bounds( |
| 58 | + lower_bound: Power, |
| 59 | + upper_bound: Power, |
| 60 | + exclusion_bounds: Bounds[Power] | None, |
| 61 | +) -> tuple[Power, Power]: |
| 62 | + """Adjust the given bounds to exclude the given exclusion bounds. |
| 63 | +
|
| 64 | + Args: |
| 65 | + lower_bound: The lower bound to adjust. |
| 66 | + upper_bound: The upper bound to adjust. |
| 67 | + exclusion_bounds: The exclusion bounds to adjust to. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + The adjusted lower and upper bounds. |
| 71 | + """ |
| 72 | + if exclusion_bounds is None: |
| 73 | + return lower_bound, upper_bound |
| 74 | + |
| 75 | + # If the given bounds are within the exclusion bounds, there's no room to adjust, |
| 76 | + # so return zero. |
| 77 | + # |
| 78 | + # And if the given bounds overlap with the exclusion bounds on one side, then clamp |
| 79 | + # the given bounds on that side. |
| 80 | + match check_exclusion_bounds_overlap(lower_bound, upper_bound, exclusion_bounds): |
| 81 | + case (True, True): |
| 82 | + return Power.zero(), Power.zero() |
| 83 | + case (False, True): |
| 84 | + return lower_bound, exclusion_bounds.lower |
| 85 | + case (True, False): |
| 86 | + return exclusion_bounds.upper, upper_bound |
| 87 | + return lower_bound, upper_bound |
| 88 | + |
| 89 | + |
| 90 | +# Just 20 lines of code in this function, but unfortunately 8 of those are return |
| 91 | +# statements, and that's too many for pylint. |
| 92 | +def clamp_to_bounds( # pylint: disable=too-many-return-statements |
| 93 | + value: Power, |
| 94 | + lower_bound: Power, |
| 95 | + upper_bound: Power, |
| 96 | + exclusion_bounds: Bounds[Power] | None, |
| 97 | +) -> tuple[Power | None, Power | None]: |
| 98 | + """Clamp the given value to the given bounds. |
| 99 | +
|
| 100 | + When the given value can falls within the exclusion zone, and can be clamped to |
| 101 | + both sides, both options will be returned. |
| 102 | +
|
| 103 | + When the given value falls outside the usable bounds and can be clamped only to |
| 104 | + one side, only that option will be returned. |
| 105 | +
|
| 106 | + Args: |
| 107 | + value: The value to clamp. |
| 108 | + lower_bound: The lower bound to clamp to. |
| 109 | + upper_bound: The upper bound to clamp to. |
| 110 | + exclusion_bounds: The exclusion bounds to clamp outside of. |
| 111 | +
|
| 112 | + Returns: |
| 113 | + The clamped value. |
| 114 | + """ |
| 115 | + # If the given bounds are within the exclusion bounds, return zero. |
| 116 | + # |
| 117 | + # And if the given bounds overlap with the exclusion bounds on one side, and the |
| 118 | + # given power is in that overlap region, clamp it to the exclusion bounds on that |
| 119 | + # side. |
| 120 | + if exclusion_bounds is not None: |
| 121 | + match check_exclusion_bounds_overlap( |
| 122 | + lower_bound, upper_bound, exclusion_bounds |
| 123 | + ): |
| 124 | + case (True, True): |
| 125 | + return None, None |
| 126 | + case (True, False): |
| 127 | + if value < exclusion_bounds.upper: |
| 128 | + return None, exclusion_bounds.upper |
| 129 | + case (False, True): |
| 130 | + if value > exclusion_bounds.lower: |
| 131 | + return exclusion_bounds.lower, None |
| 132 | + |
| 133 | + # If the given value is outside the given bounds, clamp it to the closest bound. |
| 134 | + if value < lower_bound: |
| 135 | + return lower_bound, None |
| 136 | + if value > upper_bound: |
| 137 | + return None, upper_bound |
| 138 | + |
| 139 | + # If the given value is within the exclusion bounds and the exclusion bounds are |
| 140 | + # within the given bounds, clamp the given value to the closest exclusion bound. |
| 141 | + if exclusion_bounds is not None: |
| 142 | + if exclusion_bounds.lower < value < exclusion_bounds.upper: |
| 143 | + return exclusion_bounds.lower, exclusion_bounds.upper |
| 144 | + |
| 145 | + return value, value |
0 commit comments