Skip to content

declearn.main.utils.Constraint

Base class to implement effort constraints.

This class defines a generic API and implements count-based constraints.

Usage

  • When instantiated, a base Constraint records a self.value counter, initialized based on the start parameter.
  • On each self.increment() call, the self.value attribute is incremented by 1.
  • The constraint is saturated when self.value > self.limit.
Source code in declearn/main/utils/_constraints.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Constraint:
    """Base class to implement effort constraints.

    This class defines a generic API and implements count-based constraints.

    Usage
    -----
    * When instantiated, a base Constraint records a `self.value`
      counter, initialized based on the `start` parameter.
    * On each `self.increment()` call, the `self.value` attribute
      is incremented by 1.
    * The constraint is `saturated` when `self.value > self.limit`.
    """

    def __init__(
        self,
        limit: Optional[float],
        start: float = 0.0,
        name: str = "constraint",
    ) -> None:
        """Instantiate a count-based constraint.

        Parameters
        ----------
        limit: float or None
            Value beyond which the constraint is saturated.
            If None, set to Inf, making this a counter.
        start: float, default=0.
            Start value of the counter.
            This value goes up by 1 on each `self.increment` call.
        name: str, default="constraint"
            Name of the constraint.
        """
        self.limit = limit or float("inf")
        self.value = start
        self.name = name

    def increment(
        self,
    ) -> None:
        """Update `self.value`, incrementing it by 1."""
        self.value += 1

    @property
    def saturated(
        self,
    ) -> bool:
        """Return whether the constraint is saturated."""
        return self.value >= self.limit

saturated: bool property

Return whether the constraint is saturated.

__init__(limit, start=0.0, name='constraint')

Instantiate a count-based constraint.

Parameters:

Name Type Description Default
limit Optional[float]

Value beyond which the constraint is saturated. If None, set to Inf, making this a counter.

required
start float

Start value of the counter. This value goes up by 1 on each self.increment call.

0.0
name str

Name of the constraint.

'constraint'
Source code in declearn/main/utils/_constraints.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __init__(
    self,
    limit: Optional[float],
    start: float = 0.0,
    name: str = "constraint",
) -> None:
    """Instantiate a count-based constraint.

    Parameters
    ----------
    limit: float or None
        Value beyond which the constraint is saturated.
        If None, set to Inf, making this a counter.
    start: float, default=0.
        Start value of the counter.
        This value goes up by 1 on each `self.increment` call.
    name: str, default="constraint"
        Name of the constraint.
    """
    self.limit = limit or float("inf")
    self.value = start
    self.name = name

increment()

Update self.value, incrementing it by 1.

Source code in declearn/main/utils/_constraints.py
68
69
70
71
72
def increment(
    self,
) -> None:
    """Update `self.value`, incrementing it by 1."""
    self.value += 1