# GPyOpt: using context variables¶

### Javier Gonzalez and Rodolphe Jenatton, Amazon.com¶

Last updated Monday, July 2017

In this notebook we are going to see how to used GPyOpt to solve optimizaiton problems in which certain varaibles are fixed during the optimization phase. These are called context variables. For details see:

Krause, A. & Ong, C. S. Contextual gaussian process bandit optimization Advances in Neural Information Processing Systems (NIPS), 2011, 2447-2455

In [ ]:
%pylab inline
import GPyOpt
from numpy.random import seed

In [ ]:
func  = GPyOpt.objective_examples.experimentsNd.alpine1(input_dim=5)


Now we define the domain of the function to optimize as usual.

In [ ]:
mixed_domain =[{'name': 'var1', 'type': 'continuous', 'domain': (-5,5),'dimensionality': 3},
{'name': 'var2', 'type': 'discrete', 'domain': (3,8,10)},
{'name': 'var3', 'type': 'categorical', 'domain': (0,1,2)},
{'name': 'var4', 'type': 'continuous', 'domain': (-1,2)}]

In [ ]:
myBopt = GPyOpt.methods.BayesianOptimization(f=func.f,                     # Objective function
domain=mixed_domain,          # Box-constraints of the problem
initial_design_numdata = 5,   # Number data initial design
acquisition_type='EI',        # Expected Improvement
exact_feval = True,
evaluator_type = 'local_penalization',
batch_size = 5
)           # True evaluations, no sample noise


Now, we run the optimization for 20 iterations or a maximum of 60 seconds and we show the convergence plots.

In [ ]:
max_iter = 2    ## maximum number of iterations
max_time = 60   ## maximum allowed time
eps      = 0    ## tolerance, max distance between consicutive evaluations.


To set a context, we just need to create a dicctionary with the variables to fix and pass it to the Bayesian ottimization object when running the optimization. Note that, everytime we run new iterations we can set other variables to be the context. Note that for variables in which the dimaensionality has been specified in the domain, a subindex is internally asigned. For instance if the variables is called 'var1' and has dimensionality 3, the first three positions in the internal representation of the domain will be occupied by variables 'var1_1', 'var1_2' and 'var1_3'. If no dimensionality is added, the internal naming remains the same. For instance, in the example above 'var3' should be fixed its original name. See below for details.

In [ ]:
myBopt.run_optimization(max_iter,eps=eps)
myBopt.run_optimization(max_iter,eps=eps,context = {'var1_1':.3, 'var1_2':0.4})
myBopt.run_optimization(max_iter,eps=eps,context = {'var1_1':0, 'var3':2})
myBopt.run_optimization(max_iter,eps=eps,context = {'var1_1':0, 'var2':3},)
myBopt.run_optimization(max_iter,eps=eps,context = {'var1_1':0.3, 'var3':1, 'var4':-.4})
myBopt.run_optimization(max_iter,eps=eps)


We can now visualize the results

In [ ]:
np.round(myBopt.X,2)

In [ ]: