saealib.Population¶
- class saealib.Population(attrs, init_capacity=100)[source]¶
Bases:
Generic[T_Individual]Container for population data.
- Parameters:
attrs (list[PopulationAttribute])
init_capacity (int)
- 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
Initialize a Population. |
|
Append a new individual to the population. |
|
Get the indices that would sort the population by a specific attribute. |
|
Clear the population. |
|
Delete individuals from the population. |
|
Create an empty Population with the same schema. |
|
Extend this population with another population. |
|
Extract individuals with indices, and return new Population. |
|
Get the array of a specific attribute. |
|
Get the array of a specific attribute. |
|
Get a cached value. |
|
Return a read only view of the specified key. |
|
Public method to call when the structure changes. |
|
Public method to call when the value changes. |
|
Reorder individuals in the population. |
|
Set a cache value. |
|
Cut the population to a new size. |
|
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.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
Noneif the key is not found.- Parameters:
key (str) – The key of the cache.
- Returns:
The cached value, or
Noneif 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.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_valueormod_structure).- Parameters:
key (str) – The key of the cache.
value (Any) – The value to be cached.
- Return type:
None