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
def test_init_fails_on_non_unique_emitter_instances(): archive = GridArchive([100, 100], [(-1, 1), (-1, 1)]) # All emitters are the same instance. This is bad because the same emitter # gets called multiple times. emitters = [GaussianEmitter(archive, [0.0, 0.0], 1, batch_size=1)] * 5 with pytest.raises(ValueError): Optimizer(archive, emitters)
def optimizer_fixture(): """Returns an Optimizer with GridArchive and one GaussianEmitter.""" solution_dim = 2 num_solutions = 4 archive = GridArchive([100, 100], [(-1, 1), (-1, 1)]) emitters = [ GaussianEmitter(archive, [0.0, 0.0], 1, batch_size=num_solutions) ] return Optimizer(archive, emitters), solution_dim, num_solutions
def __init__(self): self.epsilon = 1e-10 self.n_forward_frames = 10 init_nn = NNGoL() set_nograd(init_nn) init_weights = get_init_weights(init_nn) env = GoLImitator(train_brute=TRAIN_BRUTE) if BC == 0: archive = GridArchive( [10, 10], [(-5, 5), (0, 5)], ) elif BC == 1: archive = GridArchive( [200], [(-10, 10)], ) elif BC == 2: archive = GridArchive( # [50, 50], [50, 50], [(0, 1), (0, 1)] # ) elif BC == 3: archive = GridArchive( [50, 50], # [(480, 490), (485, 486)]# [(0, 150)] * 2, ) emitters = [ ImprovementEmitter( # OptimizingEmitter( archive, init_weights, 0.05, batch_size=30, ) for _ in range(5) ] # env = gym.make("GoLImitator-v0") self.seed = 420 action_dim = env.action_space.shape obs_dim = env.observation_space.shape assert action_dim == obs_dim from ribs.optimizers import Optimizer optimizer = Optimizer(archive, emitters) self.optimizer = optimizer self.archive = archive self.init_nn = init_nn self.env = env
def test_init_fails_with_mismatched_emitters(): archive = GridArchive([100, 100], [(-1, 1), (-1, 1)]) emitters = [ # Emits 2D solutions. GaussianEmitter(archive, [0.0, 0.0], 1), # Mismatch -- emits 3D solutions rather than 2D solutions. GaussianEmitter(archive, [0.0, 0.0, 0.0], 1), ] with pytest.raises(ValueError): Optimizer(archive, emitters)
def __init__(self): self.n_sims = 10 self.epsilon = 1e-10 self.n_forward_frames = 10 self.n_inputs = 128 init_nn = NNParity() set_nograd(init_nn) # init_nn.set_trg_weights() # init_nn.perturb_weights(PERTURB) init_weights = get_init_weights(init_nn) env = Parity() if not ME: archive = FlexArchive( [1], [(0, 1)], ) emitters = [ # ImprovementEmitter( OptimizingEmitter( archive, init_weights, 0.05, batch_size=30, ) for _ in range(5) ] else: archive = FlexArchive([100, 100], [(0, 1), (0, 1)]) emitters = [ # ImprovementEmitter( OptimizingEmitter( archive, init_weights, 0.05, batch_size=30, ) for _ in range(5) ] # env = gym.make("Parity-v0") self.seed = 420 from ribs.optimizers import Optimizer optimizer = Optimizer(archive, emitters) self.optimizer = optimizer self.archive = archive self.init_nn = init_nn self.env = env
def create_optimizer(seed, n_emitters, sigma0, batch_size): """Creates the Optimizer based on given configurations. See lunar_lander_main() for description of args. Returns: A pyribs optimizer set up for CMA-ME (i.e. it has ImprovementEmitter's and a GridArchive). """ env = gym.make("LunarLander-v2") action_dim = env.action_space.n obs_dim = env.observation_space.shape[0] archive = GridArchive( [50, 50], # 50 bins in each dimension. [(-1.0, 1.0), (-3.0, 0.0)], # (-1, 1) for x-pos and (-3, 0) for y-vel. seed=seed, ) # If we create the emitters with identical seeds, they will all output the # same initial solutions. The algorithm should still work -- eventually, the # emitters will produce different solutions because they get different # responses when inserting into the archive. However, using different seeds # avoids this problem altogether. seeds = ([None] * n_emitters if seed is None else [seed + i for i in range(n_emitters)]) initial_model = np.zeros((action_dim, obs_dim)) emitters = [ ImprovementEmitter( archive, initial_model.flatten(), sigma0=sigma0, batch_size=batch_size, seed=s, ) for s in seeds ] optimizer = Optimizer(archive, emitters) return optimizer
def test_init_fails_with_no_emitters(): archive = GridArchive([100, 100], [(-1, 1), (-1, 1)]) emitters = [] with pytest.raises(ValueError): Optimizer(archive, emitters)
np.zeros(n_features), 1.0, batch_size=batch_size) for _ in range(1) ] else: emitters = [ OptimizingEmitter(archive, np.zeros(n_features), 1.0, batch_size=batch_size) for _ in range(1) ] for key in obj_names: obj_names[key] = "Optimizing for " + obj_names[key] optimizer = Optimizer(archive, emitters) #get the size of the noise map that TOAD-GAN will need vec_size = 0 n_pad = int(1 * opt.num_layer) for noise_map in noise_maps: nzx = int(round((noise_map.shape[-2] - n_pad * 2) * opt.scale_v)) nzy = int(round((noise_map.shape[-1] - n_pad * 2) * opt.scale_h)) vec_size += 12 * nzx * nzy * opt.num_samples #create a random network that will take a vector from the emitter and generate a larger vector to feed into toad-gan rand_network = create_random_network(n_features, vec_size, opt.device).to(opt.device) rand_network.eval() #the model will not be trained, so we only need to save it once for reproduceability
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)