{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we run an optimization making use of all of the functionality of horsetail matching. \n", "\n", "Things to note...\n", "\n", "When specifying undertainties as sampling functions, the number of samples returned should be equal to that provided in the samples_prob argument.\n", "\n", "When evaluating the metric, only integration points that are close to the range of samples obtained are evaluated, meaning a very large number and range of integration points (which should cover the range of values of the qoi that might be seen) can be provided without excessive calculation being performed. \n", "\n", "When using the built in Polynomial Chaos surrogate, the quadrature points used to evaluate the surrogate must match those given in the surrogate_points argument. \n", "\n", "The uncertainties are stored internally with probabilistic uncertainties first, followed by interval uncertainties. Therefore when using surrogate models, this order should match that used by the surrogate." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----------\n", "At design: [3. 2.]\n", "Evaluating surrogate\n", "Getting uncertain parameter samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 50.0306015529\n", "Gradient: [20.635361177419956, 18.055941030242472]\n", "----------\n", "At design: [-5. -5.]\n", "Evaluating surrogate\n", "Re-using stored samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 194.563105867\n", "Gradient: [-38.72031491206719, -38.72031491206666]\n", "----------\n", "At design: [ 0.32596505 -0.33978058]\n", "Evaluating surrogate\n", "Re-using stored samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 2.52216836791\n", "Gradient: [0.3648343133522796, -0.413276306213772]\n", "----------\n", "At design: [-0.04565756 0.06762066]\n", "Evaluating surrogate\n", "Re-using stored samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 2.40785109661\n", "Gradient: [-0.02308350354527805, 0.08727390546024198]\n", "----------\n", "At design: [-0.00216825 0.01315345]\n", "Evaluating surrogate\n", "Re-using stored samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 2.40452696018\n", "Gradient: [0.008547102372385759, 0.02339988449416532]\n", "----------\n", "At design: [0.00364844 0.00189953]\n", "Evaluating surrogate\n", "Re-using stored samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 2.40440546225\n", "Gradient: [0.00891332253999664, 0.0072182455855742175]\n", "----------\n", "At design: [2.56875779e-03 5.89865515e-05]\n", "Evaluating surrogate\n", "Re-using stored samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 2.40438889259\n", "Gradient: [0.005036420454997827, 0.002603964375424434]\n", "----------\n", "At design: [ 0.00035244 -0.00029599]\n", "Evaluating surrogate\n", "Re-using stored samples\n", "Evaluating quantity of interest at samples\n", "Evaluating metric\n", "Metric: 2.40438245133\n", "Gradient: [0.00039628416681229336, -0.00023216333374692413]\n" ] } ], "source": [ "import numpy as np\n", "import scipy.optimize as scopt\n", "\n", "from horsetailmatching import HorsetailMatching, UniformParameter, GaussianParameter, IntervalParameter, UncertainParameter\n", "from horsetailmatching.demoproblems import TP2\n", "from horsetailmatching.surrogates import PolySurrogate\n", "\n", "def fQOI(x, u):\n", " factor = np.sqrt(u[0]**2 + u[1]**2 + u[2]**2 + 1)\n", " q = 1 + factor*(x[0]**2 + x[1]*x[0] + x[1]**2)\n", " grad = [factor*(2*x[0] + x[1]), factor*(x[0] + 2*x[1])]\n", " return q, grad\n", "\n", "def myPDF(q):\n", " if q > 1 or q < -1:\n", " return 0\n", " else:\n", " return 0.5\n", " \n", "n_samples_prob = 100\n", "n_samples_int = 10\n", "\n", "u_1 = UncertainParameter(myPDF, lower_bound=-1, upper_bound=1)\n", "u_2 = lambda : np.random.normal(0, 1, n_samples_prob)\n", "u_3 = IntervalParameter(lower_bound=-1, upper_bound=1)\n", " \n", "def ftarget_u(h):\n", " return 0 - h**5\n", "\n", "def ftarget_l(h):\n", " return 1 - h**5\n", " \n", "qPolyChaos = PolySurrogate(dimensions=3, order=3, poly_type=['legendre', 'hermite', 'legendre'])\n", "gradPolyChaos = [PolySurrogate(dimensions=3, order=3, poly_type=['legendre', 'hermite', 'legendre']),\n", " PolySurrogate(dimensions=3, order=3, poly_type=['legendre', 'hermite', 'legendre'])]\n", "u_quad_points = qPolyChaos.getQuadraturePoints()\n", " \n", "def mySurrogateWithGrad(u_quad, q_quad, grad_quad):\n", " qPolyChaos.train(q_quad)\n", " for i, gPC in enumerate(gradPolyChaos):\n", " gPC.train(grad_quad[:, i])\n", " def qmodel(u):\n", " return qPolyChaos.predict(u)\n", " def gradmodel(u):\n", " return [gPC.predict(u) for gPC in gradPolyChaos]\n", " return qmodel, gradmodel\n", "\n", "theHM = HorsetailMatching(fQOI, prob_uncertainties=[u_1, u_2], int_uncertainties=u_3, \n", " jac=True, method='kernel', ftarget=(ftarget_u, ftarget_l),\n", " samples_prob=n_samples_prob, samples_int=n_samples_int, \n", " integration_points=np.linspace(-50, 1000, 2000),\n", " surrogate=mySurrogateWithGrad, surrogate_points=u_quad_points, surrogate_jac=True,\n", " verbose=True)\n", "\n", "solution = scopt.minimize(theHM.evalMetric, x0=[3, 2], bounds=[(-5, 5), (-5, 5)], jac=True, method='SLSQP')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "For other tutorials, see: http://www-edc.eng.cam.ac.uk/aerotools/horsetailmatching/" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }