.. 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 a MDO Scenario ===================== .. GENERATED FROM PYTHON SOURCE LINES 28-46 .. code-block:: default from __future__ import absolute_import, division, print_function, unicode_literals from future import standard_library from numpy import ones from gemseo.api import ( configure_logger, create_design_space, create_discipline, create_scenario, get_available_opt_algorithms, get_available_post_processings, ) configure_logger() standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 47-68 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 :meth:`~gemseo.api.create_discipline` API function, we create a :class:`.MDODiscipline` of :class:`.AnalyticDiscipline` type from a python function: .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: default expressions_dict = {"y": "sin(x)-exp(x)"} discipline = create_discipline("AnalyticDiscipline", expressions_dict=expressions_dict) .. 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 :meth:`~gemseo.api.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 DOE scenario ----------------------- Then, by means of the :meth:`~gemseo.api.create_scenario` API function, we define a :class:`.DOEScenario` 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-104 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 104-107 .. code-block:: default scenario.execute({"algo": "L-BFGS-B", "max_iter": 100}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'algo': 'L-BFGS-B', 'max_iter': 100} .. GENERATED FROM PYTHON SOURCE LINES 108-112 The optimum results can be found in the execution log. It is also possible to extract them by invoking the :meth:`.Scenario.get_optimum` method. It returns a dictionary containing the optimum results for the scenario under consideration: .. GENERATED FROM PYTHON SOURCE LINES 112-120 .. code-block:: default opt_results = scenario.get_optimum() print( "The solution of P is (x*,f(x*)) = ({}, {})".format( opt_results.x_opt, opt_results.f_opt ), ) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none The solution of P is (x*,f(x*)) = ([-1.29269572], -1.2361083418592418) .. GENERATED FROM PYTHON SOURCE LINES 121-131 .. seealso:: You can found the `scipy `_ 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 131-134 .. code-block:: default algo_list = get_available_opt_algorithms() print("Available algorithms: {}".format(algo_list)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Available algorithms: ['NLOPT_MMA', 'NLOPT_COBYLA', 'NLOPT_SLSQP', 'NLOPT_BOBYQA', 'NLOPT_BFGS', 'NLOPT_NEWUOA', 'SLSQP', 'L-BFGS-B', 'TNC'] .. GENERATED FROM PYTHON SOURCE LINES 135-138 Available post-processing ------------------------- In order to get the list of available post-processing algorithms, use: .. GENERATED FROM PYTHON SOURCE LINES 138-140 .. code-block:: default post_list = get_available_post_processings() print("Available algorithms: {}".format(post_list)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Available algorithms: ['BasicHistory', 'ConstraintsHistory', 'Correlations', 'GradientSensitivity', 'KMeans', 'ObjConstrHist', 'OptHistoryView', 'ParallelCoordinates', 'ParetoFront', 'QuadApprox', 'RadarChart', 'Robustness', 'SOM', 'ScatterPlotMatrix', 'VariableInfluence'] .. GENERATED FROM PYTHON SOURCE LINES 141-147 You can also look at the examples: .. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.042 seconds) .. _sphx_glr_download_examples_scenario_plot_mdo_scenario.py: .. only :: html .. container:: sphx-glr-footer :class: 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 `_