{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Correlations\n\nIn this example, we illustrate the use of the :class:`.Correlations` 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\n\nA correlation coefficient indicates whether there is a linear\nrelationship between 2 quantities $x$ and $y$, in which case\nit equals 1 or -1. It is the normalized covariance between the two\nquantities:\n\n\\begin{align}R_{xy}=\\frac {\\sum \\limits _{i=1}^n(x_i-{\\bar{x}})(y_i-{\\bar{y}})}{ns_{x}s_{y}}\n   =\\frac {\\sum \\limits _{i=1}^n(x_i-{\\bar{x}})(y_i-{\\bar{y}})}{\\sqrt {\\sum\n   \\limits _{i=1}^n(x_i-{\\bar{x}})^{2}\\sum \\limits _{i=1}^n(y_i-{\\bar{y}})^{2}}}\\end{align}\n\nThe **Correlations** post-processing builds scatter plots of correlated variables\namong design variables, output functions, and constraints.\n\nThe plot method considers all variable correlations greater than 95%. A different\nthreshold value and/or a sublist of variable names can be passed as options.\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().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:`.Correlations`\nplot which provides scatter plots of correlated variables among design\nvariables, outputs functions and constraints any of the constraint or\nobjective functions w.r.t. optimization iterations or sampling snapshots.\nThis method requires the list of functions names to plot.\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(\"Correlations\", 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
}