metaheuristic_designer.constraint_handlers package

Submodules

metaheuristic_designer.constraint_handlers.bounce_bound_constraint module

class BounceBoundConstraint(dimension, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: RepairConstraint

Encodes a bound constraint by bouncing through the bounds, subtracting the leftover part of the vector that lies outside the bounds. If the subtraction still lies outside the bounds, the leftover part is added, subtraction and addition are alternated until the solution lies in bounds.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

repair_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

metaheuristic_designer.constraint_handlers.clip_bound_constraint module

class ClipBoundConstraint(dimension, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: RepairConstraint

Encodes a bound constraint by clipping solutions to the nearest point in the boundary.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

repair_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

metaheuristic_designer.constraint_handlers.composite_constraint module

class CompositeConstraint(constraints: Iterable, **kwargs)[source]

Bases: ConstraintHandler

applies every constraint handler in succession.

Parameters:

constraints (Iterable) – List of constraint handlers.

gather_params()[source]
repair_solution(solution: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

penalty(solution: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) number | float | int[source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

metaheuristic_designer.constraint_handlers.cycle_bound_constraint module

class CycleBoundConstraint(dimension, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: RepairConstraint

Encodes a bound constraint by wrapping through the bounds, performing a modulo operation componentwise.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

repair_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

metaheuristic_designer.constraint_handlers.extended_constraint module

class ExtendedConstraintHandler(solution_handler: ConstraintHandler, param_handler_dict: dict, encoding: ParameterExtendingEncoding, **kwargs)[source]

Bases: ConstraintHandler

repair_solution(genotype_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

penalty(genotype_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) number | float | int[source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

metaheuristic_designer.constraint_handlers.linear_bound_penalty_constraint module

class LinearBoundPenaltyConstraint(dimension, alpha: number | float | int = 1, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: PenalizeConstraint

Encodes a bound constraint by adding a penalty proportional to the distance of the solution’s distance to the bounds.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • alpha (float, optional) – factor to multiply to the penalty before being applied.

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

penalty(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

Module contents

class BounceBoundConstraint(dimension, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: RepairConstraint

Encodes a bound constraint by bouncing through the bounds, subtracting the leftover part of the vector that lies outside the bounds. If the subtraction still lies outside the bounds, the leftover part is added, subtraction and addition are alternated until the solution lies in bounds.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

repair_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

class ClipBoundConstraint(dimension, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: RepairConstraint

Encodes a bound constraint by clipping solutions to the nearest point in the boundary.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

repair_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

class CompositeConstraint(constraints: Iterable, **kwargs)[source]

Bases: ConstraintHandler

applies every constraint handler in succession.

Parameters:

constraints (Iterable) – List of constraint handlers.

gather_params()[source]
repair_solution(solution: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

penalty(solution: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) number | float | int[source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

class ConstraintHandler(encoding=None, **kwargs)[source]

Bases: ParametrizableMixin, ABC

Abstract base for all constraint handlers.

A constraint handler can repair solutions (make them feasible) and/or compute a penalty that is subtracted from the objective value. Subclasses must implement at least one of these operations.

Parameters:
  • encoding (Encoding, optional) – An Encoding that will be used to extract the genotype before repair or penalty (default None).

  • **kwargs – Additional keyword arguments stored as schedulable parameters.

abstract repair_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

abstract penalty(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

get_state()[source]
class ConstraintHandlerFromLambda(repair_solution_fn: Callable | None = None, penalty_fn: Callable | None = None, **kwargs)[source]

Bases: ConstraintHandler

Constraint handler built from plain callables.

At least one of repair_solution_fn or penalty_fn must be given.

Parameters:
  • repair_solution_fn (callable, optional) – A function (solution) -> repaired_solution.

  • penalty_fn (callable, optional) – A function (solution) -> penalty_value.

  • **kwargs – Keyword arguments forwarded to ConstraintHandler.

repair_solution(solution: Iterable) Iterable[source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

penalty(solution: Any) number | float | int[source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

class CycleBoundConstraint(dimension, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: RepairConstraint

Encodes a bound constraint by wrapping through the bounds, performing a modulo operation componentwise.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

repair_solution(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

class ExtendedConstraintHandler(solution_handler: ConstraintHandler, param_handler_dict: dict, encoding: ParameterExtendingEncoding, **kwargs)[source]

Bases: ConstraintHandler

repair_solution(genotype_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

penalty(genotype_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) number | float | int[source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

class LinearBoundPenaltyConstraint(dimension, alpha: number | float | int = 1, lower_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = -100, upper_bound: number | float | int | ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] = 100, **kwargs)[source]

Bases: PenalizeConstraint

Encodes a bound constraint by adding a penalty proportional to the distance of the solution’s distance to the bounds.

Parameters:
  • dimension (int) – size of the input vector (decoded).

  • alpha (float, optional) – factor to multiply to the penalty before being applied.

  • lower_bound (float | ndarray, optional) – lower limit of the bounds.

  • upper_bound (float | ndarray, optional) – upper limit of the bounds.

penalty(population_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

class NullConstraint(encoding=None, **kwargs)[source]

Bases: ConstraintHandler

Constraint handler that enforces no restrictions.

The penalty is always zero, and repairing returns the solution unchanged.

Parameters:
repair_solution(solution: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool][source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

penalty(_solution: Any) number | float | int[source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float

class PenalizeConstraint(encoding=None, **kwargs)[source]

Bases: ConstraintHandler, ABC

Abstract handler that only computes penalties.

Repairing does nothing (returns a copy). Subclasses must override penalty().

Parameters:
repair_solution(solution: Iterable) Iterable[source]

Modifies the incoming solution so that it follows the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

fixed_solution – Modified version of the input solution that fits the problem’s constraints

Return type:

Any

class RepairConstraint(encoding=None, **kwargs)[source]

Bases: ConstraintHandler, ABC

Abstract handler that only repairs solutions.

The penalty is always zero. Subclasses must override repair_solution().

Parameters:
penalty(_solution: Iterable) ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]

Offset to the objective value for the solution corresponding to violations of the problem’s constraints.

Parameters:

solution (Any) – The input solution.

Returns:

penalty – The amount of penalty to apply to the current solution.

Return type:

float