.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_03_search_space.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_01_basics_plot_03_search_space.py: The size of the search space and the meaning of complexity ========================================================== Why is equation discovery hard? The number of distinct expressions grows exponentially with their size. :func:`~eqdisco.models.enumerate_expressions` counts the canonical expressions -- with commutative arguments ordered and constant-only subtrees merged -- for two variables and growing function sets. The second part compares the complexity measures of :func:`eqdisco.fitness.complexity` on a few textbook formulas: node count, weighted node count, and the recursive measure of Kommenda et al. (2015), which charges nested nonlinear functions much more than additions. .. GENERATED FROM PYTHON SOURCE LINES 14-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq from eqdisco.fitness import complexity from eqdisco.models import enumerate_expressions .. GENERATED FROM PYTHON SOURCE LINES 23-25 Counting expressions -------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-39 .. code-block:: Python sets = {"+ x": ("add", "mul"), "+ x -": ("add", "sub", "mul", "div"), "+ x - / exp log": ("add", "sub", "mul", "div", "exp", "log")} counts = {} for label, funcs in sets.items(): pset = eq.PrimitiveSet(2, funcs) sizes = [e.size for e in enumerate_expressions(pset, max_size=7, max_params=3)] counts[label] = np.bincount(sizes)[1:] print(label, counts[label]) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) for k, (label, c) in enumerate(counts.items()): axs[0].semilogy(np.arange(1, len(c) + 1), np.maximum(c, 1), "o-", color=eq.viz.OKABE_ITO[k], label=label) axs[0].set_xlabel("expression size (nodes)") axs[0].set_ylabel("canonical expressions") axs[0].legend(frameon=False, fontsize=8, title="function set", title_fontsize=8) .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_03_search_space_001.png :alt: plot 03 search space :srcset: /auto_examples/01_basics/images/sphx_glr_plot_03_search_space_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none + x [ 3 0 10 0 60 0 470] + x - [ 3 0 22 0 396 0 8558] + x - / exp log [ 3 4 22 116 584 3496 19958] .. GENERATED FROM PYTHON SOURCE LINES 40-42 Complexity measures ------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-54 .. code-block:: Python formulas = ["x0 + x1", "x0*x1", "x0^2 + x1^2", "sqrt(x0^2 + x1^2)", "exp(-x0)*x1", "sin(x0*x1)", "exp(sin(x0*x1))"] kinds = ["size", "weighted", "recursive"] table = np.array([[complexity(eq.Expression.from_string(f), k) for k in kinds] for f in formulas]) w = 0.27 for j, k in enumerate(kinds): axs[1].barh(np.arange(len(formulas)) + (j - 1) * w, table[:, j] / table[:, j].max(), height=w, color=eq.viz.OKABE_ITO[j], label=k) axs[1].set_yticks(range(len(formulas))) axs[1].set_yticklabels(formulas, fontsize=8) axs[1].set_xlabel("complexity (relative to the largest)") axs[1].legend(frameon=False, fontsize=8) fig.tight_layout() .. GENERATED FROM PYTHON SOURCE LINES 55-59 Parameter study: the maximum number of parameters ------------------------------------------------- Every constant leaf is a free parameter. Limiting their number prunes the space considerably. .. GENERATED FROM PYTHON SOURCE LINES 59-67 .. code-block:: Python pset = eq.PrimitiveSet(2, ("add", "sub", "mul", "div")) n_by_p = [len(enumerate_expressions(pset, 7, max_params=p)) for p in range(4)] print("expressions up to size 7 with at most p parameters, p = 0..3:", n_by_p) fig, ax = plt.subplots(figsize=(3.8, 2.8)) ax.bar(range(4), n_by_p, color="#285780") ax.set_xlabel("maximum number of parameters") ax.set_ylabel("expressions up to size 7") plt.show() .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_03_search_space_002.png :alt: plot 03 search space :srcset: /auto_examples/01_basics/images/sphx_glr_plot_03_search_space_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none expressions up to size 7 with at most p parameters, p = 0..3: [1862, 5967, 8547, 8979] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.753 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_03_search_space.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/01_basics/plot_03_search_space.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_search_space.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_search_space.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_search_space.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_