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 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 test_add_requires_init(): archive = GridArchive([20, 20], [(-1, 1)] * 2) with pytest.raises(RuntimeError): archive.add(np.array([1, 2, 3]), 1.0, np.array([1.0, 1.0]))