.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/scalable/plot_diagonal.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_scalable_plot_diagonal.py: Scalable diagonal discipline ============================ Let us consider the :class:`~gemseo.problems.sobieski.wrappers.SobieskiAerodynamics` discipline. We want to build its :class:`.ScalableDiscipline` counterpart, using a :class:`.ScalableDiagonalModel` For that, we can use a 20-length :class:`.DiagonalDOE` and test different sizes of variables or different settings for the scalable diagonal discipline. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: default from __future__ import absolute_import, division, unicode_literals from future import standard_library .. GENERATED FROM PYTHON SOURCE LINES 40-42 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 42-54 .. code-block:: default from gemseo.api import ( configure_logger, create_discipline, create_scalable, create_scenario, ) from gemseo.problems.sobieski.core import SobieskiProblem configure_logger() standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 55-59 Learning dataset ---------------- The first step is to build an :class:`.AbstractFullCache` dataset from a :class:`.DiagonalDOE`. .. GENERATED FROM PYTHON SOURCE LINES 61-65 Instantiate the discipline ~~~~~~~~~~~~~~~~~~~~~~~~~~ For that, we instantiate the :class:`~gemseo.problems.sobieski.wrappers.SobieskiAerodynamics` discipline and set it up to cache all evaluations. .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: default discipline = create_discipline("SobieskiAerodynamics") discipline.set_cache_policy(discipline.MEMORY_FULL_CACHE) .. GENERATED FROM PYTHON SOURCE LINES 69-72 Get the input space ~~~~~~~~~~~~~~~~~~~ We also define the input space on which to sample the discipline. .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: default input_space = SobieskiProblem().read_design_space() input_space.filter(discipline.get_input_data_names()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 76-82 Build the DOE scenario ~~~~~~~~~~~~~~~~~~~~~~ Lastly, we sample the discipline by means of a :class:`.DOEScenario` relying on both discipline and input space. In order to build a diagonal scalable discipline, a :class:`.DiagonalDOE` must be used. .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: default scenario = create_scenario( [discipline], "DisciplinaryOpt", "y_2", input_space, scenario_type="DOE" ) scenario.execute({"algo": "DiagonalDOE", "n_samples": 20}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'eval_jac': False, 'algo': 'DiagonalDOE', 'n_samples': 20} .. GENERATED FROM PYTHON SOURCE LINES 88-90 Scalable diagonal discipline ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 92-96 Build the scalable discipline ----------------------------- The second step is to build a :class:`.ScalableDiscipline`, using a :class:`.ScalableDiagonalModel` and the cache of the discipline. .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: default scalable = create_scalable("ScalableDiagonalModel", discipline.cache) .. GENERATED FROM PYTHON SOURCE LINES 99-110 Visualize the input-output dependencies ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can easily access the underlying :class:`.ScalableDiagonalModel` and plot the corresponding input-output dependency matrix where the level of gray and the number (in [0,100]) represent the degree of dependency between inputs and outputs. Input are on the left while outputs are at the top. More precisely, for a given output component located at the top of the graph, these degrees are contributions to the output component and they add up to 1. In other words, a degree expresses this contribution in percentage and for a given column, the elements add up to 100. .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: default scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_001.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 113-117 Visualize the 1D interpolations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For every output, we can also visualize a spline interpolation of the output samples over the diagonal of the input space. .. GENERATED FROM PYTHON SOURCE LINES 117-119 .. code-block:: default scalable.scalable_model.plot_1d_interpolations(save=False, show=True) .. rst-class:: sphx-glr-horizontal * .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_002.png :alt: 1D interpolation of sdm_SobieskiAerodynamics.g_2 :class: sphx-glr-multi-img * .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_003.png :alt: 1D interpolation of sdm_SobieskiAerodynamics.y_2 :class: sphx-glr-multi-img * .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_004.png :alt: 1D interpolation of sdm_SobieskiAerodynamics.y_2 :class: sphx-glr-multi-img * .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_005.png :alt: 1D interpolation of sdm_SobieskiAerodynamics.y_2 :class: sphx-glr-multi-img * .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_006.png :alt: 1D interpolation of sdm_SobieskiAerodynamics.y_21 :class: sphx-glr-multi-img * .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_007.png :alt: 1D interpolation of sdm_SobieskiAerodynamics.y_23 :class: sphx-glr-multi-img * .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_008.png :alt: 1D interpolation of sdm_SobieskiAerodynamics.y_24 :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 120-124 Increased problem dimension --------------------------- We can repeat the construction of the scalable discipline for different sizes of variables and visualize the input-output dependency matrices. .. GENERATED FROM PYTHON SOURCE LINES 126-129 Twice as many inputs ~~~~~~~~~~~~~~~~~~~~ For example, we can increase the size of each input by a factor of 2. .. GENERATED FROM PYTHON SOURCE LINES 129-136 .. code-block:: default sizes = { name: discipline.cache.varsizes[name] * 2 for name in discipline.get_input_data_names() } scalable = create_scalable("ScalableDiagonalModel", discipline.cache, sizes) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_009.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 137-140 Twice as many outputs ~~~~~~~~~~~~~~~~~~~~~ Or we can increase the size of each output by a factor of 2. .. GENERATED FROM PYTHON SOURCE LINES 140-147 .. code-block:: default sizes = { name: discipline.cache.varsizes[name] * 2 for name in discipline.get_output_data_names() } scalable = create_scalable("ScalableDiagonalModel", discipline.cache, sizes) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_010.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 148-151 Twice as many variables ~~~~~~~~~~~~~~~~~~~~~~~ Or we can increase the size of each input and each output by a factor of 2. .. GENERATED FROM PYTHON SOURCE LINES 151-157 .. code-block:: default names = list(discipline.get_input_data_names()) names += list(discipline.get_output_data_names()) sizes = {name: discipline.cache.varsizes[name] * 2 for name in names} scalable = create_scalable("ScalableDiagonalModel", discipline.cache, sizes) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_011.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 158-171 Binary IO dependencies ---------------------- By default, any output component depends on any input component with a random level. We can also consider sparser input-output dependency by means of binary input-output dependency matrices. For that, we have to set the value of the fill factor which represents the part of connection between inputs and outputs. Then, a connection is represented by a black square while an absence of connection is presented by a white one. When the fill factor is equal to 1, any input is connected to any output. Conversely, when the fill factor is equal to 0, there is not a single connection between inputs and outputs. .. GENERATED FROM PYTHON SOURCE LINES 173-176 Fill factor = 0.2 ~~~~~~~~~~~~~~~~~ Here, there is a 20% connection rate .. GENERATED FROM PYTHON SOURCE LINES 176-181 .. code-block:: default scalable = create_scalable( "ScalableDiagonalModel", discipline.cache, sizes, fill_factor=0.2 ) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_012.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 182-185 Fill factor = 0.5 ~~~~~~~~~~~~~~~~~ Here, there is a 50% connection rate .. GENERATED FROM PYTHON SOURCE LINES 185-190 .. code-block:: default scalable = create_scalable( "ScalableDiagonalModel", discipline.cache, sizes, fill_factor=0.5 ) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_013.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 191-194 Fill factor = 0.8 ~~~~~~~~~~~~~~~~~ Here, there is a 80% connection rate .. GENERATED FROM PYTHON SOURCE LINES 194-199 .. code-block:: default scalable = create_scalable( "ScalableDiagonalModel", discipline.cache, sizes, fill_factor=0.8 ) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_014.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 200-205 Heterogeneous dependencies -------------------------- We could also imagine a more complex situation where an output would depend inputs with a particular fill factor. For example, let us consider a 20% connection rate for :code:`"y_2"`. .. GENERATED FROM PYTHON SOURCE LINES 205-210 .. code-block:: default scalable = create_scalable( "ScalableDiagonalModel", discipline.cache, sizes, fill_factor={"y_2": 0.2} ) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_015.png :alt: plot diagonal :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 211-216 Group dependencies ------------------ We could also imagine another particular situation where an output would depend only on certain inputs. For example, let us suppose that :code:`"y_2"` depends only on :code:`"x_shared"`. .. GENERATED FROM PYTHON SOURCE LINES 216-220 .. code-block:: default scalable = create_scalable( "ScalableDiagonalModel", discipline.cache, sizes, group_dep={"y_2": ["x_shared"]} ) scalable.scalable_model.plot_dependency(save=False, show=True) .. image:: /examples/scalable/images/sphx_glr_plot_diagonal_016.png :alt: plot diagonal :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.650 seconds) .. _sphx_glr_download_examples_scalable_plot_diagonal.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_diagonal.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_diagonal.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_