"""
Verbal steering: "much brighter, slightly less saturated"
=========================================================

People comment while they choose. easyiec turns comments into search
directions: each problem names *semantic axes* (``brighter``, ``more
contrast``, ``warmer`` ...) and linguistic hedges scale the step in the manner
of Zadeh's (1972) hedges as operators on fuzzy sets — *very* and *much*
concentrate (stronger move), *slightly* and *a bit* dilate, *less* and *not*
negate. With a language model connected, free comments outside the vocabulary
are mapped to parameter changes as well.
"""

# %%
# Parsing comments
# ----------------
import matplotlib.pyplot as plt

import easyiec as ei
from easyiec.evaluators import VerbalSteering
from easyiec.evaluators.steering import parse_hedged_phrases
from easyiec.problems import ImageEnhancement
from easyiec.strategies import InteractiveGA

problem = ImageEnhancement()
print(sorted(problem.semantic_axes))
for c in ["much brighter, but slightly less contrast", "a bit warmer and not so saturated", "lift the shadows a lot"]:
    print(f"{c!r:45s} -> {parse_hedged_phrases(c, problem.semantic_axes)}")
vs = VerbalSteering(problem, step=0.12)
d = vs.direction("much brighter, but slightly less contrast")
print({k: round(float(v), 3) for k, v in zip(problem.space.names, d) if v != 0})
# %%
# A user who comments
# -------------------
# The simulated user compares the chosen image with its target and says what
# should change ("much warmer", "lift the shadows a lot"). The same sessions
# with the comments ignored (``steering=False``) are the baseline. With small
# galleries of four images, where each selection carries little information,
# the comments speed up the search noticeably (eight seeds).
from easyiec.validation import curve_frame  # noqa: E402
results, labels = [], []
for seed in range(8):
    for name, steer in [("selections + comments", None), ("selections only", False)]:
        user = ei.SimulatedUser(problem, target="random", noise=0.04, comments=1.0, seed=seed)
        results.append(ei.run(problem, InteractiveGA(pop_size=4), user, generations=10, seed=seed, steering=steer))
        labels.append(name)
print([r.feedback.comment for r in results[0].log.records if r.feedback.comment][:6])
ei.viz.plot_convergence(curve_frame(results, labels=labels), title="photo enhancement, galleries of four")

# %%
# What the commenting user saw
# ----------------------------
ei.viz.plot_history(results[0].log, problem, steps=[0, 3, 6, 9], max_items=4, size=1.1)
plt.show()
