.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/scenario/plot_mdo_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_scenario_plot_mdo_scenario.py: Create an MDO Scenario ====================== .. GENERATED FROM PYTHON SOURCE LINES 26-39 .. code-block:: default from __future__ import annotations from gemseo.api import configure_logger from gemseo.api import create_design_space from gemseo.api import create_discipline from gemseo.api import create_scenario from gemseo.api import get_available_opt_algorithms from gemseo.api import get_available_post_processings from numpy import ones configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 40-61 Let :math:`(P)` be a simple optimization problem: .. math:: (P) = \left\{ \begin{aligned} & \underset{x}{\text{minimize}} & & f(x) = \sin(x) - \exp(x) \\ & \text{subject to} & & -2 \leq x \leq 2 \end{aligned} \right. In this subsection, we will see how to use |g| to solve this problem :math:`(P)` by means of an optimization algorithm. Define the discipline --------------------- Firstly, by means of the :func:`.create_discipline` API function, we create an :class:`.MDODiscipline` of :class:`.AnalyticDiscipline` type from a Python function: .. GENERATED FROM PYTHON SOURCE LINES 61-65 .. code-block:: default expressions = {"y": "sin(x)-exp(x)"} discipline = create_discipline("AnalyticDiscipline", expressions=expressions) .. GENERATED FROM PYTHON SOURCE LINES 66-69 We can quickly access the most relevant information of any discipline (name, inputs, and outputs) with Python's ``print()`` function. Moreover, we can get the default input values of a discipline with the attribute :attr:`.MDODiscipline.default_inputs` .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default print(discipline) print(f"Default inputs: {discipline.default_inputs}") .. rst-class:: sphx-glr-script-out .. code-block:: none AnalyticDiscipline Default inputs: {'x': array([0.])} .. GENERATED FROM PYTHON SOURCE LINES 73-81 Now, we can to minimize this :class:`.MDODiscipline` over a design space, by means of a quasi-Newton method from the initial point :math:`0.5`. Define the design space ----------------------- For that, by means of the :func:`.create_design_space` API function, we define the :class:`.DesignSpace` :math:`[-2, 2]` with initial value :math:`0.5` by using its :meth:`.DesignSpace.add_variable` method. .. GENERATED FROM PYTHON SOURCE LINES 81-85 .. code-block:: default design_space = create_design_space() design_space.add_variable("x", 1, l_b=-2.0, u_b=2.0, value=-0.5 * ones(1)) .. GENERATED FROM PYTHON SOURCE LINES 86-91 Define the MDO scenario ----------------------- Then, by means of the :func:`.create_scenario` API function, we define an :class:`.MDOScenario` from the :class:`.MDODiscipline` and the :class:`.DesignSpace` defined above: .. GENERATED FROM PYTHON SOURCE LINES 91-96 .. code-block:: default scenario = create_scenario( discipline, "DisciplinaryOpt", "y", design_space, scenario_type="MDO" ) .. GENERATED FROM PYTHON SOURCE LINES 97-109 What about the differentiation method? -------------------------------------- The :class:`.AnalyticDiscipline` automatically differentiates the expressions to obtain the Jacobian matrices. Therefore, there is no need to define a differentiation method in this case. Keep in mind that for a generic discipline with no defined Jacobian function, you can use the :meth:`.Scenario.set_differentiation_method` method to define a numerical approximation of the gradients. .. code:: scenario.set_differentiation_method("finite_differences") .. GENERATED FROM PYTHON SOURCE LINES 111-118 Execute the MDO scenario ------------------------ Lastly, we solve the :class:`.OptimizationProblem` included in the :class:`.MDOScenario` defined above by minimizing the objective function over the :class:`.DesignSpace`. Precisely, we choose the `L-BFGS-B algorithm `_ implemented in the function :code:`scipy.optimize.fmin_l_bfgs_b`. .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. code-block:: default scenario.execute({"algo": "L-BFGS-B", "max_iter": 100}) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 14:46:12: INFO - 14:46:12: *** Start MDOScenario execution *** INFO - 14:46:12: MDOScenario INFO - 14:46:12: Disciplines: AnalyticDiscipline INFO - 14:46:12: MDO formulation: DisciplinaryOpt INFO - 14:46:12: Optimization problem: INFO - 14:46:12: minimize y(x) INFO - 14:46:12: with respect to x INFO - 14:46:12: over the design space: INFO - 14:46:12: +------+-------------+-------+-------------+-------+ INFO - 14:46:12: | name | lower_bound | value | upper_bound | type | INFO - 14:46:12: +------+-------------+-------+-------------+-------+ INFO - 14:46:12: | x | -2 | -0.5 | 2 | float | INFO - 14:46:12: +------+-------------+-------+-------------+-------+ INFO - 14:46:12: Solving optimization problem with algorithm L-BFGS-B: INFO - 14:46:12: ... 0%| | 0/100 [00:00`_ implementation of the `L-BFGS-B algorithm `_ algorithm `by clicking here `_. # noqa Available algorithms -------------------- In order to get the list of available optimization algorithms, use: .. GENERATED FROM PYTHON SOURCE LINES 145-148 .. code-block:: default algo_list = get_available_opt_algorithms() print(f"Available algorithms: {algo_list}") .. rst-class:: sphx-glr-script-out .. code-block:: none Available algorithms: ['NLOPT_MMA', 'NLOPT_COBYLA', 'NLOPT_SLSQP', 'NLOPT_BOBYQA', 'NLOPT_BFGS', 'NLOPT_NEWUOA', 'PDFO_COBYLA', 'PDFO_BOBYQA', 'PDFO_NEWUOA', 'PSEVEN', 'PSEVEN_FD', 'PSEVEN_MOM', 'PSEVEN_NCG', 'PSEVEN_NLS', 'PSEVEN_POWELL', 'PSEVEN_QP', 'PSEVEN_SQP', 'PSEVEN_SQ2P', 'PYMOO_GA', 'PYMOO_NSGA2', 'PYMOO_NSGA3', 'PYMOO_UNSGA3', 'PYMOO_RNSGA3', 'DUAL_ANNEALING', 'SHGO', 'DIFFERENTIAL_EVOLUTION', 'LINEAR_INTERIOR_POINT', 'REVISED_SIMPLEX', 'SIMPLEX', 'SLSQP', 'L-BFGS-B', 'TNC', 'SNOPTB'] .. GENERATED FROM PYTHON SOURCE LINES 149-152 Available post-processing ------------------------- In order to get the list of available post-processing algorithms, use: .. GENERATED FROM PYTHON SOURCE LINES 152-155 .. code-block:: default post_list = get_available_post_processings() print(f"Available algorithms: {post_list}") .. rst-class:: sphx-glr-script-out .. code-block:: none Available algorithms: ['BasicHistory', 'Compromise', 'ConstraintsHistory', 'Correlations', 'GradientSensitivity', 'HighTradeOff', 'KMeans', 'MultiObjectiveDiagram', 'ObjConstrHist', 'OptHistoryView', 'ParallelCoordinates', 'ParetoFront', 'Petal', 'QuadApprox', 'Radar', 'RadarChart', 'Robustness', 'SOM', 'ScatterPareto', 'ScatterPlotMatrix', 'VariableInfluence'] .. GENERATED FROM PYTHON SOURCE LINES 156-163 Exporting the problem data. --------------------------- After the execution of the scenario, you may want to export your data to use it elsewhere. The :meth:`.Scenario.export_to_dataset` will allow you to export your results to a :class:`.Dataset`, the basic |g| class to store data. From a dataset, you can even obtain a Pandas dataframe with its method :meth:`~.Dataset.export_to_dataframe`: .. GENERATED FROM PYTHON SOURCE LINES 163-166 .. code-block:: default dataset = scenario.export_to_dataset("a_name_for_my_dataset") dataframe = dataset.export_to_dataframe() .. GENERATED FROM PYTHON SOURCE LINES 167-173 You can also look at the examples: .. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.046 seconds) .. _sphx_glr_download_examples_scenario_plot_mdo_scenario.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mdo_scenario.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mdo_scenario.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_