Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Parameter optimization inside the search#
Constants inside nonlinear functions – the 0.8 in \(e^{-0.8x_0}\) – are hard to evolve by mutation. Modern symbolic regression systems tune them by gradient methods during the search (Kommenda et al., 2020; Burlacu et al., 2020). eqdisco applies Levenberg–Marquardt with exact forward-mode Jacobians to the best share of the offspring and writes the tuned values back into the genome (Lamarckian learning). The target is \(y=2.5\,e^{-0.8x_0}\sin x_1\).
import matplotlib.pyplot as plt
import numpy as np
import eqdisco as eq
rng = np.random.default_rng(1)
X = rng.uniform(-2, 2, (200, 2))
y = 2.5 * np.exp(-0.8 * X[:, 0]) * np.sin(X[:, 1])
funcs = ("add", "sub", "mul", "div", "exp", "sin")
With and without Levenberg–Marquardt#
runs = {}
for opt in ("none", "lm"):
runs[opt] = [
eq.SymbolicRegressor(population_size=200, generations=20, optimize=opt, optimize_fraction=0.1, optimize_iterations=10, functions=funcs, random_state=s).fit(X, y)
for s in range(5)
]
for opt, rr in runs.items():
print(opt, [str(r.equation_) for r in rr[:2]])
for opt in ("none", "lm"):
print(f"{opt:4s} median final NMSE {np.median([r.history_['best_loss'].iloc[-1] for r in runs[opt]]) / np.var(y):.2e} "
f"median evaluations {np.median([r.history_['n_evaluations'].iloc[-1] for r in runs[opt]]):.0f}")
fig, axs = plt.subplots(1, 2, figsize=(10, 3.4))
for opt, color in (("none", "#D55E00"), ("lm", "#285780")):
for r in runs[opt]:
h = r.history_
axs[0].semilogy(h["n_evaluations"], h["best_loss"] / np.var(y), color=color, lw=0.8, alpha=0.6)
axs[0].plot([], [], color=color, label="no tuning" if opt == "none" else "Levenberg-Marquardt")
eq.viz.plot_pareto(runs[opt][0], ax=axs[1], annotate=False, color=color, label=opt)
axs[0].set_xlabel("fitness evaluations")
axs[0].set_ylabel("normalized MSE of the best")
axs[0].legend(frameon=False, fontsize=8)
fig.tight_layout()

none ['0.03115 + 0.07203 * sin(x1) / exp(-3.266 + x0)', '0.005319 + 0.586 * (sin(x1) / exp(x0 - 0.981) + x1 / exp(exp(x0 - 0.237)) + sin(sin(x1)))']
lm ['-0.05235 + 0.03944 * (x1 + sin(x1) / (exp(-4.312 + exp(-7.193e+13 + x0)) + exp(-3.734 + x0)) + sin(x1) / (exp(-12.53 + x0) + exp(-3.536 + x0)) + exp(-4.817 + x0) + x0 - x0 + exp(sin(x1)))', '-68.73 + 0.611 * (sin(x1) / exp(exp(x0)) + sin(sin(x1)) / exp(x0 - 1.105) + sin(x1) + sin(x1 * 0.01234) + 112.5)']
none median final NMSE 6.03e-03 median evaluations 3262
lm median final NMSE 2.49e-05 median evaluations 3319
