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_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_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_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_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_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 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_with_custom_axis__cvt(cvt_archive): _, ax = plt.subplots(figsize=(8, 6)) cvt_archive_heatmap(cvt_archive, ax=ax)
def test_heatmap_coolwarm_cmap__cvt(cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(cvt_archive, cmap="coolwarm")
def test_heatmap_listed_cmap__cvt(cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(cvt_archive, cmap=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
def test_heatmap_with_limits__cvt(cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(cvt_archive, vmin=-1.0, vmax=-0.5)
def test_heatmap_long_transpose__cvt(long_cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(long_cvt_archive, transpose_bcs=True)
def test_heatmap_long_square__cvt(long_cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(long_cvt_archive, square=True)
def test_heatmap_long__cvt(long_cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(long_cvt_archive)
def sphere_main(algorithm, dim=20, itrs=4500, outdir="sphere_output", log_freq=250, seed=None): """Demo on the Sphere function. Args: algorithm (str): Name of the algorithm. dim (int): Dimensionality of solutions. itrs (int): Iterations to run. outdir (str): Directory to save output. log_freq (int): Number of iterations to wait before recording metrics and saving heatmap. seed (int): Seed for the algorithm. By default, there is no seed. """ outdir = Path(outdir) if not outdir.is_dir(): outdir.mkdir() optimizer = create_optimizer(algorithm, dim, seed) archive = optimizer.archive metrics = { "QD Score": { "x": [0], "y": [0.0], }, "Archive Coverage": { "x": [0], "y": [0.0], }, } non_logging_time = 0.0 with alive_bar(itrs) as progress: for itr in range(1, itrs + 1): itr_start = time.time() sols = optimizer.ask() objs, bcs = sphere(sols) optimizer.tell(objs, bcs) non_logging_time += time.time() - itr_start progress() # Logging and output. final_itr = itr == itrs if itr % log_freq == 0 or final_itr: name = f"{algorithm}_{dim}" data = archive.as_pandas(include_solutions=final_itr) if final_itr: data.to_csv(str(outdir / f"{name}_archive.csv")) # Record and display metrics. total_cells = 10_000 if isinstance(archive, CVTArchive) else 500 * 500 metrics["QD Score"]["x"].append(itr) metrics["QD Score"]["y"].append(data['objective'].sum()) metrics["Archive Coverage"]["x"].append(itr) metrics["Archive Coverage"]["y"].append( len(data) / total_cells * 100) print(f"Iteration {itr} | Archive Coverage: " f"{metrics['Archive Coverage']['y'][-1]:.3f}% " f"QD Score: {metrics['QD Score']['y'][-1]:.3f}") # Generate heatmap. heatmap_path = str(outdir / f"{name}_heatmap_{itr:05d}.png") if isinstance(archive, GridArchive): heatmap_data = np.full(archive.dims, np.nan) for row in data.itertuples(): heatmap_data[row.index_0, row.index_1] = row.objective sns.heatmap(heatmap_data, cmap="magma", vmin=0, vmax=100) plt.savefig(heatmap_path) elif isinstance(archive, CVTArchive): plt.figure(figsize=(16, 12)) cvt_archive_heatmap(archive, vmin=0, vmax=100) plt.savefig(heatmap_path) plt.clf() # Plot metrics. print(f"Algorithm Time (Excludes Logging and Setup): {non_logging_time}s") for metric in metrics: plt.plot(metrics[metric]["x"], metrics[metric]["y"]) plt.title(metric) plt.xlabel("Iteration") plt.savefig( str(outdir / f"{name}_{metric.lower().replace(' ', '_')}.png")) plt.clf() with (outdir / f"{name}_metrics.json").open("w") as file: json.dump(metrics, file, indent=2)
def test_cvt_archive_heatmap_with_samples(_cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(_cvt_archive, plot_samples=True)
def test_heatmap_archive__cvt(cvt_archive): plt.figure(figsize=(8, 6)) cvt_archive_heatmap(cvt_archive)