Skip to content

cilpy.solver.chm.ConstraintHandler Documentation

The constraint handling mechanism module: Defines the constraint handling mechanism interface.

This module provides the abstract "contract" for all constraint handling mechanisms within the cilpy library.

In the future, this module could be modified to be a comparator for multi-objective optimization problems.

ConstraintHandler

Bases: ABC, Generic[FitnessType]

An abstract interface for a constraint handling mechanism.

This class defines the strategy for comparing two evaluations in the context of a constrained optimization problem. Solvers will delegate comparison logic to an implementation of this class.

Source code in cilpy/solver/chm/__init__.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class ConstraintHandler(ABC, Generic[FitnessType]):
    """
    An abstract interface for a constraint handling mechanism.

    This class defines the strategy for comparing two evaluations in the
    context of a constrained optimization problem. Solvers will delegate
    comparison logic to an implementation of this class.
    """

    @abstractmethod
    def is_better(
        self, eval_a: Evaluation[FitnessType], eval_b: Evaluation[FitnessType]
    ) -> bool:
        """
        Compares two evaluations to determine if `eval_a` is better than `eval_b`.

        Args:
            eval_a: The first evaluation.
            eval_b: The second evaluation.

        Returns:
            True if `eval_a` is considered superior to `eval_b` according to the
            specific constraint handling strategy, False otherwise.
        """
        pass

is_better(eval_a, eval_b) abstractmethod

Compares two evaluations to determine if eval_a is better than eval_b.

Parameters:

Name Type Description Default
eval_a Evaluation[FitnessType]

The first evaluation.

required
eval_b Evaluation[FitnessType]

The second evaluation.

required

Returns:

Type Description
bool

True if eval_a is considered superior to eval_b according to the

bool

specific constraint handling strategy, False otherwise.

Source code in cilpy/solver/chm/__init__.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@abstractmethod
def is_better(
    self, eval_a: Evaluation[FitnessType], eval_b: Evaluation[FitnessType]
) -> bool:
    """
    Compares two evaluations to determine if `eval_a` is better than `eval_b`.

    Args:
        eval_a: The first evaluation.
        eval_b: The second evaluation.

    Returns:
        True if `eval_a` is considered superior to `eval_b` according to the
        specific constraint handling strategy, False otherwise.
    """
    pass

DefaultComparator

Bases: ConstraintHandler[float]

A default comparator for single-objective, unconstrained problems.

This handler performs a simple fitness comparison, assuming that a lower fitness value is better. It does not consider constraints.

Source code in cilpy/solver/chm/__init__.py
45
46
47
48
49
50
51
52
53
54
class DefaultComparator(ConstraintHandler[float]):
    """
    A default comparator for single-objective, unconstrained problems.

    This handler performs a simple fitness comparison, assuming that a lower
    fitness value is better. It does not consider constraints.
    """

    def is_better(self, eval_a: Evaluation[float], eval_b: Evaluation[float]) -> bool:
        return eval_a.fitness < eval_b.fitness