{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Bayesian optimization with context variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we are going to see how to use Emukit to solve optimization problems in which certain variables are fixed during the optimization phase. These are called context variables [[1](#-references)]. This is useful when some of the variables in the optimization are controllable/known factors. And example is the optimization of a the movement of a robot under conditions of the environment change (but the change is known)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from emukit.test_functions import branin_function\n", "from emukit.core import ParameterSpace, ContinuousParameter, DiscreteParameter\n", "from emukit.experimental_design.model_free.random_design import RandomDesign\n", "from GPy.models import GPRegression\n", "from emukit.model_wrappers import GPyModelWrapper\n", "from emukit.bayesian_optimization.acquisitions import ExpectedImprovement\n", "from emukit.bayesian_optimization.loops import BayesianOptimizationLoop\n", "from emukit.core.loop import FixedIterationsStoppingCondition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loading the problem and the loop" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f, _ = branin_function()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we define the domain of the function to optimize." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "parameter_space = ParameterSpace([ContinuousParameter('x1', -5, 10),\n", " ContinuousParameter('x2', 0, 15)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We build the model:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "design = RandomDesign(parameter_space) # Collect random points\n", "X = design.get_samples(10)\n", "Y = f(X)\n", "model_gpy = GPRegression(X,Y) # Train and wrap the model in Emukit\n", "model_emukit = GPyModelWrapper(model_gpy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And prepare the optimization object to run the loop." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "expected_improvement = ExpectedImprovement(model = model_emukit)\n", "bayesopt_loop = BayesianOptimizationLoop(model = model_emukit,\n", " space = parameter_space,\n", " acquisition = expected_improvement,\n", " batch_size = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we set the number of iterations to run to 10." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true, "scrolled": false }, "outputs": [], "source": [ "max_iter = 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running the optimization by setting a context variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To set a context, we just need to create a dictionary with the variables to fix and pass it to the Bayesian optimization object when running the optimization. Note that, every time we run new iterations we can set other variables to be the context. We run 3 sequences of 10 iterations each with different values of the context." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "bayesopt_loop.run_loop(f, max_iter, context={'x1':0.3}) # we set x1 as the context variable\n", "bayesopt_loop.run_loop(f, max_iter, context={'x2':0.1}) # we set x2 as the context variable\n", "bayesopt_loop.run_loop(f, max_iter) # no context" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now inspect the collected points." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 9.13459515, 2.66235976],\n", " [ 3.96697599, 11.95100624],\n", " [-4.17885102, 4.85113642],\n", " [ 0.32507059, 0.08922947],\n", " [ 5.64977093, 13.01495774],\n", " [ 6.50174329, 11.99569935],\n", " [ 0.05185026, 8.54957567],\n", " [ 8.76057361, 0.56603508],\n", " [ 1.00369808, 13.77609726],\n", " [ 5.70878062, 11.08138747],\n", " [ 0.3 , 5.11611766],\n", " [ 0.3 , 5.88497906],\n", " [ 0.3 , 5.9823043 ],\n", " [ 0.3 , 6.17679176],\n", " [ 0.3 , 6.29604643],\n", " [ 0.3 , 6.19923853],\n", " [ 0.3 , 4.54870967],\n", " [ 0.3 , 6.13726925],\n", " [ 0.3 , 6.17352127],\n", " [ 0.3 , 14.99158071],\n", " [10. , 0.1 ],\n", " [ 4.3965671 , 0.1 ],\n", " [ 6.82134636, 0.1 ],\n", " [ 2.83010062, 0.1 ],\n", " [-5. , 0.1 ],\n", " [ 9.13361485, 0.1 ],\n", " [-2.32189126, 0.1 ],\n", " [ 8.41142546, 0.1 ],\n", " [ 8.42345634, 0.1 ],\n", " [-4.59246209, 0.1 ],\n", " [-5. , 11.87026862],\n", " [10. , 7.80138872],\n", " [ 3.83576952, 3.12647385],\n", " [-5. , 15. ],\n", " [10. , 15. ],\n", " [ 3.2292838 , 1.80201788],\n", " [ 7.74960871, 3.84223718],\n", " [-5. , 8.64581695],\n", " [10. , 4.60904445],\n", " [10. , 2.63352077]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bayesopt_loop.loop_state.X" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## References\n", "\n", "* [1] Krause, A. & Ong, C. S. Contextual gaussian process bandit optimization Advances in Neural Information Processing Systems (NIPS), 2011, 2447-2455*" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 1 }