.. 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/draft-04/schema', 'type': 'object', 'properties': {'linearization_mode': {'description': 'Linearization mode', 'enum': ['auto', 'direct', 'reverse', 'adjoint'], 'type': 'string'}, 'jac_approx_type': {'description': 'Jacobian approximation type', 'enum': ['finite_differences', 'complex_step'], 'type': 'string'}, 'jax_approx_step': {'minimum': 0, 'exclusiveMinimum': True, 'description': 'Step for finite differences or complex step for Jacobian approximation', 'type': 'number'}, 'jac_approx_use_threading': {'description': 'if True, use Threads instead of processes\n to parallelize the execution. \nMultiprocessing will serialize all the disciplines, \nwhile multithreading will share all the memory.\n This is important to note if you want to execute the same\n discipline multiple times, you shall use multiprocessing', 'type': 'boolean'}, 'jac_approx_wait_time': {'description': 'Time waited between two forks of the process or thread when using parallel jacobian approximations (parallel=True)', 'minimum': 0, 'type': 'number'}, 'jac_approx_n_processes': {'minimum': 1, 'description': 'maximum number of processors or threads on \nwhich the jacobian approximation is performed\n by default, 1 means no parallel calculations', 'type': 'integer'}, 'cache_type': {'description': 'Type of cache to be used. \nBy default, simple cache stores the last execution inputs and outputs \nin memory only to avoid computation of the outputs if the inputs are identical.\n To store more executions, use HDF5 caches, which stores data on the disk.\n There is a hashing mechanism which avoids reading on the disk for every calculation.', 'type': 'string'}, 'cache_tolerance': {'minimum': 0, 'description': 'Numerical tolerance on the relative norm of input vectors \n to consider that two sets of inputs are equal, and that the outputs may therefore be returned from the cache without calculations.', 'type': 'number'}, 'cache_hdf_file': {'format': 'uri', 'description': 'Path to the HDF5 file to store the cache data.', 'type': 'string'}, 'cache_hdf_node_path': {'description': 'Name of the HDF dataset to store the discipline\n data. If ``None``, the discipline name is used.', 'type': 'string'}, 'name': {'description': 'The name to be given to this scenario. If ``None``, use the name of the class.', 'type': 'null'}, 'grammar_type': {'description': 'The grammar for the scenario and the MDO formulation.', 'type': 'string'}, 'maximize_objective': {'description': 'Whether to maximize the objective.', 'type': 'boolean'}}} .. GENERATED FROM PYTHON SOURCE LINES 66-90 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:`.MDODiscipline` (or possibly, a single :class:`.MDODiscipline`), - ``formulation``: the formulation name, - ``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 - The other arguments are optional: - ``name``: scenario name, - ``scenario_type``: type of scenario, either `"MDO"` (default) or `"DOE"` , - ``**options``: options passed to the formulation. - This function returns an instance of :class:`.MDOScenario` or :class:`.DOEScenario`. .. GENERATED FROM PYTHON SOURCE LINES 90-107 .. 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, "DisciplinaryOpt", "y", design_space, scenario_type="DOE" ) scenario.execute({"algo": "fullfact", "n_samples": 25}) scenario.post_process( "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 - 10:51:44: INFO - 10:51:44: *** Start DOEScenario execution *** INFO - 10:51:44: DOEScenario INFO - 10:51:44: Disciplines: AnalyticDiscipline INFO - 10:51:44: MDO formulation: DisciplinaryOpt INFO - 10:51:44: Optimization problem: INFO - 10:51:44: minimize y(x1, x2) INFO - 10:51:44: with respect to x1, x2 INFO - 10:51:44: over the design space: INFO - 10:51:44: +------+-------------+-------+-------------+-------+ INFO - 10:51:44: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:51:44: +------+-------------+-------+-------------+-------+ INFO - 10:51:44: | x1 | 0 | None | 1 | float | INFO - 10:51:44: | x2 | 0 | None | 1 | float | INFO - 10:51:44: +------+-------------+-------+-------------+-------+ INFO - 10:51:44: Solving optimization problem with algorithm fullfact: INFO - 10:51:44: 4%|▍ | 1/25 [00:00<00:00, 333.78 it/sec, obj=0] INFO - 10:51:44: 8%|▊ | 2/25 [00:00<00:00, 541.03 it/sec, obj=0.25] INFO - 10:51:44: 12%|█▏ | 3/25 [00:00<00:00, 696.57 it/sec, obj=0.5] INFO - 10:51:44: 16%|█▌ | 4/25 [00:00<00:00, 812.06 it/sec, obj=0.75] INFO - 10:51:44: 20%|██ | 5/25 [00:00<00:00, 895.95 it/sec, obj=1] INFO - 10:51:44: 24%|██▍ | 6/25 [00:00<00:00, 970.19 it/sec, obj=0.25] INFO - 10:51:44: 28%|██▊ | 7/25 [00:00<00:00, 1032.14 it/sec, obj=0.5] INFO - 10:51:44: 32%|███▏ | 8/25 [00:00<00:00, 1084.68 it/sec, obj=0.75] INFO - 10:51:44: 36%|███▌ | 9/25 [00:00<00:00, 1129.39 it/sec, obj=1] INFO - 10:51:44: 40%|████ | 10/25 [00:00<00:00, 1168.23 it/sec, obj=1.25] INFO - 10:51:44: 44%|████▍ | 11/25 [00:00<00:00, 1198.93 it/sec, obj=0.5] INFO - 10:51:44: 48%|████▊ | 12/25 [00:00<00:00, 1219.87 it/sec, obj=0.75] INFO - 10:51:44: 52%|█████▏ | 13/25 [00:00<00:00, 1245.43 it/sec, obj=1] INFO - 10:51:44: 56%|█████▌ | 14/25 [00:00<00:00, 1268.78 it/sec, obj=1.25] INFO - 10:51:44: 60%|██████ | 15/25 [00:00<00:00, 1290.03 it/sec, obj=1.5] INFO - 10:51:44: 64%|██████▍ | 16/25 [00:00<00:00, 1309.34 it/sec, obj=0.75] INFO - 10:51:44: 68%|██████▊ | 17/25 [00:00<00:00, 1303.84 it/sec, obj=1] INFO - 10:51:44: 72%|███████▏ | 18/25 [00:00<00:00, 1304.51 it/sec, obj=1.25] INFO - 10:51:44: 76%|███████▌ | 19/25 [00:00<00:00, 1318.72 it/sec, obj=1.5] INFO - 10:51:44: 80%|████████ | 20/25 [00:00<00:00, 1332.82 it/sec, obj=1.75] INFO - 10:51:44: 84%|████████▍ | 21/25 [00:00<00:00, 1345.95 it/sec, obj=1] INFO - 10:51:44: 88%|████████▊ | 22/25 [00:00<00:00, 1358.20 it/sec, obj=1.25] INFO - 10:51:44: 92%|█████████▏| 23/25 [00:00<00:00, 1368.32 it/sec, obj=1.5] INFO - 10:51:44: 96%|█████████▌| 24/25 [00:00<00:00, 1375.86 it/sec, obj=1.75] INFO - 10:51:44: 100%|██████████| 25/25 [00:00<00:00, 1382.11 it/sec, obj=2] INFO - 10:51:44: Optimization result: INFO - 10:51:44: Optimizer info: INFO - 10:51:44: Status: None INFO - 10:51:44: Message: None INFO - 10:51:44: Number of calls to the objective function by the optimizer: 25 INFO - 10:51:44: Solution: INFO - 10:51:44: Objective: 0.0 INFO - 10:51:44: Design space: INFO - 10:51:44: +------+-------------+-------+-------------+-------+ INFO - 10:51:44: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:51:44: +------+-------------+-------+-------------+-------+ INFO - 10:51:44: | x1 | 0 | 0 | 1 | float | INFO - 10:51:44: | x2 | 0 | 0 | 1 | float | INFO - 10:51:44: +------+-------------+-------+-------------+-------+ INFO - 10:51:44: *** End DOEScenario execution (time: 0:00:00.031543) *** .. GENERATED FROM PYTHON SOURCE LINES 108-110 - The :func:`.get_scenario_inputs_schema` function can be used to get the inputs of a scenario: .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python get_scenario_inputs_schema(scenario) .. rst-class:: sphx-glr-script-out .. code-block:: none {'$schema': 'http://json-schema.org/draft-04/schema', 'type': 'object', 'properties': {'eval_jac': {'type': 'boolean'}, 'algo_options': {'type': 'object'}, 'n_samples': {'minimum': 1, 'type': 'integer'}, 'algo': {'enum': ['CustomDOE', 'DiagonalDOE', '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', 'fullfact', 'ff2n', 'pbdesign', 'bbdesign', 'ccdesign', 'lhs', 'Halton', 'LHS', 'MC', 'PoissonDisk', 'Sobol'], 'type': 'string'}}} .. GENERATED FROM PYTHON SOURCE LINES 113-117 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 117-119 .. code-block:: Python get_scenario_differentiation_modes() .. rst-class:: sphx-glr-script-out .. code-block:: none (, , , , ) .. GENERATED FROM PYTHON SOURCE LINES 120-132 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.AtomicExecSequence` 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 132-150 .. code-block:: Python class Observer: """Observer.""" def update(self, atom): """Update method. :param AtomicExecSequence atom: atomic execution sequence. """ print(atom) scenario = create_scenario( discipline, "DisciplinaryOpt", "y", design_space, scenario_type="DOE" ) monitor_scenario(scenario, Observer()) scenario.execute({"algo": "fullfact", "n_samples": 25}) .. rst-class:: sphx-glr-script-out .. code-block:: none DOEScenario(RUNNING) INFO - 10:51:45: INFO - 10:51:45: *** Start DOEScenario execution *** INFO - 10:51:45: DOEScenario INFO - 10:51:45: Disciplines: AnalyticDiscipline INFO - 10:51:45: MDO formulation: DisciplinaryOpt INFO - 10:51:45: Optimization problem: INFO - 10:51:45: minimize y(x1, x2) INFO - 10:51:45: with respect to x1, x2 INFO - 10:51:45: over the design space: INFO - 10:51:45: +------+-------------+-------+-------------+-------+ INFO - 10:51:45: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:51:45: +------+-------------+-------+-------------+-------+ INFO - 10:51:45: | x1 | 0 | 0 | 1 | float | INFO - 10:51:45: | x2 | 0 | 0 | 1 | float | INFO - 10:51:45: +------+-------------+-------+-------------+-------+ INFO - 10:51:45: Solving optimization problem with algorithm fullfact: AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 4%|▍ | 1/25 [00:00<00:00, 1238.72 it/sec, obj=0] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 8%|▊ | 2/25 [00:00<00:00, 1262.77 it/sec, obj=0.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 12%|█▏ | 3/25 [00:00<00:00, 1294.01 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 16%|█▌ | 4/25 [00:00<00:00, 1304.60 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 20%|██ | 5/25 [00:00<00:00, 1293.74 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 24%|██▍ | 6/25 [00:00<00:00, 1304.13 it/sec, obj=0.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 28%|██▊ | 7/25 [00:00<00:00, 1312.83 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 32%|███▏ | 8/25 [00:00<00:00, 1320.57 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 36%|███▌ | 9/25 [00:00<00:00, 1289.67 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 40%|████ | 10/25 [00:00<00:00, 1286.83 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 44%|████▍ | 11/25 [00:00<00:00, 1292.58 it/sec, obj=0.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 48%|████▊ | 12/25 [00:00<00:00, 1297.91 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 52%|█████▏ | 13/25 [00:00<00:00, 1302.33 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 56%|█████▌ | 14/25 [00:00<00:00, 1304.23 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 60%|██████ | 15/25 [00:00<00:00, 1301.53 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 64%|██████▍ | 16/25 [00:00<00:00, 1305.06 it/sec, obj=0.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 68%|██████▊ | 17/25 [00:00<00:00, 1308.15 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 72%|███████▏ | 18/25 [00:00<00:00, 1311.33 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 76%|███████▌ | 19/25 [00:00<00:00, 1314.14 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 80%|████████ | 20/25 [00:00<00:00, 1311.17 it/sec, obj=1.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 84%|████████▍ | 21/25 [00:00<00:00, 1312.75 it/sec, obj=1] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 88%|████████▊ | 22/25 [00:00<00:00, 1315.30 it/sec, obj=1.25] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 92%|█████████▏| 23/25 [00:00<00:00, 1317.59 it/sec, obj=1.5] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 96%|█████████▌| 24/25 [00:00<00:00, 1320.07 it/sec, obj=1.75] AnalyticDiscipline(RUNNING) AnalyticDiscipline(PENDING) INFO - 10:51:45: 100%|██████████| 25/25 [00:00<00:00, 1320.77 it/sec, obj=2] INFO - 10:51:45: Optimization result: INFO - 10:51:45: Optimizer info: INFO - 10:51:45: Status: None INFO - 10:51:45: Message: None INFO - 10:51:45: Number of calls to the objective function by the optimizer: 25 INFO - 10:51:45: Solution: INFO - 10:51:45: Objective: 0.0 INFO - 10:51:45: Design space: INFO - 10:51:45: +------+-------------+-------+-------------+-------+ INFO - 10:51:45: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:51:45: +------+-------------+-------+-------------+-------+ INFO - 10:51:45: | x1 | 0 | 0 | 1 | float | INFO - 10:51:45: | x2 | 0 | 0 | 1 | float | INFO - 10:51:45: +------+-------------+-------+-------------+-------+ INFO - 10:51:45: *** End DOEScenario execution (time: 0:00:00.030718) *** AnalyticDiscipline(DONE) DOEScenario(DONE) {'eval_jac': False, 'n_samples': 25, 'algo': 'fullfact'} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.783 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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_