icaros-usc / pyribs

A bare-bones Python library for quality diversity optimization.
https://pyribs.org
MIT License
210 stars 35 forks source link

Add `sigma`, `sigma0` to `gaussian_emitter` and `iso_line_emitter` #199

Closed itsdawei closed 2 years ago

itsdawei commented 2 years ago

Description

Allow user to configure different standard deviations (sigma, sigma0) for initialization and sampling steps of gaussian_emitter and iso_line_emitter.

TODO / DONE

Status

itsdawei commented 2 years ago

Visualizing using the new sigma0 parameter.

from ribs.archives import GridArchive
from ribs.emitters import GaussianEmitter, IsoLineEmitter
from ribs.optimizers import Optimizer
import numpy as np
import matplotlib.pyplot as plt
from ribs.visualize import grid_archive_heatmap

archive = GridArchive(
    dims=[20, 20],
    ranges=[(-1, 1), (-1, 1)],
)

# emitters = [
#     GaussianEmitter(
#         archive,
#         x0=[0.0] * 10,
#         sigma=1,
#         sigma0=0.1,  # should generate a small circle of solutions around (0,0)
#     )
# ]

emitters = [
    IsoLineEmitter(
        archive,
        x0=[0.0] * 10,
        iso_sigma=1,
        sigma0=0.1,  # should generate a small circle of solutions around (0,0)
    )
]

optimizer = Optimizer(archive, emitters)

for itr in range(1000):
    solutions = optimizer.ask()

    # Optimize the 10D negative Sphere function.
    objectives = -np.sum(np.square(solutions), axis=1)

    # BCs: first 2 coordinates of each 10D solution.
    bcs = solutions[:, :2]
    optimizer.tell(objectives, bcs)

    # plot every 100 iteration
    if itr % 100 == 0:
        plt.figure()
        grid_archive_heatmap(archive)
        plt.savefig(f"img/plot{itr}.png", dpi=300)
        plt.close()

GaussianEmitter (sigma0=0)

plot0

GaussianEmitter (sigma0=0.1)

plot0

IsoLineEmitter (sigma0=0)

plot0

IsoLineEmitter (sigma0=0.1)

plot0