.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_gp/plot_09_diversity.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_02_gp_plot_09_diversity.py: Diversity: semantic crossover and islands ========================================= A population that converges to copies of one structure stops exploring. Two remedies are compared on Vladislavleva-7. Semantic similarity-based crossover (Uy et al., 2011) swaps only subtrees that behave similarly but not identically on the data, so that offspring differ from their parents in small semantic steps. The island model splits the population into sub-populations that exchange their best individuals periodically, which keeps several lines of search alive. .. GENERATED FROM PYTHON SOURCE LINES 12-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq prob = eq.datasets.get_problem("vladislavleva-7") variants = { "subtree crossover": dict(), "semantic crossover": dict(crossover="semantic"), "4 islands": dict(n_islands=4, migration_interval=5, migration_size=2), } .. GENERATED FROM PYTHON SOURCE LINES 26-28 Diversity and progress ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-45 .. code-block:: Python results = {name: [eq.SymbolicRegressor(population_size=240, generations=25, functions=("add", "sub", "mul", "sin"), random_state=s, **kw).fit(prob.X_train, prob.y_train) for s in range(4)] for name, kw in variants.items()} for name, rr in results.items(): print(f"{name:20s} mean share of distinct structures {np.mean([r.history_['unique'].mean() for r in rr]):.3f} " f"median final NMSE {np.median([r.history_['best_loss'].iloc[-1] for r in rr]) / np.var(prob.y_train):.2e}") fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) for k, (name, rr) in enumerate(results.items()): U = np.array([r.history_["unique"] for r in rr]) L = np.array([r.history_["best_loss"] for r in rr]) / np.var(prob.y_train) axs[0].plot(U.mean(0), color=eq.viz.OKABE_ITO[k], lw=2, label=name) axs[1].semilogy(np.median(L, 0), color=eq.viz.OKABE_ITO[k], lw=2, label=name) axs[0].set_xlabel("generation") axs[0].set_ylabel("share of distinct structures") axs[1].set_xlabel("generation") axs[1].set_ylabel("median normalized MSE") axs[0].legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/02_gp/images/sphx_glr_plot_09_diversity_001.png :alt: plot 09 diversity :srcset: /auto_examples/02_gp/images/sphx_glr_plot_09_diversity_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none subtree crossover mean share of distinct structures 0.956 median final NMSE 1.80e-01 semantic crossover mean share of distinct structures 0.909 median final NMSE 1.65e-01 4 islands mean share of distinct structures 0.917 median final NMSE 1.45e-01 .. GENERATED FROM PYTHON SOURCE LINES 46-48 Parameter study: migration interval ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-59 .. code-block:: Python intervals = [2, 5, 10, 25] med = [] for mi in intervals: med.append(np.median([eq.SymbolicRegressor(population_size=240, generations=25, n_islands=4, migration_interval=mi, functions=("add", "sub", "mul", "sin"), random_state=s).fit(prob.X_train, prob.y_train).history_["best_loss"].iloc[-1] for s in range(3)])) print("migration interval", intervals, "median final NMSE", [f"{m / np.var(prob.y_train):.2e}" for m in med]) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.plot(intervals, np.array(med) / np.var(prob.y_train), "o-", color="#285780") ax.set_xlabel("migration interval (generations)") ax.set_ylabel("median final normalized MSE") ax.set_yscale("log") plt.show() .. image-sg:: /auto_examples/02_gp/images/sphx_glr_plot_09_diversity_002.png :alt: plot 09 diversity :srcset: /auto_examples/02_gp/images/sphx_glr_plot_09_diversity_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none migration interval [2, 5, 10, 25] median final NMSE ['1.51e-01', '1.37e-01', '1.59e-01', '1.60e-01'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 26.056 seconds) .. _sphx_glr_download_auto_examples_02_gp_plot_09_diversity.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/02_gp/plot_09_diversity.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_09_diversity.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_09_diversity.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_09_diversity.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_