metaheuristic_designer.constraint_handler module#

Base class for the Constraint Handler module.

This module implements ways to enforce constraints on the objective function.

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 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)