.. 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 :ref:`Go to the end ` 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.mdo.sobieski.disciplines.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 34-43 .. code-block:: Python from __future__ import annotations from gemseo import configure_logger from gemseo import create_discipline from gemseo import create_scalable from gemseo import create_scenario from gemseo.problems.mdo.sobieski.core.design_space import SobieskiDesignSpace .. GENERATED FROM PYTHON SOURCE LINES 44-46 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: Python configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-55 Learning dataset ---------------- The first step is to build an :class:`.AbstractFullCache` dataset from a :class:`.DiagonalDOE`. .. GENERATED FROM PYTHON SOURCE LINES 57-62 Instantiate the discipline ~~~~~~~~~~~~~~~~~~~~~~~~~~ For that, we instantiate the :class:`~gemseo.problems.mdo.sobieski.disciplines.SobieskiAerodynamics` discipline and set it up to cache all evaluations. .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: Python discipline = create_discipline("SobieskiAerodynamics") .. GENERATED FROM PYTHON SOURCE LINES 65-68 Get the input space ~~~~~~~~~~~~~~~~~~~ We also define the input space on which to sample the discipline. .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: Python input_space = SobieskiDesignSpace() input_names = [name for name in discipline.get_input_data_names() if name != "c_4"] input_space.filter(input_names) .. raw:: html
Sobieski design space:
Name Lower bound Value Upper bound Type
x_shared[0] 0.01 0.05 0.09 float
x_shared[1] 30000 45000 60000 float
x_shared[2] 1.4 1.6 1.8 float
x_shared[3] 2.5 5.5 8.5 float
x_shared[4] 40 55 70 float
x_shared[5] 500 1000 1500 float
x_2 0.75 1 1.25 float
y_32 0.235 0.5027962499999999 0.795 float
y_12[0] 24850 50606.9742 77250 float
y_12[1] 0.45 0.95 1.5 float


.. GENERATED FROM PYTHON SOURCE LINES 73-79 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 79-87 .. code-block:: Python scenario = create_scenario( [discipline], "DisciplinaryOpt", "y_2", input_space, scenario_type="DOE" ) for output_name in discipline.get_output_data_names(): if output_name != "y_2": scenario.add_observable(output_name) scenario.execute({"algo": "DiagonalDOE", "n_samples": 20}) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:56:36: INFO - 08:56:36: *** Start DOEScenario execution *** INFO - 08:56:36: DOEScenario INFO - 08:56:36: Disciplines: SobieskiAerodynamics INFO - 08:56:36: MDO formulation: DisciplinaryOpt INFO - 08:56:36: Optimization problem: INFO - 08:56:36: minimize y_2(x_shared, x_2, y_32, y_12) INFO - 08:56:36: with respect to x_2, x_shared, y_12, y_32 INFO - 08:56:36: over the design space: INFO - 08:56:36: +-------------+-------------+--------------------+-------------+-------+ INFO - 08:56:36: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:56:36: +-------------+-------------+--------------------+-------------+-------+ INFO - 08:56:36: | x_shared[0] | 0.01 | 0.05 | 0.09 | float | INFO - 08:56:36: | x_shared[1] | 30000 | 45000 | 60000 | float | INFO - 08:56:36: | x_shared[2] | 1.4 | 1.6 | 1.8 | float | INFO - 08:56:36: | x_shared[3] | 2.5 | 5.5 | 8.5 | float | INFO - 08:56:36: | x_shared[4] | 40 | 55 | 70 | float | INFO - 08:56:36: | x_shared[5] | 500 | 1000 | 1500 | float | INFO - 08:56:36: | x_2 | 0.75 | 1 | 1.25 | float | INFO - 08:56:36: | y_32 | 0.235 | 0.5027962499999999 | 0.795 | float | INFO - 08:56:36: | y_12[0] | 24850 | 50606.9742 | 77250 | float | INFO - 08:56:36: | y_12[1] | 0.45 | 0.95 | 1.5 | float | INFO - 08:56:36: +-------------+-------------+--------------------+-------------+-------+ INFO - 08:56:36: Solving optimization problem with algorithm DiagonalDOE: INFO - 08:56:36: 5%|▌ | 1/20 [00:00<00:00, 164.53 it/sec, obj=[2.48500000e+04 5.75800074e+03 4.31573408e+00]] INFO - 08:56:36: 10%|█ | 2/20 [00:00<00:00, 248.22 it/sec, obj=[2.76078947e+04 6.45112022e+03 4.27955049e+00]] INFO - 08:56:36: 15%|█▌ | 3/20 [00:00<00:00, 301.92 it/sec, obj=[3.03657895e+04 7.17864545e+03 4.23001661e+00]] INFO - 08:56:36: 20%|██ | 4/20 [00:00<00:00, 337.43 it/sec, obj=[3.31236842e+04 7.92362686e+03 4.18036902e+00]] INFO - 08:56:36: 25%|██▌ | 5/20 [00:00<00:00, 364.38 it/sec, obj=[3.58815789e+04 8.66958857e+03 4.13878683e+00]] INFO - 08:56:36: 30%|███ | 6/20 [00:00<00:00, 383.29 it/sec, obj=[3.86394737e+04 9.40253365e+03 4.10947465e+00]] INFO - 08:56:36: 35%|███▌ | 7/20 [00:00<00:00, 399.12 it/sec, obj=[4.13973684e+04 1.01086635e+04 4.09523655e+00]] INFO - 08:56:36: 40%|████ | 8/20 [00:00<00:00, 411.32 it/sec, obj=[4.41552632e+04 1.07748595e+04 4.09798968e+00]] INFO - 08:56:36: 45%|████▌ | 9/20 [00:00<00:00, 421.83 it/sec, obj=[4.69131579e+04 1.13905076e+04 4.11861872e+00]] INFO - 08:56:36: 50%|█████ | 10/20 [00:00<00:00, 428.96 it/sec, obj=[4.96710526e+04 1.19440252e+04 4.15865270e+00]] INFO - 08:56:36: 55%|█████▌ | 11/20 [00:00<00:00, 436.67 it/sec, obj=[5.24289474e+04 1.24253852e+04 4.21950279e+00]] INFO - 08:56:36: 60%|██████ | 12/20 [00:00<00:00, 442.43 it/sec, obj=[5.51868421e+04 1.28262516e+04 4.30264771e+00]] INFO - 08:56:36: 65%|██████▌ | 13/20 [00:00<00:00, 448.00 it/sec, obj=[5.79447368e+04 1.31400641e+04 4.40977580e+00]] INFO - 08:56:36: 70%|███████ | 14/20 [00:00<00:00, 452.06 it/sec, obj=[6.07026316e+04 1.33620772e+04 4.54290381e+00]] INFO - 08:56:36: 75%|███████▌ | 15/20 [00:00<00:00, 456.74 it/sec, obj=[6.34605263e+04 1.34893612e+04 4.70448714e+00]] INFO - 08:56:36: 80%|████████ | 16/20 [00:00<00:00, 459.88 it/sec, obj=[6.62184211e+04 1.35166373e+04 4.89903071e+00]] INFO - 08:56:36: 85%|████████▌ | 17/20 [00:00<00:00, 463.64 it/sec, obj=[6.89763158e+04 1.34436796e+04 5.13076164e+00]] INFO - 08:56:36: 90%|█████████ | 18/20 [00:00<00:00, 466.28 it/sec, obj=[7.17342105e+04 1.32763007e+04 5.40317758e+00]] INFO - 08:56:36: 95%|█████████▌| 19/20 [00:00<00:00, 469.44 it/sec, obj=[7.44921053e+04 1.30407235e+04 5.71226782e+00]] INFO - 08:56:36: 100%|██████████| 20/20 [00:00<00:00, 471.96 it/sec, obj=[7.72500000e+04 1.27392070e+04 6.06395673e+00]] INFO - 08:56:36: Optimization result: INFO - 08:56:36: Optimizer info: INFO - 08:56:36: Status: None INFO - 08:56:36: Message: None INFO - 08:56:36: Number of calls to the objective function by the optimizer: 20 INFO - 08:56:36: Solution: INFO - 08:56:36: Objective: 25508.372961119574 INFO - 08:56:36: Design space: INFO - 08:56:36: +-------------+-------------+-------+-------------+-------+ INFO - 08:56:36: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:56:36: +-------------+-------------+-------+-------------+-------+ INFO - 08:56:36: | x_shared[0] | 0.01 | 0.01 | 0.09 | float | INFO - 08:56:36: | x_shared[1] | 30000 | 30000 | 60000 | float | INFO - 08:56:36: | x_shared[2] | 1.4 | 1.4 | 1.8 | float | INFO - 08:56:36: | x_shared[3] | 2.5 | 2.5 | 8.5 | float | INFO - 08:56:36: | x_shared[4] | 40 | 40 | 70 | float | INFO - 08:56:36: | x_shared[5] | 500 | 500 | 1500 | float | INFO - 08:56:36: | x_2 | 0.75 | 0.75 | 1.25 | float | INFO - 08:56:36: | y_32 | 0.235 | 0.235 | 0.795 | float | INFO - 08:56:36: | y_12[0] | 24850 | 24850 | 77250 | float | INFO - 08:56:36: | y_12[1] | 0.45 | 0.45 | 1.5 | float | INFO - 08:56:36: +-------------+-------------+-------+-------------+-------+ INFO - 08:56:36: *** End DOEScenario execution (time: 0:00:00.058856) *** {'eval_jac': False, 'n_samples': 20, 'algo': 'DiagonalDOE'} .. GENERATED FROM PYTHON SOURCE LINES 88-90 Scalable diagonal discipline ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 92-97 Build the scalable discipline ----------------------------- The second step is to build a :class:`.ScalableDiscipline`, using a :class:`.ScalableDiagonalModel` and the database converted to a :class:`.Dataset`. .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: Python dataset = scenario.to_dataset(opt_naming=False) scalable = create_scalable("ScalableDiagonalModel", dataset) .. GENERATED FROM PYTHON SOURCE LINES 101-112 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 112-114 .. code-block:: Python scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_001.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 115-119 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 119-121 .. code-block:: Python scalable.scalable_model.plot_1d_interpolations(save=False, show=True) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_002.png :alt: 1D interpolation of sdm_DOEScenario.g_2 :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_003.png :alt: 1D interpolation of sdm_DOEScenario.y_2 :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_004.png :alt: 1D interpolation of sdm_DOEScenario.y_2 :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_004.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_005.png :alt: 1D interpolation of sdm_DOEScenario.y_2 :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_005.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_006.png :alt: 1D interpolation of sdm_DOEScenario.y_21 :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_006.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_007.png :alt: 1D interpolation of sdm_DOEScenario.y_23 :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_007.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_008.png :alt: 1D interpolation of sdm_DOEScenario.y_24 :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_008.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 122-126 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 128-131 Twice as many inputs ~~~~~~~~~~~~~~~~~~~~ For example, we can increase the size of each input by a factor of 2. .. GENERATED FROM PYTHON SOURCE LINES 131-135 .. code-block:: Python sizes = {name: dataset.variable_names_to_n_components[name] * 2 for name in input_names} scalable = create_scalable("ScalableDiagonalModel", dataset, sizes) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_009.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_009.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 136-139 Twice as many outputs ~~~~~~~~~~~~~~~~~~~~~ Or we can increase the size of each output by a factor of 2. .. GENERATED FROM PYTHON SOURCE LINES 139-146 .. code-block:: Python sizes = { name: discipline.cache.names_to_sizes[name] * 2 for name in discipline.get_output_data_names() } scalable = create_scalable("ScalableDiagonalModel", dataset, sizes) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_010.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_010.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 147-150 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 150-155 .. code-block:: Python names = input_names + list(discipline.get_output_data_names()) sizes = {name: dataset.variable_names_to_n_components[name] * 2 for name in names} scalable = create_scalable("ScalableDiagonalModel", dataset, sizes) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_011.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_011.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 156-169 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 171-173 Fill factor = 0.2 ~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 173-176 .. code-block:: Python scalable = create_scalable("ScalableDiagonalModel", dataset, sizes, fill_factor=0.2) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_012.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_012.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 177-179 Fill factor = 0.5 ~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 179-182 .. code-block:: Python scalable = create_scalable("ScalableDiagonalModel", dataset, sizes, fill_factor=0.5) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_013.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_013.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 183-185 Fill factor = 0.8 ~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 185-188 .. code-block:: Python scalable = create_scalable("ScalableDiagonalModel", dataset, sizes, fill_factor=0.8) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_014.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_014.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 189-191 Heterogeneous dependencies -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 191-196 .. code-block:: Python scalable = create_scalable( "ScalableDiagonalModel", dataset, sizes, fill_factor={"y_2": 0.2} ) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_015.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_015.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. GENERATED FROM PYTHON SOURCE LINES 197-199 Group dependencies ------------------ .. GENERATED FROM PYTHON SOURCE LINES 199-203 .. code-block:: Python scalable = create_scalable( "ScalableDiagonalModel", dataset, sizes, group_dep={"y_2": ["x_shared"]} ) scalable.scalable_model.plot_dependency(save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_plot_diagonal_016.png :alt: plot diagonal :srcset: /examples/scalable/images/sphx_glr_plot_diagonal_016.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'None' .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.544 seconds) .. _sphx_glr_download_examples_scalable_plot_diagonal.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_diagonal.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_diagonal.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_