.. 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 :ref:`Go to the end ` 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-49 .. code-block:: Python from __future__ import annotations from gemseo import create_design_space from gemseo import create_discipline from gemseo import create_scenario from gemseo import execute_algo from gemseo.algos.doe.openturns.openturns import OpenTURNS from gemseo.algos.optimization_problem import OptimizationProblem from gemseo.core.mdo_functions.mdo_function import MDOFunction .. GENERATED FROM PYTHON SOURCE LINES 50-60 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 an :class:`.Discipline` representing the function :math:`y=x^2`: .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python discipline = create_discipline("AnalyticDiscipline", expressions={"y": "x**2"}) .. GENERATED FROM PYTHON SOURCE LINES 63-64 This function is defined over the interval :math:`[-1,1]`: .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python design_space = create_design_space() design_space.add_variable("x", lower_bound=-1, upper_bound=1) .. GENERATED FROM PYTHON SOURCE LINES 68-71 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 71-79 .. code-block:: Python scenario = create_scenario( [discipline], "y", design_space, scenario_type="DOE", formulation_name="DisciplinaryOpt", ) .. GENERATED FROM PYTHON SOURCE LINES 80-81 and solve it: .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: Python scenario.execute(algo_name="OT_OPT_LHS", n_samples=2) scenario.formulation.optimization_problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 85-87 When using the same DOE algorithm, solving again this problem with the same scenario leads to a new result: .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python scenario.execute(algo_name="OT_OPT_LHS", n_samples=2) scenario.formulation.optimization_problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([0.04982439]), array([-0.48653892])] .. GENERATED FROM PYTHON SOURCE LINES 91-94 The value of the default seed was incremented from 0 (at first execution) to 1 (at last execution), as we can check it by setting the custom ``"seed"`` to 1: .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python scenario.execute(algo_name="OT_OPT_LHS", n_samples=2, seed=1) scenario.formulation.optimization_problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([0.04982439]), array([-0.48653892])] .. GENERATED FROM PYTHON SOURCE LINES 98-99 The default seed is incremented at each execution, whatever the custom one. .. GENERATED FROM PYTHON SOURCE LINES 101-106 At the problem level -------------------- Basic ~~~~~ Let us consider an :class:`.MDOFunction` representing the function :math:`y=x^2`: .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: Python function = MDOFunction(lambda x: x**2, "f", input_names=["x"], output_names=["y"]) .. GENERATED FROM PYTHON SOURCE LINES 109-110 and defined over the unit interval :math:`x\in[0,1]`: .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python design_space = create_design_space() design_space.add_variable("x", lower_bound=-1, upper_bound=1) .. GENERATED FROM PYTHON SOURCE LINES 114-117 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 117-120 .. code-block:: Python problem = OptimizationProblem(design_space) problem.objective = function .. GENERATED FROM PYTHON SOURCE LINES 121-122 and solve it: .. GENERATED FROM PYTHON SOURCE LINES 122-125 .. code-block:: Python execute_algo(problem, algo_name="OT_OPT_LHS", algo_type="doe", n_samples=2) problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 126-132 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 132-135 .. code-block:: Python execute_algo(problem, algo_name="OT_OPT_LHS", algo_type="doe", n_samples=2) problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 136-138 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 138-141 .. code-block:: Python execute_algo(problem, algo_name="OT_OPT_LHS", algo_type="doe", n_samples=2, seed=1) problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.89534931]), array([0.73475608])] .. GENERATED FROM PYTHON SOURCE LINES 142-144 If you want to use a different random seed, you only have to change the value of ``seed``: .. GENERATED FROM PYTHON SOURCE LINES 144-147 .. code-block:: Python execute_algo(problem, algo_name="OT_OPT_LHS", algo_type="doe", n_samples=2, seed=3) problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([0.93088508]), array([-0.81399054])] .. GENERATED FROM PYTHON SOURCE LINES 148-154 Advanced ~~~~~~~~ You can also solve your problem with a lower level API by directly instantiating the :class:`.BaseDOELibrary` of interest. A :class:`.BaseDOELibrary` has a default seed generated by a :class:`.Seeder` that is incremented at the beginning of each execution: .. GENERATED FROM PYTHON SOURCE LINES 154-157 .. code-block:: Python algo = OpenTURNS("OT_OPT_LHS") algo.execute(problem, n_samples=2) problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([0.93088508]), array([-0.81399054])] .. GENERATED FROM PYTHON SOURCE LINES 158-159 Solving again the problem will give different samples: .. GENERATED FROM PYTHON SOURCE LINES 159-161 .. code-block:: Python algo.execute(problem, n_samples=2) problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([0.04982439]), array([-0.48653892])] .. GENERATED FROM PYTHON SOURCE LINES 162-163 You can also use a specific seed instead of the default one: .. GENERATED FROM PYTHON SOURCE LINES 163-165 .. code-block:: Python algo.execute(problem, n_samples=2, seed=123) problem.database.get_last_n_x_vect(2) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-0.09505332]), array([0.98400738])] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.063 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-jupyter :download:`Download Jupyter notebook: plot_seed.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_seed.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_seed.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_