aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/MLRsearch/global_width.py
blob: 6f7df8b89431c6d031029fcec027198d7c421729 (plain)
1
2
3
4
5
6
7
<
# Copyright (c) 2023 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module defining GlobalWidth class."""


from __future__ import annotations

from dataclasses import dataclass

from .discrete_load import DiscreteLoad
from .discrete_width import DiscreteWidth


@dataclass
class GlobalWidth:
    """Primarily used to synchronize external search steps across selectors.

    The full name is global current width, but that is too long for identifiers.

    While each selector tracks its "local" (per goal) width using expander,
    it is important we do not interleave upper external search for two goals.
    That is why all selector instances refer to a singleton instance of this.

    In general, this value remains constant when main loop iterates over
    selectors and when selector iterates over strategies.
    After winner is measured, this width is set to winner width value
    and for some strategies that width is expanded when external search says so.

    The two methods are not really worth creating a new class,
    but the main reason is having a name for type hints
    that distinguish this from various other "width" and "current" values.
    """

    width: DiscreteWidth
    """Minimum width to apply at next external search step."""
    # TODO: Add a setter, so it is easier to add debug logging.

    @staticmethod
    def from_loads(load0: DiscreteLoad, load1: DiscreteLoad) -> GlobalWidth:
        """Initialize the value based on two loads from initial trials.

        :param load0: Lower (or equal) load from the two most recent trials.
        :param load1: Higher (or equal) load from the two most recent trials.
        :type load0: DiscreteLoad
        :type load1: DiscreteLoad
        :returns: Newly created instance with computed width.
        :rtype: GlobalWidth
        """
        return GlobalWidth(load1 - load0)

    def or_larger(self, width: DiscreteWidth) -> DiscreteWidth:
        """Return width from argument or self, whichever is larger.

        :param width: A selector (strategy) asks if this width is large enough.
        :type width: DiscreteWidth
        :returns: Argument or current width.
        :rtype: DiscreteWidth
        """
        return width if width > self.width else self.width
limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.