learner.ipynb
de7ba87d
 {
  "cells": [
   {
    "cell_type": "markdown",
c395daf0
    "metadata": {},
de7ba87d
    "source": [
     "# Adaptive"
    ]
   },
e3a7009c
   {
    "cell_type": "code",
    "execution_count": null,
00bcd4c3
    "metadata": {
     "collapsed": true
    },
e3a7009c
    "outputs": [],
    "source": [
73fa8d28
     "import adaptive\n",
c395daf0
     "adaptive.notebook_extension()\n",
e3a7009c
     "\n",
de7ba87d
     "def func(x, wait=True):\n",
e3a7009c
     "    \"\"\"Function with a sharp peak on a smooth background\"\"\"\n",
18212a81
     "    import numpy as np\n",
     "    from time import sleep\n",
     "    from random import randint\n",
     "\n",
de7ba87d
     "    x = np.asarray(x)\n",
e3a7009c
     "    a = 0.001\n",
de7ba87d
     "    if wait:\n",
ea41ee7a
     "        sleep(np.random.randint(0, 2) / 10)\n",
c395daf0
     "    return x + a**2/(a**2 + (x)**2)"
de7ba87d
    ]
   },
   {
    "cell_type": "markdown",
73fa8d28
    "metadata": {
c395daf0
     "collapsed": true
73fa8d28
    },
de7ba87d
    "source": [
18212a81
     "## Local Process Pool (default)"
de7ba87d
    ]
   },
ea41ee7a
   {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
00bcd4c3
     "collapsed": true,
ea41ee7a
     "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)"
    ]
   },
de7ba87d
   {
    "cell_type": "code",
    "execution_count": null,
00bcd4c3
    "metadata": {
     "collapsed": true
    },
de7ba87d
    "outputs": [],
    "source": [
ea41ee7a
     "# Same function evaluated on homogeneous grid with same amount of points\n",
     "from functools import partial\n",
     "import numpy as np\n",
c395daf0
     "\n",
ea41ee7a
     "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()"
18212a81
    ]
   },
   {
    "cell_type": "markdown",
    "metadata": {
c395daf0
     "collapsed": true
18212a81
    },
    "source": [
     "## ipyparallel"
de7ba87d
    ]
   },
   {
    "cell_type": "code",
    "execution_count": null,
e3a7009c
    "metadata": {
c395daf0
     "collapsed": true
e3a7009c
    },
de7ba87d
    "outputs": [],
    "source": [
18212a81
     "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",
73fa8d28
     "adaptive.live_plot(runner)"
e3a7009c
    ]
ea41ee7a
   },
   {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
     "## 0D Learner"
    ]
   },
   {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
00bcd4c3
     "collapsed": true,
ea41ee7a
     "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)"
    ]
de7ba87d
   }
  ],
  "metadata": {
   "anaconda-cloud": {},
   "kernelspec": {
c395daf0
    "display_name": "Python 3",
de7ba87d
    "language": "python",
c395daf0
    "name": "python3"
de7ba87d
   },
   "language_info": {
    "codemirror_mode": {
     "name": "ipython",
     "version": 3
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
c395daf0
    "version": "3.6.2"
de7ba87d
   }
  },
  "nbformat": 4,
  "nbformat_minor": 1
 }