metaheuristic_designer.objective_function module#
Base class for the Objective Function module.
This module implements the objective function that will measure the quality of the solutions.
- class ObjectiveFunc(dimension, lower_bound=None, upper_bound=None, constraint_handler=None, mode='max', name='Some function', vectorized=False, recalculate=False, **kwargs)[source]#
Bases:
ParametrizableMixin,ABCAbstract objective function with built-in fitness conversion.
Subclasses must implement
objective(), which returns the raw objective value. The base class automatically converts it to a fitness that is always maximized (flipping the sign for minimization) and applies a penalty if aConstraintHandleris present.- Parameters:
- dimensionint
Number of decision variables.
- lower_boundfloat or array-like, optional
Lower bound(s) of the feasible region. When both bounds are given, a
ClipBoundConstraintis added automatically.- upper_boundfloat or array-like, optional
Upper bound(s) of the feasible region.
- constraint_handlerConstraintHandler, optional
Handler that can repair solutions and/or compute penalties.
- modestr, optional
"max"or"min". The fitness is always maximized internally; the mode controls the sign conversion.- namestr, optional
Human-readable name for this function.
- vectorizedbool, optional
If
True,objective()receives the whole population at once and must return an array.- recalculatebool, optional
If
True, every individual is re-evaluated even if its fitness has already been computed.- **kwargs
Additional keyword arguments stored as schedulable parameters.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
- Parameters:
dimension (int)
lower_bound (Optional[ScalarLike | VectorLike])
upper_bound (Optional[ScalarLike | VectorLike])
constraint_handler (Optional[ConstraintHandler])
mode (str)
name (str)
vectorized (bool)
recalculate (bool)
Methods
__call__(population)Shorthand for executing the objective function on a vector.
Attach extra constraint handlers for extended encodings (e.g., PSO).
calculate_fitness(population)Evaluate fitness for the whole population.
get_params()Return a copy of the current parameter dictionary.
Return a dictionary with the current configuration.
objective(solution)Implementation of the objective function.
repair_population(population)Transforms an invalid vector into one that satisfies the restrictions of the problem.
restart()Reset the evaluation counter to zero.
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.
- calculate_fitness(population)[source]#
Evaluate fitness for the whole population.
The raw objective values are computed via
objective(), penalties are subtracted, and the result (always maximized) is stored inpopulation.fitness. Individuals that already have a valid fitness are skipped unlessrecalculateis set.- Return type:
- Parameters:
- populationPopulation
The population whose individuals will be evaluated.
- parallelbool, optional
Reserved for future use, currently ignored.
- threadsint, optional
Reserved for future use, currently ignored.
- Returns:
- ndarray
The new fitness values (also written in-place).
- Parameters:
population (Population)
- abstract objective(solution)[source]#
Implementation of the objective function.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool] |number|float|int- Parameters:
- solution: Any
The solution for which the fitness will be calculated.
- Returns:
- objective_value: VectorLike | ScalarLike
Value of the objective function given a solution.
- Parameters:
solution (Any)
- repair_population(population)[source]#
Transforms an invalid vector into one that satisfies the restrictions of the problem.
- Return type:
- Parameters:
- solution: MatrixLike
A solution that could be violating the restrictions of the problem.
- Returns:
- repaired_solution: MatrixLike
A modified version of the solution passed that satisfies the restrictions of the problem.
- Parameters:
population (Population)
- add_parameter_constraints(parameter_extending_encoding, param_handlers)[source]#
Attach extra constraint handlers for extended encodings (e.g., PSO).
- Parameters:
- parameter_extending_encodingParameterExtendingEncoding
The encoding that splits the genotype into solution and auxiliary parameters.
- param_handlersdict
Mapping from parameter names to
ConstraintHandlerinstances.
- Parameters:
parameter_extending_encoding (ParameterExtendingEncoding)
param_handlers (dict[str, ConstraintHandler])
- class NullObjectiveFunc(**kwargs)[source]#
Bases:
ObjectiveFuncObjective function that always returns zero.
Useful as a placeholder in tests or when the optimization criterion is handled entirely by constraints.
- Parameters:
- **kwargs
Forwarded to
ObjectiveFunc.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
Methods
__call__(population)Shorthand for executing the objective function on a vector.
add_parameter_constraints(...)Attach extra constraint handlers for extended encodings (e.g., PSO).
calculate_fitness(population)Evaluate fitness for the whole population.
get_params()Return a copy of the current parameter dictionary.
get_state()Return a dictionary with the current configuration.
objective(_)Implementation of the objective function.
repair_population(population)Transforms an invalid vector into one that satisfies the restrictions of the problem.
restart()Reset the evaluation counter to zero.
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.
- objective(_)[source]#
Implementation of the objective function.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool] |number|float|int- Parameters:
- solution: Any
The solution for which the fitness will be calculated.
- Returns:
- objective_value: VectorLike | ScalarLike
Value of the objective function given a solution.
- class ObjectiveFromLambda(obj_func, dimension, lower_bound=None, upper_bound=None, constraint_handler=None, mode='max', name=None, vectorized=False, recalculate=False, **kwargs)[source]#
Bases:
ObjectiveFuncObjective function indicated by a function call.
- Parameters:
- obj_func: Callable
Objective function as a callable object.
- dimension: int
The dimension of the vectors accepted by the objective function.
- mode: str, optional
Whether to maximize or minimize the function (using the string ‘max’ or ‘min’).
- lower_bound: float, optional
Lower limit restriction for the vectors.
- upper_bound: float, optional
Upper limit restriction for the vectors.
- name: str, optional
The name that will be displayed to represent this function.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
- Parameters:
obj_func (Callable)
dimension (int)
lower_bound (Optional[ScalarLike | VectorLike])
upper_bound (Optional[ScalarLike | VectorLike])
constraint_handler (Optional[ConstraintHandler])
mode (str)
name (Optional[str])
vectorized (bool)
recalculate (bool)
Methods
__call__(population)Shorthand for executing the objective function on a vector.
add_parameter_constraints(...)Attach extra constraint handlers for extended encodings (e.g., PSO).
calculate_fitness(population)Evaluate fitness for the whole population.
get_params()Return a copy of the current parameter dictionary.
get_state()Return a dictionary with the current configuration.
objective(solution)Implementation of the objective function.
repair_population(population)Transforms an invalid vector into one that satisfies the restrictions of the problem.
restart()Reset the evaluation counter to zero.
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.
- objective(solution)[source]#
Implementation of the objective function.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool] |number|float|int- Parameters:
- solution: Any
The solution for which the fitness will be calculated.
- Returns:
- objective_value: VectorLike | ScalarLike
Value of the objective function given a solution.
- Parameters:
solution (Any)