.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/design_space/plot_parameter_space.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_design_space_plot_parameter_space.py: Parameter space =============== In this example, we will see the basics of :class:`.ParameterSpace`. .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: default from __future__ import annotations from gemseo import configure_logger from gemseo import create_discipline from gemseo import create_scenario from gemseo.algos.parameter_space import ParameterSpace from gemseo.post.dataset.scatter_plot_matrix import ScatterMatrix configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 39-43 Firstly, a :class:`.ParameterSpace` does not require any mandatory argument. Create a parameter space ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: default parameter_space = ParameterSpace() .. GENERATED FROM PYTHON SOURCE LINES 46-50 Then, we can add either deterministic variables from their lower and upper bounds (use :meth:`.DesignSpace.add_variable`) or uncertain variables from their distribution names and parameters (use :meth:`.add_random_variable`) .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: default parameter_space.add_variable("x", l_b=-2.0, u_b=2.0) parameter_space.add_random_variable("y", "SPNormalDistribution", mu=0.0, sigma=1.0) parameter_space .. raw:: html
Parameter space:
name lower_bound value upper_bound type Initial distribution
x -2 None 2 float
y -inf 0 inf float norm(mu=0.0, sigma=1.0)


.. GENERATED FROM PYTHON SOURCE LINES 56-58 We can check that the variables *x* and *y* are implemented as deterministic and uncertain variables respectively: .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: default parameter_space.is_deterministic("x"), parameter_space.is_uncertain("y") .. rst-class:: sphx-glr-script-out .. code-block:: none (True, True) .. GENERATED FROM PYTHON SOURCE LINES 61-65 Sample from the parameter space ------------------------------- We can sample the uncertain variables from the :class:`.ParameterSpace` and get values either as an array (default value): .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: default sample = parameter_space.compute_samples(n_samples=2, as_dict=True) sample .. rst-class:: sphx-glr-script-out .. code-block:: none [{'y': array([-0.97694264])}, {'y': array([-0.46624564])}] .. GENERATED FROM PYTHON SOURCE LINES 69-70 or as a dictionary: .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default sample = parameter_space.compute_samples(n_samples=4) sample .. rst-class:: sphx-glr-script-out .. code-block:: none array([[-0.26843244], [ 0.27673196], [ 1.46675697], [-1.20904408]]) .. GENERATED FROM PYTHON SOURCE LINES 74-79 Sample a discipline over the parameter space -------------------------------------------- We can also sample a discipline over the parameter space. For simplicity, we instantiate an :class:`.AnalyticDiscipline` from a dictionary of expressions. .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: default discipline = create_discipline("AnalyticDiscipline", expressions={"z": "x+y"}) .. GENERATED FROM PYTHON SOURCE LINES 82-93 From these parameter space and discipline, we build a :class:`.DOEScenario` and execute it with a Latin Hypercube Sampling algorithm and 100 samples. .. warning:: A :class:`.Scenario` deals with all variables available in the :class:`.DesignSpace`. By inheritance, a :class:`.DOEScenario` deals with all variables available in the :class:`.ParameterSpace`. Thus, if we do not filter the uncertain variables, the :class:`.DOEScenario` will consider all variables. In particular, the deterministic variables will be considered as uniformly distributed. .. GENERATED FROM PYTHON SOURCE LINES 93-98 .. code-block:: default scenario = create_scenario( [discipline], "DisciplinaryOpt", "z", parameter_space, scenario_type="DOE" ) scenario.execute({"algo": "lhs", "n_samples": 100}) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 13:55:42: INFO - 13:55:42: *** Start DOEScenario execution *** INFO - 13:55:42: DOEScenario INFO - 13:55:42: Disciplines: AnalyticDiscipline INFO - 13:55:42: MDO formulation: DisciplinaryOpt INFO - 13:55:42: Optimization problem: INFO - 13:55:42: minimize z(x, y) INFO - 13:55:42: with respect to x, y INFO - 13:55:42: over the design space: INFO - 13:55:42: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 13:55:42: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 13:55:42: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 13:55:42: | x | -2 | None | 2 | float | | INFO - 13:55:42: | y | -inf | 0 | inf | float | norm(mu=0.0, sigma=1.0) | INFO - 13:55:42: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 13:55:42: Solving optimization problem with algorithm lhs: INFO - 13:55:42: ... 0%| | 0/100 [00:00
GROUP inputs outputs
VARIABLE x y z
COMPONENT 0 0 0
0 1.869403 1.246453 3.115855
1 -1.567970 3.285041 1.717071
2 0.282640 -0.101706 0.180934
3 1.916313 1.848317 3.764630
4 1.562653 0.586038 2.148691
... ... ... ...
95 0.120633 -0.327477 -0.206844
96 -0.999225 1.461403 0.462178
97 -1.396066 -0.972779 -2.368845
98 1.090093 0.225565 1.315658
99 -1.433207 -0.779330 -2.212536

100 rows × 3 columns



.. GENERATED FROM PYTHON SOURCE LINES 108-109 or graphical by means of a scatter plot matrix for example: .. GENERATED FROM PYTHON SOURCE LINES 109-111 .. code-block:: default ScatterMatrix(dataset).execute(save=False, show=True) .. image-sg:: /examples/design_space/images/sphx_glr_plot_parameter_space_001.png :alt: plot parameter space :srcset: /examples/design_space/images/sphx_glr_plot_parameter_space_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [
] .. GENERATED FROM PYTHON SOURCE LINES 112-116 Sample a discipline over the uncertain space -------------------------------------------- If we want to sample a discipline over the uncertain space, we need to filter the uncertain variables: .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: default parameter_space.filter(parameter_space.uncertain_variables) .. raw:: html
Parameter space:
name lower_bound value upper_bound type Initial distribution
y -inf -1.368441899688857 inf float norm(mu=0.0, sigma=1.0)


.. GENERATED FROM PYTHON SOURCE LINES 119-121 Then, we create a new scenario from this parameter space containing only the uncertain variables and execute it. .. GENERATED FROM PYTHON SOURCE LINES 121-126 .. code-block:: default scenario = create_scenario( [discipline], "DisciplinaryOpt", "z", parameter_space, scenario_type="DOE" ) scenario.execute({"algo": "lhs", "n_samples": 100}) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 13:55:43: INFO - 13:55:43: *** Start DOEScenario execution *** INFO - 13:55:43: DOEScenario INFO - 13:55:43: Disciplines: AnalyticDiscipline INFO - 13:55:43: MDO formulation: DisciplinaryOpt INFO - 13:55:43: Optimization problem: INFO - 13:55:43: minimize z(y) INFO - 13:55:43: with respect to y INFO - 13:55:43: over the design space: INFO - 13:55:43: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 13:55:43: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 13:55:43: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 13:55:43: | y | -inf | -1.368441899688857 | inf | float | norm(mu=0.0, sigma=1.0) | INFO - 13:55:43: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 13:55:43: Solving optimization problem with algorithm lhs: INFO - 13:55:43: ... 0%| | 0/100 [00:00
GROUP inputs outputs
VARIABLE y z
COMPONENT 0 0
0 -0.640726 -0.640726
1 -0.393653 -0.393653
2 0.550565 0.550565
3 0.944369 0.944369
4 -2.115275 -2.115275
... ... ...
95 0.081947 0.081947
96 -1.085812 -1.085812
97 -0.761651 -0.761651
98 -0.042932 -0.042932
99 -0.813354 -0.813354

100 rows × 2 columns



.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.653 seconds) .. _sphx_glr_download_examples_design_space_plot_parameter_space.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_parameter_space.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_parameter_space.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_