.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_gpols/plot_12_mggp.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_03_gpols_plot_12_mggp.py: Multi-gene genetic programming ============================== Instead of one tree, a multi-gene individual carries several small trees, the genes :math:`F_1,\dots,F_m`, and the model is their least-squares combination :math:`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, :math:`r=2c_Ac_B-0.5c_A^2+0.3\,c_B/(1+c_A)`. .. GENERATED FROM PYTHON SOURCE LINES 12-23 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Fitting and the genes --------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-37 .. code-block:: Python 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() .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_12_mggp_001.png :alt: plot 12 mggp :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_12_mggp_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 38-40 Parameter study: the maximum number of genes -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-49 .. code-block:: Python 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() .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_12_mggp_002.png :alt: plot 12 mggp :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_12_mggp_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.292 seconds) .. _sphx_glr_download_auto_examples_03_gpols_plot_12_mggp.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/eqdisco/main?urlpath=lab/tree/notebooks/auto_examples/03_gpols/plot_12_mggp.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_12_mggp.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_12_mggp.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_12_mggp.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_