def test_tell_inserts_solutions_with_multiple_emitters(tell_metadata): archive = GridArchive([100, 100], [(-1, 1), (-1, 1)]) emitters = [ GaussianEmitter(archive, [0.0, 0.0], 1, batch_size=1), GaussianEmitter(archive, [0.5, 0.5], 1, batch_size=2), GaussianEmitter(archive, [-0.5, -0.5], 1, batch_size=3), ] optimizer = Optimizer(archive, emitters) _ = optimizer.ask() behavior_values = [[1.0, 1.0], [-1.0, 1.0], [-1.0, -1.0], [1.0, -1.0], [0.0, 0.0], [0.0, 1.0]] metadata = [f"metadata_{i}" for i in range(6)] if tell_metadata else None expected_metadata = metadata if tell_metadata else [None] * 6 # The sum of all the emitters' batch sizes is 6. optimizer.tell( objective_values=np.ones(6), behavior_values=behavior_values, metadata=metadata, ) # Note: This assumes data() returns entries in order of insertion, but this # may change in the future. all_sols, all_objs, all_behs, _, all_meta = optimizer.archive.data() assert len(all_sols) == 6 assert (behavior_values == all_behs).all() assert (all_objs == np.ones(6)).all() assert (expected_metadata == all_meta).all()
def test_from_config_with_valid_input(use_toml, tmp_path): seed = 42 batch_size = 4 archive = GridArchive([64, 64], [(-1, 1), (-1, 1)], seed=seed) emitters = [ GaussianEmitter(archive, [0.0, 0.0], 0.1, batch_size=batch_size, seed=seed) ] optimizer = Optimizer(archive, emitters) config_dict = { "archive": { "type": "GridArchive", "dims": [64, 64], "ranges": [(-1, 1), (-1, 1)], "seed": seed, }, "emitters": [{ "type": "GaussianEmitter", "x0": [0.0, 0.0], "sigma0": 0.1, "batch_size": batch_size, "seed": seed, }], "optimizer": { "type": "Optimizer", }, } if use_toml: config_path = tmp_path / "config.toml" with config_path.open("w") as file: toml.dump(config_dict, file) created_optimizer = ribs.factory.from_config(config_path) else: created_optimizer = ribs.factory.from_config(config_dict) # Check types. assert isinstance(created_optimizer, Optimizer) assert isinstance(created_optimizer.archive, GridArchive) assert len(created_optimizer.emitters) == 1 assert isinstance(created_optimizer.emitters[0], GaussianEmitter) # Check results from ask() and tell() -- since seeds are the same, all # results should be the same. optimizer_sols = optimizer.ask() created_optimizer_sols = created_optimizer.ask() assert len(optimizer_sols) == batch_size assert (optimizer_sols == created_optimizer_sols).all() objective_values = [0.0] * batch_size behavior_values = np.array([[1, 1], [-1, 1], [-1, -1], [1, -1]]) optimizer.tell(objective_values, behavior_values) created_optimizer.tell(objective_values, behavior_values) assert (optimizer.archive.as_pandas() == created_optimizer.archive.as_pandas()).all(None)
def test_tell_inserts_solutions_with_multiple_emitters(): archive = GridArchive([100, 100], [(-1, 1), (-1, 1)]) emitters = [ GaussianEmitter(archive, [0.0, 0.0], 1, batch_size=1), GaussianEmitter(archive, [0.5, 0.5], 1, batch_size=2), GaussianEmitter(archive, [-0.5, -0.5], 1, batch_size=3), ] optimizer = Optimizer(archive, emitters) _ = optimizer.ask() # The sum of all the emitters' batch sizes is 6. optimizer.tell( objective_values=[1.0] * 6, behavior_values=[[1.0, 1.0], [-1.0, 1.0], [-1.0, -1.0], [1.0, -1.0], [0.0, 0.0], [0.0, 1.0]], ) assert len(optimizer.archive.as_pandas()) == 6
num_samples=opt.num_samples) for solution in solutions ] bc0 = 0 bc1 = 0 end_states = [] for result in results: bcs.append([result[1], result[2]]) objectives.append(result[0]) bc0 += result[1] bc1 += result[2] end_states.append(result[3]) optimizer.tell(objectives, bcs) score_obj = max(objectives) bc0 = bc0 / float(len(objectives)) bc1 = bc1 / float(len(objectives)) tb_logging(archive, i, start_time, logdir, score_obj, bc0, bc1, end_states) if i % 100 == 0: #generate a heatmap plt.figure(figsize=(8, 6)) #plt.figure(figsize=(8,2)) grid_archive_heatmap(archive) plt.title(obj_names[opt.obj]) plt.xlabel(bc_names[opt.bcs[0]]) #objective 0