feat(MLRsearch): MLRsearch v7
[csit.git] / resources / libraries / python / MLRsearch / strategy / refine_hi.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 RefineHiStrategy 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 RefineHiStrategy(StrategyBase):
28     """If initial upper bound is still worth it, nominate it.
29
30     This usually happens when halving resulted in relevant lower bound,
31     or if there was no halving (and RefineLoStrategy confirmed initial
32     lower bound became a relevant lower bound for the new current target).
33
34     This either ensures a matching upper bound (target is achieved)
35     or moves the relevant lower bound higher (triggering external search).
36     """
37
38     def nominate(
39         self, bounds: RelevantBounds
40     ) -> Tuple[Optional[DiscreteLoad], Optional[DiscreteWidth]]:
41         """Nominate the initial upper bound.
42
43         :param bounds: Freshly updated bounds relevant for current target.
44         :type bounds: RelevantBounds
45         :returns: Two nones or candidate intended load and duration.
46         :rtype: Tuple[Optional[DiscreteLoad], Optional[DiscreteWidth]]
47         """
48         if not (load := self.initial_upper_load):
49             return None, None
50         if self.not_worth(bounds=bounds, load=load):
51             return None, None
52         self.debug(f"Upperbound refinement available: {load}")
53         # TODO: Limit to possibly smaller than target width?
54         self.expander.limit(self.target.discrete_width)
55         return load, self.target.discrete_width