Skip to content

easyscience.Constraints

ConstraintBase

Bases: ComponentSerializer

A base class used to describe a constraint to be applied to EasyScience base objects.

enabled property writable

Is the current constraint enabled.

:return: Logical answer to if the constraint is enabled.

__call__(*args, no_set=False, **kwargs)

Method which applies the constraint

:return: None if no_set is False, float otherwise.

get_obj(key)

Get an EasyScience object from its unique key

:param key: an EasyScience objects unique key :return: EasyScience object

FunctionalConstraint

Bases: ConstraintBase

A functional constraint that applies a mathematical function to a parameter.

Unlike traditional constraints, functional constraints do not depend on other parameters directly but instead use a function to transform the parameter value. Example functions include abs(), log(), and custom mathematical operations.

Attributes:

Name Type Description
function Callable

The function applied to the parameter.

external bool

Indicates whether the constraint operates externally.

__init__(dependent_obj, func, independent_objs=None)

Initializes a FunctionalConstraint that applies a function to a parameter.

Parameters:

Name Type Description Default
dependent_obj V

The parameter to which the function is applied.

required
func Callable

A function that takes the parameter value and optional arguments, in the form f(value, *args, **kwargs).

required
independent_objs Optional[List[V]]

A list of independent parameters, if applicable. Defaults to None.

None
Example
import numpy as np
from easyscience.fitting.Constraints import FunctionalConstraint
from easyscience.Objects.Base import Parameter

# Define a parameter
a = Parameter('a', 0.2, max=1)

# Apply an absolute value constraint
constraint = FunctionalConstraint(a, np.abs)
a.user_constraints['abs'] = constraint

# Update values and trigger the constraint
a.value = -0.5  # `a` is set to 0.5 due to np.abs

MultiObjConstraint

Bases: ConstraintBase

A constraint that relates a dependent parameter to multiple independent parameters.

This constraint extends ObjConstraint by allowing multiple independent parameters with different operators, enabling expressions such as:

  • a + b = 1
  • a + b - 2*c = 0

Attributes:

Name Type Description
external bool

Indicates that this constraint operates on external parameters.

__init__(independent_objs, operator, dependent_obj, value)

Initializes a MultiObjConstraint to relate multiple independent parameters to a dependent one.

Parameters:

Name Type Description Default
independent_objs List[V]

List of independent parameters.

required
operator List[str]

List of operators applied to independent parameters.

required
dependent_obj V

The dependent parameter.

required
value Number

The result of the constraint expression.

required
Example
from easyscience.fitting.Constraints import MultiObjConstraint
from easyscience.Objects.Base import Parameter

# Define parameters
a = Parameter('a', 0.2)
b = Parameter('b', 0.3)
c = Parameter('c', 0.1)

# Create a constraint: a + b - 2*c = 0
constraint = MultiObjConstraint([b, c], ['+', '-2*'], a, 0)
b.user_constraints['SET_A'] = constraint
c.user_constraints['SET_A'] = constraint

# Update values and trigger the constraint
b.value = 0.4
print(a.value)  # Should be 0.2
Note

This constraint is evaluated as:

dependent = value - SUM(operator[i] * independent[i])

NumericConstraint

Bases: ConstraintBase

A numeric constraint that restricts a parameter based on a numerical comparison.

This constraint ensures that a parameter (dependent_obj) adheres to a specified condition, such as being less than, greater than, or equal to a given value.

Example constraints: - a < 1 - a > 5 - b == 10

Attributes:

Name Type Description
operator str

The comparison operator used (=, <, >, <=, >=).

value Number

The numerical value to compare the parameter against.

__init__(dependent_obj, operator, value)

Initializes a NumericConstraint to enforce a numerical restriction on a parameter.

Parameters:

Name Type Description Default
dependent_obj V

The parameter whose value is being constrained.

required
operator str

The relational operator defining the constraint (=, <, >, <=, >=).

required
value Number

The numerical threshold for comparison.

required
Example
from easyscience.fitting.Constraints import NumericConstraint
from easyscience.Objects.Base import Parameter

# Define a parameter with an upper limit of 1
a = Parameter('a', 0.2)

# Apply a constraint: a ≤ 1
constraint = NumericConstraint(a, '<=', 1)
a.user_constraints['LEQ_1'] = constraint

# Assign valid values
a.value = 0.85  # Allowed, remains 0.85

# Assign an invalid value
a.value = 2.0  # Exceeds the constraint; `a` is reset to 1

__repr__()

Returns a string representation of the constraint.

Returns:

Name Type Description
str str

A descriptive string including the operator and threshold value.

ObjConstraint

Bases: ConstraintBase

A ObjConstraint is a constraint whereby a dependent parameter is something of an independent parameter value. E.g. a (Dependent Parameter) = 2* b (Independent Parameter)

__init__(dependent_obj, operator, independent_obj)

A ObjConstraint is a constraint whereby a dependent parameter is something of an independent parameter value. E.g. a (Dependent Parameter) < b (Independent Parameter)

:param dependent_obj: Dependent Parameter :param operator: Relation to between the independent parameter and dependent parameter. e.g. 2 *, 1 + :param independent_obj: Independent Parameter

:example:

.. code-block:: python

from easyscience.fitting.Constraints import ObjConstraint
from easyscience.Objects.Base import Parameter
# Create an `a = 2 * b` constraint
a = Parameter('a', 0.2)
b = Parameter('b', 1)

constraint = ObjConstraint(a, '2*', b)
b.user_constraints['SET_A'] = constraint
b.value = 1
# This triggers the constraint
a.value # Should equal 2

SelfConstraint

Bases: ConstraintBase

A SelfConstraint is a constraint which tests a logical constraint on a property of itself, similar to a NumericConstraint. i.e. a > a.min. These constraints are usually used in the internal EasyScience logic.

__init__(dependent_obj, operator, value)

A SelfConstraint is a constraint which tests a logical constraint on a property of itself, similar to a NumericConstraint. i.e. a > a.min.

:param dependent_obj: Dependent Parameter :param operator: Relation to between the parameter and the values. e.g. =, <, > :param value: Name of attribute to be compared against

:example:

.. code-block:: python

from easyscience.fitting.Constraints import SelfConstraint
from easyscience.Objects.Base import Parameter
# Create an `a < a.max` constraint
a = Parameter('a', 0.2, max=1)
constraint = SelfConstraint(a, '<=', 'max')
a.user_constraints['MAX'] = constraint
# This works
a.value = 0.85
# This triggers the constraint
a.value = 2.0
# `a` is set to the maximum of the constraint (`a = 1`)

cleanup_constraint(obj_id, enabled)

Enables or disables a constraint based on the given object ID.

Parameters:

Name Type Description Default
obj_id str

The unique identifier of the object.

required
enabled bool

Whether to enable or disable the constraint.

required

Raises:

Type Description
ValueError

If the object ID does not exist in the global object map.

Example
cleanup_constraint("param_123", False)  # Disables the constraint for object with ID "param_123"