{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(user_guide_optimization_ga_tuning)=" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Selection Strategies and Population Diversity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This page continues the {doc}`bga` walkthrough, again minimizing the Rosenbrock function, and looks at two knobs for tuning convergence behaviour: the selection strategy and the diagnostic `diversity` metric." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from typing import Iterable\n", "from numbers import Number\n", "import matplotlib.pyplot as plt\n", "\n", "from sigmaepsilon.math.optimize import BinaryGeneticAlgorithm as BGA\n", "\n", "\n", "def Rosenbrock(x: Iterable[Number], a: Number = 1, b: Number = 100) -> float:\n", " return (a - x[0]) ** 2 + b * (x[1] - x[0] ** 2) ** 2\n", "\n", "\n", "ranges = [[-2, 2], [-1, 3]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selection strategies" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The winners of each generation (beyond the automatically-surviving elite) are picked by a pluggable {py:class}`~sigmaepsilon.math.optimize.selection.SelectionStrategy`, injected via the `selection_strategy` constructor argument. Three are built in: {py:class}`~sigmaepsilon.math.optimize.selection.TournamentSelection` (the default), {py:class}`~sigmaepsilon.math.optimize.selection.RouletteSelection` (fitness-proportionate) and {py:class}`~sigmaepsilon.math.optimize.selection.RankSelection`. \ud83d\udcd6 {ref}`Selection strategies API Reference `." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "from sigmaepsilon.math.optimize import TournamentSelection, RouletteSelection, RankSelection\n", "\n", "for strategy in [TournamentSelection(), RouletteSelection(), RankSelection()]:\n", " bga = BGA(\n", " Rosenbrock,\n", " ranges,\n", " length=12,\n", " nPop=100,\n", " minimize=True,\n", " seed=0,\n", " selection_strategy=strategy,\n", " )\n", " result = bga.solve()\n", " print(f\"{type(strategy).__name__:<20} fitness={result.fitness:.6g}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Population diversity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `diversity` property (and the `state.diversity` snapshot updated after every `evolve` call) measures how spread out the current population still is (the mean, per-dimension standard deviation of the phenotypes). A value close to zero, alongside a champion that hasn't improved in a while, is a sign of premature convergence \u2014 in addition to `champion.age`, it's a second, independent signal you can use to tune `maxage`, `p_m`, or the selection strategy." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "bga = BGA(Rosenbrock, ranges, length=12, nPop=100, minimize=True, seed=0)\n", "\n", "fitness_history = []\n", "diversity_history = []\n", "\n", "for _ in range(60):\n", " bga.evolve(1)\n", " fitness_history.append(bga.champion.fitness)\n", " diversity_history.append(bga.state.diversity)\n", "\n", "fig, ax1 = plt.subplots(figsize=(8, 4))\n", "ax1.plot(fitness_history, color=\"tab:blue\")\n", "ax1.set_xlabel(\"generation\")\n", "ax1.set_ylabel(\"best fitness\", color=\"tab:blue\")\n", "ax2 = ax1.twinx()\n", "ax2.plot(diversity_history, color=\"tab:orange\")\n", "ax2.set_ylabel(\"diversity\", color=\"tab:orange\")\n", "plt.title(\"Best fitness and population diversity over generations\")\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "sigmaepsilon-math-NMVzm02N-py3.12", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }