.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/optimization_problem/plot_simple_opt_1.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_optimization_problem_plot_simple_opt_1.py: Analytical test case # 1 ======================== .. GENERATED FROM PYTHON SOURCE LINES 26-31 In this example, we consider a simple optimization problem to illustrate algorithms interfaces and :class:`.MDOFunction`. Imports ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-45 .. code-block:: Python from __future__ import annotations from numpy import cos from numpy import exp from numpy import ones from numpy import sin from scipy import optimize from gemseo import configure_logger from gemseo.core.mdofunctions.mdo_function import MDOFunction configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 46-50 Define the objective function ----------------------------- We define the objective function :math:`f(x)=\sin(x)-\exp(x)` using an :class:`.MDOFunction` defined by the sum of :class:`.MDOFunction` s. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python f_1 = MDOFunction(sin, name="f_1", jac=cos, expr="sin(x)") f_2 = MDOFunction(exp, name="f_2", jac=exp, expr="exp(x)") objective = f_1 - f_2 .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. seealso:: The following operators are implemented: :math:`+`, :math:`-` and :math:`*`. The minus operator is also defined. .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python objective .. rst-class:: sphx-glr-script-out .. code-block:: none [f_1-f_2] = sin(x)-exp(x) .. GENERATED FROM PYTHON SOURCE LINES 64-74 Minimize the objective function ------------------------------- We want to minimize this objective function over :math:`[-2,2]`, starting from 1. We use scipy.optimize for illustration. .. note:: :class:`.MDOFunction` objects are callable like a Python function. .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: Python x_0 = -ones(1) opt = optimize.fmin_l_bfgs_b(objective, x_0, fprime=objective.jac, bounds=[(-0.2, 2.0)]) opt .. rst-class:: sphx-glr-script-out .. code-block:: none (array([-0.2]), -1.017400083873043, {'grad': array([0.16133582]), 'task': 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL', 'funcalls': 1, 'nit': 0, 'warnflag': 0}) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.003 seconds) .. _sphx_glr_download_examples_optimization_problem_plot_simple_opt_1.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simple_opt_1.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simple_opt_1.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_