culebra.abc.Trainer class

class Trainer

Bases: Base

Construct a trainer.

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:

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.

Return type:

Callable

Setter:

Set a new function

Parameters:

func (Callable) – The new function

Raises:

TypeError – If func is not a valid function

abstract property Trainer.fitness_func: FitnessFunction

Training fitness function.

Return type:

FitnessFunction

This property must be overridden by subclasses to return a correct value.

Return type:

tuple[str]

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:

tuple[str]

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:

dict

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 None if the training has not been done yet

Return type:

Logbook

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 None if the training has not been done yet

Return type:

int

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 None if the training has not been done yet

Return type:

int

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 None if the training has not been done yet.

Return type:

float

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:

bool

Raises:

NotImplementedError – If has not been overridden

Private properties

property Trainer._default_cooperative_fitness_estimation_func: Callable[Sequence[Sequence[float]], Sequence[float]]

Default cooperative fitness estimation function.

Return the average of all fitness trials.

Methods

Trainer.best_cooperators() list[list[Solution | None]] | None

Return a list of cooperators from each species.

Only used for cooperative trainers.

Returns:

A list of cooperators lists if the trainer is cooperative or None in other cases

Return type:

list[list[Solution]]

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:

tuple[HallOfFame]

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

int

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