"""
Beyond images: melodies and slogans
===================================

The evaluator of a melody listens; the evaluator of a slogan reads.
:class:`~easyiec.problems.Melody` evolves 16-step melodies in a chosen scale
(GenJam of Biles, 1994, evolved jazz solos interactively); a gallery shows them
as piano rolls and :meth:`~easyiec.problems.Melody.wav_bytes` renders them as
sound for a notebook player.
:class:`~easyiec.problems.TextDesign` evolves slogans from templates or, with a
language model connected, lets the model propose variations of the chosen
ones — an LLM acting as the *variation operator* while the person (or another
model) judges.
"""

# %%
# Melodies
# --------
import matplotlib.pyplot as plt

import easyiec as ei
from easyiec.problems import Melody, TextDesign
from easyiec.strategies import InteractiveGA

problem = Melody()
user = ei.SimulatedUser(problem, target="random", noise=0.03, seed=3)
res = ei.run(problem, InteractiveGA(pop_size=6), user, generations=10, seed=3)
ei.viz.plot_history(res.log, problem, steps=[0, 9], max_items=6, size=1.3)
audio = problem.wav_bytes(res.best.genome)       # IPython.display.Audio(audio) plays it in a notebook
print(f"{len(audio)} bytes of WAV;", problem.describe(res.best.genome))

# %%
# Slogans from templates
# ----------------------
# Without a language model the variation operator recombines template phrases;
# the simulated reader prefers short, positive slogans that use the keywords.
text = TextDesign(topic="the open day of a university of technology", keywords=("future", "together", "discover"))
reader = ei.SimulatedUser(text, target="default", noise=0.03, seed=1)
res_t = ei.run(text, InteractiveGA(pop_size=6), reader, generations=8, seed=1)
for r in (res_t.log.records[0], res_t.log.records[-1]):
    print([text.describe(it.genome) for it in r.query.items][:3])
ax = ei.viz.plot_anytime(res_t, label="slogans")
ax.set_title("slogan design with a simulated reader", fontsize=9)
plt.show()
