def three_d_grid_archive(): """Deterministic archive, but there are three behavior axes of different sizes, and some of the axes are not totally filled.""" archive = GridArchive([10, 10, 10], [(-2, 2), (-1, 1), (-2, 1)], seed=42) archive.initialize(solution_dim=3) add_uniform_3d_sphere(archive, (0, 2), (-1, 1), (-1, 0)) return archive
def _grid_archive(): """Deterministically created GridArchive.""" # The archive must be low-res enough that we can tell if the number of cells # is correct, yet high-res enough that we can see different colors. archive = GridArchive([10, 10], [(-1, 1), (-1, 1)], seed=42) archive.initialize(solution_dim=2) _add_uniform_sphere(archive, (-1, 1), (-1, 1)) return archive
def setup(): archive = GridArchive((64, 64), [(-1, 1), (-1, 1)]) archive.initialize(solutions.shape[1]) # Let numba compile. archive.add(solutions[0], objective_values[0], behavior_values[0]) return (archive, ), {}
def benchmark_get_10k_random_elites(benchmark, benchmark_data_10k): n, solutions, objective_values, behavior_values = benchmark_data_10k archive = GridArchive((64, 64), [(-1, 1), (-1, 1)]) archive.initialize(solutions.shape[1]) for i in range(n): archive.add(solutions[i], objective_values[i], behavior_values[i]) @benchmark def get_elites(): for i in range(n): sol, obj, beh = archive.get_random_elite()
def test_dtypes(emitter_class, dtype): archive = GridArchive([20, 20], [(-1.0, 1.0)] * 2, dtype=dtype) archive.initialize(10) emitter = emitter_class(archive, np.zeros(10), 1.0) assert emitter.x0.dtype == dtype # Try running with the negative sphere function for a few iterations. for _ in range(10): sols = emitter.ask() objs = -np.sum(np.square(sols), axis=1) bcs = sols[:, :2] emitter.tell(sols, objs, bcs)
def benchmark_as_pandas_2025_items(benchmark): dim = 45 archive = GridArchive((dim, dim), [(-1, 1), (-1, 1)]) archive.initialize(10) for x in np.linspace(-1, 1, dim): for y in np.linspace(-1, 1, dim): sol = np.random.random(10) sol[0] = x sol[1] = y archive.add(sol, 1.0, np.array([x, y])) # Archive should be full. assert len(archive.as_pandas()) == dim * dim benchmark(archive.as_pandas)
def get_archive_data(name, dtype=np.float64): """Returns ArchiveFixtureData to use for testing each archive. The archives vary, but there will always be an empty 2D archive, as well as a 2D archive with a single solution added to it. This solution will have a value of [1, 2, 3], its objective value will be 1.0, and its behavior values will be [0.25, 0.25]. The name is the name of an archive to create. It should come from ARCHIVE_NAMES. """ # Characteristics of a single solution to insert into archive_with_entry. solution = np.array([1, 2, 3]) objective_value = 1.0 behavior_values = np.array([0.25, 0.25]) grid_indices = None centroid = None if name == "GridArchive": # Grid archive with 10 bins and range (-1, 1) in first dim, and 20 bins # and range (-2, 2) in second dim. bins = 10 * 20 archive = GridArchive([10, 20], [(-1, 1), (-2, 2)], dtype=dtype) archive.initialize(len(solution)) archive_with_entry = GridArchive([10, 20], [(-1, 1), (-2, 2)], dtype=dtype) archive_with_entry.initialize(len(solution)) grid_indices = (6, 11) elif name.startswith("CVTArchive-"): # CVT archive with bounds (-1,1) and (-1,1), and 4 centroids at (0.5, # 0.5), (-0.5, 0.5), (-0.5, -0.5), and (0.5, -0.5). The entry in # archive_with_entry should match with centroid (0.5, 0.5). bins = 4 kd_tree = name == "CVTArchive-kd_tree" samples = [[0.5, 0.5], [-0.5, 0.5], [-0.5, -0.5], [0.5, -0.5]] centroid = [0.5, 0.5] archive = CVTArchive(4, [(-1, 1), (-1, 1)], samples=samples, use_kd_tree=kd_tree, dtype=dtype) archive.initialize(len(solution)) archive_with_entry = CVTArchive(4, [(-1, 1), (-1, 1)], samples=samples, use_kd_tree=kd_tree, dtype=dtype) archive_with_entry.initialize(len(solution)) elif name == "SlidingBoundariesArchive": # Sliding boundary archive with 10 bins and range (-1, 1) in first dim, # and 20 bins and range (-2, 2) in second dim. bins = 10 * 20 archive = SlidingBoundariesArchive([10, 20], [(-1, 1), (-2, 2)], remap_frequency=100, buffer_capacity=1000, dtype=dtype) archive.initialize(len(solution)) archive_with_entry = SlidingBoundariesArchive([10, 20], [(-1, 1), (-2, 2)], remap_frequency=100, buffer_capacity=1000, dtype=dtype) archive_with_entry.initialize(len(solution)) grid_indices = (6, 11) archive_with_entry.add(solution, objective_value, behavior_values) return ArchiveFixtureData( archive, archive_with_entry, solution, objective_value, behavior_values, grid_indices, centroid, bins, )
def _long_grid_archive(): """Same as above, but the behavior space is longer in one direction.""" archive = GridArchive([10, 10], [(-2, 2), (-1, 1)], seed=42) archive.initialize(solution_dim=2) _add_uniform_sphere(archive, (-2, 2), (-1, 1)) return archive
def archive_fixture(): """Provides a simple archive and initial solution.""" archive = GridArchive([10, 10], [(-1, 1), (-1, 1)]) x0 = np.array([1, 2, 3, 4]) archive.initialize(len(x0)) return archive, x0