.. 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 :ref:`Go to the end ` 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-41 .. code-block:: Python from __future__ import annotations from numpy import ones 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_opt_algorithms from gemseo import get_available_post_processings configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 42-63 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 high-level function :func:`.create_discipline`, we create an :class:`.Discipline` of :class:`.AnalyticDiscipline` type from a Python function: .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python expressions = {"y": "sin(x)-exp(x)"} discipline = create_discipline("AnalyticDiscipline", expressions=expressions) .. GENERATED FROM PYTHON SOURCE LINES 68-71 We can quickly access the most relevant information of any discipline (name, inputs, and outputs) with their string representations. Moreover, we can get the default input values of a discipline with the attribute :attr:`.Discipline.default_input_data` .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python discipline, discipline.default_input_data .. rst-class:: sphx-glr-script-out .. code-block:: none (AnalyticDiscipline Inputs: x Outputs: y, {'x': array([0.])}) .. GENERATED FROM PYTHON SOURCE LINES 74-82 Now, we can minimize an output of this :class:`.Discipline` 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 high-level function :func:`.create_design_space`, 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 82-86 .. code-block:: Python design_space = create_design_space() design_space.add_variable("x", lower_bound=-2.0, upper_bound=2.0, value=-0.5 * ones(1)) .. GENERATED FROM PYTHON SOURCE LINES 87-92 Define the MDO scenario ----------------------- Then, by means of the :func:`.create_scenario` API function, we define an :class:`.MDOScenario` from the :class:`.Discipline` and the :class:`.DesignSpace` defined above: .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: Python scenario = create_scenario( discipline, "y", design_space, formulation_name="DisciplinaryOpt" ) .. GENERATED FROM PYTHON SOURCE LINES 98-110 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 112-119 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 ``scipy.optimize.fmin_l_bfgs_b``. .. GENERATED FROM PYTHON SOURCE LINES 119-122 .. code-block:: Python scenario.execute(algo_name="L-BFGS-B", max_iter=100) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:36:18: INFO - 08:36:18: *** Start MDOScenario execution *** INFO - 08:36:18: MDOScenario INFO - 08:36:18: Disciplines: AnalyticDiscipline INFO - 08:36:18: MDO formulation: DisciplinaryOpt INFO - 08:36:18: Optimization problem: INFO - 08:36:18: minimize y(x) INFO - 08:36:18: with respect to x INFO - 08:36:18: over the design space: INFO - 08:36:18: +------+-------------+-------+-------------+-------+ INFO - 08:36:18: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:36:18: +------+-------------+-------+-------------+-------+ INFO - 08:36:18: | x | -2 | -0.5 | 2 | float | INFO - 08:36:18: +------+-------------+-------+-------------+-------+ INFO - 08:36:18: Solving optimization problem with algorithm L-BFGS-B: INFO - 08:36:18: 1%| | 1/100 [00:00<00:00, 388.94 it/sec, obj=-1.09] INFO - 08:36:18: 2%|▏ | 2/100 [00:00<00:00, 477.00 it/sec, obj=-1.04] INFO - 08:36:18: 3%|▎ | 3/100 [00:00<00:00, 573.31 it/sec, obj=-1.24] INFO - 08:36:18: 4%|▍ | 4/100 [00:00<00:00, 597.59 it/sec, obj=-1.23] INFO - 08:36:18: 5%|▌ | 5/100 [00:00<00:00, 617.01 it/sec, obj=-1.24] INFO - 08:36:18: 6%|▌ | 6/100 [00:00<00:00, 609.90 it/sec, obj=-1.24] INFO - 08:36:18: 7%|▋ | 7/100 [00:00<00:00, 621.67 it/sec, obj=-1.24] INFO - 08:36:18: Optimization result: INFO - 08:36:18: Optimizer info: INFO - 08:36:18: Status: 0 INFO - 08:36:18: Message: CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL INFO - 08:36:18: Number of calls to the objective function by the optimizer: 8 INFO - 08:36:18: Solution: INFO - 08:36:18: Objective: -1.2361083418592416 INFO - 08:36:18: Design space: INFO - 08:36:18: +------+-------------+--------------------+-------------+-------+ INFO - 08:36:18: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:36:18: +------+-------------+--------------------+-------------+-------+ INFO - 08:36:18: | x | -2 | -1.292695718944152 | 2 | float | INFO - 08:36:18: +------+-------------+--------------------+-------------+-------+ INFO - 08:36:18: *** End MDOScenario execution (time: 0:00:00.015763) *** .. GENERATED FROM PYTHON SOURCE LINES 123-125 The optimum results can be found in the execution log. It is also possible to access them with :attr:`.Scenario.optimization_result`: .. GENERATED FROM PYTHON SOURCE LINES 125-129 .. code-block:: Python optimization_result = scenario.optimization_result f"The solution of P is (x*, f(x*)) = ({optimization_result.x_opt}, {optimization_result.f_opt})" .. rst-class:: sphx-glr-script-out .. code-block:: none 'The solution of P is (x*, f(x*)) = ([-1.29269572], -1.2361083418592416)' .. GENERATED FROM PYTHON SOURCE LINES 130-140 .. seealso:: You can find the `SciPy `_ implementation of the `L-BFGS-B algorithm `_ algorithm `by clicking here `_. Available algorithms -------------------- In order to get the list of available optimization algorithms, use: .. GENERATED FROM PYTHON SOURCE LINES 140-142 .. code-block:: Python get_available_opt_algorithms() .. rst-class:: sphx-glr-script-out .. code-block:: none ['Augmented_Lagrangian_order_0', 'Augmented_Lagrangian_order_1', 'MNBI', 'MultiStart', 'NLOPT_MMA', 'NLOPT_COBYLA', 'NLOPT_SLSQP', 'NLOPT_BOBYQA', 'NLOPT_BFGS', 'NLOPT_NEWUOA', 'DUAL_ANNEALING', 'SHGO', 'DIFFERENTIAL_EVOLUTION', 'INTERIOR_POINT', 'DUAL_SIMPLEX', 'Scipy_MILP', 'SLSQP', 'L-BFGS-B', 'TNC', 'NELDER-MEAD'] .. GENERATED FROM PYTHON SOURCE LINES 143-146 Available post-processing ------------------------- In order to get the list of available post-processing algorithms, use: .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python get_available_post_processings() .. rst-class:: sphx-glr-script-out .. code-block:: none ['Animation', 'BasicHistory', 'ConstraintsHistory', 'Correlations', 'GradientSensitivity', 'HessianHistory', 'ObjConstrHist', 'OptHistoryView', 'ParallelCoordinates', 'ParetoFront', 'QuadApprox', 'RadarChart', 'Robustness', 'SOM', 'ScatterPlotMatrix', 'TopologyView', 'VariableInfluence'] .. GENERATED FROM PYTHON SOURCE LINES 149-154 Exporting the problem data. --------------------------- After the execution of the scenario, you may want to export your data to use it elsewhere. The :meth:`.Scenario.to_dataset` will allow you to export your results to a :class:`.Dataset`, the basic |g| class to store data. .. GENERATED FROM PYTHON SOURCE LINES 154-156 .. code-block:: Python dataset = scenario.to_dataset("a_name_for_my_dataset") .. GENERATED FROM PYTHON SOURCE LINES 157-163 You can also look at the examples: .. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.038 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-jupyter :download:`Download Jupyter notebook: plot_mdo_scenario.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mdo_scenario.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mdo_scenario.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_