2929from ...timeseries import Power
3030from . import _bounds
3131from ._base_classes import BaseAlgorithm , Proposal , _Report
32- from ._sorted_set import SortedSet
3332
3433if typing .TYPE_CHECKING :
3534 from ...timeseries ._base_types import SystemBounds
@@ -44,12 +43,12 @@ class Matryoshka(BaseAlgorithm):
4443 def __init__ (self , max_proposal_age : timedelta ) -> None :
4544 """Create a new instance of the matryoshka algorithm."""
4645 self ._max_proposal_age_sec = max_proposal_age .total_seconds ()
47- self ._component_buckets : dict [frozenset [int ], SortedSet [Proposal ]] = {}
46+ self ._component_buckets : dict [frozenset [int ], set [Proposal ]] = {}
4847 self ._target_power : dict [frozenset [int ], Power ] = {}
4948
5049 def _calc_target_power (
5150 self ,
52- proposals : SortedSet [Proposal ],
51+ proposals : set [Proposal ],
5352 system_bounds : SystemBounds ,
5453 ) -> Power :
5554 """Calculate the target power for the given components.
@@ -83,7 +82,7 @@ def _calc_target_power(
8382 exclusion_bounds = system_bounds .exclusion_bounds
8483
8584 target_power = Power .zero ()
86- for next_proposal in reversed (proposals ):
85+ for next_proposal in sorted (proposals , reverse = True ):
8786 if upper_bound < lower_bound :
8887 break
8988 if next_proposal .preferred_power :
@@ -183,9 +182,10 @@ def calculate_target_power(
183182 return None
184183
185184 if proposal is not None :
186- self ._component_buckets .setdefault (component_ids , SortedSet ()).insert (
187- proposal
188- )
185+ bucket = self ._component_buckets .setdefault (component_ids , set ())
186+ if proposal in bucket :
187+ bucket .remove (proposal )
188+ bucket .add (proposal )
189189
190190 # If there has not been any proposal for the given components, don't calculate a
191191 # target power and just return `None`.
@@ -243,7 +243,9 @@ def get_status(
243243 ):
244244 exclusion_bounds = system_bounds .exclusion_bounds
245245
246- for next_proposal in reversed (self ._component_buckets .get (component_ids , [])):
246+ for next_proposal in sorted (
247+ self ._component_buckets .get (component_ids , []), reverse = True
248+ ):
247249 if next_proposal .priority <= priority :
248250 break
249251 proposal_lower = next_proposal .bounds .lower or lower_bound
@@ -286,4 +288,4 @@ def drop_old_proposals(self, loop_time: float) -> None:
286288 if (loop_time - proposal .creation_time ) > self ._max_proposal_age_sec :
287289 to_delete .append (proposal )
288290 for proposal in to_delete :
289- bucket .delete (proposal )
291+ bucket .remove (proposal )
0 commit comments