saealib.Population

class saealib.Population(attrs, init_capacity=100)[source]

Bases: Generic[T_Individual]

Container for population data.

Parameters:
schema

Schema defining the attributes of the population.

Type:

Dict[str, PopulationAttribute]

_data

Dictionary to store population data arrays.

Type:

Dict[str, np.ndarray]

_cache

Dictionary to store cached values (ex: nds_rank). Cleared on every value or structure modification.

Type:

Dict[str, Any]

_capacity

Current capacity of the population.

Type:

int

_size

Current size of the population.

Type:

int

_structure_version

Version number to track structure modifications.

Type:

int

_value_version

Version number to track value modifications.

Type:

int

Methods

__init__

Initialize a Population.

append

Append a new individual to the population.

argsort

Get the indices that would sort the population by a specific attribute.

clear

Clear the population.

delete

Delete individuals from the population.

empty_like

Create an empty Population with the same schema.

extend

Extend this population with another population.

extract

Extract individuals with indices, and return new Population.

get

Get the array of a specific attribute.

get_array

Get the array of a specific attribute.

get_cache

Get a cached value.

get_readonly_array

Return a read only view of the specified key.

mod_structure

Public method to call when the structure changes.

mod_value

Public method to call when the value changes.

reorder

Reorder individuals in the population.

set_cache

Set a cache value.

truncate

Cut the population to a new size.

update_array

Update array in place and bump the value version.

Method Details

Population.__init__(attrs, init_capacity=100)[source]

Initialize a Population.

Parameters:
  • attrs (List[PopulationAttribute]) – List of population attributes. Each attribute defines a column in the population.

  • init_capacity (int, optional) – Initial capacity of the population, by default 100.

Return type:

None

Population.append(element=None, **kwargs)[source]

Append a new individual to the population.

Parameters:
  • element (Individual | dict | None) – Data for the additional individual

  • **kwargs – Set attribute values individually and add them. Alternatively, overwrite based on the element’s value and add it.

Return type:

None

Examples

>>> pop.append(ind)
>>> pop.append({"x": x_val})
>>> pop.append(x=x_val, f=0.1)
>>> pop.append(ind, f=0.1)
Population.argsort(name, reverse=False)[source]

Get the indices that would sort the population by a specific attribute.

Parameters:
  • name (str) – The attribute name to sort by.

  • reverse (bool, optional) – Whether to sort in descending order, by default False.

Return type:

ndarray

Population.clear()[source]

Clear the population.

Return type:

None

Population.delete(index)[source]

Delete individuals from the population.

Parameters:

index (int, slice, list[int], np.ndarray) – The index or indices of individuals to delete.

Return type:

None

Population.empty_like(capacity=None)[source]

Create an empty Population with the same schema.

Parameters:

capacity (int) – Initial capacity of the new Population. Defaults to self._capacity.

Population.extend(other)[source]

Extend this population with another population.

Parameters:

other (Population | dict) – The other population to extend from.

Return type:

None

Population.extract(indices)[source]

Extract individuals with indices, and return new Population.

Parameters:

indices (np.ndarray | List[int] | slice) – Indices to extract

Return type:

Self

Population.get(key, default=None)[source]

Get the array of a specific attribute.

Parameters:
  • key (str) – The attribute name to get the array for.

  • default (Any) – Returned when the key is absent.

Return type:

ndarray

Population.get_array(key)[source]

Get the array of a specific attribute.

Parameters:

key (str) – The attribute name to get the array for.

Return type:

ndarray

Population.get_cache(key)[source]

Get a cached value.

Returns None if the key is not found.

Parameters:

key (str) – The key of the cache.

Returns:

The cached value, or None if not found.

Return type:

Any | None

Population.get_readonly_array(key)[source]

Return a read only view of the specified key.

Parameters:

key (str)

Return type:

ndarray

Population.mod_structure()[source]

Public method to call when the structure changes.

Return type:

None

Population.mod_value()[source]

Public method to call when the value changes.

Return type:

None

Population.reorder(order)[source]

Reorder individuals in the population.

Parameters:

order (np.ndarray) – The new order of individuals.

Return type:

None

Population.set_cache(key, value)[source]

Set a cache value.

The cache is automatically cleared when the population is modified (via mod_value or mod_structure).

Parameters:
  • key (str) – The key of the cache.

  • value (Any) – The value to be cached.

Return type:

None

Population.truncate(new_size)[source]

Cut the population to a new size.

Parameters:

new_size (int) – The new size of the population.

Return type:

None

Population.update_array(key, value)[source]

Update array in place and bump the value version.

Parameters:
  • key (str)

  • value (Any)

Return type:

ndarray