.. 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 Click :ref:`here ` 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-37 .. code-block:: default from __future__ import annotations 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 configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-42 Firstly, a :class:`.ParameterSpace` does not require any mandatory argument. Create a parameter space ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: default parameter_space = ParameterSpace() .. GENERATED FROM PYTHON SOURCE LINES 48-52 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 52-57 .. 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 .. 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 58-60 We can check that the deterministic and uncertain variables are implemented as deterministic and deterministic variables respectively: .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. 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 .. code-block:: none x is deterministic: True y is deterministic: False x is uncertain: False y is uncertain: True .. GENERATED FROM PYTHON SOURCE LINES 66-70 Sample from the parameter space ------------------------------- We can sample the uncertain variables from the :class:`.ParameterSpace` and get values either as an array (default value) or as a dictionary: .. GENERATED FROM PYTHON SOURCE LINES 70-75 .. 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 .. code-block:: none [{'y': array([-0.22455908])}, {'y': array([0.96599831])}] [[-0.94119273] [ 0.71294261] [ 0.82955208] [ 1.40263977]] .. GENERATED FROM PYTHON SOURCE LINES 76-81 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 81-84 .. code-block:: default discipline = create_discipline("AnalyticDiscipline", expressions={"z": "x+y"}) .. GENERATED FROM PYTHON SOURCE LINES 85-96 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 consider as uniformly distributed. .. GENERATED FROM PYTHON SOURCE LINES 96-102 .. 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 - 14:48:21: INFO - 14:48:21: *** Start DOEScenario execution *** INFO - 14:48:21: DOEScenario INFO - 14:48:21: Disciplines: AnalyticDiscipline INFO - 14:48:21: MDO formulation: DisciplinaryOpt INFO - 14:48:21: Optimization problem: INFO - 14:48:21: minimize z(x, y) INFO - 14:48:21: with respect to x, y INFO - 14:48:21: over the design space: INFO - 14:48:21: | Parameter space | INFO - 14:48:21: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 14:48:21: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 14:48:21: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 14:48:21: | x | -2 | None | 2 | float | | INFO - 14:48:21: | y | -inf | 0 | inf | float | norm(mu=0.0, sigma=1.0) | INFO - 14:48:21: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 14:48:21: Solving optimization problem with algorithm lhs: INFO - 14:48:21: ... 0%| | 0/100 [00:00 .. GENERATED FROM PYTHON SOURCE LINES 116-120 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 120-122 .. code-block:: default parameter_space.filter(parameter_space.uncertain_variables) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 123-125 Then, we create a new scenario from this parameter space containing only the uncertain variables and execute it. .. GENERATED FROM PYTHON SOURCE LINES 125-130 .. 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 - 14:48:21: INFO - 14:48:21: *** Start DOEScenario execution *** INFO - 14:48:21: DOEScenario INFO - 14:48:21: Disciplines: AnalyticDiscipline INFO - 14:48:21: MDO formulation: DisciplinaryOpt INFO - 14:48:21: Optimization problem: INFO - 14:48:21: minimize z(y) INFO - 14:48:21: with respect to y INFO - 14:48:21: over the design space: INFO - 14:48:21: | Parameter space | INFO - 14:48:21: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 14:48:21: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 14:48:21: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 14:48:21: | y | -inf | -1.368441899688857 | inf | float | norm(mu=0.0, sigma=1.0) | INFO - 14:48:21: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 14:48:21: Solving optimization problem with algorithm lhs: INFO - 14:48:21: ... 0%| | 0/100 [00:00` .. 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 `_