culebra.abc.Trainer class¶
Class methods¶
- classmethod Trainer.load(filename: str) Base¶
Load a serialized object from a file.
- Parameters:
filename (str) – The file name.
- Returns:
The loaded object
- Raises:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
SERIALIZED_FILE_EXTENSION
Properties¶
- property Trainer.cooperative_fitness_estimation_func: Callable[Sequence[Sequence[float]], Sequence[float]]¶
Cooperative fitness estimation function.
Funtion to estimate the cooperative fitness of a solution from several fitness trials with different cooperators. Only used by cooperative trainers.
- abstract property Trainer.fitness_func: FitnessFunction¶
Training fitness function.
- Return type:
This property must be overridden by subclasses to return a correct value.
- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract property Trainer.iteration_metric_names: tuple(str)¶
Names of the metrics recorded each iteration.
This property must be overridden by subclasses to return a correct value.
- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract property Trainer.iteration_obj_stats: dict(str, Callable)¶
Stats applied to each objective every iteration.
This property must be overridden by subclasses to return a correct value.
- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract property Trainer.logbook: Logbook | None¶
Trainer logbook.
This property must be overridden by subclasses to return a correct value.
- Returns:
A logbook with the statistics of the training or
Noneif the training has not been done yet- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract property Trainer.num_evals: int | None¶
Number of evaluations performed while training.
This property must be overridden by subclasses to return a correct value.
- Returns:
The number of evaluations or
Noneif the training has not been done yet- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract property Trainer.num_iters: int | None¶
Number of iterations performed while training.
This property must be overridden by subclasses to return a correct value.
- Returns:
The number of iterations or
Noneif the training has not been done yet- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract property Trainer.runtime: float | None¶
Training runtime.
This property must be overridden by subclasses to return a correct value.
- Returns:
The training runtime or
Noneif the training has not been done yet.- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract property Trainer.training_finished: bool¶
Check if training has finished.
This property must be overridden by subclasses to return a correct value.
- Returns:
:data`True` if training has finished
- Return type:
- Raises:
NotImplementedError – If has not been overridden
Private properties¶
Methods¶
- Trainer.best_cooperators() list[list[Solution | None]] | None¶
Return a list of cooperators from each species.
Only used for cooperative trainers.
- abstract Trainer.best_solutions() tuple[HallOfFame]¶
Get the best solutions found for each species.
This method must be overridden by subclasses to return a correct value.
- Returns:
One Hall of Fame for each species
- Return type:
- Raises:
NotImplementedError – If has not been overridden
- Trainer.dump(filename: str) None¶
Serialize this object and save it to a file.
- Parameters:
filename (str) – The file name.
- Raises:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
SERIALIZED_FILE_EXTENSION
- Trainer.evaluate(sol: Solution, fitness_func: FitnessFunction | None = None, index: int | None = None, cooperators: Sequence[Sequence[Solution | None]] | None = None) int¶
Evaluate one solution.
Its fitness will be modified according with the fitness function results.
- Parameters:
sol (Solution) – The solution
fitness_func (FitnessFunction) – The fitness function. If omitted, the training function is used
index (int) – Index where sol should be inserted in the cooperators sequence to form a complete solution for the problem
cooperators (Sequence[Sequence[Solution]]) – Sequence of cooperators of other species or
None(if no cooperators are needed to evaluate sol)
- Returns:
The number of evaluations performed
- Return type:
- abstract Trainer.reset() None¶
Reset the trainer.
This method must be overridden by subclasses to allow reseting the trainer. :raises NotImplementedError: If has not been overridden
- Trainer.test(best_found: Sequence[HallOfFame], fitness_func: FitnessFunction, cooperators: Sequence[Sequence[Solution]] | None = None) None¶
Apply the test fitness function to the solutions found.
Update the solutions in best_found with their test fitness.
- Parameters:
best_found (Sequence[HallOfFame]) – The best solutions found for each species. One
HallOfFamefor each speciesfitness_func (FitnessFunction) – Fitness function used to evaluate the final solutions
cooperators (Sequence[ Sequence[Solution]]) – Sequence of cooperators of other species or
None(if no cooperators are needed)
- Raises:
TypeError – If any parameter has a wrong type
ValueError – If any parameter has an invalid value.
- Trainer.train()¶
Perform the training process.
Private methods¶
- abstract Trainer._do_training() None¶
Apply the training algorithm.
This abstract method should be implemented by subclasses in order to implement the desired behavior. :raises NotImplementedError: If has not been overridden
- abstract Trainer._finish_training() None¶
Finish the training process.
- Raises:
NotImplementedError – If has not been overridden
- abstract Trainer._init_training() None¶
Init the training process.
- Raises:
NotImplementedError – If has not been overridden

