.. 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 Click :ref:`here ` 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 27-44 .. code-block:: default from __future__ import division, unicode_literals from gemseo.api import ( configure_logger, create_design_space, create_discipline, create_scenario, get_available_scenario_types, get_scenario_differentiation_modes, get_scenario_inputs_schema, get_scenario_options_schema, monitor_scenario, ) configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-59 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 API function :meth:`~gemseo.api.get_available_scenario_types` can be used to get the available scenario types (:class:`.MDOScenario` and :class:`.DOEScenario`). .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: default print(get_available_scenario_types()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['MDO', 'DOE'] .. GENERATED FROM PYTHON SOURCE LINES 62-66 Get scenario options schema --------------------------- The :meth:`~gemseo.api.get_scenario_options_schema` function can be used to get the options of a given scenario type: .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: default print(get_scenario_options_schema("MDO")) .. rst-class:: sphx-glr-script-out 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_name': {'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 type of grammar to use for IO declaration , e.g. JSON_GRAMMAR_TYPE or SIMPLE_GRAMMAR_TYPE.', 'type': 'string'}}, 'required': ['grammar_type']} .. GENERATED FROM PYTHON SOURCE LINES 69-93 Create a scenario ----------------- The API function :meth:`~gemseo.api.create_scenario` can be used to create a scenario: - The four first arguments are mandatory: - :code:`disciplines`: the list of :class:`.MDODiscipline` (or possibly, a single :class:`.MDODiscipline`), - :code:`formulation`: the formulation name, - :code:`objective_name`: the name of the objective function (one of the discipline outputs) - :code:`design_space`: the :class:`.DesignSpace` or the file path of the design space - The other arguments are optional: - :code:`name`: scenario name, - :code:`scenario_type`: type of scenario, either `"MDO"` (default) or `"DOE"` , - :code:`**options`: options passed to the formulation. - This function returns an instance of :class:`.MDOScenario` or :class:`.DOEScenario`. .. GENERATED FROM PYTHON SOURCE LINES 93-107 .. code-block:: default discipline = create_discipline("AnalyticDiscipline", expressions_dict={"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", variables_list=["x1", "x2", "y"], save=False, show=True ) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 14:42:45: INFO - 14:42:45: *** Start DOE Scenario execution *** INFO - 14:42:45: DOEScenario INFO - 14:42:45: Disciplines: AnalyticDiscipline INFO - 14:42:45: MDOFormulation: DisciplinaryOpt INFO - 14:42:45: Algorithm: fullfact INFO - 14:42:45: Optimization problem: INFO - 14:42:45: Minimize: y(x1, x2) INFO - 14:42:45: With respect to: x1, x2 INFO - 14:42:45: Full factorial design required. Number of samples along each direction for a design vector of size 2 with 25 samples: 5 INFO - 14:42:45: Final number of samples for DOE = 25 vs 25 requested INFO - 14:42:45: DOE sampling: 0%| | 0/25 [00:00 .. GENERATED FROM PYTHON SOURCE LINES 108-110 - The :meth:`~gemseo.api.get_scenario_inputs_schema` function can be used to get the inputs of a scenario: .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: default print(get_scenario_inputs_schema(scenario)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'$schema': 'http://json-schema.org/draft-04/schema', 'type': 'object', 'properties': {'eval_jac': {'type': 'boolean'}, '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'], 'type': 'string'}, 'algo_options': {'type': 'object'}, 'n_samples': {'minimum': 1, 'type': 'integer'}}, 'required': ['algo']} .. GENERATED FROM PYTHON SOURCE LINES 113-117 Get scenario differentiation modes ---------------------------------- The :meth:`~gemseo.api.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:: default print(get_scenario_differentiation_modes()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['user', 'complex_step', 'finite_differences', 'no_derivatives'] .. 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 :meth:`~gemseo.api.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:: default class Observer(object): """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 Out: .. code-block:: none DOEScenario(RUNNING) INFO - 14:42:46: INFO - 14:42:46: *** Start DOE Scenario execution *** INFO - 14:42:46: DOEScenario INFO - 14:42:46: Disciplines: AnalyticDiscipline INFO - 14:42:46: MDOFormulation: DisciplinaryOpt INFO - 14:42:46: Algorithm: fullfact INFO - 14:42:46: Optimization problem: INFO - 14:42:46: Minimize: y(x1, x2) INFO - 14:42:46: With respect to: x1, x2 INFO - 14:42:46: Full factorial design required. Number of samples along each direction for a design vector of size 2 with 25 samples: 5 INFO - 14:42:46: Final number of samples for DOE = 25 vs 25 requested INFO - 14:42:46: DOE sampling: 0%| | 0/25 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_scenario.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_