culebra.trainer.abc.CommonFitnessFunctionDistributedTrainer class¶
- class CommonFitnessFunctionDistributedTrainer(*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:
DistributedTrainerCreate a new distributed trainer.
- Parameters:
subtrainers (tuple[CentralizedTrainer]) – The subtrainers
num_representatives (int) – Number of representative solutions that will be sent to the other subtrainers. If omitted,
_default_num_representativeswill be used. Defaults toNonerepresentatives_selection_func (Callable) – Policy function to choose the representatives from each subtrainer. If omitted,
_default_representatives_selection_funcwill be used. Defaults toNonerepresentatives_exchange_freq (int) – Number of iterations between representatives sendings. If omitted,
_default_representatives_exchange_freqwill be used. Defaults toNonetopology_func (Callable) – Topology function for representatives sending. If omitted,
_default_topology_funcwill be used. Defaults toNone
- Raises:
TypeError – If any argument is not of the appropriate type
ValueError – If any argument has an incorrect value
Class methods¶
- classmethod CommonFitnessFunctionDistributedTrainer.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 CommonFitnessFunctionDistributedTrainer.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.
- property CommonFitnessFunctionDistributedTrainer.fitness_func: FitnessFunction¶
Training fitness function.
- Return type:
- Return type:
- property CommonFitnessFunctionDistributedTrainer.iteration_metric_names: tuple(str)¶
Names of the metrics recorded each iteration.
- property CommonFitnessFunctionDistributedTrainer.iteration_obj_stats: dict(str, Callable)¶
Stats applied to each objective every iteration.
- Return type:
- property CommonFitnessFunctionDistributedTrainer.num_evals: int | None¶
Number of evaluations performed while training.
- property CommonFitnessFunctionDistributedTrainer.num_iters: int | None¶
Number of iterations performed while training.
- property CommonFitnessFunctionDistributedTrainer.num_representatives: int¶
Number of representatives.
- Returns:
The number of representatives sent to the other subtrainers
- Return type:
- Setter:
Set a new number of representatives
- Parameters:
value (int) – The new value. If set to
None,_default_num_representativesis chosen- Raises:
TypeError – If value is not an integer number
ValueError – If value is not positive
- property CommonFitnessFunctionDistributedTrainer.num_subtrainers: int¶
Number of subtrainers.
- Return type:
- property CommonFitnessFunctionDistributedTrainer.representatives_exchange_freq: int¶
Number of iterations between representatives sendings.
- Return type:
- Setter:
Set a new value for the frequency
- Parameters:
value (int) – The new frequency. If set to
None,_default_representatives_exchange_freqis chosen- Raises:
TypeError – If value is not an integer
ValueError – If value is not a positive number
- property CommonFitnessFunctionDistributedTrainer.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:
- Setter:
Set new representatives selection policy function.
- Parameters:
func (Callable) – The new function. If set to
None,_default_representatives_selection_funcis chosen- Raises:
TypeError – If func is not callable
- abstract property CommonFitnessFunctionDistributedTrainer.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
- property CommonFitnessFunctionDistributedTrainer.subtrainers: list[Trainer] | None¶
Subtrainers.
One single-species trainer for each subtrainer.
- Return type:
- property CommonFitnessFunctionDistributedTrainer.topology_func: Callable[[int, int, Any], list[int]]¶
Topology function.
- Return type:
- Setter:
Set new topology function
- Parameters:
func (Callable) – The new function. If set to
None,_default_topology_funcis chosen- Raises:
TypeError – If func is not callable
Private properties¶
- property CommonFitnessFunctionDistributedTrainer._default_cooperative_fitness_estimation_func: Callable[Sequence[Sequence[float]], Sequence[float]]¶
Default cooperative fitness estimation function.
Return the average of all fitness trials.
- property CommonFitnessFunctionDistributedTrainer._default_num_representatives: int¶
Default number of representatives sent to the other subtrainers.
- Returns:
- Return type:
- property CommonFitnessFunctionDistributedTrainer._default_representatives_exchange_freq: int¶
Default number of iterations between representatives sending.
- Returns:
- Return type:
Static methods¶
- abstract static CommonFitnessFunctionDistributedTrainer.receive_representatives(subtrainer: CentralizedTrainer) None¶
Receive representative solutions.
This method must be overridden by subclasses.
- Parameters:
subtrainer (CentralizedTrainer) – The subtrainer receiving representatives
- Raises:
NotImplementedError – If has not been overridden
- abstract static CommonFitnessFunctionDistributedTrainer.send_representatives(subtrainer: CentralizedTrainer) None¶
Send representatives.
This method must be overridden by subclasses.
- Parameters:
subtrainer (CentralizedTrainer) – The sender subtrainer
- Raises:
NotImplementedError – If has not been overridden
Methods¶
- CommonFitnessFunctionDistributedTrainer.best_cooperators() list[list[Solution | None]] | None¶
Return a list of cooperators from each species.
Only used for cooperative trainers.
- abstract CommonFitnessFunctionDistributedTrainer.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
- CommonFitnessFunctionDistributedTrainer.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
- CommonFitnessFunctionDistributedTrainer.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:
- CommonFitnessFunctionDistributedTrainer.reset() None¶
Reset the trainer.
Delete all the internal data structures needed to perform the training.
- CommonFitnessFunctionDistributedTrainer.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.
- CommonFitnessFunctionDistributedTrainer.train()¶
Perform the training process.
Private methods¶
- abstract CommonFitnessFunctionDistributedTrainer._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
- CommonFitnessFunctionDistributedTrainer._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.
- CommonFitnessFunctionDistributedTrainer._init_internals() None¶
Set up the trainer internal data structures to start training.
Create the subtrainer communication queues.

