Skip to content

declearn.main.utils.TimeoutConstraint

Bases: Constraint

Class implementing a simple time-based constraint.

Usage

  • When instantiated, a TimeoutConstraint records a reference time (that at instantiation, optionally adjusted based on the start parameter) under the self.start attribute.
  • On each self.increment() call, the self.value attribute is updated to the difference between the current time and the self.start attribute.
  • The constraint is saturated when self.value > self.limit.
Source code in declearn/main/utils/_constraints.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class TimeoutConstraint(Constraint):
    """Class implementing a simple time-based constraint.

    Usage
    -----
    * When instantiated, a TimeoutConstraint records a reference
      time (that at instantiation, optionally adjusted based on
      the `start` parameter) under the `self.start` attribute.
    * On each `self.increment()` call, the `self.value` attribute
      is updated to the difference between the current time and
      the `self.start` attribute.
    * The constraint is `saturated` when `self.value > self.limit`.
    """

    def __init__(
        self,
        limit: Optional[float],
        start: float = 0.0,
        name: str = "timeout",
    ) -> None:
        """Instantiate a time-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 duration to substract from the current time
            to set up the reference starting time.
        name: str, default="timeout"
            Name of the constraint.
        """
        super().__init__(limit, start, name)
        self.start = time.time() - start

    def increment(
        self,
    ) -> None:
        """Update `self.value`, storing time passed since `self.start`."""
        self.value = time.time() - self.start

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

Instantiate a time-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 duration to substract from the current time to set up the reference starting time.

0.0
name str

Name of the constraint.

'timeout'
Source code in declearn/main/utils/_constraints.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def __init__(
    self,
    limit: Optional[float],
    start: float = 0.0,
    name: str = "timeout",
) -> None:
    """Instantiate a time-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 duration to substract from the current time
        to set up the reference starting time.
    name: str, default="timeout"
        Name of the constraint.
    """
    super().__init__(limit, start, name)
    self.start = time.time() - start

increment()

Update self.value, storing time passed since self.start.

Source code in declearn/main/utils/_constraints.py
118
119
120
121
122
def increment(
    self,
) -> None:
    """Update `self.value`, storing time passed since `self.start`."""
    self.value = time.time() - self.start