metaheuristic_designer.constraint_handlers package#

Submodules#

Module contents#

Constraint handling implementation provided by the library.

class BounceBoundConstraint(dimension, lower_bound=-100, upper_bound=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.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • lower_bound (ScalarLike | VectorLike)

  • upper_bound (ScalarLike | VectorLike)

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(solutions)

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

repair_solutions(population_matrix)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_solutions(population_matrix)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

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

class ClipBoundConstraint(dimension, lower_bound=-100, upper_bound=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.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • lower_bound (ScalarLike | VectorLike)

  • upper_bound (ScalarLike | VectorLike)

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(solutions)

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

repair_solutions(population_matrix)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_solutions(population_matrix)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

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

class CompositeConstraint(constraints, **kwargs)[source]#

Bases: ConstraintHandler

Applies every constraint handler in succession.

Parameters:
constraints: Iterable

List of constraint handlers.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

constraints (Iterable)

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(solution)

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

repair_solutions(solution)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

gather_params

get_state

repair_population

gather_params()[source]#
repair_population(population)[source]#
Return type:

Population

Parameters:

population (Population)

repair_solutions(solution)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

solution (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

penalty(solution)[source]#

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

Return type:

number | float | int

Parameters:
solution: Any

The input solution.

Returns:
penalty: float

The amount of penalty to apply to the current solution.

Parameters:

solution (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

class ConstraintHandler(**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:
**kwargs

Additional keyword arguments stored as schedulable parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(population_matrix)

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

repair_solutions(population_matrix)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_population(population)[source]#
Return type:

Population

Parameters:

population (Population)

abstract repair_solutions(population_matrix)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

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

abstract penalty(population_matrix)[source]#

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

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
penalty: float

The amount of penalty to apply to the current solution.

Parameters:

population_matrix (Iterable)

get_state()[source]#
class ConstraintHandlerFromLambda(repair_solution_fn=None, penalty_fn=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_fncallable, optional

A function (solution) -> repaired_solution.

penalty_fncallable, optional

A function (solution) -> penalty_value.

**kwargs

Keyword arguments forwarded to ConstraintHandler.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • repair_solution_fn (Optional[Callable])

  • penalty_fn (Optional[Callable])

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(solutions)

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

repair_solutions(solution)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_solutions(solution)[source]#

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

Return type:

Iterable

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

solution (Iterable)

penalty(solutions)[source]#

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

Return type:

number | float | int

Parameters:
solution: Any

The input solution.

Returns:
penalty: float

The amount of penalty to apply to the current solution.

Parameters:

solutions (Any)

class CycleBoundConstraint(dimension, lower_bound=-100, upper_bound=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.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • lower_bound (ScalarLike | VectorLike)

  • upper_bound (ScalarLike | VectorLike)

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(solutions)

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

repair_solutions(population_matrix)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_solutions(population_matrix)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

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

class ExtendedConstraintHandler(solution_handler, param_handler_dict, encoding, **kwargs)[source]#

Bases: ConstraintHandler

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(genotype_matrix)

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

repair_solutions(genotype_matrix)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_solutions(genotype_matrix)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

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

penalty(genotype_matrix)[source]#

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

Return type:

number | float | int

Parameters:
solution: Any

The input solution.

Returns:
penalty: float

The amount of penalty to apply to the current solution.

Parameters:

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

class LinearBoundPenaltyConstraint(dimension, alpha=1, lower_bound=-100, upper_bound=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.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • alpha (ScalarLike)

  • lower_bound (ScalarLike | VectorLike)

  • upper_bound (ScalarLike | VectorLike)

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(population_matrix)

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

repair_solutions(solution)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

penalty(population_matrix)[source]#

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

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
penalty: float

The amount of penalty to apply to the current solution.

Parameters:

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

class NullConstraint(**kwargs)[source]#

Bases: ConstraintHandler

Constraint handler that enforces no restrictions.

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

Parameters:
encodingEncoding, optional

See ConstraintHandler.

**kwargs

See ConstraintHandler.

Attributes:
params

Access parameter values by attribute-style lookup.

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(solutions)

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

repair_solutions(solution)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_solutions(solution)[source]#

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

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

solution (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

penalty(solutions)[source]#

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

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
penalty: float

The amount of penalty to apply to the current solution.

Parameters:

solutions (Iterable)

class PenalizeConstraint(**kwargs)[source]#

Bases: ConstraintHandler, ABC

Abstract handler that only computes penalties.

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

Parameters:
encodingEncoding, optional

See ConstraintHandler.

**kwargs

See ConstraintHandler.

Attributes:
params

Access parameter values by attribute-style lookup.

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(population_matrix)

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

repair_solutions(solution)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

repair_solutions(solution)[source]#

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

Return type:

Iterable

Parameters:
solution: Any

The input solution.

Returns:
fixed_solution: Any

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

Parameters:

solution (Iterable)

class RepairConstraint(**kwargs)[source]#

Bases: ConstraintHandler, ABC

Abstract handler that only repairs solutions.

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

Parameters:
encodingEncoding, optional

See ConstraintHandler.

**kwargs

See ConstraintHandler.

Attributes:
params

Access parameter values by attribute-style lookup.

Methods

get_params()

Return a copy of the current parameter dictionary.

penalty(solutions)

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

repair_solutions(population_matrix)

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

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Re-evaluate all stored parameters at the current progress.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

get_state

repair_population

penalty(solutions)[source]#

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

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
solution: Any

The input solution.

Returns:
penalty: float

The amount of penalty to apply to the current solution.

Parameters:

solutions (Iterable)