Created
April 7, 2016 14:50
-
-
Save lotka/a89cabdb3ac8fbf08a55ad2224767edd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# How to optimise an n-dimensional function with pybo.solve_bayesopt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "from pybo import solve_bayesopt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "As an example, define some function with an obvious maximum:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# function with maximum at (1,2,3)\n", | |
| "def f(input):\n", | |
| " x,y,z = input\n", | |
| " x = float(x)\n", | |
| " y = float(y)\n", | |
| " z = float(z)\n", | |
| " x = x - 1\n", | |
| " y = y - 2\n", | |
| " z = z - 3\n", | |
| " return -(x**2)*(y**2)*(z**2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Now all you need to do is:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "i=000, x=[ 1.899 2.941 2.008], y=-0.704, xbest=[ 0.000 3.000 3.053]\n", | |
| "i=001, x=[ 1.834 1.024 3.999], y=-0.660, xbest=[ 0.000 1.000 3.264]\n", | |
| "i=002, x=[ 0.050 1.017 2.016], y=-0.843, xbest=[ 0.891 3.000 2.372]\n", | |
| "i=003, x=[ 0.342 3.000 4.000], y=-0.433, xbest=[ 1.237 3.000 3.520]\n", | |
| "i=004, x=[ 1.134 2.971 3.485], y=-0.004, xbest=[ 1.517 2.058 3.204]\n", | |
| "i=005, x=[ 0.191 1.265 3.297], y=-0.031, xbest=[ 1.426 2.878 3.279]\n", | |
| "i=006, x=[ 0.000 3.000 3.304], y=-0.092, xbest=[ 1.406 3.000 3.382]\n", | |
| "i=007, x=[ 1.406 2.974 3.324], y=-0.016, xbest=[ 0.928 2.020 2.464]\n", | |
| "i=008, x=[ 0.341 2.864 2.718], y=-0.026, xbest=[ 1.328 1.752 3.378]\n", | |
| "i=009, x=[ 0.446 1.041 2.279], y=-0.146, xbest=[ 0.825 2.426 2.408]\n", | |
| "i=010, x=[ 0.866 2.935 2.409], y=-0.005, xbest=[ 1.606 1.900 2.961]\n", | |
| "i=011, x=[ 1.677 1.052 2.451], y=-0.124, xbest=[ 1.638 2.046 3.077]\n", | |
| "i=012, x=[ 0.049 2.041 3.191], y=-0.000, xbest=[ 1.484 1.631 3.086]\n", | |
| "i=013, x=[ 0.240 2.100 3.057], y=-0.000, xbest=[ 1.386 1.000 3.036]\n", | |
| "i=014, x=[ 0.014 2.024 3.996], y=-0.001, xbest=[ 1.617 1.226 3.008]\n", | |
| "i=015, x=[ 0.005 1.278 3.850], y=-0.373, xbest=[ 0.260 1.431 3.025]\n", | |
| "i=016, x=[ 0.007 2.801 3.894], y=-0.505, xbest=[ 1.532 2.168 3.063]\n", | |
| "i=017, x=[ 0.495 1.323 3.229], y=-0.006, xbest=[ 1.454 2.217 2.878]\n", | |
| "i=018, x=[ 1.453 2.222 2.753], y=-0.001, xbest=[ 1.395 1.420 2.000]\n", | |
| "i=019, x=[ 0.024 2.948 2.004], y=-0.849, xbest=[ 0.827 1.870 3.842]\n", | |
| "i=020, x=[ 0.522 1.005 2.713], y=-0.019, xbest=[ 1.151 2.075 4.000]\n", | |
| "i=021, x=[ 1.157 2.154 3.996], y=-0.001, xbest=[ 0.975 2.208 3.464]\n", | |
| "i=022, x=[ 1.456 1.820 2.250], y=-0.004, xbest=[ 0.850 2.126 3.452]\n", | |
| "i=023, x=[ 0.869 2.154 3.461], y=-0.000, xbest=[ 0.303 1.802 3.277]\n", | |
| "i=024, x=[ 1.998 1.456 3.247], y=-0.018, xbest=[ 0.254 1.704 3.260]\n", | |
| "i=025, x=[ 1.800 1.030 2.006], y=-0.595, xbest=[ 0.326 1.774 3.373]\n", | |
| "i=026, x=[ 2.000 3.000 3.981], y=-0.962, xbest=[ 0.747 2.226 4.000]\n", | |
| "i=027, x=[ 0.598 2.089 4.000], y=-0.001, xbest=[ 0.290 1.649 3.180]\n", | |
| "i=028, x=[ 1.768 1.609 2.806], y=-0.003, xbest=[ 0.281 1.657 3.205]\n", | |
| "i=029, x=[ 1.025 2.104 2.330], y=-0.000, xbest=[ 1.499 2.296 3.322]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Define the ranges in which to look for an optimum\n", | |
| "bounds = [[0,2],[1,3],[2,4]]\n", | |
| "# Define how many iterations to do\n", | |
| "n = 30\n", | |
| "# Run the solver:\n", | |
| "xbest,model,info = solve_bayesopt(f,bounds,niter=n,verbose=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[ 1.49889517 2.29618329 3.32210738]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print xbest" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Looks like it did okay... Probably needs some more iterations but it's quite slow on my laptop" | |
| ] | |
| }, | |
| { | |
| "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.10" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment