.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_placement/plot_04_method_comparison.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_04_method_comparison.py: Comparing the five placement methods ==================================== :func:`nocad.placement.improve_configuration` runs the five methods of the third NOCAD module for drivers and sensors with the same bound and reports the size, the cost, the maximal and mean relative order and the structural controllability (observability) of every configuration; :func:`nocad.robustness_table` adds the share of single-node failures each one tolerates (MATLAB ``extendData`` and ``robust``). .. GENERATED FROM PYTHON SOURCE LINES 13-15 The NOCAD example with bound 2 ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 15-35 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd import nocad from nocad.datasets import load_nocad_example, make_hen_case_study from nocad.placement import improve_configuration pd.set_option("display.width", 140) adj = load_nocad_example().adj # [snippet:compare] table = improve_configuration(adj, r_max=2, beta=0.5, n_iter=300, n_runs=5, random_state=0) shown = table.assign(nodes=table.nodes.apply(lambda v: " ".join(f"x{i + 1}" for i in v))) print(shown[["kind", "method", "nodes", "cost", "max_order", "mean_order", "structural"]].round(4).to_string(index=False)) configs = {m: (table.query("kind == 'drivers' and method == @m").nodes.iloc[0], table.query("kind == 'sensors' and method == @m").nodes.iloc[0]) for m in table.method.unique()} labels = [f"x{i + 1}" for i in range(9)] print(nocad.robustness_table(adj, configs, labels=labels).round(3).to_string(index=False)) # [/snippet:compare] .. rst-class:: sphx-glr-script-out .. code-block:: none kind method nodes cost max_order mean_order structural drivers set_cover x4 x8 1.5556 2.0 1.1111 True drivers retrofit x4 x8 1.5556 2.0 1.1111 True drivers centrality x4 x8 1.5556 2.0 1.1111 True drivers mclasa x5 x8 1.5556 2.0 1.1111 True drivers gdfcmsa x4 x8 1.5556 2.0 1.1111 True sensors set_cover x2 x7 x9 1.4444 2.0 0.8889 True sensors retrofit x1 x4 x9 1.3889 2.0 0.7778 True sensors centrality x1 x7 x9 1.3889 2.0 0.7778 True sensors mclasa x1 x4 x9 1.3889 2.0 0.7778 True sensors gdfcmsa x1 x7 x9 1.3889 2.0 0.7778 True configuration control_robustness observe_robustness control_critical observe_critical set_cover 0.556 0.444 [x2, x4, x7, x8] [x1, x2, x4, x6, x7] retrofit 0.556 0.667 [x2, x4, x7, x8] [x1, x6, x9] centrality 0.556 0.778 [x2, x4, x7, x8] [x1, x6] mclasa 0.778 0.667 [x2, x7] [x1, x6, x9] gdfcmsa 0.556 0.778 [x2, x4, x7, x8] [x1, x6] .. GENERATED FROM PYTHON SOURCE LINES 36-38 Parameter study: the heat exchanger network ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-57 .. code-block:: Python hen = make_hen_case_study() fig, axes = plt.subplots(1, 2, figsize=(9.6, 3.4), sharey=True) for ax, r in zip(axes, (1, 3), strict=True): t = improve_configuration(hen.adj, r_max=r, n_iter=300, n_runs=4, random_state=0) for k, kind in enumerate(("drivers", "sensors")): sub = t[t.kind == kind] x = np.arange(len(sub)) ax.bar(x + (k - 0.5) * 0.38, sub.n_nodes, width=0.38, color=nocad.plotting.PALETTE[k], label=kind) for xi, (n, ok) in enumerate(zip(sub.n_nodes, sub.structural, strict=True)): if not ok: ax.text(xi + (k - 0.5) * 0.38, n + 0.2, "×", ha="center", fontsize=9, color="0.2") ax.set_xticks(np.arange(len(sub))) ax.set_xticklabels([m.replace("_", "\n") for m in sub.method], fontsize=8) ax.set_title(f"HEN, bound r_max = {r} (× : structure lost)", fontsize=10) ax.grid(True, axis="y", color="0.9") axes[0].set_ylabel("number of nodes") axes[0].legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/06_placement/images/sphx_glr_plot_04_method_comparison_001.png :alt: HEN, bound r_max = 1 (× : structure lost), HEN, bound r_max = 3 (× : structure lost) :srcset: /auto_examples/06_placement/images/sphx_glr_plot_04_method_comparison_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 58-61 **Take-away.** All five methods meet the bound; the annealing methods reach it with the fewest additional nodes while keeping the matching-based nodes, and only grassroot set covering can lose controllability or observability. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.389 seconds) .. _sphx_glr_download_auto_examples_06_placement_plot_04_method_comparison.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_04_method_comparison.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_method_comparison.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_method_comparison.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_method_comparison.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_