.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/doe/plot_skip_doe_samples.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_skip_doe_samples.py: Skip samples when using DOE. ============================ .. GENERATED FROM PYTHON SOURCE LINES 24-44 .. code-block:: Python from __future__ import annotations from typing import TYPE_CHECKING from numpy import array from numpy import sqrt from gemseo import configure_logger from gemseo import create_design_space from gemseo import create_scenario from gemseo.core.discipline import Discipline from gemseo.settings.doe import CustomDOE_Settings from gemseo.settings.post import BasicHistory_Settings if TYPE_CHECKING: from gemseo.typing import StrKeyMapping configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-52 In this example, we show how to skip the evaluation at a DOE point during a :class:`.DOEScenario` run. This is useful in situations where the evaluation of a sample fails or when the user wants to avoid evaluating some samples when certain conditions are met. The DOE algorithms in GEMSEO are able to catch ``ValueError`` exceptions (and only this specific type of exception) at runtime and move to the next sample. .. GENERATED FROM PYTHON SOURCE LINES 54-57 Let us consider a discipline implementing the function :math:`y=sqrt(a)`. The ``_run`` method of this discipline raises a ``ValueError`` when :math:`a < 0`. Of course, you may use any other set of conditions to raise the exception in your scripts. .. GENERATED FROM PYTHON SOURCE LINES 57-77 .. code-block:: Python class ValueErrorDiscipline(Discipline): default_grammar_type = Discipline.GrammarType.SIMPLE def __init__(self) -> None: super().__init__() self.input_grammar.update_from_names("a") self.output_grammar.update_from_names("y") def _run(self, input_data: StrKeyMapping): a = input_data["a"] if a < 0: msg = "The sample is undefined for a < 0." raise ValueError(msg) return {"y": sqrt(a)} discipline = ValueErrorDiscipline() .. GENERATED FROM PYTHON SOURCE LINES 78-79 We define a design space with the variable :math:`a\in[-1,10]`: .. GENERATED FROM PYTHON SOURCE LINES 79-84 .. code-block:: Python design_space = create_design_space() design_space.add_variable( "a", type_=design_space.DesignVariableType.FLOAT, lower_bound=-1, upper_bound=10 ) .. GENERATED FROM PYTHON SOURCE LINES 85-87 We want to evaluate this discipline over this design space at points 1, -1 and 4: .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python samples = array([[1.0], [-1.0], [4.0]]) .. GENERATED FROM PYTHON SOURCE LINES 90-92 For that, we can create a scenario and execute it with a :class:`.CustomDOE` with the setting "samples": .. GENERATED FROM PYTHON SOURCE LINES 92-102 .. code-block:: Python scenario = create_scenario( [discipline], "y", design_space, scenario_type="DOE", formulation_name="DisciplinaryOpt", ) custom_doe_settings = CustomDOE_Settings(samples=samples) scenario.execute(custom_doe_settings) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 20:38:58: *** Start DOEScenario execution *** INFO - 20:38:58: DOEScenario INFO - 20:38:58: Disciplines: ValueErrorDiscipline INFO - 20:38:58: MDO formulation: DisciplinaryOpt INFO - 20:38:58: Optimization problem: INFO - 20:38:58: minimize y(a) INFO - 20:38:58: with respect to a INFO - 20:38:58: over the design space: INFO - 20:38:58: +------+-------------+-------+-------------+-------+ INFO - 20:38:58: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:58: +------+-------------+-------+-------------+-------+ INFO - 20:38:58: | a | -1 | None | 10 | float | INFO - 20:38:58: +------+-------------+-------+-------------+-------+ INFO - 20:38:58: Solving optimization problem with algorithm CustomDOE: INFO - 20:38:58: 33%|███▎ | 1/3 [00:00<00:00, 3495.25 it/sec, obj=1] ERROR - 20:38:58: Failed to evaluate function y ValueError: The sample is undefined for a < 0. ERROR - 20:38:58: The evaluation of the functions at point [-1.] raised a ValueError; skipping to the next point. ValueError: The sample is undefined for a < 0. INFO - 20:38:58: 67%|██████▋ | 2/3 [00:00<00:00, 1844.46 it/sec, obj=2] INFO - 20:38:58: Optimization result: INFO - 20:38:58: Optimizer info: INFO - 20:38:58: Status: None INFO - 20:38:58: Message: None INFO - 20:38:58: Number of calls to the objective function by the optimizer: 0 INFO - 20:38:58: Solution: INFO - 20:38:58: Objective: 1.0 INFO - 20:38:58: Design space: INFO - 20:38:58: +------+-------------+-------+-------------+-------+ INFO - 20:38:58: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:58: +------+-------------+-------+-------------+-------+ INFO - 20:38:58: | a | -1 | 1 | 10 | float | INFO - 20:38:58: +------+-------------+-------+-------------+-------+ INFO - 20:38:58: *** End DOEScenario execution (time: 0:00:00.003094) *** .. GENERATED FROM PYTHON SOURCE LINES 103-107 The logger shows that GEMSEO ignores the ``ValueError`` raised at the second point and switches to the third point. The post-processing of the scenario only includes two runs: .. GENERATED FROM PYTHON SOURCE LINES 107-112 .. code-block:: Python basic_history_settings = BasicHistory_Settings( variable_names=["y"], save=False, show=True ) scenario.post_process(basic_history_settings) .. image-sg:: /examples/doe/images/sphx_glr_plot_skip_doe_samples_001.png :alt: History plot :srcset: /examples/doe/images/sphx_glr_plot_skip_doe_samples_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 113-118 .. warning:: In order to be able to continue the execution of a :class:`.DOEScenario`, the execution status and statistics of disciplines must be disabled. This is the default behavior, but you can also disable them explicitly with the :func:`.configure` function. .. GENERATED FROM PYTHON SOURCE LINES 120-124 Note that if your discipline is able to return a ``NaN`` without raising any exceptions, you do not need to use the mechanism explained here, in that case the ``NaN`` values will be handled by GEMSEO and stored in the database of the scenario. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.092 seconds) .. _sphx_glr_download_examples_doe_plot_skip_doe_samples.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_skip_doe_samples.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_skip_doe_samples.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_skip_doe_samples.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_