Multi-gene genetic programming#

Instead of one tree, a multi-gene individual carries several small trees, the genes \(F_1,\dots,F_m\), and the model is their least-squares combination \(w_0+\sum_i w_iF_i\) (Searson, Leahy and Willis, 2010). Genes evolve by crossover inside genes and by exchanging whole genes between individuals. The data come from a reaction rate with an inhibition term, \(r=2c_Ac_B-0.5c_A^2+0.3\,c_B/(1+c_A)\).

import matplotlib.pyplot as plt
import numpy as np

import eqdisco as eq

rng = np.random.default_rng(3)
cA, cB = rng.uniform(0.1, 2, 300), rng.uniform(0.1, 2, 300)
X = np.column_stack([cA, cB])
r = 2 * cA * cB - 0.5 * cA**2 + 0.3 * cB / (1 + cA) + 0.01 * rng.normal(size=300)

Fitting and the genes#

mg = eq.MultiGeneRegressor(population_size=250, generations=30, max_genes=4, functions=("add", "sub", "mul", "div"), feature_names=["cA", "cB"], random_state=0)
mg.fit(X, r)
print(mg.equation_)
print(mg.gene_table())
fig, axs = plt.subplots(1, 2, figsize=(10, 3.4))
eq.viz.plot_pareto(mg, ax=axs[0], annotate=True, max_labels=4)
t = mg.gene_table()
t = t.rename(columns={"gene": "term"})
eq.viz.plot_terms(t, ax=axs[1])
fig.tight_layout()
plot 12 mggp
0.0006664 - 0.5007 * cA * cA + 0.2987 * cB / (cA / cA + cA) + 2.001 * cB * cA
                  gene  size    weight       err
0              cB * cA     3  2.000601  0.899868
1              cA * cA     3 -0.500652  0.098394
2  cB / (cA / cA + cA)     7  0.298696  0.001697

Parameter study: the maximum number of genes#

fig, ax = plt.subplots(figsize=(4.5, 3))
for k, m in enumerate((1, 2, 4, 6)):
    h = np.median([eq.MultiGeneRegressor(population_size=200, generations=25, max_genes=m, functions=("add", "sub", "mul", "div"), random_state=s).fit(X, r).history_["best_loss"] for s in range(3)], axis=0)
    print(f"at most {m} genes: median final NMSE {h[-1] / np.var(r):.2e}")
    ax.semilogy(h / np.var(r), color=eq.viz.OKABE_ITO[k], label=f"at most {m} genes")
ax.set_xlabel("generation")
ax.set_ylabel("median normalized MSE")
ax.legend(frameon=False, fontsize=8)
plt.show()
plot 12 mggp
at most 1 genes: median final NMSE 2.55e-03
at most 2 genes: median final NMSE 8.61e-05
at most 4 genes: median final NMSE 4.31e-05
at most 6 genes: median final NMSE 4.04e-05

Total running time of the script: (0 minutes 12.292 seconds)

Gallery generated by Sphinx-Gallery