.. 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 Click :ref:`here ` 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 28-33 In this example, we consider a simple optimization problem to illustrate algorithms interfaces and :class:`.MDOFunction`. Imports ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-44 .. code-block:: default from __future__ import division, unicode_literals from numpy import cos, exp, ones, sin from scipy import optimize from gemseo.api import configure_logger from gemseo.core.mdofunctions.mdo_function import MDOFunction configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-49 Define the objective function ----------------------------- We define the objective function :math:`f(x)=sin(x)-exp(x)` using a :class:`.MDOFunction` defined by the sum of :class:`.MDOFunction` s. .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: default 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 55-60 .. seealso:: The following operators are implemented: :math:`+`, :math:`-` and :math:`*`. The minus operator is also defined. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: default print("Objective function = ", objective) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Objective function = 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-79 .. code-block:: default x_0 = -ones(1) opt = optimize.fmin_l_bfgs_b(objective, x_0, fprime=objective.jac, bounds=[(-0.2, 2.0)]) print("Optimum = ", opt) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Optimum = (array([-0.2]), array([-1.01740008]), {'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 :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simple_opt_1.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simple_opt_1.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_