.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/api/plot_scenario.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_api_plot_scenario.py: Scenario ======== .. GENERATED FROM PYTHON SOURCE LINES 25-41 .. code-block:: Python from __future__ import annotations from gemseo import configure_logger from gemseo import create_design_space from gemseo import create_discipline from gemseo import create_scenario from gemseo import get_available_scenario_types from gemseo import get_scenario_differentiation_modes from gemseo import get_scenario_inputs_schema from gemseo import get_scenario_options_schema from gemseo import monitor_scenario configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 42-56 In this example, we will discover the different functions of the API to related to scenarios, which are the |g|' objects dedicated to the resolution of a problem, e.g. optimization or trade-off, associated with a list of disciplines and a design space. All classes implementing scenarios inherit from :class:`.Scenario` which is an abstract class. Classical concrete classes are :class:`.MDOScenario` and :class:`.DOEScenario`, respectively dedicated to optimization and trade-off problems. Get available scenario type --------------------------- The high-level function :func:`~gemseo.get_available_scenario_types` can be used to get the available scenario types (:class:`.MDOScenario` and :class:`.DOEScenario`). .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python get_available_scenario_types() .. rst-class:: sphx-glr-script-out .. code-block:: none ['MDO', 'DOE'] .. GENERATED FROM PYTHON SOURCE LINES 59-63 Get scenario options schema --------------------------- The :func:`.get_scenario_options_schema` function can be used to get the options of a given scenario type: .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python get_scenario_options_schema("MDO") .. rst-class:: sphx-glr-script-out .. code-block:: none {'$schema': 'http://json-schema.org/schema#', 'type': 'object', 'properties': {'name': {'description': 'The name to be given to this scenario. If empty, use the name of the class.', 'type': 'string'}, 'maximize_objective': {'description': 'Whether to maximize the objective.', 'type': 'boolean'}, 'formulation_settings_model': {'description': 'The formulation settings as a Pydantic model. If ``None``, use ``**settings``.', 'type': 'null'}}} .. GENERATED FROM PYTHON SOURCE LINES 66-92 Create a scenario ----------------- The API function :func:`.create_scenario` can be used to create a scenario: - The four first arguments are mandatory: - ``disciplines``: the list of :class:`.Discipline` (or possibly, a single :class:`.Discipline`), - ``objective_name``: the name of the objective function (one of the discipline outputs), - ``design_space``: the :class:`.DesignSpace` or the file path of the design space, - either a ``formulation_name`` followed by its ``formulation_settings```; or - a ``formulation_settings_model`` (see :ref:`formulation_settings`). - The other arguments are optional: - ``name``: scenario name, - ``scenario_type``: type of scenario, either `"MDO"` (default) or `"DOE"` , - ``**formulation_settings``: settings passed to the formulation as keyword arguments when the ``formulation_settings_model`` was not provided. - This function returns an instance of :class:`.MDOScenario` or :class:`.DOEScenario`. .. GENERATED FROM PYTHON SOURCE LINES 92-113 .. code-block:: Python discipline = create_discipline("AnalyticDiscipline", expressions={"y": "x1+x2"}) design_space = create_design_space() design_space.add_variable("x1", 1, "float", 0.0, 1.0) design_space.add_variable("x2", 1, "float", 0.0, 1.0) scenario = create_scenario( discipline, "y", design_space, scenario_type="DOE", formulation_name="DisciplinaryOpt", ) scenario.execute(algo_name="PYDOE_FULLFACT", n_samples=25) scenario.post_process( post_name="ScatterPlotMatrix", variable_names=["x1", "x2", "y"], save=False, show=True, ) .. image-sg:: /examples/api/images/sphx_glr_plot_scenario_001.png :alt: plot scenario :srcset: /examples/api/images/sphx_glr_plot_scenario_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:36:14: INFO - 08:36:14: *** Start DOEScenario execution *** INFO - 08:36:14: DOEScenario INFO - 08:36:14: Disciplines: AnalyticDiscipline INFO - 08:36:14: MDO formulation: DisciplinaryOpt INFO - 08:36:14: Optimization problem: INFO - 08:36:14: minimize y(x1, x2) INFO - 08:36:14: with respect to x1, x2 INFO - 08:36:14: over the design space: INFO - 08:36:14: +------+-------------+-------+-------------+-------+ INFO - 08:36:14: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:36:14: +------+-------------+-------+-------------+-------+ INFO - 08:36:14: | x1 | 0 | None | 1 | float | INFO - 08:36:14: | x2 | 0 | None | 1 | float | INFO - 08:36:14: +------+-------------+-------+-------------+-------+ INFO - 08:36:14: Solving optimization problem with algorithm PYDOE_FULLFACT: INFO - 08:36:14: 4%|▍ | 1/25 [00:00<00:00, 456.70 it/sec, obj=0] INFO - 08:36:14: 8%|▊ | 2/25 [00:00<00:00, 750.86 it/sec, obj=0.25] INFO - 08:36:14: 12%|█▏ | 3/25 [00:00<00:00, 949.44 it/sec, obj=0.5] INFO - 08:36:14: 16%|█▌ | 4/25 [00:00<00:00, 1114.25 it/sec, obj=0.75] INFO - 08:36:14: 20%|██ | 5/25 [00:00<00:00, 1251.28 it/sec, obj=1] INFO - 08:36:14: 24%|██▍ | 6/25 [00:00<00:00, 1355.11 it/sec, obj=0.25] INFO - 08:36:14: 28%|██▊ | 7/25 [00:00<00:00, 1447.60 it/sec, obj=0.5] INFO - 08:36:14: 32%|███▏ | 8/25 [00:00<00:00, 1519.12 it/sec, obj=0.75] INFO - 08:36:14: 36%|███▌ | 9/25 [00:00<00:00, 1588.08 it/sec, obj=1] INFO - 08:36:14: 40%|████ | 10/25 [00:00<00:00, 1644.37 it/sec, obj=1.25] INFO - 08:36:14: 44%|████▍ | 11/25 [00:00<00:00, 1696.41 it/sec, obj=0.5] INFO - 08:36:14: 48%|████▊ | 12/25 [00:00<00:00, 1733.18 it/sec, obj=0.75] INFO - 08:36:14: 52%|█████▏ | 13/25 [00:00<00:00, 1766.82 it/sec, obj=1] INFO - 08:36:14: 56%|█████▌ | 14/25 [00:00<00:00, 1802.62 it/sec, obj=1.25] INFO - 08:36:14: 60%|██████ | 15/25 [00:00<00:00, 1836.17 it/sec, obj=1.5] INFO - 08:36:14: 64%|██████▍ | 16/25 [00:00<00:00, 1861.50 it/sec, obj=0.75] INFO - 08:36:14: 68%|██████▊ | 17/25 [00:00<00:00, 1888.43 it/sec, obj=1] INFO - 08:36:14: 72%|███████▏ | 18/25 [00:00<00:00, 1913.90 it/sec, obj=1.25] INFO - 08:36:14: 76%|███████▌ | 19/25 [00:00<00:00, 1935.35 it/sec, obj=1.5] INFO - 08:36:14: 80%|████████ | 20/25 [00:00<00:00, 1954.75 it/sec, obj=1.75] INFO - 08:36:14: 84%|████████▍ | 21/25 [00:00<00:00, 1975.07 it/sec, obj=1] INFO - 08:36:14: 88%|████████▊ | 22/25 [00:00<00:00, 1986.45 it/sec, obj=1.25] INFO - 08:36:14: 92%|█████████▏| 23/25 [00:00<00:00, 2002.26 it/sec, obj=1.5] INFO - 08:36:14: 96%|█████████▌| 24/25 [00:00<00:00, 2018.47 it/sec, obj=1.75] INFO - 08:36:14: 100%|██████████| 25/25 [00:00<00:00, 2034.21 it/sec, obj=2] INFO - 08:36:14: Optimization result: INFO - 08:36:14: Optimizer info: INFO - 08:36:14: Status: None INFO - 08:36:14: Message: None INFO - 08:36:14: Number of calls to the objective function by the optimizer: 25 INFO - 08:36:14: Solution: INFO - 08:36:14: Objective: 0.0 INFO - 08:36:14: Design space: INFO - 08:36:14: +------+-------------+-------+-------------+-------+ INFO - 08:36:14: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:36:14: +------+-------------+-------+-------------+-------+ INFO - 08:36:14: | x1 | 0 | 0 | 1 | float | INFO - 08:36:14: | x2 | 0 | 0 | 1 | float | INFO - 08:36:14: +------+-------------+-------+-------------+-------+ INFO - 08:36:14: *** End DOEScenario execution (time: 0:00:00.016556) *** .. GENERATED FROM PYTHON SOURCE LINES 114-116 - The :func:`.get_scenario_inputs_schema` function can be used to get the inputs of a scenario: .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: Python get_scenario_inputs_schema(scenario) .. rst-class:: sphx-glr-script-out .. code-block:: none {'$defs': {'_algo_enum': {'enum': ['CustomDOE', 'DiagonalDOE', 'MorrisDOE', 'OATDOE', 'OT_SOBOL', 'OT_RANDOM', 'OT_HASELGROVE', 'OT_REVERSE_HALTON', 'OT_HALTON', 'OT_FAURE', 'OT_MONTE_CARLO', 'OT_FACTORIAL', 'OT_COMPOSITE', 'OT_AXIAL', 'OT_OPT_LHS', 'OT_LHS', 'OT_LHSC', 'OT_FULLFACT', 'OT_SOBOL_INDICES', 'PYDOE_BBDESIGN', 'PYDOE_CCDESIGN', 'PYDOE_FF2N', 'PYDOE_FULLFACT', 'PYDOE_LHS', 'PYDOE_PBDESIGN', 'Halton', 'LHS', 'MC', 'PoissonDisk', 'Sobol'], 'title': 'algo_enum', 'type': 'string'}}, 'properties': {'algo_name': {'$ref': '#/$defs/_algo_enum', 'description': 'The name of the algorithm.'}, 'algo_settings': {'description': 'The settings for the algorithm.', 'title': 'Algo Settings', 'type': 'object'}}, 'required': ['algo_name'], 'title': 'Settings', 'type': 'object'} .. GENERATED FROM PYTHON SOURCE LINES 119-123 Get scenario differentiation modes ---------------------------------- The :func:`.get_scenario_differentiation_modes` can be used to get the available differentiation modes of a scenario: .. GENERATED FROM PYTHON SOURCE LINES 123-125 .. code-block:: Python get_scenario_differentiation_modes() .. rst-class:: sphx-glr-script-out .. code-block:: none (, , , , ) .. GENERATED FROM PYTHON SOURCE LINES 126-138 Monitor a scenario ------------------ To monitor a scenario execution programmatically, ie get a notification when a discipline status is changed, use :func:`.monitor_scenario`. The first argument is the scenario to monitor, and the second is an observer object, that is notified by its update(atom) method, which takes an :class:`~gemseo.core.execution_sequence.ExecutionSequence` as argument. This method will be called every time a discipline status changes. The atom represents a discipline's position in the process. One discipline can have multiple atoms, since one discipline can be used in multiple positions in the MDO formulation. .. GENERATED FROM PYTHON SOURCE LINES 138-160 .. code-block:: Python class Observer: """Observer.""" def update(self, atom) -> None: """Update method. :param ExecutionSequence atom: atomic execution sequence. """ print(atom) scenario = create_scenario( discipline, "y", design_space, scenario_type="DOE", formulation_name="DisciplinaryOpt", ) monitor_scenario(scenario, Observer()) scenario.execute(algo_name="PYDOE_FULLFACT", n_samples=25) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:36:15: INFO - 08:36:15: *** Start DOEScenario execution *** INFO - 08:36:15: DOEScenario INFO - 08:36:15: Disciplines: AnalyticDiscipline INFO - 08:36:15: MDO formulation: DisciplinaryOpt DOEScenario(RUNNING) INFO - 08:36:15: Optimization problem: INFO - 08:36:15: minimize y(x1, x2) INFO - 08:36:15: with respect to x1, x2 INFO - 08:36:15: over the design space: INFO - 08:36:15: +------+-------------+-------+-------------+-------+ INFO - 08:36:15: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:36:15: +------+-------------+-------+-------------+-------+ INFO - 08:36:15: | x1 | 0 | 0 | 1 | float | INFO - 08:36:15: | x2 | 0 | 0 | 1 | float | INFO - 08:36:15: +------+-------------+-------+-------------+-------+ INFO - 08:36:15: Solving optimization problem with algorithm PYDOE_FULLFACT: AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 4%|▍ | 1/25 [00:00<00:00, 1707.78 it/sec, obj=0] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 8%|▊ | 2/25 [00:00<00:00, 1771.99 it/sec, obj=0.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 12%|█▏ | 3/25 [00:00<00:00, 1806.07 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 16%|█▌ | 4/25 [00:00<00:00, 1831.77 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 20%|██ | 5/25 [00:00<00:00, 1854.25 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 24%|██▍ | 6/25 [00:00<00:00, 1867.73 it/sec, obj=0.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 28%|██▊ | 7/25 [00:00<00:00, 1881.46 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 32%|███▏ | 8/25 [00:00<00:00, 1863.62 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 36%|███▌ | 9/25 [00:00<00:00, 1869.58 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 40%|████ | 10/25 [00:00<00:00, 1877.74 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 44%|████▍ | 11/25 [00:00<00:00, 1879.55 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 48%|████▊ | 12/25 [00:00<00:00, 1877.07 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 52%|█████▏ | 13/25 [00:00<00:00, 1878.33 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 56%|█████▌ | 14/25 [00:00<00:00, 1883.39 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 60%|██████ | 15/25 [00:00<00:00, 1888.87 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 64%|██████▍ | 16/25 [00:00<00:00, 1886.25 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 68%|██████▊ | 17/25 [00:00<00:00, 1889.58 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 72%|███████▏ | 18/25 [00:00<00:00, 1890.79 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 76%|███████▌ | 19/25 [00:00<00:00, 1890.09 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 80%|████████ | 20/25 [00:00<00:00, 1892.91 it/sec, obj=1.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 84%|████████▍ | 21/25 [00:00<00:00, 1893.43 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 88%|████████▊ | 22/25 [00:00<00:00, 1895.46 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 92%|█████████▏| 23/25 [00:00<00:00, 1894.45 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 96%|█████████▌| 24/25 [00:00<00:00, 1893.27 it/sec, obj=1.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 08:36:15: 100%|██████████| 25/25 [00:00<00:00, 1895.47 it/sec, obj=2] INFO - 08:36:15: Optimization result: INFO - 08:36:15: Optimizer info: INFO - 08:36:15: Status: None INFO - 08:36:15: Message: None INFO - 08:36:15: Number of calls to the objective function by the optimizer: 25 INFO - 08:36:15: Solution: INFO - 08:36:15: Objective: 0.0 INFO - 08:36:15: Design space: INFO - 08:36:15: +------+-------------+-------+-------------+-------+ INFO - 08:36:15: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:36:15: +------+-------------+-------+-------------+-------+ INFO - 08:36:15: | x1 | 0 | 0 | 1 | float | INFO - 08:36:15: | x2 | 0 | 0 | 1 | float | INFO - 08:36:15: +------+-------------+-------+-------------+-------+ DOEScenario(DONE) INFO - 08:36:15: *** End DOEScenario execution (time: 0:00:00.017261) *** .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.683 seconds) .. _sphx_glr_download_examples_api_plot_scenario.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_scenario.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_scenario.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_scenario.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_