saealib.Archive#

class saealib.Archive(attrs, init_capacity=100, key_attr='x', atol=<object object>, rtol=<object object>, duplicate_policy='keep_first', genomes=None, dense_numeric_view=None, dense_view=None, services=None, space=None, fingerprint_service=None, equivalence_service=None, distance_service=None, **kwargs)[source]#

Bases: ArchiveMixin, Population

Concrete archive combining ArchiveMixin with Population.

Its constructor is inherited from ArchiveMixin.

Parameters:
  • attrs (list[PopulationAttribute])

  • init_capacity (int)

  • key_attr (str)

  • atol (float | object)

  • rtol (float | object)

  • duplicate_policy (str)

  • genomes (GenomeBatch | None)

  • dense_numeric_view (DenseNumericView | None)

  • dense_view (DenseNumericView | None)

  • services (Any | None)

  • space (Any | None)

  • fingerprint_service (FingerprintService | None)

  • equivalence_service (EquivalenceService | None)

  • distance_service (DistanceService | None)

Methods

__init__

Initialize archive storage and duplicate-resolution services.

add

Add a solution according to the configured duplicate policy.

append

Append one candidate.

argsort

Return row indices that order a named column.

clear

Clear rows and invalidate identity/neighbor caches.

delete

Delete candidates selected by index in place.

empty_like

Create an empty archive while retaining resolved identity services.

extend

Extend the archive with another candidate collection.

extract

Return a new population containing rows selected by indices.

get

Get the array of a specific attribute.

get_array

Return a read-only view of the active rows in a named column.

get_cache

Return a cached value without changing population state.

get_knn

Get k-nearest neighbors of the given solution from the archive.

mod_structure

Mark population structure as changed and invalidate cached results.

mod_value

Invalidate the kNN cache on every value-only mutation, too.

reorder

Reorder rows according to a complete row permutation.

set_cache

Store a value in the population cache without changing its version.

truncate

Keep the first new_size rows in place.

update_array

Update array in place and bump the value version.

update_rows

Atomically update multiple columns for the given rows.

Method Details

Archive.__init__(attrs, init_capacity=100, key_attr='x', atol=<object object>, rtol=<object object>, duplicate_policy='keep_first', genomes=None, dense_numeric_view=None, dense_view=None, services=None, space=None, fingerprint_service=None, equivalence_service=None, distance_service=None, **kwargs)#

Initialize archive storage and duplicate-resolution services.

Parameters:
  • attrs (list of PopulationAttribute) – Column definitions for archive rows. With duplicate_policy="append", the schema must contain scalar int64 columns named id and request_id.

  • init_capacity (int, optional) – Initial row-storage capacity passed to Population.

  • key_attr (str, optional) – Schema column used by compatibility duplicate detection. In a service-backed archive, "x" may also provide the dense genome used to resolve an incoming candidate.

  • atol (float, optional) – Explicit absolute-tolerance override. When omitted, the archive uses the equivalence service’s tolerance when applicable, or zero for exact matching.

  • rtol (float, optional) – Explicit relative-tolerance override, with the same resolution rules as atol.

  • duplicate_policy ({"keep_first", "replace", "append"}, optional) – Action for a duplicate candidate. "keep_first" leaves the existing row unchanged, "replace" updates the existing row, and "append" retains every observation while requiring the observation schema described for attrs.

  • genomes (GenomeBatch or None, optional) – Initial opaque genome batch passed to Population.

  • dense_numeric_view (DenseNumericView or None, optional) – Service used when archive operations need dense coordinates for a non-dense genome representation.

  • dense_view (DenseNumericView or None, optional) – Compatibility alias for dense_numeric_view.

  • services (object or None, optional) – Service provider or registry used when space is not supplied. Explicit service arguments take precedence over services resolved from this provider.

  • space (object or None, optional) – Search-space or service provider. When supplied, it takes precedence over services and its services registry is queried for omitted service arguments.

  • fingerprint_service (FingerprintService or None, optional) – Explicit service for exact identity lookup of opaque genomes.

  • equivalence_service (EquivalenceService or None, optional) – Explicit service for tolerance-based identity lookup of opaque genomes.

  • distance_service (DistanceService or None, optional) – Explicit service for nearest-neighbor queries on opaque genomes.

  • **kwargs (Any) – Additional constructor keywords accepted for population compatibility.

Raises:
  • ValueError – If duplicate_policy is unknown or a compatibility key_attr is absent from the schema.

  • ValidationError – If an append archive has an invalid observation schema, or if service-backed duplicate detection lacks its required identity service.

Archive.add(element=None, **kwargs)#

Add a solution according to the configured duplicate policy.

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.

  • self (Any)

Returns:

idx – Index of the retained or newly added row.

Return type:

int

Notes

"keep_first" returns the existing row for a duplicate, "replace" updates or replaces that row, and "append" retains every observation subject to its ID and request-ID constraints.

Examples

>>> arcv.add(ind)
>>> arcv.add({"x": x_val})
>>> arcv.add(x=x_val, f=0.1)
>>> arcv.add(ind, f=0.1)
Archive.append(element=None, **kwargs)#

Append one candidate.

Parameters:
  • element (Individual-like object, dict, or None) – Candidate values read from an individual or mapping. Values passed through kwargs take precedence over values from element.

  • **kwargs (Any) – Column values to assign. genome or genomes supplies the single genome required by a non-dense genome representation.

Raises:

ValidationError – If an explicit candidate ID other than the unassigned -1 sentinel is supplied, or if a custom genome representation does not receive exactly one genome.

Return type:

None

Notes

The population grows its storage as needed. Appending invalidates cached values and structure-dependent views.

Archive.argsort(name, reverse=False)#

Return row indices that order a named column.

Parameters:
  • name (str) – Name of a column in the population schema.

  • reverse (bool, optional) – If true, return the indices in descending rather than ascending column order.

Returns:

Indices for the active rows in sorted order. The population is not mutated.

Return type:

ndarray of int

Raises:

KeyError – If name is not present in the population schema.

Archive.clear()#

Clear rows and invalidate identity/neighbor caches.

Return type:

None

Archive.delete(index)#

Delete candidates selected by index in place.

Parameters:

index (int, slice, list of int, or ndarray) – Row index or indices to remove from the active archive.

Notes

Surviving rows retain their relative order. The operation invalidates the archive’s identity and nearest-neighbor caches.

Archive.empty_like(capacity=None)#

Create an empty archive while retaining resolved identity services.

Parameters:
  • capacity (int or None, optional) – Storage capacity for the new archive. If omitted, the current capacity is reused.

  • self (Any)

Returns:

An empty archive of the same concrete type and schema with independent row storage and the same identity services.

Return type:

Archive

Archive.extend(other)#

Extend the archive with another candidate collection.

Parameters:

other (Population or dict) – Source population or column-data mapping accepted by Population.extend().

Return type:

None

Notes

The operation invalidates the archive’s identity and nearest-neighbor caches after copying the source rows.

Archive.extract(indices)#

Return a new population containing rows selected by indices.

Parameters:

indices (ndarray, list of int, or slice) – Indices into the active rows. Rows appear in the requested order; a slice follows normal Python slicing semantics.

Returns:

A new population of the same concrete type, schema, and genome representation. Row and genome data have independent storage, and the source population is unchanged.

Return type:

Population

Archive.get(key, default=None)#

Get the array of a specific attribute.

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

  • default (any, optional) – Returned when the key is absent. Defaults to None.

Returns:

The attribute array if key exists, otherwise default.

Return type:

np.ndarray or default

Archive.get_array(key)#

Return a read-only view of the active rows in a named column.

Parameters:

key (str) – Column name. For "x", the value is obtained through the configured DenseNumericView unless this is a legacy scalar-x population.

Returns:

A non-writeable view covering the active rows of the column. The returned array for stored columns shares backing storage rather than being an owned copy; "x" uses the service-provided view.

Return type:

np.ndarray

Raises:
  • KeyError – If key is not a stored non-genome column.

  • AttributeError – If key is "x" and the required DenseNumericView is unavailable or reports that it cannot provide a view.

Notes

The population is not modified by this method. Mutating the returned view raises ValueError; use update_array() or update_rows() so value versions and caches remain synchronized.

Archive.get_cache(key)#

Return a cached value without changing population state.

Parameters:

key (Hashable) – Cache key to look up.

Returns:

The cached value, or None when no value is stored for key. A cached None is therefore indistinguishable from a missing key through this method.

Return type:

Any or None

Raises:

TypeError – If key is not hashable.

Notes

Cached values are removed when the population is modified through mod_value() or mod_structure().

Archive.get_knn(x, k)#

Get k-nearest neighbors of the given solution from the archive.

Parameters:
  • x (np.ndarray) – The solution to find neighbors for.

  • k (int) – The number of neighbors to retrieve.

Returns:

Indices and distances of the k-nearest neighbors.

Return type:

tuple[np.ndarray, np.ndarray]

Archive.mod_structure()#

Mark population structure as changed and invalidate cached results.

Return type:

None

Archive.mod_value()#

Invalidate the kNN cache on every value-only mutation, too.

Return type:

None

Archive.reorder(order)#

Reorder rows according to a complete row permutation.

Parameters:

order (ndarray of int) – Permutation containing one row index for every active candidate.

Raises:

ValueError – If order does not contain one entry per active row.

Return type:

None

Notes

Reordering is in place and invalidates caches whose values are row-indexed.

Archive.set_cache(key, value)#

Store a value in the population cache without changing its version.

Parameters:
  • key (Hashable) – Cache key. An existing value for the key is replaced.

  • value (Any) – Value to retain until the population is modified.

Raises:

TypeError – If key is not hashable.

Return type:

None

Notes

The cache is cleared by mod_value() and mod_structure(). Storing a value replaces only the entry for key; it does not clear other cached entries or bump the population’s value or structure version.

Archive.truncate(new_size)#

Keep the first new_size rows in place.

Parameters:

new_size (int) – Number of rows to retain.

Raises:

ValueError – If new_size is negative.

Return type:

None

Notes

Truncation invalidates the archive’s identity and nearest-neighbor caches.

Archive.update_array(key, value)#

Update array in place and bump the value version.

Parameters:
  • key (str)

  • value (Any)

Return type:

None

Archive.update_rows(indices, values, genome=None)#

Atomically update multiple columns for the given rows.

Validates every index and value before mutating any backing storage, so a validation failure never leaves a partial update applied. Bumps the value version exactly once for a non-empty update; an update with empty indices or an empty values mapping is a no-op and does NOT bump the version.

Parameters:
  • indices (np.ndarray | list[int]) – Row indices to update. Must be 1-D, integer dtype, within [0, len(self)), and free of duplicates.

  • values (dict[str, np.ndarray]) – Mapping of column name to the new values for those rows. Each value array must match shape (len(indices), *schema[key].shape) and dtype schema[key].dtype exactly.

  • genome (GenomeBatch | ndarray | None)

Raises:

ValidationError – If indices is invalid (wrong dtype/ndim, out of range, or contains duplicates), if a key in values is unknown (or is "id"), or if a value array’s shape/dtype does not match the schema exactly.

Return type:

None