Skip to content

declearn.main.utils.ConstraintSet

Utility class to wrap sets of Constraint instances.

Source code in declearn/main/utils/_constraints.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
class ConstraintSet:
    """Utility class to wrap sets of Constraint instances."""

    def __init__(
        self,
        *constraints: Constraint,
    ) -> None:
        """Wrap an ensemble of Constraint objects."""
        self.constraints = constraints

    def increment(
        self,
    ) -> None:
        """Increment each and every wrapped constraint."""
        for constraint in self.constraints:
            constraint.increment()

    @property
    def saturated(
        self,
    ) -> bool:
        """Return whether any wrapped constraint is saturated."""
        return any(c.saturated for c in self.constraints)

    def get_values(
        self,
    ) -> Dict[str, float]:
        """Return the wrapped constraints' current values, as a dict.

        Returns
        -------
        values: dict[str, float]
            {constraint.name: constraint.value} dictionary.
            If multiple constraints have the same name, suffixes
            will be appended in order to disambiguate them.
        """
        values = {}  # type: Dict[str, float]
        for constraint in self.constraints:
            name = constraint.name
            idx = 0
            while name in values:
                name = f"{constraint.name}.{idx}"
                idx += 1
            values[name] = constraint.value
        return values

saturated: bool property

Return whether any wrapped constraint is saturated.

__init__(*constraints)

Wrap an ensemble of Constraint objects.

Source code in declearn/main/utils/_constraints.py
128
129
130
131
132
133
def __init__(
    self,
    *constraints: Constraint,
) -> None:
    """Wrap an ensemble of Constraint objects."""
    self.constraints = constraints

get_values()

Return the wrapped constraints' current values, as a dict.

Returns:

Name Type Description
values dict[str, float]

{constraint.name: constraint.value} dictionary. If multiple constraints have the same name, suffixes will be appended in order to disambiguate them.

Source code in declearn/main/utils/_constraints.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def get_values(
    self,
) -> Dict[str, float]:
    """Return the wrapped constraints' current values, as a dict.

    Returns
    -------
    values: dict[str, float]
        {constraint.name: constraint.value} dictionary.
        If multiple constraints have the same name, suffixes
        will be appended in order to disambiguate them.
    """
    values = {}  # type: Dict[str, float]
    for constraint in self.constraints:
        name = constraint.name
        idx = 0
        while name in values:
            name = f"{constraint.name}.{idx}"
            idx += 1
        values[name] = constraint.value
    return values

increment()

Increment each and every wrapped constraint.

Source code in declearn/main/utils/_constraints.py
135
136
137
138
139
140
def increment(
    self,
) -> None:
    """Increment each and every wrapped constraint."""
    for constraint in self.constraints:
        constraint.increment()