"""
The Blind Watchmaker and novelty-filtered galleries
===================================================

Dawkins' *Blind Watchmaker* (1986) showed one parent surrounded by its mutants;
the user picks one, which becomes the next parent. The scheme is fast and
intuitive but greedy, and galleries soon look alike. NoveltyIEC breeds a larger
pool and shows the candidates that are both promising and far from what the
user has already seen (novelty search of Lehman and Stanley, 2011), keeping the
gallery diverse.
"""

# %%
# Biomorphs bred by a simulated user
# ----------------------------------
import matplotlib.pyplot as plt

import easyiec as ei
from easyiec.problems import Biomorph
from easyiec.strategies import InteractiveGA, NoveltyIEC, Watchmaker

problem = Biomorph()
user = ei.SimulatedUser(problem, target="random", noise=0.03, seed=6)
res_w = ei.run(problem, Watchmaker(pop_size=9), user, generations=15, seed=6)
ei.viz.plot_history(res_w.log, problem, steps=[0, 4, 9, 14], size=0.9)

# %%
# Gallery diversity: GA, Watchmaker and NoveltyIEC
# ------------------------------------------------
# Mean pairwise distance of the shown genomes per query, three strategies, the
# same user. Novelty filtering keeps the galleries varied without slowing the
# progress towards the target.
fig, axes = plt.subplots(1, 2, figsize=(10, 3.4))
for k, (name, strat) in enumerate({"Watchmaker": Watchmaker(pop_size=9), "GA": InteractiveGA(pop_size=9),
                                   "NoveltyIEC": NoveltyIEC(pop_size=9)}.items()):
    user = ei.SimulatedUser(problem, target="random", noise=0.03, seed=6)
    res = ei.run(problem, strat, user, generations=15, seed=6)
    d = ei.validation.diversity(res.log, problem)
    axes[0].plot(d, lw=2, label=name, color=["#285780", "#D55E00", "#009E73"][k])
    ei.viz.plot_anytime(res, ax=axes[1], label=name, color=["#285780", "#D55E00", "#009E73"][k], show_shown=False)
axes[0].set_xlabel("query", fontsize=8)
axes[0].set_ylabel("genome diversity", fontsize=8)
axes[0].legend(frameon=False, fontsize=8)
axes[1].legend(frameon=False, fontsize=8)
plt.show()
