Revert "fix(IPsecUtil): Delete keywords no longer used"
[csit.git] / resources / libraries / python / MLRsearch / target_spec.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 TargetSpec class."""
15
16 from __future__ import annotations
17
18 from dataclasses import dataclass, field
19 from typing import Optional
20
21 from .discrete_width import DiscreteWidth
22
23
24 @dataclass(frozen=True, eq=True)
25 class TargetSpec:
26     """Composite object holding attributes specifying one search target.
27
28     Abstractly, this has several similar meanings.
29     With discrete_width attribute this specifies when a selector is Done.
30     With expansion_coefficient attribute it tells selector how quickly
31     should it expand interval in external search.
32     With "preceding" attribute it helps selector, so it does not need to point
33     to preceding target separately from its current target.
34     Without those three attributes this object is still sufficient
35     for LoadStats to classify loads as lower bound, upper bound, or unknown.
36     """
37
38     loss_ratio: float
39     """Target loss ratio. Equal and directly analogous to goal loss ratio,
40     but applicable also for non-final targets."""
41     exceed_ratio: float
42     """Target exceed ratio. Equal and directly analogous to goal exceed ratio,
43     but applicable also for non-final targets."""
44     discrete_width: DiscreteWidth
45     """Target relative width. Analogous to goal relative width,
46     but coarser for non-final targets."""
47     trial_duration: float
48     """Duration to use for trials for this target. Shorter trials have lesser
49     (and more complicated) impact when determining upper and lower bounds."""
50     duration_sum: float
51     """Sum of trial durations sufficient to classify a load
52     as an upper or lower bound.
53     For non-final targets, this is shorter than goal duration_sum."""
54     expansion_coefficient: int = field(repr=False)
55     """Equal and directly analogous to goal expansion coefficient,
56     but applicable also for non-final targets."""
57     fail_fast: bool = field(repr=False)
58     """Copied from goal. If true and min load is not an upper bound, raise."""
59     preceding: Optional[TargetSpec] = field(repr=False)
60     """Reference to next coarser target (if any) belonging to the same goal."""
61
62     # No conversions or validations, as this is an internal structure.
63
64     def __str__(self) -> str:
65         """Convert into a short human-readable string.
66
67         :returns: The short string.
68         :rtype: str
69         """
70         return (
71             f"lr={self.loss_ratio},er={self.exceed_ratio}"
72             f",ds={self.duration_sum}"
73         )
74
75     def with_preceding(self, preceding: Optional[TargetSpec]) -> TargetSpec:
76         """Create an equivalent instance but with different preceding field.
77
78         This is useful in initialization. Create semi-initialized targets
79         starting from final one, than add references in reversed order.
80
81         :param preceding: New value for preceding field, cannot be None.
82         :type preceding: Optional[TargetSpec]
83         :returns: Instance with the new value applied.
84         :rtype: TargetSpec
85         """
86         return TargetSpec(
87             loss_ratio=self.loss_ratio,
88             exceed_ratio=self.exceed_ratio,
89             discrete_width=self.discrete_width,
90             trial_duration=self.trial_duration,
91             duration_sum=self.duration_sum,
92             expansion_coefficient=self.expansion_coefficient,
93             fail_fast=self.fail_fast,
94             preceding=preceding,
95         )