metaheuristic_designer.constraint_handlers package#
Submodules#
- metaheuristic_designer.constraint_handlers.bounce_bound_constraint module
- metaheuristic_designer.constraint_handlers.clip_bound_constraint module
- metaheuristic_designer.constraint_handlers.composite_constraint module
- metaheuristic_designer.constraint_handlers.cycle_bound_constraint module
- metaheuristic_designer.constraint_handlers.extended_constraint module
- metaheuristic_designer.constraint_handlers.linear_bound_penalty_constraint module
Module contents#
Constraint handling implementation provided by the library.
- class BounceBoundConstraint(dimension, lower_bound=-100, upper_bound=100, **kwargs)[source]#
Bases:
RepairConstraintEncodes 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:
paramsAccess 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:
RepairConstraintEncodes 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:
paramsAccess 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:
ConstraintHandlerApplies every constraint handler in succession.
- Parameters:
- constraints: Iterable
List of constraint handlers.
- Attributes:
paramsAccess 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
- repair_population(population)[source]#
- Return type:
- 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,ABCAbstract 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:
paramsAccess 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:
- 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)
- class ConstraintHandlerFromLambda(repair_solution_fn=None, penalty_fn=None, **kwargs)[source]#
Bases:
ConstraintHandlerConstraint 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:
paramsAccess 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:
RepairConstraintEncodes 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:
paramsAccess 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:
paramsAccess parameter values by attribute-style lookup.
- Parameters:
solution_handler (ConstraintHandler)
param_handler_dict (dict)
encoding (ParameterExtendingEncoding)
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:
PenalizeConstraintEncodes 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:
paramsAccess 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:
ConstraintHandlerConstraint 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:
paramsAccess 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,ABCAbstract handler that only computes penalties.
Repairing does nothing (returns a copy). Subclasses must override
penalty().- Parameters:
- encodingEncoding, optional
See
ConstraintHandler.- **kwargs
See
ConstraintHandler.
- Attributes:
paramsAccess 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,ABCAbstract handler that only repairs solutions.
The penalty is always zero. Subclasses must override
repair_solution().- Parameters:
- encodingEncoding, optional
See
ConstraintHandler.- **kwargs
See
ConstraintHandler.
- Attributes:
paramsAccess 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)