def test_grid_archive_with_boundaries(_grid_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(_grid_archive, pcm_kwargs={ "edgecolor": "black", "linewidth": 0.1 })
def test_heatmap_archive(_grid_archive, _cvt_archive, _sliding_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(_grid_archive) plt.figure(figsize=(8, 6)) cvt_archive_heatmap(_cvt_archive) plt.figure(figsize=(8, 6)) sliding_boundaries_archive_heatmap(_sliding_archive)
def test_cvt_archive_heatmap_with_custom_axis(_grid_archive, _cvt_archive, _sliding_archive): _, ax = plt.subplots(figsize=(8, 6)) grid_archive_heatmap(_grid_archive, ax=ax) _, ax = plt.subplots(figsize=(8, 6)) cvt_archive_heatmap(_cvt_archive, ax=ax) _, ax = plt.subplots(figsize=(8, 6)) sliding_boundaries_archive_heatmap(_sliding_archive, ax=ax)
def test_heatmap_long_square(_long_grid_archive, _long_cvt_archive, _long_sliding_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(_long_grid_archive, square=True) plt.figure(figsize=(8, 6)) cvt_archive_heatmap(_long_cvt_archive, square=True) plt.figure(figsize=(8, 6)) sliding_boundaries_archive_heatmap(_long_sliding_archive, square=True)
def test_heatmap_long_transpose(_long_grid_archive, _long_cvt_archive, _long_sliding_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(_long_grid_archive, transpose_bcs=True) plt.figure(figsize=(8, 6)) cvt_archive_heatmap(_long_cvt_archive, transpose_bcs=True) plt.figure(figsize=(8, 6)) sliding_boundaries_archive_heatmap(_long_sliding_archive, transpose_bcs=True)
def test_heatmap_listed_cmap(_grid_archive, _cvt_archive, _sliding_archive): cmap = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] # Primary red, green, blue. plt.figure(figsize=(8, 6)) grid_archive_heatmap(_grid_archive, cmap=cmap) plt.figure(figsize=(8, 6)) cvt_archive_heatmap(_cvt_archive, cmap=cmap) plt.figure(figsize=(8, 6)) sliding_boundaries_archive_heatmap(_sliding_archive, cmap=cmap)
def test_heatmap_coolwarm_cmap(_grid_archive, _cvt_archive, _sliding_archive): cmap = matplotlib.cm.get_cmap("coolwarm") plt.figure(figsize=(8, 6)) grid_archive_heatmap(_grid_archive, cmap=cmap) plt.figure(figsize=(8, 6)) cvt_archive_heatmap(_cvt_archive, cmap=cmap) plt.figure(figsize=(8, 6)) sliding_boundaries_archive_heatmap(_sliding_archive, cmap=cmap)
def save_heatmap(archive, filename): """Saves a heatmap of the optimizer's archive to the filename. Args: archive (GridArchive): Archive with results from an experiment. filename (str): Path to an image file. """ fig, ax = plt.subplots(figsize=(8, 6)) grid_archive_heatmap(archive, vmin=-300, vmax=300, ax=ax) ax.invert_yaxis() # Makes more sense if larger velocities are on top. ax.set_ylabel("Impact y-velocity") ax.set_xlabel("Impact x-position") fig.savefig(filename)
def test_heatmap_with_limits(_grid_archive, _cvt_archive, _sliding_archive): # Negative sphere function should have range (-2, 0). These limits should # give a more uniform-looking archive. kwargs = {"vmin": -1.0, "vmax": -0.5} plt.figure(figsize=(8, 6)) grid_archive_heatmap(_grid_archive, **kwargs) plt.figure(figsize=(8, 6)) cvt_archive_heatmap(_cvt_archive, **kwargs) plt.figure(figsize=(8, 6)) sliding_boundaries_archive_heatmap(_sliding_archive, **kwargs)
def save_heatmap(archive, heatmap_path): """Saves a heatmap of the archive to the given path. Args: archive (GridArchive or CVTArchive): The archive to save. heatmap_path: Image path for the heatmap. """ if isinstance(archive, GridArchive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(archive, vmin=0, vmax=100) plt.tight_layout() plt.savefig(heatmap_path) elif isinstance(archive, CVTArchive): plt.figure(figsize=(16, 12)) cvt_archive_heatmap(archive, vmin=0, vmax=100) plt.tight_layout() plt.savefig(heatmap_path) plt.clf()
def test_heatmap_long__grid(long_grid_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(long_grid_archive)
def evolve(self, eval_elites=False): if TRAIN_BRUTE: init_states = np.zeros((2**9, 1, 3, 3)) s_idxs = np.arange(2**9) for x in range(3): for y in range(3): s = np.where((s_idxs // (2**(x * 3 + y))) % 2 == 0) init_states[s, 0, x, y] = 1 for i, s in enumerate(init_states): for j, t in enumerate(init_states): if not i == j: assert (s != t).any() n_sims = 2**9 else: n_sims = 12 init_states = np.random.choice((0, 1), (n_sims, 1, MAP_WIDTH, MAP_WIDTH), p=(0.9, 0.1)) self.eval_elites = eval_elites init_nn = self.init_nn optimizer = self.optimizer archive = self.archive seed = self.seed start_time = time.time() total_itrs = 10000 for itr in tqdm.tqdm(range(1, total_itrs + 1)): # Request models from the optimizer. sols = optimizer.ask() # Evaluate the models and record the objectives and BCs. objs, bcs = [], [] bcs = np.zeros((len(sols), 2)) # pool_in = [] for (i, model) in enumerate(sols): m_objs = [] #, m_bcs = [], [] m_bcs = [] init_nn = set_weights(init_nn, model) # for i in range(n_sims): # pool_in.append(self.env, init_nn, model, seed, ) obj, bc = simulate(self.env, init_nn, model, seed, state=init_states) m_objs.append(obj) bcs[i, :] = bc obj = np.mean(m_objs) # bc = np.mean(m_bcs) objs.append(obj) # bcs.append([bc]) # print(np.min(bcs), np.max(bcs)) # print(bcs) optimizer.tell(objs, bcs) df = archive.as_pandas(include_solutions=True) # Logging. if itr % 1 == 0: elapsed_time = time.time() - start_time print(f"> {itr} itrs completed after {elapsed_time:.2f} s") print(f" - Archive Size: {len(df)}") print(f" - Max Score: {df['objective'].max()}") print(f" - Mean Score: {df['objective'].mean()}") del (self.env) self.env = None pickle.dump(self, open(SAVE_PATH, 'wb')) # if df['objective'].max() > - self.epsilon: # print("incrementing number of forward frames") # self.n_forward_frames += 1 # self.eval_elites = True self.restore() plt.figure(figsize=(8, 6)) grid_archive_heatmap(archive, vmin=-300, vmax=300) plt.gca().invert_yaxis( ) # Makes more sense if larger velocities are on top. plt.ylabel("Impact y-velocity") plt.xlabel("Impact x-position")
def test_heatmap_long_transpose__grid(long_grid_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(long_grid_archive, transpose_bcs=True)
def test_heatmap_long_square__grid(long_grid_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(long_grid_archive, square=True)
def test_heatmap_with_limits__grid(grid_archive): # Negative sphere function should have range (-2, 0). These limits should # give a more uniform-looking archive. plt.figure(figsize=(8, 6)) grid_archive_heatmap(grid_archive, vmin=-1.0, vmax=-0.5)
def test_heatmap_listed_cmap__grid(grid_archive): # cmap consists of primary red, green, and blue. plt.figure(figsize=(8, 6)) grid_archive_heatmap(grid_archive, cmap=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
def test_heatmap_coolwarm_cmap__grid(grid_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(grid_archive, cmap="coolwarm")
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 plt.ylabel(bc_names[opt.bcs[1]]) #objective 1 ''' plt.tick_params( axis='y', # changes apply to the x-axis which='both', # both major and minor ticks are affected left=False, # ticks along the bottom edge are off right=False, # ticks along the top edge are off labelleftFalse) # labels along the bottom edge are off ''' figname = '/map_' + str(i) plt.savefig(logdir + figname) plt.close()
def test_heatmap_with_custom_axis__grid(grid_archive): _, ax = plt.subplots(figsize=(8, 6)) grid_archive_heatmap(grid_archive, ax=ax)
def evolve(self, eval_elites=False): # if TRAIN_BRUTE: # init_states = np.zeros((2**9, 1, 3, 3)) # s_idxs = np.arange(2**9) # for x in range(3): # for y in range(3): # s = np.where((s_idxs // (2**(x*3+y))) % 2 == 0) # init_states[s, 0, x, y] = 1 # for i, s in enumerate(init_states): # for j, t in enumerate(init_states): # if not i == j: # assert (s != t).any() # n_sims = 2**9 # else: # n_sims = 12 # init_states = np.random.choice((0, 1), (n_sims, 1, MAP_WIDTH, MAP_WIDTH), p=(0.9, 0.1)) self.eval_elites = eval_elites init_nn = self.init_nn optimizer = self.optimizer archive = self.archive seed = self.seed start_time = time.time() total_itrs = 10000 init_states = np.random.randint(0, 2, (self.n_sims, 64, N_INPUTS)) for itr in tqdm.tqdm(range(1, total_itrs + 1)): # Request models from the optimizer. sols = optimizer.ask() # Evaluate the models and record the objectives and BCs. objs, bcs = [], [] if ME: n_bcs = 2 else: n_bcs = 1 bcs = np.zeros((len(sols), n_bcs)) # pool_in = [] for (i, model) in enumerate(sols): m_objs = [] # , m_bcs = [], [] m_bcs = [] init_nn = set_weights(init_nn, model) for j in range(self.n_sims): obj, bc = simulate(self.env, init_nn, model, seed, state=init_states[j]) m_bcs.append(bc) m_objs.append(obj) bc = (np.mean([b[0] for b in m_bcs]), np.mean([b[1] for b in m_bcs])) bcs[i, :] = bc obj = np.mean(m_objs) objs.append(obj) # bcs.append([bc]) # print(np.min(bcs), np.max(bcs)) # print(bcs) optimizer.tell(objs, bcs) if eval_elites and not archive.empty: # Re-evaluate elites in case we have made some change # prior to reloading which may affect fitness elites = [ archive.get_random_elite() for _ in range(len(archive._solutions)) ] for (model, score, behavior_values) in elites: init_nn = set_weights(init_nn, model) m_objs = [] # , m_bcs = [], [] for j in range(self.n_sims): obj = simulate(self.env, init_nn, model, seed, state=init_states[j]) m_objs.append(obj) obj = np.mean(m_objs) archive.update_elite(behavior_values, obj) # m_objs.append(obj) #bc_a = get_bcs(init_nn) #obj = np.mean(m_objs) # objs.append(obj) # bcs.append([bc_a]) df = archive.as_pandas(include_solutions=False) max_score = df['objective'].max() df = archive.as_pandas(include_solutions=True) # Logging. if itr % 1 == 0: elapsed_time = time.time() - start_time print(f"> {itr} itrs completed after {elapsed_time:.2f} s") print(f" - Archive Size: {len(df)}") print(f" - Max Score: {df['objective'].max()}") print(f" - Mean Score: {df['objective'].mean()}") if itr % 10 == 0: env = self.env self.env = None pickle.dump(self, open(SAVE_PATH, 'wb')) # if df['objective'].max() > - self.epsilon: # print("incrementing number of forward frames") # self.n_forward_frames += 1 # self.eval_elites = True self.env = env self.restore() plt.figure(figsize=(8, 6)) grid_archive_heatmap(archive, vmin=-300, vmax=300) # Makes more sense if larger velocities are on top. plt.gca().invert_yaxis() plt.ylabel("Impact y-velocity") plt.xlabel("Impact x-position")
def test_heatmap_archive__grid(grid_archive): plt.figure(figsize=(8, 6)) grid_archive_heatmap(grid_archive)