"""
Search spaces and the problem zoo
=================================

An IEC problem has three parts: a *search space* (a :class:`~easyiec.Box` of
real, integer, log-scaled and categorical genes), a *rendering* that turns a
genome into something a person can judge, and optionally a ground-truth
*utility* that simulated users and benchmarks use. easyiec ships 25 problems
from engineering, image processing, generative art, design, music and text.
"""

# %%
# A search space
# --------------
# Strategies work in the unit cube; the box maps to and from the physical units.
import matplotlib.pyplot as plt
import numpy as np

import easyiec as ei
from easyiec.problems import list_problems, make
space = ei.Box(
    lower=[0.1, 1.0, 0, 0],
    upper=[10.0, 100.0, 5, 3],
    names=["gain", "time_constant", "filter_order", "mode"],
    kinds=["real", "real", "int", "cat"],
    log=[True, True, False, False],
    categories={3: ["P", "PI", "PID"]},
    units=["", "s", "", ""],
)
x = space.denormalize(np.array([0.5, 0.5, 0.5, 0.9]))
print(space.describe(x))        # gain and time constant at the geometric mean
# %%
# The problem zoo
# ---------------
# :func:`~easyiec.problems.list_problems` lists the registry; every problem
# renders a random genome in the same way, so a gallery can show any of them.
table = list_problems()
print(table[["name", "domain", "genes", "modality"]].to_string(index=False))

names = list(table["name"])
rng = np.random.default_rng(7)
ncols = 6
nrows = int(np.ceil(len(names) / ncols))
fig, axes = plt.subplots(nrows, ncols, figsize=(1.55 * ncols, 1.75 * nrows))
for ax, name in zip(axes.flat, names):
    p = make(name)
    x = p.presets().get("default", p.random(rng))
    ax.imshow(p.to_image(x, 160))
    ax.set_title(p.name, fontsize=7)
    ax.axis("off")
for ax in list(axes.flat)[len(names):]:
    ax.axis("off")
fig.tight_layout()
plt.show()
