.. 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': {'type': 'string', 'description': 'The name to be given to this scenario. If empty, use the name of the class.'}, 'maximize_objective': {'type': 'boolean', 'description': 'Whether to maximize the objective.'}, 'formulation_settings_model': {'type': 'null', 'description': 'The formulation settings as a Pydantic model. If ``None``, use ``**settings``.'}}} .. 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 - 11:43:10: *** Start DOEScenario execution *** INFO - 11:43:10: DOEScenario INFO - 11:43:10: Disciplines: AnalyticDiscipline INFO - 11:43:10: MDO formulation: DisciplinaryOpt INFO - 11:43:10: Optimization problem: INFO - 11:43:10: minimize y(x1, x2) INFO - 11:43:10: with respect to x1, x2 INFO - 11:43:10: over the design space: INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | Name | Lower bound | Value | Upper bound | Type | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | x1 | 0 | None | 1 | float | INFO - 11:43:10: | x2 | 0 | None | 1 | float | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: Solving optimization problem with algorithm PYDOE_FULLFACT: INFO - 11:43:10: 4%|▍ | 1/25 [00:00<00:00, 446.16 it/sec, obj=0] INFO - 11:43:10: 8%|▊ | 2/25 [00:00<00:00, 769.24 it/sec, obj=0.25] INFO - 11:43:10: 12%|█▏ | 3/25 [00:00<00:00, 1030.46 it/sec, obj=0.5] INFO - 11:43:10: 16%|█▌ | 4/25 [00:00<00:00, 1257.95 it/sec, obj=0.75] INFO - 11:43:10: 20%|██ | 5/25 [00:00<00:00, 1446.51 it/sec, obj=1] INFO - 11:43:10: 24%|██▍ | 6/25 [00:00<00:00, 1599.25 it/sec, obj=0.25] INFO - 11:43:10: 28%|██▊ | 7/25 [00:00<00:00, 1744.20 it/sec, obj=0.5] INFO - 11:43:10: 32%|███▏ | 8/25 [00:00<00:00, 1883.07 it/sec, obj=0.75] INFO - 11:43:10: 36%|███▌ | 9/25 [00:00<00:00, 2011.44 it/sec, obj=1] INFO - 11:43:10: 40%|████ | 10/25 [00:00<00:00, 2118.44 it/sec, obj=1.25] INFO - 11:43:10: 44%|████▍ | 11/25 [00:00<00:00, 2218.57 it/sec, obj=0.5] INFO - 11:43:10: 48%|████▊ | 12/25 [00:00<00:00, 2247.95 it/sec, obj=0.75] INFO - 11:43:10: 52%|█████▏ | 13/25 [00:00<00:00, 2253.70 it/sec, obj=1] INFO - 11:43:10: 56%|█████▌ | 14/25 [00:00<00:00, 2313.55 it/sec, obj=1.25] INFO - 11:43:10: 60%|██████ | 15/25 [00:00<00:00, 2381.14 it/sec, obj=1.5] INFO - 11:43:10: 64%|██████▍ | 16/25 [00:00<00:00, 2447.44 it/sec, obj=0.75] INFO - 11:43:10: 68%|██████▊ | 17/25 [00:00<00:00, 2500.99 it/sec, obj=1] INFO - 11:43:10: 72%|███████▏ | 18/25 [00:00<00:00, 2559.06 it/sec, obj=1.25] INFO - 11:43:10: 76%|███████▌ | 19/25 [00:00<00:00, 2612.50 it/sec, obj=1.5] INFO - 11:43:10: 80%|████████ | 20/25 [00:00<00:00, 2664.32 it/sec, obj=1.75] INFO - 11:43:10: 84%|████████▍ | 21/25 [00:00<00:00, 2706.50 it/sec, obj=1] INFO - 11:43:10: 88%|████████▊ | 22/25 [00:00<00:00, 2750.61 it/sec, obj=1.25] INFO - 11:43:10: 92%|█████████▏| 23/25 [00:00<00:00, 2797.01 it/sec, obj=1.5] INFO - 11:43:10: 96%|█████████▌| 24/25 [00:00<00:00, 2839.43 it/sec, obj=1.75] INFO - 11:43:10: 100%|██████████| 25/25 [00:00<00:00, 2872.26 it/sec, obj=2] INFO - 11:43:10: Optimization result: INFO - 11:43:10: Optimizer info: INFO - 11:43:10: Status: None INFO - 11:43:10: Message: None INFO - 11:43:10: Number of calls to the objective function by the optimizer: 25 INFO - 11:43:10: Solution: INFO - 11:43:10: Objective: 0.0 INFO - 11:43:10: Design space: INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | Name | Lower bound | Value | Upper bound | Type | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | x1 | 0 | 0 | 1 | float | INFO - 11:43:10: | x2 | 0 | 0 | 1 | float | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: *** End DOEScenario execution (time: 0:00:00.011125) *** .. 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 - 11:43:10: *** Start DOEScenario execution *** INFO - 11:43:10: DOEScenario INFO - 11:43:10: Disciplines: AnalyticDiscipline INFO - 11:43:10: MDO formulation: DisciplinaryOpt DOEScenario(RUNNING) INFO - 11:43:10: Optimization problem: INFO - 11:43:10: minimize y(x1, x2) INFO - 11:43:10: with respect to x1, x2 INFO - 11:43:10: over the design space: INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | Name | Lower bound | Value | Upper bound | Type | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | x1 | 0 | 0 | 1 | float | INFO - 11:43:10: | x2 | 0 | 0 | 1 | float | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: Solving optimization problem with algorithm PYDOE_FULLFACT: AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 4%|▍ | 1/25 [00:00<00:00, 2069.22 it/sec, obj=0] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 8%|▊ | 2/25 [00:00<00:00, 2316.02 it/sec, obj=0.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 12%|█▏ | 3/25 [00:00<00:00, 2437.13 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 16%|█▌ | 4/25 [00:00<00:00, 2591.88 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 20%|██ | 5/25 [00:00<00:00, 2709.50 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 24%|██▍ | 6/25 [00:00<00:00, 2673.52 it/sec, obj=0.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 28%|██▊ | 7/25 [00:00<00:00, 2728.13 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 32%|███▏ | 8/25 [00:00<00:00, 2759.18 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 36%|███▌ | 9/25 [00:00<00:00, 2800.35 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 40%|████ | 10/25 [00:00<00:00, 2849.20 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 44%|████▍ | 11/25 [00:00<00:00, 2893.71 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 48%|████▊ | 12/25 [00:00<00:00, 2908.67 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 52%|█████▏ | 13/25 [00:00<00:00, 2920.36 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 56%|█████▌ | 14/25 [00:00<00:00, 2947.21 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 60%|██████ | 15/25 [00:00<00:00, 2959.01 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 64%|██████▍ | 16/25 [00:00<00:00, 2956.34 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 68%|██████▊ | 17/25 [00:00<00:00, 2975.55 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 72%|███████▏ | 18/25 [00:00<00:00, 2978.32 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 76%|███████▌ | 19/25 [00:00<00:00, 2995.82 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 80%|████████ | 20/25 [00:00<00:00, 3011.20 it/sec, obj=1.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 84%|████████▍ | 21/25 [00:00<00:00, 3016.35 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 88%|████████▊ | 22/25 [00:00<00:00, 3030.27 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 92%|█████████▏| 23/25 [00:00<00:00, 3046.93 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 96%|█████████▌| 24/25 [00:00<00:00, 3063.87 it/sec, obj=1.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(DONE) INFO - 11:43:10: 100%|██████████| 25/25 [00:00<00:00, 3069.24 it/sec, obj=2] INFO - 11:43:10: Optimization result: INFO - 11:43:10: Optimizer info: INFO - 11:43:10: Status: None INFO - 11:43:10: Message: None INFO - 11:43:10: Number of calls to the objective function by the optimizer: 25 INFO - 11:43:10: Solution: INFO - 11:43:10: Objective: 0.0 INFO - 11:43:10: Design space: INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | Name | Lower bound | Value | Upper bound | Type | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ INFO - 11:43:10: | x1 | 0 | 0 | 1 | float | INFO - 11:43:10: | x2 | 0 | 0 | 1 | float | INFO - 11:43:10: +------+-------------+-------+-------------+-------+ DOEScenario(DONE) INFO - 11:43:10: *** End DOEScenario execution (time: 0:00:00.010658) *** .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.532 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 `_