{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mix/max search using optimization\n",
    "\n",
    "In this example we are going to evaluate the min and max values of the output variable of interest in a domain using an optimization algorithm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import math as m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the marginal distributions of the parameters\n",
    "dist_E = ot.Beta(0.93, 3.2, 2.8e7, 4.8e7)\n",
    "dist_F = ot.LogNormalMuSigma(30000, 9000, 15000).getDistribution()\n",
    "dist_L = ot.Uniform(250, 260)\n",
    "dist_I = ot.Beta(2.5, 4.0, 3.1e2, 4.5e2)\n",
    "marginals = [dist_E, dist_F, dist_L, dist_I]\n",
    "distribution = ot.ComposedDistribution(marginals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Define bounds\n",
    "lowerBound = distribution.computeQuantile(0.1)\n",
    "upperBound = distribution.computeQuantile(0.9)\n",
    "bounds = ot.Interval(lowerBound, upperBound)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the model\n",
    "model = ot.SymbolicFunction(['E', 'F', 'L', 'I'], ['F*L^3/(3*E*I)'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the problems\n",
    "minProblem = ot.OptimizationProblem(model)\n",
    "minProblem.setBounds(bounds)\n",
    "\n",
    "maxProblem = ot.OptimizationProblem(model)\n",
    "maxProblem.setBounds(bounds)\n",
    "maxProblem.setMinimization(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create a solver\n",
    "solver = ot.TNC()\n",
    "solver.setStartingPoint(distribution.getMean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min: y= [8.30278] with x= [4.38053e+07,29031.6,255.623,444.429]\n",
      "max: y= [22.5231] with x= [3.37062e+07,52782.1,259.74,406.111]\n"
     ]
    }
   ],
   "source": [
    "# Solve the problems\n",
    "solver.setProblem(minProblem)\n",
    "solver.run()\n",
    "minResult = solver.getResult()\n",
    "print('min: y=', minResult.getOptimalValue(), 'with x=', minResult.getOptimalPoint())\n",
    "\n",
    "solver.setProblem(maxProblem)\n",
    "solver.run()\n",
    "maxResult = solver.getResult()\n",
    "print('max: y=', maxResult.getOptimalValue(), 'with x=', maxResult.getOptimalPoint())"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
