feat(MLRsearch): MLRsearch v7
[csit.git] / resources / libraries / python / MLRsearch / strategy / refine_lo.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Module defining RefineLoStrategy class."""
15
16
17 from dataclasses import dataclass
18 from typing import Optional, Tuple
19
20 from ..discrete_load import DiscreteLoad
21 from ..discrete_width import DiscreteWidth
22 from ..relevant_bounds import RelevantBounds
23 from .base import StrategyBase
24
25
26 @dataclass
27 class RefineLoStrategy(StrategyBase):
28     """If initial lower bound is still worth it, nominate it.
29
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
33     exists.
34     """
35
36     def nominate(
37         self, bounds: RelevantBounds
38     ) -> Tuple[Optional[DiscreteLoad], Optional[DiscreteWidth]]:
39         """Nominate the initial lower bound.
40
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]]
45         """
46         if not (load := self.initial_lower_load):
47             return None, None
48         if self.not_worth(bounds=bounds, load=load):
49             return None, None
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