<s> import os <EOL> import numpy as np <EOL> from robo . acquisition . base import AcquisitionFunction <EOL> try : <EOL> import cpickle as pickle <EOL> except : <EOL> import pickle <EOL> from robo . util . exc import BayesianOptimizationError <EOL> from robo import BayesianOptimization <EOL> from argparse import ArgumentError <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> class _ContextualAcquisitionFunction ( AcquisitionFunction ) : <EOL> """<STR_LIT>""" <EOL> long_name = "<STR_LIT>" <EOL> def __init__ ( self , acquisition_fkt , model , X_lower , X_upper , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( _ContextualAcquisitionFunction , self ) . __init__ ( model , X_lower , X_upper , ** kwargs ) <EOL> self . acquisition_fkt = acquisition_fkt <EOL> self . Z = None <EOL> def set_context ( self , Z ) : <EOL> """<STR_LIT>""" <EOL> self . Z = Z <EOL> def __call__ ( self , S , derivative = False , ** kwargs ) : <EOL> X = np . concatenate ( ( np . tile ( self . Z , ( S . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) , S ) , axis = <NUM_LIT:1> ) <EOL> return self . acquisition_fkt ( X , derivative , ** kwargs ) <EOL> def update ( self , model ) : <EOL> self . acquisition_fkt . update ( model ) <EOL> def plot ( self , fig , minx , maxx , plot_attr = { "<STR_LIT>" : "<STR_LIT>" } , resolution = <NUM_LIT:1000> ) : <EOL> self . acquisition_fkt . plot ( fig , minx , maxx , plot_attr , resolution ) <EOL> def __str__ ( self ) : <EOL> return type ( self ) . __name__ + "<STR_LIT>" + self . acquisition_fkt + "<STR_LIT:)>" <EOL> class MeanAcquisitionFunction ( AcquisitionFunction ) : <EOL> """<STR_LIT>""" <EOL> long_name = "<STR_LIT>" <EOL> def __init__ ( self , model , X_lower , X_upper , ** kwargs ) : <EOL> self . model = model <EOL> self . X_lower = np . array ( X_lower ) <EOL> self . X_upper = np . array ( X_upper ) <EOL> def __call__ ( self , X , derivative = False , ** kwargs ) : <EOL> if derivative : <EOL> raise BayesianOptimizationError ( <EOL> BayesianOptimizationError . NO_DERIVATIVE , <EOL> "<STR_LIT>" ) <EOL> if np . any ( X < self . X_lower ) or np . any ( X > self . X_upper ) : <EOL> return np . array ( [ [ - np . finfo ( np . float ) . max ] ] ) <EOL> mean , _ = self . model . predict ( X ) <EOL> return - mean <EOL> def update ( self , model ) : <EOL> self . model = model <EOL> class ContextualBayesianOptimization ( BayesianOptimization ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dims_Z = None , dims_S = None , context_fkt = None , <EOL> acquisition_fkt = None , model = None , <EOL> maximize_fkt = None , S_lower = None , S_upper = None , <EOL> objective_fkt = None , save_dir = None , num_save = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if context_fkt is None : <EOL> raise ArgumentError ( context_fkt , "<STR_LIT>" ) <EOL> self . S_lower = S_lower <EOL> self . S_upper = S_upper <EOL> self . dims_Z = dims_Z <EOL> self . dims_S = dims_S <EOL> dims = dims_Z + dims_S <EOL> self . Z = None <EOL> X_lower = np . concatenate ( ( np . tile ( - np . inf , dims_Z ) , S_lower ) ) <EOL> X_upper = np . concatenate ( ( np . tile ( np . inf , dims_Z ) , S_upper ) ) <EOL> maximize_fkt_new = lambda acquisition_fkt , X_lower , X_upper : np . concatenate ( <EOL> ( self . Z , maximize_fkt ( acquisition_fkt , X_lower [ dims_Z : ] , X_upper [ dims_Z : ] ) ) , axis = <NUM_LIT:1> ) <EOL> self . context_fkt = context_fkt <EOL> acquisition_fkt_new = _ContextualAcquisitionFunction ( <EOL> acquisition_fkt , model , X_lower , X_upper ) <EOL> objective_fkt_new = lambda X : objective_fkt ( X [ : , : dims_Z ] , X [ : , dims_Z : ] ) <EOL> self . predict_acquisition_fkt = _ContextualAcquisitionFunction ( <EOL> MeanAcquisitionFunction ( model , S_lower , S_upper ) , model , X_lower , X_upper ) <EOL> super ( <EOL> ContextualBayesianOptimization , <EOL> self ) . __init__ ( <EOL> acquisition_fkt_new , <EOL> model , <EOL> maximize_fkt_new , <EOL> X_lower , <EOL> X_upper , <EOL> dims , <EOL> objective_fkt_new , <EOL> save_dir , <EOL> num_save ) <EOL> self . recommendation_strategy = lambda model , acquisition_fkt : None <EOL> def initialize ( self , rng = None ) : <EOL> self . Z = self . context_fkt ( ) <EOL> if rng is None : <EOL> self . rng = np . random . RandomState ( <NUM_LIT> ) <EOL> else : <EOL> self . rng = rng <EOL> self . X = np . concatenate ( <EOL> ( self . Z , np . array ( [ self . rng . uniform ( self . S_lower , self . S_upper , self . dims_S ) ] ) ) , <NUM_LIT:1> ) <EOL> print "<STR_LIT>" % ( str ( self . X [ <NUM_LIT:0> ] ) ) <EOL> self . Y = self . objective_fkt ( self . X ) <EOL> print "<STR_LIT>" % ( self . Y [ <NUM_LIT:0> ] ) <EOL> def predict ( self , Z = None ) : <EOL> """<STR_LIT>""" <EOL> if Z : <EOL> self . Z = Z <EOL> else : <EOL> self . Z = self . context_fkt ( ) <EOL> self . predict_acquisition_fkt . set_context ( self . Z ) <EOL> return self . maximize_fkt ( self . predict_acquisition_fkt , self . X_lower , self . X_upper ) <EOL> def choose_next ( self , X = None , Y = None , Z = None ) : <EOL> """<STR_LIT>""" <EOL> if Z : <EOL> self . Z = Z <EOL> else : <EOL> self . Z = self . context_fkt ( ) <EOL> self . acquisition_fkt . set_context ( self . Z ) <EOL> return super ( ContextualBayesianOptimization , self ) . choose_next ( X , Y ) </s>
