evolvepy.generator package

Subpackages

Submodules

evolvepy.generator.basic_layers module

class evolvepy.generator.basic_layers.Block(chromosome_names: List[str] | str | None = None, run: bool = False, name: str | None = None)[source]

Bases: Layer

Block a determined chromosome for the next layers

__init__(chromosome_names: List[str] | str | None = None, run: bool = False, name: str | None = None)[source]

Initialization for the Block class with the blocked chromosomes.

Parameters:
  • chromosome_names (Union[List[str], str]) – names of the chromosomes

  • run (bool) – define the flag to run the code only once

  • name (string) – name of the layer

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]

Generic call to apply the mutation function

Parameters:
  • population (np.ndarray) – array of inidividuals

  • fitness (np.ndarray) – array of individuals fitness

  • context (Context) – context of the population from the previous layers

Returns:

new array of inidividuals fitness (np.ndarray): new array of individuals fitness

Return type:

population (np.ndarray)

class evolvepy.generator.basic_layers.ElitismLayer(n_to_pass: int = 1, name: str | None = None)[source]

Bases: Layer

Chooses the best n_to_pass individuals from the population

__init__(n_to_pass: int = 1, name: str | None = None)[source]

Generic initialization for the object, used only for the parent class

Parameters:
  • n_to_pass (int) – Number of individuals to be selected. Defaults 1.

  • name (str) – Layer’s name. Defaults None (will use class name).

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]

Sort the population by fitness and then filter the best n_to_pass individuals

Parameters:
  • population (np.ndarray) – New array of inidividuals

  • fitness (np.ndarray) – New array of individuals fitness

  • context (Context) – Context of the population from the previous layers

Returns:

Array of individuals. fitness (np.ArrayLike): Array of fitness per individual.

Return type:

population (np.ArrayLike)

class evolvepy.generator.basic_layers.FilterFirsts(n_to_pass: int = 1, name: str | None = None)[source]

Bases: Layer

Filter to get the first n_to_pass individuals.

__init__(n_to_pass: int = 1, name: str | None = None)[source]

Initialize the Filter from the top layer

Parameters:
  • n_to_pass (int) – Number of best individuals selected.

  • name (strig) – Name of the layer.

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]

Generic call to funcion to use the class as funtion call

Parameters:
  • population (np.ArrayLike) – array of individuals

  • parameters (Dict(string, string)) – parameters for the function

  • dynamic_parameters (Dit(string, string)) – dinamic parameters of the function

Returns:

Number of best individuals selected

Return type:

n_to_pass (int)

class evolvepy.generator.basic_layers.RandomPredation(n_to_predate: int = 1, name: str | None = None)[source]

Bases: Layer

Replaces the last n_to_predate individuals for random ones

__init__(n_to_predate: int = 1, name: str | None = None)[source]

Initialization for the RandomPredation class with the number individuals to be replaced.

Parameters:
  • n_to_predate (int) – number of individuals to be replaced.

  • name (string) – name of the layer

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]

Generic call to apply the mutation function

Parameters:
  • population (np.ndarray) – Array of inidividuals

  • fitness (np.ndarray) – Array of individuals fitness

  • context (Context) – Context of the population from the previous layers

Returns:

New array of inidividuals fitness (np.ndarray): New array of individuals fitness

Return type:

population (np.ndarray)

class evolvepy.generator.basic_layers.Sort(name: str | None = None)[source]

Bases: Layer

Sorting class for the model pipeline, it sorts the population acording to fitness

__init__(name: str | None = None)[source]

Generic initialization for the object, used only for the parent class

Parameters:

name (str) – name of the layer

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]

Sort the population by fitness.

Parameters:
  • population (np.ndarray) –

  • fitness (np.ndarray) –

  • context (Context) –

Returns:

Array of individuals. fitness (np.ArrayLike): Array of fitness per individual.

Return type:

population (np.ArrayLike)

evolvepy.generator.combine module

class evolvepy.generator.combine.CombineLayer(selection_function: Callable, crossover_function: Callable, n_combine: int = 2, name: str | None = None, chromosome_names: List[str] | str | None = None)[source]

Bases: ChromosomeOperator

Layer to join different pipeline paths

__init__(selection_function: Callable, crossover_function: Callable, n_combine: int = 2, name: str | None = None, chromosome_names: List[str] | str | None = None)[source]

Initialization for combine layer setting selection, crossover and n_combine parameters

Parameters:
  • selection_function (Callable) – Selection function used fot the layer

  • crossover_function (Callable) – Crossover function used for the layer

  • n_combine (int) – Number of layers to be combined

  • name (string) – Name of the layer

  • chromosome_names (List[string]) – list of names from the chromosomes

call_chromosomes(chromosomes: ndarray, fitness: ndarray, context: Context, name: str | None) ndarray[source]

Generic call for combine function

static combine(chromosomes: ndarray, fitness: ndarray, selection_function: Callable, crossover_function: Callable, n_combine: int)[source]

Combine two or more layers

Parameters:
  • chromossomes (np.ndarray) – Array of chromosomes

  • fitness (np.ndarray) – Array of inidividuals fitness

  • selection_function (Callable) – Selection function for the layer

  • crossover_function (Callable) – Crossover function for the layer

  • n_combine (int) – Number of layers to combine

Returns:

Array of new population

Return type:

result (np.ArrayLike)

evolvepy.generator.context module

class evolvepy.generator.context.Context(population_size: int, chromosome_names: List[str] | None = None, sorted=False)[source]

Bases: object

Layer to pass the context and restrictions from the previous layers

__init__(population_size: int, chromosome_names: List[str] | None = None, sorted=False)[source]

Initialization for the context layer

Parameters:
  • population_size (int) – Number of individuals

  • chromosome_names (List[string]) – Names of chromosomes

  • sorted (bool) – Flag to indicate f the population is sorted or not.

property block_all: bool

Prevents any alteration on all chromosomes

Setter:
Raises:

ValueError: raised if block_all is not a boolean.

property chromosome_names: List[str]

Return chromosome names

Returns:

chromosome names

Return type:

chromosome_names (int)

copy() Context[source]

Create a copy of this instance

default_values = ['sorted', '_sorted', 'blocked', '_chromosome_names', 'chromosome_names', '_values', 'have_value', 'copy', '_block_all', 'block_all', 'population_size', '_population_size']
have_value(name: str) bool[source]

Returns if name if within the registered values

property population_size: int

Return population size

Returns:

population

Return type:

population_size (int)

property sorted: bool

Return if the population and fitness are sorted or not

Setter:
Raises:

ValueError: raised if value is not a boolean.

evolvepy.generator.descriptor module

class evolvepy.generator.descriptor.Descriptor(chromosome_sizes: int | float | complex | str | bytes | ~numpy.generic | ~typing.Sequence[int | float | complex | str | bytes | ~numpy.generic] | ~typing.Sequence[~typing.Sequence[~typing.Any]] | ~numpy.typing._array_like._SupportsArray | None = 1, chromosome_ranges: None | ~typing.List[None | ~typing.Tuple] | ~typing.Tuple = None, types: list | ~numpy.dtype | None | type | ~numpy.typing._dtype_like._SupportsDType | str | ~typing.Tuple[~typing.Any, int] | ~typing.Tuple[~typing.Any, int | ~typing.Sequence[int]] | ~typing.List[~typing.Any] | ~numpy.typing._dtype_like._DTypeDict | ~typing.Tuple[~typing.Any, ~typing.Any] = [<class 'numpy.float32'>], names: list | str | None = None)[source]

Bases: object

Object for the caracteristics of the individuals in population

__init__(chromosome_sizes: int | float | complex | str | bytes | ~numpy.generic | ~typing.Sequence[int | float | complex | str | bytes | ~numpy.generic] | ~typing.Sequence[~typing.Sequence[~typing.Any]] | ~numpy.typing._array_like._SupportsArray | None = 1, chromosome_ranges: None | ~typing.List[None | ~typing.Tuple] | ~typing.Tuple = None, types: list | ~numpy.dtype | None | type | ~numpy.typing._dtype_like._SupportsDType | str | ~typing.Tuple[~typing.Any, int] | ~typing.Tuple[~typing.Any, int | ~typing.Sequence[int]] | ~typing.List[~typing.Any] | ~numpy.typing._dtype_like._DTypeDict | ~typing.Tuple[~typing.Any, ~typing.Any] = [<class 'numpy.float32'>], names: list | str | None = None)[source]

Defines the initial population caracteristics

Parameters:
  • chromosome_sizes (int) – Number of chromosomes per individual

  • chromosome_ranges (Tuple[float, float]) – Range of possible values for each chromosome

  • types (List[DTypeLikes]) – Data type of chromosome

  • names (List[string]) – Names of the chromossomes

property chromosome_names

Returns: Chromosome names of the individuals

property chromosome_ranges

Returns: Chromosome ranges of the individuals

property dtype

Returns: Data type of the individuals

evolvepy.generator.firstgen module

class evolvepy.generator.firstgen.FirstGenLayer(descriptor: Descriptor, initialize_zeros: bool = False, name: str | None = None, chromosome_names: List[str] | str | None = None, run: bool = True)[source]

Bases: ChromosomeOperator

Generates the first population for the pipeline

__init__(descriptor: Descriptor, initialize_zeros: bool = False, name: str | None = None, chromosome_names: List[str] | str | None = None, run: bool = True)[source]

Initialization fr the first population, it defines the base of the evolution

Parameters:
  • descriptor (class Descriptor) – Descriptor object defining the individuals

  • initialize_zeros (bool) – Flag to indentify type of initialization, if True the population will init with zero value individuals

  • name (string) – Name of the layer

  • chromosome_names (List[string]) – Names of the chromossomes of each inidividual

  • run (bool) – Flag to define if the code will run or not, it is needed to make it run only on command

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]

Generic call to initialize the population nd its fitness

Parameters:
  • population (np.ndarray) – Population array

  • fitness (np.ndarray) – Fitness array

  • context (Context) – COntext and restrictions from previous layers

Returns:

New population array fitness (np.ndarray): New fitness array

Return type:

population (np.ndarray)

call_chromosomes(chromosomes: ndarray, fitness: ndarray, context: Context, name: str | None) ndarray[source]

Return chromosome values

Parameters:
  • chromosomes (np.ndarray) – Array of chromosomes

  • fitness (np.ndarray) – Array of population fitness

  • context (Context) – Context from previous layers

  • name (string) – Layer name

Returns:

Array of chromosomes

Return type:

chromosomes (np.ndarray)

evolvepy.generator.generator module

class evolvepy.generator.generator.Generator(layers: None | List[Layer] = None, first_layer: Layer | None = None, last_layer: Layer | None = None, descriptor: Descriptor | None = None)[source]

Bases: object

Main class of the pipeline, it defines the layers oder, bifurcations and parameters

__init__(layers: None | List[Layer] = None, first_layer: Layer | None = None, last_layer: Layer | None = None, descriptor: Descriptor | None = None)[source]

Initialization for the Generator class with the desired layer order and individuasl description

Parameters:
  • layers (List[Layer]) – List of layers used in the pieline

  • first_layer (Layer) – First layer of the pipeline

  • last_layer (Layer) – Last layer of a pipeline

  • descriptor (Descriptor) – Object describing the individuals chromosome number, type and range

add(layer: Layer) None[source]

Add layer to the pipeline

Parameters:

layer (Layer) – Logical Layer to be added to the pipeline

property fitness: ndarray

Return fitness atribute from population

Returns:

Array of population fitness

Return type:

fitness (np.ndarray)

generate(population_size: int) ndarray[source]

Generate the pipeline from the given layers and atributes building the evolution model to pass to the evolver

Parameters:

population_size (int) – Number of individuals in a population

Returns:

generated population

Return type:

population (np.ndarray)

get_all_dynamic_parameters() Dict[str, object][source]

Recover all static imutable parameters

Returns:

Dictionay of pipeline’s dynamic parameters

Return type:

dynamic_parameters (Dict[string, object])

get_all_static_parameters() Dict[str, object][source]

Recover all static imutable parameters

Returns:

Dictionay of pipeline’s static parameters

Return type:

static_parameters (Dict[string, object])

get_parameter(layer_name: str, parameter_name: str | None = None) object[source]

Recover the value of a specified parameter for a specified layer

Parameters:
  • layer_name (string) – Name of the desired layer

  • parameter_name (string) – Name of th desired parameter

Returns:

Object containing the parameter values

Return type:

parameter (object)

get_parameters(layer_name: str) Dict[str, object][source]

Recover the value of all parameters for a specified layer

Parameters:

layer_name (string) – Name of the desired layer

Returns:

Dict of parameters and their values

Return type:

parameters (Dict[str, object])

set_parameter(layer_name: str, parameter_name: str, value: object) None[source]

Defines the value of a specified parameter for a specified layer

Parameters:
  • layer_name (string) – Name of the desired layer

  • parameter_name (string) – Name of th desired parameter

  • value (object) – Value to update the parameter

set_parameters(layer_name: str, parameters: Dict[str, object]) None[source]

Defines the value of n specified parameters for a specified layer

Parameters:
  • layer_name (string) – Name of the desired layer

  • parameters (Dict[str, object]) – Dict of parameters to update and their new values

evolvepy.generator.layer module

class evolvepy.generator.layer.ChromosomeOperator(name: str | None = None, dynamic_parameters: Dict[str, bool] | None = None, parameters: Dict[str, object] | None = None, chromosome_names: List[str] | str | None = None)[source]

Bases: Layer

Base layer for chromossome operations such as numeric and boolean mutations

__init__(name: str | None = None, dynamic_parameters: Dict[str, bool] | None = None, parameters: Dict[str, object] | None = None, chromosome_names: List[str] | str | None = None)[source]

Initialization for the chromossome operator layer with name and parameters

Parameters:
  • name (string) – Layer’s name

  • dynamic_parameters (Dict[string, bool]) – Dictionary of mutable parameters

  • parameters (Dict[string, object]) – Dictionary of immutable parameters

  • chromosome_names (List[str]) – Name of each chromosome

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]

Generic call to use the object as a funtion call, applying the layer oparetion

Parameters:
  • population (np.ndarray) – Population array

  • fitness (np.ndarray) – Fitness array

  • context (Context) – Context object for the next layers

Returns:

New population array fitness (np.ndarray): New fitness array

Return type:

population (np.ndarray)

call_chromosomes(chromosomes: ndarray, fitness: ndarray, context: Context, name: str | None) ndarray[source]
class evolvepy.generator.layer.Concatenate(name: str | None = None)[source]

Bases: Layer

Concatenation layer to join 2 or more layers

__init__(name: str | None = None)[source]

Initialization for thr Cconcatenate layer with the number of layers

Parameters:

name (string) – Layer’s name

class evolvepy.generator.layer.Layer(name: str | None = None, dynamic_parameters: Dict[str, bool] | None = None, parameters: Dict[str, object] | None = None)[source]

Bases: Configurable

Base Layer class with essential properties and methods, can be used as base for especialized layers of as simple layer in the pipeline

__init__(name: str | None = None, dynamic_parameters: Dict[str, bool] | None = None, parameters: Dict[str, object] | None = None)[source]

Base initialization for a layer and it’s dynamic and static parameters

Parameters:
  • name (string) – Layer’s name

  • dynamic_parameters (Dict[string, bool]) – Dictionary of mutable parameters

  • static_parameters (Dict[string, object]) – Dictionary of immutable parameters

call(population: ndarray, fitness: ndarray, context: Context) Tuple[ndarray, ndarray][source]
property context: Context

Returns the context object

Retuns:

context (Context): Context object for the next layers

property fitness: ndarray

Returns the fitness array

Retuns:

fitness (np.ndarray): Fitness array

property next: List[Layer]

Return the layer sucessor, so the generator and evolver only need to know the first and last layer of the pipeline.

Returns:

List of immediate successors of the current layer, it is possible that the layers has n sucessors leading to n different pipes.

Return type:

next(List[Layer])

property population: ndarray

Returns the population array

Retuns:

population (np.ndarray): Population array

send_next(population, fitness, context)[source]

Send context, population and fitness to the next layers

Parameters:
  • population (np.ndarray) – Population array

  • fitness (np.ndarray) – Fitness array

  • context (Context) – Context object for the next layers

evolvepy.generator.thread_pool module

class evolvepy.generator.thread_pool.ThreadPool[source]

Bases: object

Manager for threads in a multithreaded generator.

Should not be instantiated, all methods are static.

static add_job(job: Tuple[object, ndarray, ndarray, object])[source]

Add a job to be run.

Parameters:

job (Tuple[Layer, np.ndarray, np.ndarray, Context]) – Info needed to the job be runned.

Raises:

ValueError – If job does not have 4 elements.

end_queue: Queue | None = None
initialized = False
job_queue: Queue | None = None
n_thread: int | None = None
serial = False
thread_pool: List[Thread] = []
static wait_for_end()[source]

Blocks while waits all jobs to be completed

waiting: Set[str] | None = None
evolvepy.generator.thread_pool.thread_function(job_queue: Queue, end_queue: Queue)[source]

Thread function to get and run jobs.

Parameters:
  • job_queue (Queue) – Queue to receive jobs

  • end_queue (Queue) – Queue to mark job as done

Module contents

EvolvePy’s callbacks. Objects that can be called upon during evolution to change its behavior.