{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adaptive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import adaptive\n",
    "adaptive.notebook_extension()\n",
    "\n",
    "def func(x, wait=True):\n",
    "    \"\"\"Function with a sharp peak on a smooth background\"\"\"\n",
    "    import numpy as np\n",
    "    from time import sleep\n",
    "    from random import randint\n",
    "\n",
    "    x = np.asarray(x)\n",
    "    a = 0.001\n",
    "    if wait:\n",
    "        sleep(np.random.randint(0, 2) / 10)\n",
    "    return x + a**2/(a**2 + (x)**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Local Process Pool (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "learner = adaptive.learner.Learner1D(func, bounds=(-1.01, 1.0))\n",
    "runner = adaptive.Runner(learner, goal=lambda l: l.loss(real=True) < 0.01)\n",
    "adaptive.live_plot(runner)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Same function evaluated on homogeneous grid with same amount of points\n",
    "from functools import partial\n",
    "import numpy as np\n",
    "\n",
    "learner2 = adaptive.learner.Learner1D(func, bounds=(-1.01, 1.0))\n",
    "xs = np.linspace(-1.01, 1.0, len(learner.data))\n",
    "learner2.add_data(xs, map(partial(func, wait=False), xs))\n",
    "learner2.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## ipyparallel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ipyparallel\n",
    "\n",
    "client = ipyparallel.Client()\n",
    "\n",
    "# Initialize the learner\n",
    "learner = adaptive.learner.Learner1D(func)\n",
    "runner = adaptive.Runner(learner, client, goal=lambda l: l.loss() < 0.1)\n",
    "adaptive.live_plot(runner)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0D Learner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "def func(x):\n",
    "    import random\n",
    "    import numpy as np\n",
    "    from time import sleep\n",
    "    sleep(np.random.randint(0, 2))\n",
    "    return random.gauss(0.05, 1)\n",
    "\n",
    "learner = adaptive.learner.AverageLearner(func, None, 0.1)\n",
    "runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 1)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}