"""
A one-page report of a search
=============================

:func:`~eqdisco.viz.plot_dashboard` summarizes a fitted search on one page: the
selected equation, its tree, the Pareto front on test data, a parity plot and
the convergence of the run. Together with the model card it documents a model
for colleagues who did not run the search. The data are Feynman I.12.2,
Coulomb's law :math:`F=q_1q_2/(4\\pi\\varepsilon r^2)`, with 0.5 % noise.
"""

import matplotlib.pyplot as plt

import eqdisco as eq

prob = eq.datasets.get_problem("feynman-I.12.2", noise=0.005, n_train=300)

# %%
# Search and report
# -----------------
sr = eq.SymbolicRegressor(population_size=400, generations=30, functions=("add", "sub", "mul", "div", "sqrt", "square"), optimize="lm", feature_names=prob.feature_names, random_state=0)
sr.fit(prob.X_train, prob.y_train)
print("selected:  ", sr.equation_)
print("simplified:", eq.simplify.simplify(sr.equation_, prob.X_train, prob.y_train, sympy=True, refit=True, round_digits=True))
print("law recovered (affine, tol 1e-2):", eq.benchmarks.is_symbolic_solution(sr.equation_, prob.truth, X=prob.X_test, affine=True, tol=1e-2))
fig = eq.viz.plot_dashboard(sr, prob.X_train, prob.y_train, prob.X_test, prob.y_test)
# %%
# Parameter study: population size and recovery
# ---------------------------------------------
sizes = [100, 200, 400]
hits = []
for mu in sizes:
    h = 0
    for s in range(3):
        m = eq.SymbolicRegressor(population_size=mu, generations=20, functions=("add", "sub", "mul", "div", "sqrt", "square"), optimize="lm", feature_names=prob.feature_names, random_state=s).fit(prob.X_train, prob.y_train)
        h += eq.benchmarks.is_symbolic_solution(m.equation_, prob.truth, X=prob.X_test, affine=True, tol=1e-2)
    hits.append(h)
print("population", sizes, "recoveries of 3", hits)
fig, ax = plt.subplots(figsize=(4, 3))
ax.bar([str(s) for s in sizes], hits, color="#285780")
ax.set_xlabel("population size")
ax.set_ylabel("recoveries (of 3 runs)")
plt.show()
