{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Quadratic approximations\n\nIn this example, we illustrate the use of the :class:`.QuadApprox` plot\non the Sobieski's SSBJ problem.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from gemseo.api import configure_logger\nfrom gemseo.api import create_discipline\nfrom gemseo.api import create_scenario\nfrom gemseo.problems.sobieski.core.problem import SobieskiProblem\nfrom matplotlib import pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Import\nThe first step is to import some functions from the API\nand a method to get the design space.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "configure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Description\n\nThe :class:`~gemseo.post.quad_approx.QuadApprox` post-processing\nperforms a quadratic approximation of a given function\nfrom an optimization history\nand plot the results as cuts of the approximation.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create disciplines\nThen, we instantiate the disciplines of the Sobieski's SSBJ problem:\nPropulsion, Aerodynamics, Structure and Mission\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "disciplines = create_discipline(\n    [\n        \"SobieskiPropulsion\",\n        \"SobieskiAerodynamics\",\n        \"SobieskiStructure\",\n        \"SobieskiMission\",\n    ]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create design space\nWe also read the design space from the :class:`.SobieskiProblem`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "design_space = SobieskiProblem().design_space"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create and execute scenario\nThe next step is to build an MDO scenario in order to maximize the range,\nencoded 'y_4', with respect to the design parameters, while satisfying the\ninequality constraints 'g_1', 'g_2' and 'g_3'. We can use the MDF formulation,\nthe SLSQP optimization algorithm\nand a maximum number of iterations equal to 100.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario = create_scenario(\n    disciplines,\n    formulation=\"MDF\",\n    objective_name=\"y_4\",\n    maximize_objective=True,\n    design_space=design_space,\n)\nscenario.set_differentiation_method(\"user\")\nfor constraint in [\"g_1\", \"g_2\", \"g_3\"]:\n    scenario.add_constraint(constraint, \"ineq\")\nscenario.execute({\"algo\": \"SLSQP\", \"max_iter\": 10})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Post-process scenario\nLastly, we post-process the scenario by means of the :class:`.QuadApprox`\nplot which performs a quadratic approximation of a given function\nfrom an optimization history and plot the results as cuts of the\napproximation.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. tip::\n\n   Each post-processing method requires different inputs and offers a variety\n   of customization options. Use the API function\n   :meth:`~gemseo.api.get_post_processing_options_schema` to print a table with\n   the options for any post-processing algorithm.\n   Or refer to our dedicated page:\n   `gen_post_algos`.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The first plot shows an approximation of the Hessian matrix\n$\\frac{\\partial^2 f}{\\partial x_i \\partial x_j}$ based on the\n*Symmetric Rank 1* method (SR1) :cite:`Nocedal2006`. The\ncolor map uses a symmetric logarithmic (symlog) scale.\nThis plots the cross influence of the design variables on the objective function\nor constraints. For instance, on the last figure, the maximal second-order\nsensitivity is $\\frac{\\partial^2 -y_4}{\\partial^2 x_0} = 2.10^5$,\nwhich means that the $x_0$ is the most influential variable. Then,\nthe cross derivative\n$\\frac{\\partial^2 -y_4}{\\partial x_0 \\partial x_2} = 5.10^4$\nis positive and relatively high compared to the previous one but the combined\neffects of $x_0$ and  $x_2$ are non-negligible in comparison.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario.post_process(\"QuadApprox\", function=\"-y_4\", save=False, show=False)\n# Workaround for HTML rendering, instead of ``show=True``\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The second plot represents the quadratic approximation of the objective around the\noptimal solution : $a_{i}(t)=0.5 (t-x^*_i)^2\n\\frac{\\partial^2 f}{\\partial x_i^2} + (t-x^*_i) \\frac{\\partial\nf}{\\partial x_i} + f(x^*)$, where $x^*$ is the optimal solution.\nThis approximation highlights the sensitivity of the :term:`objective function`\nwith respect to the :term:`design variables`: we notice that the design\nvariables $x\\_1, x\\_5, x\\_6$ have little influence , whereas\n$x\\_0, x\\_2, x\\_9$ have a huge influence on the objective. This\ntrend is also noted in the diagonal terms of the :term:`Hessian` matrix\n$\\frac{\\partial^2 f}{\\partial x_i^2}$.\n\n"
      ]
    }
  ],
  "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.9.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}