<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GPyOpt: mixing different types of variables\n",
    "\n",
    "### <NAME>, Amazon Research Cambridge.\n",
    "\n",
    "\n",
    "*Last updated Monday, July 2017*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook we are going to see how to used GPyOpt to solve optimizaiton problems in which the domain of the fucntion is defined in terms of a variety of continous and discrete variables. To this end we start by loading GPyOpt. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%pylab inline  \n",
    "import GPyOpt\n",
    "from numpy.random import seed\n",
    "import numpy as np\n",
    "seed(123)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will use the **Alpine1** function, that it is available in the benchmark of functions of the package. This function is defined for arbitrary dimension. In this example we will work in dimension 5. The functional form of the Alpine1 function is:\n",
    "\n",
    "$$f(x_1,x_2,x_3,x_4,x_5)=\\sum_{i=1}^{5} \\lvert {x_i \\sin \\left( x_i \\right) + 0.1 x_i} \\rvert$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We load the function from GPyOpt, assuming that noisy free values will be sampled."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "func  = GPyOpt.objective_examples.experimentsNd.alpine1(input_dim=9) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will consider that variables $x_1$ and $x_2$ are continuous and defined in the interval $[-5,5]$, variable $x_3$ is takes continuous values in the interval $[-1,5]$ and the variables $x_4$ and $x_5$ are discrete and take values $\\{-3,0,3\\}$ and $\\{-5,-1,1,5\\}$ respectively. Next we define this domain to use it in GPyOpt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "mixed_domain =[{'name': 'var1', 'type': 'continuous', 'domain': (-5,5),'dimensionality': 3},\n",
    "               {'name': 'var3', 'type': 'discrete', 'domain': (3,8,10),'dimensionality': 2},\n",
    "               {'name': 'var4', 'type': 'categorical', 'domain': (0,1,2),'dimensionality': 1},\n",
    "               {'name': 'var5', 'type': 'continuous', 'domain': (-1,2)}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt = GPyOpt.methods.BayesianOptimization(f=func.f,                     # Objective function       \n",
    "                                             domain=mixed_domain,          # Box-constraints of the problem\n",
    "                                             initial_design_numdata = 5,   # Number data initial design\n",
    "                                             acquisition_type='EI',        # Expected Improvement\n",
    "                                             exact_feval = True)           # True evaluations, no sample noise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we run the optimization for 20 iterations or a maximum of 60 seconds and we show the convergence plots."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "max_iter = 20       ## maximum number of iterations\n",
    "max_time = 60       ## maximum allowed time\n",
    "eps      = 1e-6     ## tolerance, max distance between consicutive evaluations.\n",
    "\n",
    "myBopt.run_optimization(max_iter,eps=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.round(myBopt.X,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.plot_convergence()"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
<commit_msg>Fixed a few mishaps between the text and the code<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GPyOpt: mixing different types of variables\n",
    "\n",
    "### <NAME>, Amazon Research Cambridge.\n",
    "\n",
    "\n",
    "*Last updated Monday, July 2017*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook we are going to see how to used GPyOpt to solve optimizaiton problems in which the domain of the fucntion is defined in terms of a variety of continous and discrete variables. To this end we start by loading GPyOpt. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%pylab inline  \n",
    "import GPyOpt\n",
    "from numpy.random import seed\n",
    "import numpy as np\n",
    "seed(123)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will use the **Alpine1** function, that it is available in the benchmark of functions of the package. This function is defined for arbitrary dimension. In this example we will work in dimension 9. The functional form of the Alpine1 function is:\n",
    "\n",
    "$$f(x_1,x_2,...,x_9)=\\sum_{i=1}^{9} \\lvert {x_i \\sin \\left( x_i \\right) + 0.1 x_i} \\rvert$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We load the function from GPyOpt, assuming that noisy free values will be sampled."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "func  = GPyOpt.objective_examples.experimentsNd.alpine1(input_dim=9) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will consider that variables $x_1$, $x_2$ and $x_3$ are continuous and defined in the interval $[-5,5]$, variable $x_9$ is takes continuous values in the interval $[-1,2]$, variable $x_3$ is discrete and takes values $\\{3,8,10\\}$, and finally $x_4$ is categorical with three different categories. Next we define this domain to use it in GPyOpt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "mixed_domain =[{'name': 'var1', 'type': 'continuous', 'domain': (-5,5),'dimensionality': 3},\n",
    "               {'name': 'var3', 'type': 'discrete', 'domain': (3,8,10),'dimensionality': 2},\n",
    "               {'name': 'var4', 'type': 'categorical', 'domain': (0,1,2),'dimensionality': 1},\n",
    "               {'name': 'var5', 'type': 'continuous', 'domain': (-1,2)}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt = GPyOpt.methods.BayesianOptimization(f=func.f,                     # Objective function       \n",
    "                                             domain=mixed_domain,          # Box-constraints of the problem\n",
    "                                             initial_design_numdata = 5,   # Number data initial design\n",
    "                                             acquisition_type='EI',        # Expected Improvement\n",
    "                                             exact_feval = True)           # True evaluations, no sample noise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we run the optimization for 20 iterations or a maximum of 60 seconds and we show the convergence plots."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "max_iter = 20       ## maximum number of iterations\n",
    "max_time = 60       ## maximum allowed time\n",
    "eps      = 1e-6     ## tolerance, max distance between consicutive evaluations.\n",
    "\n",
    "myBopt.run_optimization(max_iter,eps=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.round(myBopt.X,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.plot_convergence()"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

{'fraction_non_alphanumeric': 0.17679272576978713, 'fraction_numerical': 0.015499070055796652, 'mean_word_length': 2.411702502643638, 'pattern_counts': {'":': 162, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7036677', 'n_tokens_mistral': 3391, 'n_tokens_neox': 3253, 'n_words': 846}