.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/doe/plot_seed.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_doe_plot_seed.py: Change the seed of a DOE ======================== `Latin hypercube sampling `__ is an example of stochastic DOE algorithm: given an input dimension and a number of samples, running the algorithm twice will give two different DOEs. For the sake of reproducibility, |g| uses a `random seed `__: given an input dimension, a number of samples and a random seed, running the algorithm twice will give the same DOE. In this example, we will see how |g| uses the random seed and how the user can change its value. .. GENERATED FROM PYTHON SOURCE LINES 38-48 .. code-block:: default from __future__ import annotations from gemseo.algos.doe.lib_openturns import OpenTURNS from gemseo.algos.opt_problem import OptimizationProblem from gemseo.api import create_design_space from gemseo.api import create_discipline from gemseo.api import create_scenario from gemseo.api import execute_algo from gemseo.core.mdofunctions.mdo_function import MDOFunction .. GENERATED FROM PYTHON SOURCE LINES 49-59 At the scenario level --------------------- First, we illustrate the use of the random seed at the :class:`.DOEScenario` level which is the appropriate level for most users. Then, we will illustrate this use at the :class:`.OptimizationProblem` level which can be useful for developers. Let us consider a :class:`.MDODiscipline` representing the function :math:`y=x^2`: .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: default discipline = create_discipline("AnalyticDiscipline", expressions={"y": "x**2"}) .. GENERATED FROM PYTHON SOURCE LINES 62-63 This function is defined over the interval :math:`[-1,1]`: .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: default design_space = create_design_space() design_space.add_variable("x", l_b=-1, u_b=1) .. GENERATED FROM PYTHON SOURCE LINES 67-70 We want to sample this discipline over this design space. For that, we express the sampling problem as a :class:`.DOEScenario`: .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: default scenario = create_scenario( [discipline], "DisciplinaryOpt", "y", design_space, scenario_type="DOE" ) .. GENERATED FROM PYTHON SOURCE LINES 75-76 and solve it: .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: default scenario.execute({"algo": "OT_OPT_LHS", "n_samples": 2}) print(scenario.formulation.opt_problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 80-81 You can get the value of the random :attr:`.DOEScenario.seed` that has been used: .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: default print(scenario.seed) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 84-93 Note: This :attr:`~.DOEScenario.seed` is initialized at 0. Each call to :meth:`~.DOEScenario.execute` increments this :attr:`~.DOEScenario.seed` and then passes this value to the underlying :meth:`~.DOELibrary.execute` (or a custom seed value if any). Then, solving again this problem with the same configuration leads to a new result: .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: default scenario.execute({"algo": "OT_OPT_LHS", "n_samples": 2}) print(scenario.formulation.opt_problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([0.04982439]), array([-0.48653892])] .. GENERATED FROM PYTHON SOURCE LINES 98-99 and we can check that the value of the seed was incremented: .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: default print(scenario.seed) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 102-105 You can also pass a custom ``"seed"`` to the DOE algorithm with the key ``"algo_options"`` of the ``input_data`` passed to :meth:`.DOEScenario.execute`: .. GENERATED FROM PYTHON SOURCE LINES 105-108 .. code-block:: default scenario.execute({"algo": "OT_OPT_LHS", "n_samples": 2, "algo_options": {"seed": 123}}) print(scenario.formulation.opt_problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.09505332]), array([0.98400738])] .. GENERATED FROM PYTHON SOURCE LINES 109-112 You can verify that the :attr:`.DOEScenario.seed` has not been replaced by the custom value but incremented: .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: default print(scenario.seed) .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 115-118 Lastly, you can change the value of the :attr:`~.DOEScenario.seed` attached to the :class:`.DOEScenario`: .. GENERATED FROM PYTHON SOURCE LINES 118-123 .. code-block:: default scenario.seed = 123 scenario.execute({"algo": "OT_OPT_LHS", "n_samples": 2}) print(scenario.seed) print(scenario.formulation.opt_problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 124 [array([-0.19646111]), array([0.97538418])] .. GENERATED FROM PYTHON SOURCE LINES 124-129 At the problem level -------------------- Basic ~~~~~ Let us consider a :class:`.MDOFunction` representing the function :math:`y=x^2`: .. GENERATED FROM PYTHON SOURCE LINES 129-131 .. code-block:: default function = MDOFunction(lambda x: x**2, "f", args=["x"], outvars=["y"]) .. GENERATED FROM PYTHON SOURCE LINES 132-133 and defined over the unit interval :math:`x\in[0,1]`: .. GENERATED FROM PYTHON SOURCE LINES 133-136 .. code-block:: default design_space = create_design_space() design_space.add_variable("x", l_b=-1, u_b=1) .. GENERATED FROM PYTHON SOURCE LINES 137-140 We want to sample this function over this design space. For that, we express the sampling problem as an :class:`.OptimizationProblem`: .. GENERATED FROM PYTHON SOURCE LINES 140-143 .. code-block:: default problem = OptimizationProblem(design_space) problem.objective = function .. GENERATED FROM PYTHON SOURCE LINES 144-145 and solve it: .. GENERATED FROM PYTHON SOURCE LINES 145-148 .. code-block:: default execute_algo(problem, "OT_OPT_LHS", algo_type="doe", n_samples=2) print(problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 149-155 Note: We use the function :func:`.execute_algo` as the :class:`.OptimizationProblem` does not have a method :func:`execute` unlike the :class:`.Scenario`. Solving again this problem with the same configuration leads to the same result: .. GENERATED FROM PYTHON SOURCE LINES 155-158 .. code-block:: default execute_algo(problem, "OT_OPT_LHS", algo_type="doe", n_samples=2) print(problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 159-161 and the result is still the same if we take 1 as random seed, as 1 is the default value of this seed: .. GENERATED FROM PYTHON SOURCE LINES 161-164 .. code-block:: default execute_algo(problem, "OT_OPT_LHS", algo_type="doe", n_samples=2, seed=1) print(problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 165-167 If you want to use a different random seed, you only have to change the value of ``seed``: .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: default execute_algo(problem, "OT_OPT_LHS", algo_type="doe", n_samples=2, seed=3) print(problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([0.93088508]), array([-0.81399054])] .. GENERATED FROM PYTHON SOURCE LINES 171-177 Advanced ~~~~~~~~ You can also solve your problem with a lower level API by directly instantiating the :class:`.DOELibrary` of interest. A :class:`.DOELibrary` has a :attr:`~.DOELibrary.seed` initialized at 0 that is incremented at the beginning of each execution: .. GENERATED FROM PYTHON SOURCE LINES 177-182 .. code-block:: default library = OpenTURNS() library.algo_name = "OT_OPT_LHS" library.execute(problem, n_samples=2) print(scenario.seed) print(problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 124 [array([0.93088508]), array([-0.81399054])] .. GENERATED FROM PYTHON SOURCE LINES 183-184 Solving again the problem will give different samples: .. GENERATED FROM PYTHON SOURCE LINES 184-187 .. code-block:: default library.execute(problem, n_samples=2) print(scenario.seed) print(problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 124 [array([0.04982439]), array([-0.48653892])] .. GENERATED FROM PYTHON SOURCE LINES 188-189 You can also change the seed: .. GENERATED FROM PYTHON SOURCE LINES 189-193 .. code-block:: default library.seed = 123 library.execute(problem, n_samples=2) print(scenario.seed) print(problem.database.get_last_n_x(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 124 [array([-0.19646111]), array([0.97538418])] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.120 seconds) .. _sphx_glr_download_examples_doe_plot_seed.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_seed.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_seed.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_