Revert "fix(IPsecUtil): Delete keywords no longer used"
[csit.git] / resources / libraries / python / MLRsearch / expander.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 TargetExpander class."""
15
16
17 from dataclasses import dataclass, field
18 from typing import Callable, Optional
19
20 from .dataclass import secondary_field
21 from .discrete_load import DiscreteLoad
22 from .discrete_width import DiscreteWidth
23 from .global_width import GlobalWidth
24 from .limit_handler import LimitHandler
25 from .target_spec import TargetSpec
26
27
28 @dataclass
29 class TargetedExpander:
30     """Utility class to track expanding width during external search.
31
32     One instance per selector but takes into consideration global current width.
33
34     Generally, many strategies may limit next_width immediately,
35     but next_width expands only after measurement
36     when external search fails to find its bound (global width is also bumped).
37     See strategy classes for specific details on external and internal search.
38     """
39
40     target: TargetSpec
41     """The target this strategy is focusing on."""
42     global_width: GlobalWidth
43     """Reference to the global width tracking instance."""
44     initial_lower_load: Optional[DiscreteLoad]
45     """Smaller of the two loads distinguished at instance creation.
46     Can be None if initial upper bound is the min load."""
47     initial_upper_load: Optional[DiscreteLoad]
48     """Larger of the two loads distinguished at instance creation.
49     Can be None if initial lower bound is the max load."""
50     handler: LimitHandler = field(repr=False)
51     """Reference to the class used to avoid too narrow intervals."""
52     debug: Callable[[str], None] = field(repr=False)
53     """Injectable function for debug logging."""
54     # Primary above, derived below.
55     next_width: DiscreteWidth = secondary_field()
56     """This will be used in next search step if no strategy intervenes."""
57
58     def __post_init__(self) -> None:
59         """Prepare next width."""
60         self.next_width = self.target.discrete_width
61         if self.initial_lower_load and self.initial_upper_load:
62             interval_width = self.initial_upper_load - self.initial_lower_load
63             self.next_width = max(self.next_width, interval_width)
64         self.expand(bump_global=False)
65
66     def expand(self, bump_global: bool = True) -> None:
67         """Multiply next width by expansion coefficient.
68
69         The global current width should be bumped when external search
70         is done but load is not the bound we were looking for.
71
72         For global width shrinking, set the field directly.
73
74         :param bump_global: False if called from limit or post init.
75         :type bump_global: bool
76         """
77         self.next_width *= self.target.expansion_coefficient
78         if bump_global:
79             self.global_width.width = self.next_width
80
81     def get_width(self) -> DiscreteWidth:
82         """Return next width corrected by global current width.
83
84         :returns: The width to use, see GlobalWidth.
85         :rtype: DiscreteWidth
86         """
87         return self.global_width.or_larger(self.next_width)
88
89     def limit(self, last_width: DiscreteWidth) -> None:
90         """Decrease the prepared next width.
91
92         This is called by other strategies when bounds are getting narrower.
93
94         Global current width is not updated yet,
95         as the other strategy may not end up becoming the winner
96         and we want to avoid interfering with other selector strategies.
97
98         :param last_width: As applied by other strategy, smaller of two halves.
99         :type last_width: DiscreteWidth
100         """
101         self.next_width = max(last_width, self.target.discrete_width)
102         self.expand(bump_global=False)