.. 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-37 .. 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 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 .. GENERATED FROM PYTHON SOURCE LINES 38-52 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:`.get_available_scenario_types` can be used to get the available scenario types (:class:`.MDOScenario` and :class:`.DOEScenario`). .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python get_available_scenario_types() .. rst-class:: sphx-glr-script-out .. code-block:: none ['MDO', 'DOE'] .. GENERATED FROM PYTHON SOURCE LINES 55-59 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 59-61 .. 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 62-88 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 88-109 .. 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 - 16:24:59: *** Start DOEScenario execution *** INFO - 16:24:59: DOEScenario INFO - 16:24:59: Disciplines: AnalyticDiscipline INFO - 16:24:59: MDO formulation: DisciplinaryOpt INFO - 16:24:59: Optimization problem: INFO - 16:24:59: minimize y(x1, x2) INFO - 16:24:59: with respect to x1, x2 INFO - 16:24:59: over the design space: INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | x1 | 0 | None | 1 | float | INFO - 16:24:59: | x2 | 0 | None | 1 | float | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: Solving optimization problem with algorithm PYDOE_FULLFACT: INFO - 16:24:59: 4%|▍ | 1/25 [00:00<00:00, 559.02 it/sec, feas=True, obj=0] INFO - 16:24:59: 8%|▊ | 2/25 [00:00<00:00, 953.90 it/sec, feas=True, obj=0.25] INFO - 16:24:59: 12%|█▏ | 3/25 [00:00<00:00, 1286.86 it/sec, feas=True, obj=0.5] INFO - 16:24:59: 16%|█▌ | 4/25 [00:00<00:00, 1566.79 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 20%|██ | 5/25 [00:00<00:00, 1796.89 it/sec, feas=True, obj=1] INFO - 16:24:59: 24%|██▍ | 6/25 [00:00<00:00, 2000.94 it/sec, feas=True, obj=0.25] INFO - 16:24:59: 28%|██▊ | 7/25 [00:00<00:00, 2182.91 it/sec, feas=True, obj=0.5] INFO - 16:24:59: 32%|███▏ | 8/25 [00:00<00:00, 2344.33 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 36%|███▌ | 9/25 [00:00<00:00, 2488.71 it/sec, feas=True, obj=1] INFO - 16:24:59: 40%|████ | 10/25 [00:00<00:00, 2601.44 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 44%|████▍ | 11/25 [00:00<00:00, 2715.72 it/sec, feas=True, obj=0.5] INFO - 16:24:59: 48%|████▊ | 12/25 [00:00<00:00, 2822.86 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 52%|█████▏ | 13/25 [00:00<00:00, 2909.76 it/sec, feas=True, obj=1] INFO - 16:24:59: 56%|█████▌ | 14/25 [00:00<00:00, 2995.47 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 60%|██████ | 15/25 [00:00<00:00, 3063.47 it/sec, feas=True, obj=1.5] INFO - 16:24:59: 64%|██████▍ | 16/25 [00:00<00:00, 3139.89 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 68%|██████▊ | 17/25 [00:00<00:00, 3208.53 it/sec, feas=True, obj=1] INFO - 16:24:59: 72%|███████▏ | 18/25 [00:00<00:00, 3276.37 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 76%|███████▌ | 19/25 [00:00<00:00, 3336.90 it/sec, feas=True, obj=1.5] INFO - 16:24:59: 80%|████████ | 20/25 [00:00<00:00, 3378.42 it/sec, feas=True, obj=1.75] INFO - 16:24:59: 84%|████████▍ | 21/25 [00:00<00:00, 3430.46 it/sec, feas=True, obj=1] INFO - 16:24:59: 88%|████████▊ | 22/25 [00:00<00:00, 3480.36 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 92%|█████████▏| 23/25 [00:00<00:00, 3530.56 it/sec, feas=True, obj=1.5] INFO - 16:24:59: 96%|█████████▌| 24/25 [00:00<00:00, 3576.60 it/sec, feas=True, obj=1.75] INFO - 16:24:59: 100%|██████████| 25/25 [00:00<00:00, 3558.23 it/sec, feas=True, obj=2] INFO - 16:24:59: Optimization result: INFO - 16:24:59: Optimizer info: INFO - 16:24:59: Status: None INFO - 16:24:59: Message: None INFO - 16:24:59: Solution: INFO - 16:24:59: Objective: 0.0 INFO - 16:24:59: Design space: INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | x1 | 0 | 0 | 1 | float | INFO - 16:24:59: | x2 | 0 | 0 | 1 | float | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: *** End DOEScenario execution *** .. GENERATED FROM PYTHON SOURCE LINES 110-112 - The :func:`.get_scenario_inputs_schema` function can be used to get the inputs of a scenario: .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. 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': {'additionalProperties': True, 'description': 'The settings for the algorithm.', 'title': 'Algo Settings', 'type': 'object'}}, 'required': ['algo_name'], 'title': 'Settings', 'type': 'object'} .. GENERATED FROM PYTHON SOURCE LINES 115-119 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 119-121 .. code-block:: Python get_scenario_differentiation_modes() .. rst-class:: sphx-glr-script-out .. code-block:: none (, , , , ) .. GENERATED FROM PYTHON SOURCE LINES 122-134 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:`.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 134-156 .. 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 - 16:24:59: *** Start DOEScenario execution *** INFO - 16:24:59: DOEScenario INFO - 16:24:59: Disciplines: AnalyticDiscipline INFO - 16:24:59: MDO formulation: DisciplinaryOpt INFO - 16:24:59: Optimization problem: INFO - 16:24:59: minimize y(x1, x2) INFO - 16:24:59: with respect to x1, x2 INFO - 16:24:59: over the design space: INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | x1 | 0 | 0 | 1 | float | INFO - 16:24:59: | x2 | 0 | 0 | 1 | float | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: Solving optimization problem with algorithm PYDOE_FULLFACT: INFO - 16:24:59: 4%|▍ | 1/25 [00:00<00:00, 3518.71 it/sec, feas=True, obj=0] INFO - 16:24:59: 8%|▊ | 2/25 [00:00<00:00, 3517.24 it/sec, feas=True, obj=0.25] INFO - 16:24:59: 12%|█▏ | 3/25 [00:00<00:00, 3742.69 it/sec, feas=True, obj=0.5] INFO - 16:24:59: 16%|█▌ | 4/25 [00:00<00:00, 3925.41 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 20%|██ | 5/25 [00:00<00:00, 4050.90 it/sec, feas=True, obj=1] INFO - 16:24:59: 24%|██▍ | 6/25 [00:00<00:00, 4133.00 it/sec, feas=True, obj=0.25] INFO - 16:24:59: 28%|██▊ | 7/25 [00:00<00:00, 4134.06 it/sec, feas=True, obj=0.5] INFO - 16:24:59: 32%|███▏ | 8/25 [00:00<00:00, 4187.50 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 36%|███▌ | 9/25 [00:00<00:00, 4234.77 it/sec, feas=True, obj=1] INFO - 16:24:59: 40%|████ | 10/25 [00:00<00:00, 4287.78 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 44%|████▍ | 11/25 [00:00<00:00, 4290.65 it/sec, feas=True, obj=0.5] INFO - 16:24:59: 48%|████▊ | 12/25 [00:00<00:00, 4323.28 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 52%|█████▏ | 13/25 [00:00<00:00, 4363.12 it/sec, feas=True, obj=1] INFO - 16:24:59: 56%|█████▌ | 14/25 [00:00<00:00, 4403.47 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 60%|██████ | 15/25 [00:00<00:00, 4442.49 it/sec, feas=True, obj=1.5] INFO - 16:24:59: 64%|██████▍ | 16/25 [00:00<00:00, 4454.33 it/sec, feas=True, obj=0.75] INFO - 16:24:59: 68%|██████▊ | 17/25 [00:00<00:00, 4480.81 it/sec, feas=True, obj=1] INFO - 16:24:59: 72%|███████▏ | 18/25 [00:00<00:00, 4507.04 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 76%|███████▌ | 19/25 [00:00<00:00, 4528.71 it/sec, feas=True, obj=1.5] INFO - 16:24:59: 80%|████████ | 20/25 [00:00<00:00, 4551.85 it/sec, feas=True, obj=1.75] INFO - 16:24:59: 84%|████████▍ | 21/25 [00:00<00:00, 4553.37 it/sec, feas=True, obj=1] INFO - 16:24:59: 88%|████████▊ | 22/25 [00:00<00:00, 4570.09 it/sec, feas=True, obj=1.25] INFO - 16:24:59: 92%|█████████▏| 23/25 [00:00<00:00, 4595.07 it/sec, feas=True, obj=1.5] INFO - 16:24:59: 96%|█████████▌| 24/25 [00:00<00:00, 4616.10 it/sec, feas=True, obj=1.75] INFO - 16:24:59: 100%|██████████| 25/25 [00:00<00:00, 4561.01 it/sec, feas=True, obj=2] INFO - 16:24:59: Optimization result: INFO - 16:24:59: Optimizer info: INFO - 16:24:59: Status: None INFO - 16:24:59: Message: None INFO - 16:24:59: Solution: INFO - 16:24:59: Objective: 0.0 INFO - 16:24:59: Design space: INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: | x1 | 0 | 0 | 1 | float | INFO - 16:24:59: | x2 | 0 | 0 | 1 | float | INFO - 16:24:59: +------+-------------+-------+-------------+-------+ INFO - 16:24:59: *** End DOEScenario execution *** .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.342 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 `_