{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Robustness\n\nIn this example, we illustrate the use of the :class:`.Robustness` plot\non the Sobieski's SSBJ problem.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom future import standard_library"
      ]
    },
    {
      "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()\n\nstandard_library.install_aliases()"
      ]
    },
    {
      "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 a 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:`.Robustness`\nplot which performs a quadratic approximation from an optimization history,\nand plot the results as cuts of the approximation computes the quadratic\napproximations of all the output functions, propagate analytically a normal\ndistribution centered on the optimal design variable with a standard\ndeviation which is a percentage of the mean passed in option (default: 1%)\nand plot the corresponding output boxplot. plots any of the constraint or\nobjective functions w.r.t. optimization iterations or sampling snapshots.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario.post_process(\"Robustness\", save=False, show=True)"
      ]
    }
  ],
  "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.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}