.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/uncertainty/plot_u_parameter_space.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_uncertainty_plot_u_parameter_space.py: Parameter space =============== In this example, we will see the basics of :class:`.ParameterSpace`. .. GENERATED FROM PYTHON SOURCE LINES 28-37 .. code-block:: default from gemseo.algos.parameter_space import ParameterSpace from gemseo.api import configure_logger from gemseo.api import create_discipline from gemseo.api import create_scenario from matplotlib import pyplot as plt configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-42 Create a parameter space ------------------------ Firstly, the creation of a :class:`.ParameterSpace` does not require any mandatory argument: .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: default parameter_space = ParameterSpace() .. GENERATED FROM PYTHON SOURCE LINES 45-50 Then, we can add either deterministic variables from their lower and upper bounds (use :meth:`.ParameterSpace.add_variable`) or uncertain variables from their distribution names and parameters (use :meth:`.ParameterSpace.add_random_variable`) .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. 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) print(parameter_space) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none +----------------------------------------------------------------------------+ | 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 55-57 We can check that the deterministic and uncertain variables are implemented as deterministic and deterministic variables respectively: .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: default print("x is deterministic: ", parameter_space.is_deterministic("x")) print("y is deterministic: ", parameter_space.is_deterministic("y")) print("x is uncertain: ", parameter_space.is_uncertain("x")) print("y is uncertain: ", parameter_space.is_uncertain("y")) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none x is deterministic: True y is deterministic: False x is uncertain: False y is uncertain: True .. GENERATED FROM PYTHON SOURCE LINES 63-68 Sample from the parameter space ------------------------------- We can sample the uncertain variables from the :class:`.ParameterSpace` and get values either as a NumPy array (by default) or as a dictionary of NumPy arrays indexed by the names of the variables: .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: default sample = parameter_space.compute_samples(n_samples=2, as_dict=True) print(sample) sample = parameter_space.compute_samples(n_samples=4) print(sample) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [{'y': array([-0.29202523])}, {'y': array([0.62106936])}] [[-0.05989239] [-0.31236041] [ 0.48944185] [-1.33652064]] .. 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-99 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:`.DOEScenario` considers all the variables available in its :class:`.DesignSpace`. By inheritance, in the special case of a :class:`.ParameterSpace`, a :class:`.DOEScenario` considers all the variables available in this :class:`.ParameterSpace`. Thus, if we do not filter the uncertain variables, the :class:`.DOEScenario` will consider both the deterministic variables as uniformly distributed variables and the uncertain variables with their specified probability distributions. .. GENERATED FROM PYTHON SOURCE LINES 99-105 .. 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 Out: .. code-block:: none INFO - 07:19:15: INFO - 07:19:15: *** Start DOEScenario execution *** INFO - 07:19:15: DOEScenario INFO - 07:19:15: Disciplines: AnalyticDiscipline INFO - 07:19:15: MDO formulation: DisciplinaryOpt INFO - 07:19:15: Optimization problem: INFO - 07:19:15: minimize z(x, y) INFO - 07:19:15: with respect to x, y INFO - 07:19:15: over the design space: INFO - 07:19:15: | Parameter space | INFO - 07:19:15: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 07:19:15: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 07:19:15: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 07:19:15: | x | -2 | None | 2 | float | | INFO - 07:19:15: | y | -inf | 0 | inf | float | norm(mu=0.0, sigma=1.0) | INFO - 07:19:15: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 07:19:15: Solving optimization problem with algorithm lhs: INFO - 07:19:15: ... 0%| | 0/100 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_u_parameter_space.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_