feat(MLRsearch): MLRsearch v7
[csit.git] / resources / libraries / python / MLRsearch / strategy / extend_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 ExtendHiStrategy 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 ExtendHiStrategy(StrategyBase):
28     """This strategy is applied when there is no relevant upper bound.
29
30     Typically this is needed after RefineHiStrategy turned initial upper bound
31     into a current relevant lower bound.
32     """
33
34     def nominate(
35         self, bounds: RelevantBounds
36     ) -> Tuple[Optional[DiscreteLoad], Optional[DiscreteWidth]]:
37         """Nominate current relevant lower bound plus expander width.
38
39         This performs external search in upwards direction,
40         until a valid upper bound for the current target is found,
41         or until max load is hit.
42         Limit handling is used to avoid nominating too close
43         (or above) the max rate.
44
45         Width expansion is only applied if the candidate becomes a lower bound,
46         so that is detected in done method.
47
48         :param bounds: Freshly updated bounds relevant for current target.
49         :type bounds: RelevantBounds
50         :returns: Two nones or candidate intended load and duration.
51         :rtype: Tuple[Optional[DiscreteLoad], Optional[DiscreteWidth]]
52         """
53         if bounds.chi or not bounds.clo or bounds.clo >= self.handler.max_load:
54             return None, None
55         width = self.expander.get_width()
56         load = self.handler.handle(
57             load=bounds.clo + width,
58             width=self.target.discrete_width,
59             clo=bounds.clo,
60             chi=bounds.chi,
61         )
62         if self.not_worth(bounds=bounds, load=load):
63             return None, None
64         self.debug(f"No chi, extending up: {load}")
65         return load, width
66
67     def won(self, bounds: RelevantBounds, load: DiscreteLoad) -> None:
68         """Expand width if the load became the new lower bound.
69
70         :param bounds: Freshly updated bounds relevant for current target.
71         :param load: The current load, so strategy does not need to remember.
72         :type bounds: RelevantBounds
73         :type load: DiscreteLoad
74         """
75         if load == bounds.clo:
76             self.expander.expand()