{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Optimization History View\n\nIn this example, we illustrate the use of the :class:`.OptHistoryView` plot\non the Sobieski's SSBJ problem.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import division, unicode_literals\n\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": [
        "from gemseo.api import configure_logger, create_discipline, create_scenario\nfrom gemseo.problems.sobieski.core import SobieskiProblem\n\nconfigure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Description\nThe **OptHistoryView** post-processing\ncreates a series of plots:\n\n- The design variables history - This graph shows the normalized values of the\n  design variables, the $y$ axis is the index of the inputs in the vector;\n  and the $x$ axis represents the iterations.\n- The objective function history - It shows the evolution of the objective\n  value during the optimization.\n- The distance to the best design variables - Plots the vector\n  $log( ||x-x^*|| )$ in log scale.\n- The history of the Hessian approximation of the objective - Plots an approximation\n  of the second order derivatives of the objective function\n  $\\frac{\\partial^2 f(x)}{\\partial x^2}$, which is a measure of\n  the sensitivity of the function with respect to the design variables,\n  and of the anisotropy of the problem (differences of curvatures in the\n  design space).\n- The inequality constraint history - Portrays the evolution of the values of the\n  :term:`constraints`. The inequality constraints must be non-positive, that is why\n  the plot must be green or white for satisfied constraints (white = active,\n  red = violated). For an `IDF formulation <idf_formulation>`, an additional\n  plot is created to track the equality constraint history.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create disciplines\nAt this point we instantiate the disciplines of 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().read_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:`.OptHistoryView`\nplot which plots the history of optimization for both objective function,\nconstraints, design parameters and distance to the optimum.\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": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario.post_process(\"OptHistoryView\", save=False, show=False)\n# Workaround for HTML rendering, instead of ``show=True``\nplt.show()"
      ]
    }
  ],
  "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.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}