culebra.trainer.ea.ParallelCooperativeEA class¶
- class ParallelCooperativeEA(solution_classes: Sequence[type[Individual]], species: Sequence[Species], fitness_function: FitnessFunction, subtrainer_cls: type[SinglePopEA], max_num_iters: int | None = None, custom_termination_func: Callable[[ParallelCooperativeEA], bool] | None = None, pop_sizes: int | Sequence[int] | None = None, crossover_funcs: Callable[[Individual, Individual], tuple[Individual, Individual]] | Sequence[Callable[[Individual, Individual], tuple[Individual, Individual]]] | None = None, mutation_funcs: Callable[[Individual, float], tuple[Individual]] | Sequence[Callable[[Individual, float], tuple[Individual]]] | None = None, selection_funcs: Callable[[list[Individual], int, Any], list[Individual]] | Sequence[Callable[[list[Individual], int, Any], list[Individual]]] | None = None, crossover_probs: float | Sequence[float] | None = None, mutation_probs: float | Sequence[float] | None = None, gene_ind_mutation_probs: float | Sequence[float] | None = None, selection_funcs_params: dict[str, Any] | Sequence[dict[str, Any]] | None = None, num_subtrainers: int | None = None, representation_size: int | None = None, representation_freq: int | None = None, representation_topology_func: Callable[[int, int, Any], list[int]] | None = None, representation_topology_func_params: dict[str, Any] | None = None, representation_selection_func: Callable[[list[Individual], Any], Individual] | None = None, representation_selection_func_params: dict[str, Any] | None = None, checkpoint_activation: bool | None = None, checkpoint_freq: int | None = None, checkpoint_filename: str | None = None, verbosity: bool | None = None, random_seed: int | None = None, **subtrainer_params: Any)¶
Bases:
CooperativeEA,ParallelDistributedTrainerCreate a new trainer.
Each species is evolved in a different subpopulation.
- Parameters:
solution_classes (Sequence[type[Solution]]) – The individual class for each species
fitness_function (FitnessFunction) – The training fitness function
subtrainer_cls (type[SinglePopEA]) – Single-population EA trainer class to handle the subpopulations
max_num_iters (int) – Maximum number of iterations. If omitted,
_default_max_num_iterswill be used. Defaults toNonecustom_termination_func (Callable) – Custom termination criterion. If omitted,
_default_termination_func()is used. Defaults toNonepop_sizes (int | Sequence[int]) – The population size for each subpopulation. If only a single value is provided, the same size will be used for all the subpopulations. Different sizes can be provided in a
Sequence. All the sizes must be greater then zero. If omitted,_default_pop_sizeswill be used. Defaults toNonecrossover_funcs (Callable | Sequence[Callable]) – The crossover function for each subpopulation. If only a single value is provided, the same function will be used for all the subpopulations. Different functions can be provided in a
Sequence. If omitted,_default_crossover_funcswill be used. Defaults toNonemutation_funcs (Callable | Sequence[Callable]) – The mutation function for each subpopulation. If only a single value is provided, the same function will be used for all the subpopulations. Different functions can be provided in a
Sequence. If omitted,_default_mutation_funcswill be used. Defaults toNoneselection_funcs (Callable | Sequence[Callable]) – The selection function for each subpopulation. If only a single value is provided, the same function will be used for all the subpopulations. Different functions can be provided in a
Sequence. If omitted,_default_selection_funcswill be used. Defaults toNonecrossover_probs (float | Sequence[float]) – The crossover probability for each subpopulation. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. All the probabilities must be in (0, 1). If omitted,_default_crossover_probswill be used. Defaults toNonemutation_probs (float | Sequence[float]) – The mutation probability for each subpopulation. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. All the probabilities must be in (0, 1). If omitted,_default_mutation_probswill be used. Defaults toNonegene_ind_mutation_probs (float | Sequence[float]) – The gene independent mutation probability for each subpopulation. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. All the probabilities must be in (0, 1). If omitted,_default_gene_ind_mutation_probswill be used. Defaults toNoneselection_funcs_params (dict | Sequence[dict]) – The parameters for the selection function of each subpopulation. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. If omitted,_default_selection_funcs_paramswill be used. Defaults toNonenum_subtrainers (int) – The number of subtrainers (species). If omitted,
_default_num_subtrainerswill be used. Defaults toNonerepresentation_size (int) – Number of representative individuals that will be sent to the other subpopulations. If omitted,
_default_representation_sizewill be used. Defaults toNonerepresentation_freq (int) – Number of iterations between representatives sendings. If omitted,
_default_representation_freqwill be used. Defaults toNonerepresentation_topology_func (Callable) – Topology function for representatives sending. If omitted,
_default_representation_topology_funcwill be used. Defaults toNonerepresentation_topology_func_params (dict) – Parameters to obtain the destinations with the topology function. If omitted,
_default_representation_topology_func_paramswill be used. Defaults toNonerepresentation_selection_func (Callable) – Policy function to choose the representatives from each subpopulation (species). If omitted,
_default_representation_selection_funcwill be used. Defaults toNonerepresentation_selection_func_params (dict) – Parameters to obtain the representatives with the selection policy function. If omitted,
_default_representation_selection_func_paramswill be used. Defaults toNonecheckpoint_activation (bool) – Checkpoining activation. If omitted,
_default_checkpoint_activationwill be used. Defaults toNonecheckpoint_freq (int) – The checkpoint frequency. If omitted,
_default_checkpoint_freqwill be used. Defaults toNonecheckpoint_filename (str) – The checkpoint file path. If omitted,
_default_checkpoint_filenamewill be used. Defaults toNoneverbosity (bool) – The verbosity. If omitted,
_default_verbositywill be used. Defaults toNonesubtrainer_params (dict) – Custom parameters for the subpopulations (species) trainer
- Raises:
TypeError – If any argument is not of the appropriate type
ValueError – If any argument has an incorrect value
Class attributes¶
- ParallelCooperativeEA.objective_stats = {'Avg': <function mean>, 'Max': <function max>, 'Min': <function min>, 'Std': <function std>}¶
Statistics calculated for each objective.
- ParallelCooperativeEA.stats_names = ('Iter', 'Pop', 'NEvals')¶
Statistics calculated each iteration.
Class methods¶
- classmethod ParallelCooperativeEA.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 ParallelCooperativeEA.checkpoint_filename: str¶
Checkpoint file path.
- Return type:
- Setter:
Modify the checkpoint file path
- Parameters:
value (str) – New value for the checkpoint file path. If set to
None,_default_checkpoint_filenameis chosen- Raises:
TypeError – If value is not a valid file name
ValueError – If the value extension is not
SERIALIZED_FILE_EXTENSION
- property ParallelCooperativeEA.checkpoint_freq: int¶
Checkpoint frequency.
- Return type:
- Setter:
Modify the checkpoint frequency
- Parameters:
value (int) – New value for the checkpoint frequency. If set to
None,_default_checkpoint_freqis chosen- Raises:
TypeError – If value is not an integer
ValueError – If value is not a positive number
- property ParallelCooperativeEA.container: Trainer | None¶
Container of this trainer.
The trainer container is only used by distributed trainers. For the rest of trainers defaults to
None.
- property ParallelCooperativeEA.crossover_funcs: tuple[Callable[[Individual, Individual], tuple[Individual, Individual]] | None]¶
Crossover function for each subpopulation.
- Returns:
If subtrainers have been generated, the subtrainers crossover function. Otherwise, the crossover functions used to call the constructor
- Return type:
- Setter:
Set the crossover function for each subpopulation
- Parameters:
funcs (Callable | Sequence[Callable]) – The new crossover functions. If only a single value is provided, the same function will be used for all the subpopulations. Different functions can be provided in a
Sequence. If set to :data`None`,_default_crossover_funcsis chosen- Raises:
TypeError – If funcs is not
Callableor aSequenceofCallable
- property ParallelCooperativeEA.crossover_probs: tuple[float | None]¶
Crossover probability for each subpopulation.
- Returns:
If subtrainers have been generated, the subtrainers crossover probability. Otherwise, the crossover probabilities used to call the constructor
- Return type:
- Setter:
Set a new crossover probability for each subpopulation
- Parameters:
probs (float | Sequence[float]) – The new probabilities. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. All the probabilities must be in (0, 1). If set to :data`None`,_default_crossover_probsis chosen- Raises:
TypeError – If probs is not a real number or a
Sequenceof real numbersValueError – If any value in probs is not in (0, 1)
- property ParallelCooperativeEA.custom_termination_func: Callable[[Trainer], bool] | None¶
Custom termination criterion.
Although the trainer will always stop when the
max_num_itersare reached, a custom termination criterion can be set to detect convergente and stop the trainer earlier. This custom termination criterion must be a function which receives the trainer as its unique argument and returns a boolean value,Trueif the search should terminate orFalseotherwise.If more than one arguments are needed to define the termination condition,
functools.partial()can be used:from functools import partial def my_crit(trainer, max_iters): if trainer.current_iter < max_iters: return False return True trainer.custom_termination_func = partial(my_crit, max_iters=10)
- property ParallelCooperativeEA.fitness_function: FitnessFunction¶
Training fitness function.
- Return type:
- Setter:
Set a new fitness function
- Parameters:
func (FitnessFunction) – The new training fitness function
- Raises:
TypeError – If func is not a valid fitness function
- property ParallelCooperativeEA.gene_ind_mutation_probs: tuple[float | None]¶
Gene independent mutation probabilities.
- Returns:
If subtrainers have been generated, the subtrainers gene independent mutation probability. Otherwise, the gene independent mutation probabilities used to call the constructor
- Return type:
- Setter:
Set new gene independent mutation probabilities
- Parameters:
probs (float | Sequence[float]) – The new probabilities. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. All the probabilities must be in (0, 1). If set to :data`None`,_default_gene_ind_mutation_probsis chosen- Raises:
TypeError – If probs is not a real number or a
Sequenceof real numbersValueError – If any value in probs is not in (0, 1)
- property ParallelCooperativeEA.index: int¶
Trainer index.
The trainer index is only used by distributed trainers. For the rest of trainers
_default_indexis used.- Return type:
- Setter:
Set a new value for trainer index.
- Parameters:
value (int) – New value for the trainer index. If set to
None,_default_indexis chosen- Raises:
TypeError – If value is not an integer
ValueError – If value is a negative number
- property ParallelCooperativeEA.max_num_iters: int¶
Maximum number of iterations.
- Return type:
- Setter:
Set a new value for the maximum number of iterations
- Parameters:
value (int) – The new maximum number of iterations. If set to
None, the default maximum number of iterations,_default_max_num_iters, is chosen- Raises:
TypeError – If value is not an integer
ValueError – If value is not a positive number
- property ParallelCooperativeEA.mutation_funcs: tuple[Callable[[Individual, float], tuple[Individual]] | None]¶
Mutation function for each subpopulation.
- Returns:
If subtrainers have been generated, the subtrainers mutation function. Otherwise, the mutation functions used to call the constructor
- Return type:
- Setter:
Set the mutation function for each subpopulation
- Parameters:
funcs (Callable | Sequence[Callable]) – The new mutation functions. If only a single value is provided, the same function will be used for all the subpopulations. Different functions can be provided in a
Sequence. If set to :data`None`,_default_mutation_funcsis chosen- Raises:
TypeError – If funcs is not
Callableor aSequenceofCallable
- property ParallelCooperativeEA.mutation_probs: tuple[float | None]¶
Mutation probability for each subpopulation.
- Returns:
If subtrainers have been generated, the subtrainers mutation probability. Otherwise, the mutation probabilities used to call the constructor
- Return type:
- Setter:
Set a new mutation probability for each subpopulation
- Parameters:
probs (float | Sequence[float]) – The new probabilities. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. All the probabilities must be in (0, 1). If set to :data`None`,_default_mutation_probsis chosen- Raises:
TypeError – If probs is not a real number or a
Sequenceof real numbersValueError – If any value in probs is not in (0, 1)
- property ParallelCooperativeEA.num_evals: int | None¶
Number of evaluations performed while training.
- property ParallelCooperativeEA.num_subtrainers: int¶
Number of subtrainers.
- Return type:
- Setter:
Set a new value for the number of subtrainers
- Parameters:
value (int) – The new number of subtrainers. If set to
None,_default_num_subtrainersis chosen- Raises:
TypeError – If value is not an integer
ValueError – If value is not a positive number
- property ParallelCooperativeEA.pop_sizes: tuple[int | None]¶
Population size for each subtrainer.
- Returns:
If subtrainers have been generated, the subtrainers population size. Otherwise, the population size values used to call the constructor
- Return type:
- Setter:
Set the population size for each subtrainer
- Parameters:
sizes (int | Sequence[int]) – The new population sizes. If only a single value is provided, the same size will be used for all the subtrainers. Different sizes can be provided in a
Sequence. All the sizes must be greater then zero. If set to :data`None`,_default_pop_sizesis chosen- Raises:
ValueError – If any value in sizes is not greater than zero
- property ParallelCooperativeEA.representation_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_representation_freqis chosen- Raises:
TypeError – If value is not an integer
ValueError – If value is not a positive number
- property ParallelCooperativeEA.representation_selection_func: Callable[[list[Solution], Any], Solution]¶
Representation selection policy function.
- Returns:
A function that chooses which solutions are selected as representatives of each subtrainer
- Return type:
- Setter:
Set new representation selection policy function.
- Parameters:
func (Callable) – The new function. If set to
None,_default_representation_selection_funcis chosen- Raises:
TypeError – If func is not callable
- property ParallelCooperativeEA.representation_selection_func_params: dict[str, Any]¶
Parameters of the representation selection function.
- property ParallelCooperativeEA.representation_size: int¶
Representation size.
- Returns:
The number of representatives sent to the other subtrainers
- Return type:
- Setter:
Set a new representation size
- Parameters:
size (int) – The new size. If set to
None,_default_representation_sizeis chosen- Raises:
TypeError – If size is not an integer number
ValueError – If size is not positive
- property ParallelCooperativeEA.representation_topology_func: Callable[[int, int, Any], list[int]]¶
Representation topology function.
- Return type:
- Setter:
Set new representation topology function
- Parameters:
func (Callable) – The new function. If set to
None,_default_representation_topology_funcis chosen- Raises:
TypeError – If func is not callable
- property ParallelCooperativeEA.representation_topology_func_params: dict[str, Any]¶
Parameters of the representation topology function.
- property ParallelCooperativeEA.representatives: list[list[Solution | None]] | None¶
Representatives of the other species.
- property ParallelCooperativeEA.selection_funcs: tuple[Callable[[list[Individual], int, Any], list[Individual]] | None]¶
Selection function for each subpopulation.
- Returns:
If subtrainers have been generated, the subtrainers selection function. Otherwise, the selection functions used to call the constructor
- Return type:
- Setter:
Set a new selection function for each subpopulation
- Parameters:
funcs (Callable | Sequence[Callable]) – The new selection functions. If only a single value is provided, the same function will be used for all the subpopulations. Different functions can be provided in a
Sequence. If set to :data`None`,_default_selection_funcsis chosen- Raises:
TypeError – If funcs is not
Callableor aSequenceofCallable
- property ParallelCooperativeEA.selection_funcs_params: tuple[dict[str, Any] | None]¶
Parameters for the selection function of each subpopulation.
- Returns:
The parameters for the selection function of each subtrainer. If subtrainers have not been initialized, the parameters of the selection function defined for each subtrainer in the trainer constructor is returned
- Return type:
- Setter:
Set new parameters for the selection function of each subpopulation
- Parameters:
param_dicts (dict | Sequence[dict]) – The new parameters. If only a single value is provided, the same probability will be used for all the subpopulations. Different probabilities can be provided in a
Sequence. If set to :data`None`,_default_selection_funcs_paramsis chosen- Raises:
ValueError – If any element of the sequence is not a
dict
- property ParallelCooperativeEA.subtrainer_checkpoint_filenames: Generator[str, None, None]¶
Checkpoint file name for all the subtrainers.
- property ParallelCooperativeEA.subtrainer_cls: type[SingleSpeciesTrainer]¶
Trainer class to handle the subtrainers.
Each subtrainer will be handled by a single-species trainer.
- Return type:
- Setter:
Set a new trainer class to handle the subtrainers
- Parameters:
cls (type[SingleSpeciesTrainer]) – The new class
- Raises:
TypeError – If cls is not a valid trainer class
- property ParallelCooperativeEA.subtrainer_params: dict[str, Any]¶
Custom parameters for the subtrainers.
- property ParallelCooperativeEA.subtrainers: list[SingleSpeciesTrainer] | None¶
Subtrainers.
One single-species trainer for each subtrainer.
- Return type:
Private properties¶
- property ParallelCooperativeEA._default_checkpoint_activation: bool¶
Default checkpointing activation.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_checkpoint_filename: str¶
Default checkpointing file name.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_checkpoint_freq: int¶
Default checkpointing frequency.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_crossover_funcs: tuple[None]¶
Default crossover function for each subpopulation.
- property ParallelCooperativeEA._default_crossover_probs: tuple[None]¶
Default crossover probability for each subpopulation.
- property ParallelCooperativeEA._default_gene_ind_mutation_probs: tuple[None]¶
Default gene independent mutation probability for each subpopulation.
- property ParallelCooperativeEA._default_max_num_iters: int¶
Default maximum number of iterations.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_mutation_funcs: tuple[None]¶
Default mutation function for each subpopulation.
- property ParallelCooperativeEA._default_mutation_probs: tuple[None]¶
Default mutation probability for each subpopulation.
- property ParallelCooperativeEA._default_num_subtrainers: int¶
Default number of subtrainers.
- Returns:
The number of species
- Return type:
- property ParallelCooperativeEA._default_pop_sizes: tuple[None]¶
Default population size for each subtrainer.
- property ParallelCooperativeEA._default_representation_freq: int¶
Default number of iterations between representatives sending.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_representation_selection_func: Callable[[list[Solution], Any], Solution]¶
Default selection policy function to choose the representatives.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_representation_selection_func_params: dict[str, Any]¶
Default parameters for the representatives selection policy function.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_representation_size: int¶
Default number of representatives sent to the other subtrainers.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_representation_topology_func: Callable[[int, int, Any], list[int]]¶
Default representation topology function.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_representation_topology_func_params: dict[str, Any]¶
Default parameters of the representation topology function.
- Returns:
- Return type:
- property ParallelCooperativeEA._default_selection_funcs: tuple[None]¶
Default selection function for each subpopulation.
- property ParallelCooperativeEA._default_selection_funcs_params: tuple[None]¶
Default parameters for the selection function of each subpopulation.
Static methods¶
- static ParallelCooperativeEA.receive_representatives(subtrainer: SinglePopEA) None¶
Receive representative individuals.
- Parameters:
subtrainer (SinglePopEA) – The subtrainer receiving representatives
- static ParallelCooperativeEA.send_representatives(subtrainer: SinglePopEA) None¶
Send representatives.
- Parameters:
subtrainer (SinglePopEA) – The sender subtrainer
Private static methods¶
- static ParallelCooperativeEA._init_subtrainer_representatives(subtrainer: SingleSpeciesTrainer, solution_classes: Sequence[type[Solution]], species: Sequence[Species], representation_size: int)¶
Init the representatives of the other species for a subtrainer.
This method is used to override dynamically the
_init_representatives()of all the subtrainers, when they are generated with the_generate_subtrainers()method, to let them initialize the list of representative individuals of the other species- Parameters:
subtrainer (SingleSpeciesTrainer) – The subtrainer. The representatives from the remaining subtrainers will be initialized for this subtrainer
solution_classes (Sequence[type[Solution]]) – The individual class for each species.
species (Sequence[Species]) – The species to be evolved by this trainer
representation_size (int) – Number of representative individuals generated for each species
Methods¶
- ParallelCooperativeEA.best_representatives() list[list[Solution]] | None¶
Return a list of representatives from each species.
- ParallelCooperativeEA.best_solutions() tuple[HallOfFame]¶
Get the best solutions found for each species.
- Returns:
One Hall of Fame for each species
- Return type:
- ParallelCooperativeEA.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
- ParallelCooperativeEA.evaluate(sol: Solution, fitness_func: FitnessFunction | None = None, index: int | None = None, representatives: Sequence[Sequence[Solution | None]] | None = None) None¶
Evaluate one solution.
Its fitness will be modified according with the fitness function results. Besides, if called during training, the number of evaluations will be also updated.
- Parameters:
sol (Solution) – The solution
fitness_func (FitnessFunction) – The fitness function. If omitted, the default training fitness function (
fitness_function) is usedindex (int) – Index where sol should be inserted in the representatives sequence to form a complete solution for the problem. If omitted,
indexis usedrepresentatives (Sequence[Sequence[Solution]]) – Sequence of representatives of other species or
None(if no representatives are needed to evaluate sol). If omitted, the current value ofrepresentativesis used
- ParallelCooperativeEA.reset() None¶
Reset the trainer.
Delete the state of the trainer (with
_reset_state()) and also all the internal data structures needed to perform the search (with_reset_internals()).This method should be invoqued each time a hyper parameter is modified.
- ParallelCooperativeEA.test(best_found: Sequence[HallOfFame], fitness_func: FitnessFunction | None = None, representatives: 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. If ommited, the default training fitness function (
fitness_function) will be usedrepresentatives (Sequence[Sequence[Solution]]) – Sequence of representatives of other species or
None(if no representatives are needed). If omitted, the current value ofrepresentativesis used
- Raises:
TypeError – If any parameter has a wrong type
ValueError – If any parameter has an invalid value.
- ParallelCooperativeEA.train(state_proxy: DictProxy | None = None) None¶
Perform the training process.
- Parameters:
state_proxy (DictProxy) – dictionary proxy to copy the output state of the trainer procedure. Only used if train is executed within a
multiprocess.Process. Defaults toNone
Private methods¶
- ParallelCooperativeEA._default_termination_func() bool¶
Default termination criterion.
- Returns:
Trueifmax_num_itersiterations have been run- Return type:
- abstract ParallelCooperativeEA._do_iteration() None¶
Implement an iteration of the search process.
This abstract method should be implemented by subclasses in order to implement the desired behavior.
- ParallelCooperativeEA._do_iteration_stats() None¶
Perform the iteration stats.
This method should be implemented by subclasses in order to perform the adequate stats.
- ParallelCooperativeEA._finish_iteration() None¶
Finish an iteration.
Finish the iteration metrics (number of evaluations, execution time) after each iteration is run.
- ParallelCooperativeEA._finish_search() None¶
Finish the search process.
This method is called after the search has finished. It can be overridden to perform any treatment of the solutions found.
- ParallelCooperativeEA._generate_subtrainers() None¶
Generate the subpopulation trainers.
Also assign an
indexand acontainerto eachSinglePopEAsubtrainer, and change the subpopulation subtrainers’checkpoint_filenameaccording to the container checkpointing file name and each subpopulation index.Finally, the
_preprocess_iteration()and_postprocess_iteration()methods of thesubtrainer_clsclass are dynamically overridden, in order to allow individuals exchange between subtrainers, if necessary.- Raises:
RuntimeError – If the length of any properties sequence does not match the number of subpopulations.
- ParallelCooperativeEA._get_state() dict[str, Any]¶
Return the state of this trainer.
Default state is a dictionary composed of the values of the
logbook,num_evals,runtime,current_iter, andrepresentativestrainer properties, along with a private boolean attribute that informs if the search has finished and also the states of therandomandnumpy.randommodules.If subclasses use any more properties to keep their state, the
_get_state()and_set_state()methods must be overridden to take into account such properties.- Return type:
- ParallelCooperativeEA._init_internals() None¶
Set up the trainer internal data structures to start searching.
Overridden to create a multiprocess manager and proxies to communicate with the processes running the subtrainers.
- ParallelCooperativeEA._init_representatives() None¶
Init the representatives of the other species.
Only used for cooperative approaches, which need representatives of all the species to form a complete solution for the problem. Cooperative subclasses of the
Trainerclass should override this method to get the representatives of the other species initialized.
- ParallelCooperativeEA._init_search() None¶
Init the search process.
Initialize the state of the trainer (with
_init_state()) and all the internal data structures needed (with_init_internals()) to perform the search.
- ParallelCooperativeEA._init_state() None¶
Init the trainer state.
If there is any checkpoint file, the state is initialized from it with the
_load_state()method. Otherwise a new initial state is generated with the_new_state()method.
- ParallelCooperativeEA._load_state() None¶
Load the state of the last checkpoint.
- Raises:
Exception – If the checkpoint file can’t be loaded
- ParallelCooperativeEA._new_state() None¶
Generate a new trainer state.
Overridden to set the overall runtime and number of evaluations to
None, since their final values will be generated from the subtrainers’ state, once the trainer has finished.
- ParallelCooperativeEA._postprocess_iteration() None¶
Postprocess after doing the iteration.
Subclasses should override this method to make any postprocessment after performing an iteration.
- ParallelCooperativeEA._preprocess_iteration() None¶
Preprocess before doing the iteration.
Subclasses should override this method to make any preprocessment before performing an iteration.
- ParallelCooperativeEA._reset_internals() None¶
Reset the internal structures of the trainer.
Overridden to reset the multiprocess manager and proxies.
- ParallelCooperativeEA._reset_state() None¶
Reset the trainer state.
If subclasses overwrite the
_new_state()method to add any new property to keep their state, this method should also be overridden to reset the full state of the trainer.
- ParallelCooperativeEA._save_state() None¶
Save the state at a new checkpoint.
- Raises:
Exception – If the checkpoint file can’t be written
- ParallelCooperativeEA._search() None¶
Apply the search algorithm.
Each subtrainer runs in a different process.
- ParallelCooperativeEA._set_cooperative_fitness(sol: Solution, fitness_trials_values: [Sequence[tuple[float]]]) None¶
Estimate a solution fitness from multiple evaluation trials.
Applies an average of the fitness trials values. Trainers requiring another estimation should override this method.
- ParallelCooperativeEA._set_state(state: dict[str, Any]) None¶
Set the state of this trainer.
If subclasses use any more properties to keep their state, the
_get_state()and_set_state()methods must be overridden to take into account such properties.- Parameters:
state (dict) – The last loaded state
- ParallelCooperativeEA._start_iteration() None¶
Start an iteration.
Prepare the iteration metrics (number of evaluations, execution time) before each iteration is run.
- ParallelCooperativeEA._termination_criterion() bool¶
Control the search termination.
- Returns:
Trueif either the default termination criterion or a custom termination criterion is met. The default termination criterion is implemented by the_default_termination_func()method. Another custom termination criterion can be set withcustom_termination_funcmethod.- Return type:

