{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Analytical test case # 1\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In this example, we consider a simple optimization problem to illustrate\nalgorithms interfaces and :class:`.MDOFunction`.\n\n## Imports\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import division, unicode_literals\n\nfrom numpy import cos, exp, ones, sin\nfrom scipy import optimize\n\nfrom gemseo.api import configure_logger\nfrom gemseo.core.function import MDOFunction\n\nconfigure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Define the objective function\nWe define the objective function $f(x)=sin(x)-exp(x)$\nusing a :class:`.MDOFunction` defined by the sum of :class:`.MDOFunction` s.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "f_1 = MDOFunction(sin, name=\"f_1\", jac=cos, expr=\"sin(x)\")\nf_2 = MDOFunction(exp, name=\"f_2\", jac=exp, expr=\"exp(x)\")\nobjective = f_1 - f_2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. seealso::\n\n   The following operators are implemented: $+$, $-$\n   and $*$. The minus operator is also defined.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(\"Objective function = \", objective)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Minimize the objective function\nWe want to minimize this objective function over $[-2,2]$,\nstarting from 1.\nWe use scipy.optimize for illustration.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>:class:`.MDOFunction` objects are callable like a Python function.</p></div>\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x_0 = -ones(1)\nopt = optimize.fmin_l_bfgs_b(objective, x_0, fprime=objective.jac, bounds=[(-0.2, 2.0)])\n\nprint(\"Optimum = \", opt)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}