culebra.trainer.abc.IslandsTrainer class

class IslandsTrainer(*subtrainers: tuple[CentralizedTrainer], num_representatives: int | None = None, representatives_selection_func: Callable[[list[Solution], int], list[Solution]] | None = None, representatives_exchange_freq: int | None = None, topology_func: Callable[[int, int, Any], list[int]] | None = None)

Bases: CommonFitnessFunctionDistributedTrainer

Create a new distributed trainer.

Parameters:
Raises:
  • TypeError – If any argument is not of the appropriate type

  • ValueError – If any argument has an incorrect value

Class methods

classmethod IslandsTrainer.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property IslandsTrainer.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

property IslandsTrainer.fitness_func: FitnessFunction

Training fitness function.

Return type:

FitnessFunction

Return type:

tuple[str]

property IslandsTrainer.iteration_metric_names: tuple(str)

Names of the metrics recorded each iteration.

Return type:

tuple[str]

property IslandsTrainer.iteration_obj_stats: dict(str, Callable)

Stats applied to each objective every iteration.

Return type:

dict

property IslandsTrainer.logbook: Logbook | None

Trainer logbook.

Returns:

A logbook with the statistics of the training or None if the training has not been done yet

Return type:

Logbook

property IslandsTrainer.num_evals: int | None

Number of evaluations performed while training.

Returns:

The number of evaluations or None if the training has not been done yet

Return type:

int

property IslandsTrainer.num_iters: int | None

Number of iterations performed while training.

Returns:

The number of iterations performed by the subtrainer that has performed more iterations or None if the training has not been done yet

Return type:

int

property IslandsTrainer.num_representatives: int

Number of representatives.

Returns:

The number of representatives sent to the other subtrainers

Return type:

int

Setter:

Set a new number of representatives

Parameters:

value (int) – The new value. If set to None, _default_num_representatives is chosen

Raises:
property IslandsTrainer.num_subtrainers: int

Number of subtrainers.

Return type:

int

property IslandsTrainer.representatives_exchange_freq: int

Number of iterations between representatives sendings.

Return type:

int

Setter:

Set a new value for the frequency

Parameters:

value (int) – The new frequency. If set to None, _default_representatives_exchange_freq is chosen

Raises:
property IslandsTrainer.representatives_selection_func: Callable[[list[Solution], int], list[Solution]]

Representatives selection policy function.

Returns:

A function that chooses which solutions are selected as representatives of each subtrainer

Return type:

Callable

Setter:

Set new representatives selection policy function.

Parameters:

func (Callable) – The new function. If set to None, _default_representatives_selection_func is chosen

Raises:

TypeError – If func is not callable

abstract property IslandsTrainer.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

property IslandsTrainer.subtrainers: list[Trainer] | None

Subtrainers.

One single-species trainer for each subtrainer.

Return type:

list[CentralizedTrainer]

property IslandsTrainer.topology_func: Callable[[int, int, Any], list[int]]

Topology function.

Return type:

Callable

Setter:

Set new topology function

Parameters:

func (Callable) – The new function. If set to None, _default_topology_func is chosen

Raises:

TypeError – If func is not callable

property IslandsTrainer.training_finished: bool

Check if training has finished.

Returns:

:data`True` if training has finished

Return type:

bool

Private properties

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

Default cooperative fitness estimation function.

Return the average of all fitness trials.

property IslandsTrainer._default_num_representatives: int

Default number of representatives sent to the other subtrainers.

Returns:

DEFAULT_NUM_REPRESENTATIVES

Return type:

int

property IslandsTrainer._default_representatives_exchange_freq: int

Default number of iterations between representatives sending.

Returns:

DEFAULT_REPRESENTATIVES_EXCHANGE_FREQ

Return type:

int

property IslandsTrainer._default_representatives_selection_func: Callable[[list[Solution], int], list[Solution]]

Default selection policy function to choose the representatives.

Returns:

DEFAULT_REPRESENTATIVES_SELECTION_FUNC

Return type:

Callable

property IslandsTrainer._default_topology_func: Callable[[int, int, Any], list[int]]

Default topology function.

Returns:

DEFAULT_ISLANDS_TOPOLOGY_FUNC

Return type:

Callable

Static methods

static IslandsTrainer.receive_representatives(subtrainer: CentralizedTrainer) None

Receive representative solutions.

Parameters:

subtrainer (CentralizedTrainer) – The subtrainer receiving representatives

static IslandsTrainer.send_representatives(subtrainer: CentralizedTrainer) None

Send representatives.

Parameters:

subtrainer (CentralizedTrainer) – The sender subtrainer

Methods

IslandsTrainer.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]]

IslandsTrainer.best_solutions() tuple[HallOfFame]

Get the best solutions found for each species.

Returns:

One Hall of Fame for each species

Return type:

tuple[HallOfFame]

IslandsTrainer.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
IslandsTrainer.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

IslandsTrainer.reset() None

Reset the trainer.

Delete all the internal data structures needed to perform the training.

IslandsTrainer.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.

IslandsTrainer.train()

Perform the training process.

Private methods

abstract IslandsTrainer._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

IslandsTrainer._finish_training() None

Finish the training process.

This method is called after the training has finished. It can be overridden to perform any treatment of the solutions found.

IslandsTrainer._init_internals() None

Set up the trainer internal data structures to start training.

Create the subtrainer communication queues.

IslandsTrainer._init_training() None

Init the training process.

Initialize all the internal data structures needed to perform the training.

IslandsTrainer._reset_internals() None

Reset the internal structures of the trainer.

Reset the subtrainer communication queues.