1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Module defining RefineLoStrategy class."""
17 from dataclasses import dataclass
18 from typing import Optional, Tuple
20 from ..discrete_load import DiscreteLoad
21 from ..discrete_width import DiscreteWidth
22 from ..relevant_bounds import RelevantBounds
23 from .base import StrategyBase
27 class RefineLoStrategy(StrategyBase):
28 """If initial lower bound is still worth it, nominate it.
30 This usually happens when halving resulted in relevant upper bound,
31 or if there was no halving.
32 This ensures a relevant bound (upper or lower) for the current target
37 self, bounds: RelevantBounds
38 ) -> Tuple[Optional[DiscreteLoad], Optional[DiscreteWidth]]:
39 """Nominate the initial lower bound.
41 :param bounds: Freshly updated bounds relevant for current target.
42 :type bounds: RelevantBounds
43 :returns: Two nones or candidate intended load and duration.
44 :rtype: Tuple[Optional[DiscreteLoad], Optional[DiscreteWidth]]
46 if not (load := self.initial_lower_load):
48 if self.not_worth(bounds=bounds, load=load):
50 self.debug(f"Lowerbound refinement available: {load}")
51 # TODO: Limit to possibly smaller than target width?
52 self.expander.limit(self.target.discrete_width)
53 return load, self.target.discrete_width