Skip to content

Commit 9cb09d7

Browse files
committed
Add code comments explaining the ShiftingMatryoshka implementation
1 parent 22653fd commit 9cb09d7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/frequenz/sdk/microgrid/_power_managing/_shifting_matryoshka.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,45 +99,66 @@ def _calc_targets(
9999

100100
target_power = Power.zero()
101101
for next_proposal in sorted(proposals, reverse=True):
102+
# if a priority is given, the bounds calculated until that priority is
103+
# reached will be the bounds available to an actor with the given priority.
104+
#
105+
# This could mean that the calculated target power is incorrect and should
106+
# not be used.
102107
if priority is not None and next_proposal.priority <= priority:
103108
break
104109

110+
# When the upper bound is less than the lower bound, if means that there's
111+
# no more room to process further proposals, so we break out of the loop.
105112
if upper_bound < lower_bound:
106113
break
107114

108115
proposal_lower = next_proposal.bounds.lower or lower_bound
109116
proposal_upper = next_proposal.bounds.upper or upper_bound
110117
proposal_power = next_proposal.preferred_power
111118

119+
# Make sure that if the proposal specified bounds, they make sense.
112120
if proposal_upper < proposal_lower:
113121
continue
114122

123+
# If the proposal bounds are outside the available bounds, we need to
124+
# adjust the proposal bounds to fit within the available bounds.
115125
if proposal_lower >= upper_bound:
116126
proposal_lower = upper_bound
117127
proposal_upper = upper_bound
118128
elif proposal_upper <= lower_bound:
119129
proposal_lower = lower_bound
120130
proposal_upper = lower_bound
121131

132+
# Clamp the available bounds by the proposal bounds.
122133
lower_bound = max(lower_bound, proposal_lower)
123134
upper_bound = min(upper_bound, proposal_upper)
124135

125136
if proposal_power is not None:
137+
# If this is the first power setting proposal, then hold on to the
138+
# bounds that were available at that time, for use when applying the
139+
# exclusion bounds to the target power at the end.
126140
if top_pri_bounds is None and proposal_power != Power.zero():
127141
top_pri_bounds = Bounds[Power](lower=lower_bound, upper=upper_bound)
142+
# Clamp the proposal power to its available bounds.
128143
proposal_power = _get_nearest_possible_power(
129144
proposal_power,
130145
lower_bound,
131146
upper_bound,
132147
None,
133148
)
149+
# Shift the available bounds by the proposal power.
134150
lower_bound = lower_bound - proposal_power
135151
upper_bound = upper_bound - proposal_power
152+
# Add the proposal power to the target power (aka shift in the opposite direction).
136153
target_power += proposal_power
137154

155+
# The `top_pri_bounds` is to ensure that when applying the exclusion bounds to
156+
# the target power at the end, we respect the bounds that were set by the first
157+
# power-proposing actor.
138158
if top_pri_bounds is not None:
139159
available_bounds = top_pri_bounds
140160

161+
# Apply the exclusion bounds to the target power.
141162
target_power = _get_nearest_possible_power(
142163
target_power,
143164
available_bounds.lower,

0 commit comments

Comments
 (0)