ribs.archives.DNSArchive

class ribs.archives.DNSArchive(*, solution_dim: int | integer | tuple[int | integer, ...], measure_dim: int | integer, capacity: int | integer, k_neighbors: int | integer, qd_score_offset: float | floating = 0.0, seed: int | integer | None = None, solution_dtype: numpy.typing.DTypeLike = None, objective_dtype: numpy.typing.DTypeLike = None, measures_dtype: numpy.typing.DTypeLike = None, dtype: numpy.typing.DTypeLike = None, extra_fields: dict[str, tuple[int | integer | tuple[int | integer, ...], DTypeLike]] | None = None, kdtree_kwargs: dict | None = None)[source]

An archive that maintains a fixed-size population via Dominated Novelty Search.

Each generation, candidates are merged with the current population, and survivors are selected by their Dominated Novelty Search (DNS) score: for each solution, the DNS score is the mean distance in the chosen space to the k nearest neighbors with strictly higher objective (“fitter” neighbors). If no fitter neighbors exist, the DNS score is treated as +inf.

More info can be found in the DNS paper by Bahlous-Boldi, R, and Faldor, M et al.

By default, this archive stores the following data fields: solution, objective, measures, and index.

Parameters:
solution_dim: int | integer | tuple[int | integer, ...]

Dimensionality of the solution space. Scalar or multi-dimensional solution shapes are allowed by passing an empty tuple or tuple of integers, respectively.

measure_dim: int | integer

Dimensionality of the measure space.

capacity: int | integer

Fixed population size to maintain.

k_neighbors: int | integer

Number of fitter neighbors to average over when computing DNS.

qd_score_offset: float | floating = 0.0

Subtracted from objective values when computing QD score.

seed: int | integer | None = None

Value to seed the random number generator.

solution_dtype: numpy.typing.DTypeLike = None

Data type of the solutions. Defaults to float64 (numpy’s default floating point type).

objective_dtype: numpy.typing.DTypeLike = None

Data type of the objectives. Defaults to float64 (numpy’s default floating point type).

measures_dtype: numpy.typing.DTypeLike = None

Data type of the measures. Defaults to float64 (numpy’s default floating point type).

dtype: numpy.typing.DTypeLike = None

Shortcut for providing data type of the solutions, objectives, and measures. Defaults to float64 (numpy’s default floating point type). This parameter sets all the dtypes simultaneously. To set individual dtypes, pass solution_dtype, objective_dtype, and measures_dtype. Note that dtype cannot be used at the same time as those parameters.

extra_fields: dict[str, tuple[int | integer | tuple[int | integer, ...], DTypeLike]] | None = None

Extra fields to store alongside solutions.

kdtree_kwargs: dict | None = None

Kwargs for scipy.spatial.KDTree used in retrieval.

Methods

__iter__()

Creates an iterator over the elites in the archive.

__len__()

Number of elites in the archive.

add(solution, objective, measures, **fields)

Inserts a batch of solutions with DNS-based survival selection.

add_single(solution, objective, measures, ...)

Inserts a single solution into the archive.

clear()

Removes all elites in the archive.

compute_dns(measures, objectives)

Computes DNS scores for a current population (evaluation) with respect to itself.

data()

Returns data of the elites in the archive.

index_of(measures)

Returns the index of the closest solution to the given measures.

index_of_single(measures)

Returns the index of the measures for one solution.

retrieve(measures)

Queries the archive for elites with the given batch of measures.

retrieve_single(measures)

Queries the archive for an elite with the given measures.

sample_elites(n[, replace])

Randomly samples elites from the archive.

Attributes

best_elite

The elite with the highest objective in the archive.

capacity

Fixed number of solutions stored in this archive.

cells

Total capacity of the archive (for coverage/statistics).

dtypes

Mapping from field name to dtype for all fields in the archive.

empty

Whether the archive is empty.

field_list

List of data fields in the archive.

k_neighbors

The number of fitter neighbors for computing DNS.

measure_dim

Dimensionality of the measure space.

objective_dim

Dimensionality of the objective space.

qd_score_offset

Subtracted from objective values when computing the QD score.

solution_dim

Dimensionality of the solution space.

stats

Statistics about the archive.

add(solution: ArrayLike, objective: ArrayLike | None, measures: ArrayLike, **fields: ArrayLike) BatchData[source]

Inserts a batch of solutions with DNS-based survival selection.

The current population and the incoming batch are merged, DNS scores are computed over the union, and the top capacity solutions by DNS are kept.

add_single(solution: ArrayLike, objective: ArrayLike | None, measures: ArrayLike, **fields: ArrayLike) SingleData[source]

Inserts a single solution into the archive.

Parameters:
solution: ArrayLike

Parameters of the solution.

objective: ArrayLike | None

Set to None to get the default value of 0; otherwise, a valid objective value is also acceptable.

measures: ArrayLike

Coordinates in measure space of the solution.

**fields: ArrayLike

Additional data for the solution.

Returns:

Information describing the result of the add operation. The dict contains status and dns keys; refer to add() for the meaning of status and dns.

Raises:
  • ValueError – The array arguments do not match their specified shapes.

  • ValueErrorobjective is non-finite (inf or NaN) or measures has non-finite values.

  • ValueErrorlocal_competition is turned on but objective was not passed in.

clear() None[source]

Removes all elites in the archive.

compute_dns(measures: numpy.typing.ArrayLike, objectives: numpy.typing.ArrayLike) ndarray[source]

Computes DNS scores for a current population (evaluation) with respect to itself.

data(fields: str, return_type: 'dict' | 'tuple' | 'pandas' = 'dict') ndarray[source]
data(fields: None | Collection[str] = None, return_type: 'dict' = 'dict') dict[str, ndarray]
data(fields: None | Collection[str] = None, return_type: 'tuple' = 'tuple') tuple[ndarray]
data(fields: None | Collection[str] = None, return_type: 'pandas' = 'pandas') ArchiveDataFrame

Returns data of the elites in the archive.

Parameters:
fields: str
fields: None | Collection[str] = None

List of fields to include, such as "solution", "objective", "measures", and other fields in the archive. This can also be a single str indicating a field name.

return_type: 'dict' | 'tuple' | 'pandas' = 'dict'
return_type: 'dict' = 'dict'
return_type: 'tuple' = 'tuple'
return_type: 'pandas' = 'pandas'

Data to return; see below. Ignored if fields is a str.

Returns:

The data for all elites in the archive. All data returned by this method will be a copy, i.e., the data will not update as the archive changes. If fields was a single str, the returned data will just be an array holding data for the given field, such as:

measures = archive.data("measures")

Otherwise, the returned data can take the following forms, depending on the return_type argument:

  • return_type="dict": Dict mapping from the field name to the field data at the given indices. An example is:

    {
      "solution": [[1.0, 1.0, ...], ...],
      "objective": [1.5, ...],
      "measures": [[1.0, 2.0], ...],
      ...
    }
    

    The keys in this dict can be modified with the fields arg; duplicate fields will be ignored since the dict stores unique keys.

  • return_type="tuple": Tuple of arrays matching the field order in fields. For instance, if fields is ["objective", "measures"], this method would return a tuple of (objective_arr, measures_arr) that could be unpacked as:

    objective, measures = archive.data(["objective", "measures"],
                                       return_type="tuple")
    

    Unlike with the dict return type, duplicate fields will show up as duplicate entries in the tuple, e.g., fields=["objective", "objective"] will result in two objective arrays being returned.

    When fields=None (the default case), the fields in the tuple will be ordered according to the field_list.

  • return_type="pandas": An ArchiveDataFrame with the following columns:

    • For fields that are scalars, a single column with the field name. For example, objective would have a single column called objective.

    • For fields that are 1D arrays, multiple columns with the name suffixed by its index. To illustrate, for a measures field of length 10, the dataframe would contain 10 columns with names measures_0, measures_1, …, measures_9. The output format for fields with >1D data is currently not defined.

    In short, the dataframe might look like this by default:

    solution_0

    objective

    measures_0

    Like the other return types, the columns returned can be adjusted with the fields parameter.

Raises:
  • ValueError – Invalid field name provided.

  • ValueError – Invalid return_type provided.

  • ValueError – Passed return_type="pandas" when one of the fields has >1D data.

index_of(measures: numpy.typing.ArrayLike) ndarray[source]

Returns the index of the closest solution to the given measures.

Unlike the structured archives like GridArchive, this archive does not have indexed cells where each measure “belongs.” Thus, this method instead returns the index of the solution with the closest measure to each solution passed in.

This means that retrieve() will return the solution with the closest measure to each measure passed into that method.

Parameters:
measures: numpy.typing.ArrayLike

(batch_size, measure_dim) array of coordinates in measure space.

Returns:

(batch_size,) array of integer indices representing the location of the solution in the archive.

Raises:
index_of_single(measures: numpy.typing.ArrayLike) int | integer[source]

Returns the index of the measures for one solution.

See index_of().

Parameters:
measures: numpy.typing.ArrayLike

(measure_dim,) array of measures for a single solution.

Returns:

Integer index of the measures in the archive’s storage arrays.

Raises:
retrieve(measures: numpy.typing.ArrayLike) tuple[ndarray, dict[str, ndarray]][source]

Queries the archive for elites with the given batch of measures.

This method operates in batch. It takes in a batch of measures and outputs the batched data for the elites:

occupied, elites = archive.retrieve(...)
occupied  # Shape: (batch_size,)
elites["solution"]  # Shape: (batch_size, solution_dim)
elites["objective"]  # Shape: (batch_size, objective_dim)
elites["measures"]  # Shape: (batch_size, measure_dim)
...

occupied indicates whether an elite was found for each measure, i.e., whether the archive was occupied at each queried measure. If occupied[i] is True, then elites["solution"][i], elites["objective"][i], elites["measures"][i], and other fields will contain the data of the elite for the input measures[i]. If occupied[i] is False, then those fields will instead have arbitrary values, e.g., elites["solution"][i] may be set to all NaN.

Parameters:
measures: numpy.typing.ArrayLike

(batch_size, measure_dim) array of measure space points at which to retrieve solutions.

Returns:

2-element tuple of (boolean occupied array, dict of elite data). See above for description.

Raises:
retrieve_single(measures: numpy.typing.ArrayLike) tuple[bool, dict[str, Any]][source]

Queries the archive for an elite with the given measures.

While retrieve() takes in a batch of measures, this method takes in the measures for only one solution and returns a single bool and a dict with single entries:

occupied, elite = archive.retrieve_single(...)
occupied  # Bool
elite["solution"]  # Shape: (solution_dim,)
elite["objective"]  # Shape: (objective_dim,)
elite["measures"]  # Shape: (measure_dim,)
...
Parameters:
measures: numpy.typing.ArrayLike

(measure_dim,) array of measures.

Returns:

2-element tuple of (boolean, dict of data for one elite)

Raises:
sample_elites(n: int | integer, replace: bool = True) dict[str, ndarray][source]

Randomly samples elites from the archive.

Currently, this sampling is done uniformly at random, either with or without replacement. Additional sampling methods may be supported in the future.

Example

elites = archive.sample_elites(16)
elites["solution"]  # Shape: (16, solution_dim)
elites["objective"]
elites["measures"]
...
Parameters:
n: int | integer

Number of elites to sample.

replace: bool = True

Whether to replace the elites when sampling. If True, the elites will be replaced and thus will be sampled independently.

Returns:

A batch of elites randomly selected from the archive.

Raises:
  • IndexError – The archive is empty.

  • ValueErrorn was greater than the number of elites in the archive when replace=False.

property best_elite : dict[str, Any] | None

The elite with the highest objective in the archive.

None if there are no elites in the archive.

property capacity : int

Fixed number of solutions stored in this archive.

property cells : int

Total capacity of the archive (for coverage/statistics).

property dtypes : dict[str, dtype]

Mapping from field name to dtype for all fields in the archive.

property empty : bool

Whether the archive is empty.

property field_list : list[str]

List of data fields in the archive.

property k_neighbors : int

The number of fitter neighbors for computing DNS.

property measure_dim : int | integer

Dimensionality of the measure space.

property objective_dim : tuple[()] | int | integer

Dimensionality of the objective space.

The empty tuple () indicates a scalar objective.

property qd_score_offset : float

Subtracted from objective values when computing the QD score.

property solution_dim : int | integer | tuple[int | integer, ...]

Dimensionality of the solution space.

property stats : ArchiveStats

Statistics about the archive.

See ArchiveStats for more info.