Пример #1
0
def benchmark_ask_tell_100k(benchmark, fake_archive_fixture):
    archive, x0 = fake_archive_fixture
    batch_size = 32
    emitter = IsoLineEmitter(archive, x0, batch_size=batch_size)
    n = 100_000

    np.random.seed(0)

    objective_values = np.random.rand(batch_size)
    behavior_values = np.random.rand(batch_size, 2)

    # Let numba compile.
    temp_sol = emitter.ask()
    emitter.tell(temp_sol, objective_values, behavior_values)

    obj_vals = np.random.rand(n, batch_size)
    behavior_vals = np.random.rand(n, batch_size, 2)

    def ask_and_tell():
        for i in range(n):
            solutions = emitter.ask()
            objective_values = obj_vals[i]
            behavior_values = behavior_vals[i]
            emitter.tell(solutions, objective_values, behavior_values)

    benchmark(ask_and_tell)
Пример #2
0
def test_degenerate_gauss_emits_parent(archive_fixture):
    archive, x0 = archive_fixture
    emitter = IsoLineEmitter(archive, x0, iso_sigma=0, batch_size=2)
    archive.add(x0, 1, np.array([0, 0]))

    solutions = emitter.ask()

    assert (solutions == np.expand_dims(x0, axis=0)).all()
Пример #3
0
def test_lower_bounds_enforced(archive_fixture):
    archive, _ = archive_fixture
    emitter = IsoLineEmitter(archive, [-2, -2],
                             iso_sigma=0,
                             line_sigma=0,
                             bounds=[(-1, 1)] * 2)
    sols = emitter.ask()
    assert np.all(sols >= -1)
Пример #4
0
def test_degenerate_gauss_emits_along_line(archive_fixture):
    archive, x0 = archive_fixture
    emitter = IsoLineEmitter(archive, x0, iso_sigma=0, batch_size=100)
    archive.add(np.array([0, 0, 0, 0]), 1, np.array([0, 0]))
    archive.add(np.array([10, 0, 0, 0]), 1, np.array([1, 1]))

    solutions = emitter.ask()

    # All solutions should either come from a degenerate distribution around
    # [0,0,0,0], a degenerate distribution around [10,0,0,0], or the "iso line
    # distribution" between [0,0,0,0] and [10,0,0,0] (i.e. a line between those
    # two points). By having a large batch size, we should be able to cover all
    # cases. In any case, this assertion should hold for all solutions
    # generated.
    assert (solutions[:, 1:] == 0).all()
Пример #5
0
def test_properties_are_correct(archive_fixture):
    archive, x0 = archive_fixture
    iso_sigma = 1
    line_sigma = 2
    emitter = IsoLineEmitter(archive, x0, iso_sigma, line_sigma, batch_size=2)

    assert (emitter.x0 == x0).all()
    assert emitter.iso_sigma == iso_sigma
    assert emitter.line_sigma == line_sigma
Пример #6
0
def _emitter_fixture(request, archive_fixture):
    """Creates an archive, emitter, and initial solution.

    Returns:
        Tuple of (archive, emitter, batch_size, x0).
    """
    emitter_type = request.param
    archive, x0 = archive_fixture
    batch_size = 3

    if emitter_type == "GaussianEmitter":
        emitter = GaussianEmitter(archive, x0, 5, batch_size=batch_size)
    elif emitter_type == "IsoLineEmitter":
        emitter = IsoLineEmitter(archive, x0, batch_size=batch_size)
    elif emitter_type == "ImprovementEmitter":
        emitter = ImprovementEmitter(archive, x0, 5, batch_size=batch_size)
    elif emitter_type == "RandomDirectionEmitter":
        emitter = RandomDirectionEmitter(archive, x0, 5, batch_size=batch_size)
    elif emitter_type == "OptimizingEmitter":
        emitter = OptimizingEmitter(archive, x0, 5, batch_size=batch_size)
    else:
        raise NotImplementedError(f"Unknown emitter type {emitter_type}")

    return archive, emitter, batch_size, x0
Пример #7
0
def create_optimizer(algorithm, dim, seed):
    """Creates an optimizer based on the algorithm name.

    Args:
        algorithm (str): Name of the algorithm passed into sphere_main.
        dim (int): Dimensionality of the sphere function.
        seed (int): Main seed or the various components.
    Returns:
        Optimizer: A ribs Optimizer for running the algorithm.
    """
    max_bound = dim / 2 * 5.12
    bounds = [(-max_bound, max_bound), (-max_bound, max_bound)]
    initial_sol = np.zeros(dim)
    batch_size = 37
    num_emitters = 15

    # Create archive.
    if algorithm in [
            "map_elites", "line_map_elites", "cma_me_imp", "cma_me_imp_mu",
            "cma_me_rd", "cma_me_rd_mu", "cma_me_opt", "cma_me_mixed"
    ]:
        archive = GridArchive((500, 500), bounds, seed=seed)
    elif algorithm in ["cvt_map_elites", "line_cvt_map_elites"]:
        archive = CVTArchive(10_000, bounds, samples=100_000, use_kd_tree=True)
    else:
        raise ValueError(f"Algorithm `{algorithm}` is not recognized")

    # Create emitters. Each emitter needs a different seed, so that they do not
    # all do the same thing.
    emitter_seeds = [None] * num_emitters if seed is None else list(
        range(seed, seed + num_emitters))
    if algorithm in ["map_elites", "cvt_map_elites"]:
        emitters = [
            GaussianEmitter(archive,
                            initial_sol,
                            0.5,
                            batch_size=batch_size,
                            seed=s) for s in emitter_seeds
        ]
    elif algorithm in ["line_map_elites", "line_cvt_map_elites"]:
        emitters = [
            IsoLineEmitter(archive,
                           initial_sol,
                           iso_sigma=0.1,
                           line_sigma=0.2,
                           batch_size=batch_size,
                           seed=s) for s in emitter_seeds
        ]
    elif algorithm in ["cma_me_imp", "cma_me_imp_mu"]:
        selection_rule = "filter" if algorithm == "cma_me_imp" else "mu"
        emitters = [
            ImprovementEmitter(archive,
                               initial_sol,
                               0.5,
                               batch_size=batch_size,
                               selection_rule=selection_rule,
                               seed=s) for s in emitter_seeds
        ]
    elif algorithm in ["cma_me_rd", "cma_me_rd_mu"]:
        selection_rule = "filter" if algorithm == "cma_me_rd" else "mu"
        emitters = [
            RandomDirectionEmitter(archive,
                                   initial_sol,
                                   0.5,
                                   batch_size=batch_size,
                                   selection_rule=selection_rule,
                                   seed=s) for s in emitter_seeds
        ]
    elif algorithm == "cma_me_opt":
        emitters = [
            OptimizingEmitter(archive,
                              initial_sol,
                              0.5,
                              batch_size=batch_size,
                              seed=s) for s in emitter_seeds
        ]
    elif algorithm == "cma_me_mixed":
        emitters = [
            RandomDirectionEmitter(
                archive, initial_sol, 0.5, batch_size=batch_size, seed=s)
            for s in emitter_seeds[:7]
        ] + [
            ImprovementEmitter(
                archive, initial_sol, 0.5, batch_size=batch_size, seed=s)
            for s in emitter_seeds[7:]
        ]

    return Optimizer(archive, emitters)
Пример #8
0
def test_degenerate_gauss_emits_x0(archive_fixture):
    archive, x0 = archive_fixture
    emitter = IsoLineEmitter(archive, x0, iso_sigma=0, batch_size=2)
    solutions = emitter.ask()
    assert (solutions == np.expand_dims(x0, axis=0)).all()