{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Parametric scalable MDO problem - MDF\nWe define a scalable problem based on two strongly coupled disciplines\nand a weakly one, with the following properties:\n\n- 3 shared design parameters,\n- 2 local design parameters for the first strongly coupled discipline,\n- 2 coupling variables for the first strongly coupled discipline,\n- 4 local design parameters for the second strongly coupled discipline,\n- 3 coupling variables for the second strongly coupled discipline.\n\nWe would like to solve this MDO problem by means of an MDF formulation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import division, unicode_literals\n\nfrom gemseo.api import configure_logger, generate_n2_plot\nfrom gemseo.problems.scalable.parametric.problem import TMScalableProblem\nfrom gemseo.uncertainty.umdo.umdo_scenario import UMDOScenario\n\nconfigure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Instantiation of the scalable problem\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n_shared = 3\nn_local = [2, 4]\nn_coupling = [2, 3]\nproblem = TMScalableProblem(n_shared, n_local, n_coupling, noised_coupling=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Display the coupling structure\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "generate_n2_plot(problem.disciplines, save=False, show=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Solve the U-MDO using an MDF formulation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sampling_options = {\"algo\": \"OT_MONTE_CARLO\", \"n_samples\": 100}\nscenario = UMDOScenario(\n    problem.disciplines,\n    \"UMDF\",\n    \"obj\",\n    problem.design_space,\n    sampling_options=sampling_options,\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We set the robustness measures for the objective and the constraints.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario.set_robustness_measure(\"obj\", \"mean\")\nscenario.set_robustness_measure(\"cstr_0\", \"mean_std\", std_factor=3.0)\nscenario.set_robustness_measure(\"cstr_1\", \"mean_std\", std_factor=3.0)\nscenario.add_constraint(\"cstr_0\", \"ineq\", \"p_cstr_0\", value=0.0)\nscenario.add_constraint(\"cstr_1\", \"ineq\", \"p_cstr_1\", value=0.0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Display XDSM\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario.xdsmize(latex_output=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Execute\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario.execute({\"algo\": \"NLOPT_COBYLA\", \"max_iter\": 100, \"xtol_abs\": 1e-3})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Post-process the results\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "scenario.post_process(\"OptHistoryView\", 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.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}