.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_placement/plot_03_annealing.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_06_placement_plot_03_annealing.py: Simulated annealing: mCLASA and GDFCMSA ======================================= Placing :math:`K^+` additional sensors to minimize the cost is a medoid problem on the network (every state belongs to its nearest sensor). Leitold, Vathy-Fogarassy and Abonyi solved it by simulated annealing: *mCLASA* replaces a random additional sensor by a random state, *GDFCMSA* draws the replacement from the fuzzy memberships of the states in the removed sensor, computed with geodesic distances and a fuzziness exponent that falls from 10 to 2 during the run. The nodes required for observability are kept fixed. .. GENERATED FROM PYTHON SOURCE LINES 15-17 Cost against the number of additional sensors --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 17-39 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import nocad from nocad.datasets import make_hen_case_study from nocad.distances import geodesic_distances, placement_cost from nocad.placement import Placement, anneal from nocad.viz import plot_placement_history hen = make_hen_case_study() hist = {} # [snippet:annealing] for method in ("mclasa", "gdfcmsa"): p = Placement(kind="sensors", method=method, r_max=0, n_iter=400, n_runs=5, random_state=0).fit(hen.adj) hist[method] = p.history_ print(method, p.history_[["n_added", "cost", "max_order", "mean_order"]].round(3).to_string(index=False)) # [/snippet:annealing] fig, axes = plt.subplots(1, 2, figsize=(9.6, 3.4)) plot_placement_history(hist, ax=axes[0], value="cost", title="best cost of 5 runs") plot_placement_history(hist, ax=axes[1], value="max_order", title="maximal relative order") fig.tight_layout() .. image-sg:: /auto_examples/06_placement/images/sphx_glr_plot_03_annealing_001.png :alt: best cost of 5 runs, maximal relative order :srcset: /auto_examples/06_placement/images/sphx_glr_plot_03_annealing_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none mclasa n_added cost max_order mean_order 0 4.318 6.0 2.636 1 2.864 4.0 1.727 2 2.182 3.0 1.364 3 2.045 3.0 1.091 4 1.455 2.0 0.909 5 1.386 2.0 0.773 6 1.341 2.0 0.682 7 0.773 1.0 0.545 8 0.750 1.0 0.500 9 0.727 1.0 0.455 10 0.705 1.0 0.409 11 0.682 1.0 0.364 12 0.659 1.0 0.318 13 0.636 1.0 0.273 14 0.614 1.0 0.227 15 0.591 1.0 0.182 16 0.568 1.0 0.136 17 0.545 1.0 0.091 18 0.523 1.0 0.045 19 0.000 0.0 0.000 gdfcmsa n_added cost max_order mean_order 0 4.318 6.0 2.636 1 2.864 4.0 1.727 2 2.182 3.0 1.364 3 2.045 3.0 1.091 4 1.455 2.0 0.909 5 1.386 2.0 0.773 6 1.318 2.0 0.636 7 0.773 1.0 0.545 8 0.750 1.0 0.500 9 0.727 1.0 0.455 10 0.705 1.0 0.409 11 0.682 1.0 0.364 12 0.659 1.0 0.318 13 0.636 1.0 0.273 14 0.614 1.0 0.227 15 0.591 1.0 0.182 16 0.568 1.0 0.136 17 0.545 1.0 0.091 18 0.523 1.0 0.045 19 0.000 0.0 0.000 .. GENERATED FROM PYTHON SOURCE LINES 40-45 Parameter study: iterations and convergence ------------------------------------------- Mean and spread of the final cost of 20 independent runs with :math:`K^+=4` against the number of iterations: the membership-guided moves of GDFCMSA reach good configurations with fewer iterations. .. GENERATED FROM PYTHON SOURCE LINES 45-72 .. code-block:: Python G = nocad.as_adjacency(hen.adj).T D = geodesic_distances(G) D[~np.isfinite(D)] = D.shape[0] fixed = nocad.sensor_nodes(hen.adj) iters = [25, 50, 100, 200, 400] rng = np.random.default_rng(0) fig, ax = plt.subplots(figsize=(5.2, 3.4)) for k, method in enumerate(("mclasa", "gdfcmsa")): mean, lo, hi = [], [], [] for it in iters: c = [placement_cost(D, anneal(D, fixed, 4, method=method, n_iter=it, rng=rng)[0])[0] for _ in range(20)] mean.append(np.mean(c)) lo.append(np.percentile(c, 10)) hi.append(np.percentile(c, 90)) col = nocad.plotting.PALETTE[k] ax.plot(iters, mean, marker="os"[k], color=col, label=method) ax.fill_between(iters, lo, hi, color=col, alpha=0.15) ax.set_xscale("log") ax.set_xticks(iters, [str(i) for i in iters]) ax.minorticks_off() ax.set_xlabel("iterations per run") ax.set_ylabel("final cost ($K^+$ = 4)") ax.set_title("convergence of the annealing methods", fontsize=10) ax.grid(True, color="0.9") ax.legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/06_placement/images/sphx_glr_plot_03_annealing_002.png :alt: convergence of the annealing methods :srcset: /auto_examples/06_placement/images/sphx_glr_plot_03_annealing_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-75 **Take-away.** Both annealing methods reduce the relative order steadily as sensors are added; guiding the moves by fuzzy memberships makes the search converge faster. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.359 seconds) .. _sphx_glr_download_auto_examples_06_placement_plot_03_annealing.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/nocad/main?urlpath=lab/tree/notebooks/auto_examples/06_placement/plot_03_annealing.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_annealing.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_annealing.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_annealing.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_