<s> from __future__ import print_function <EOL> from __future__ import division <EOL> import numpy as np <EOL> from sklearn . gaussian_process import GaussianProcess <EOL> from scipy . optimize import minimize <EOL> from . helpers import UtilityFunction , unique_rows , PrintLog <EOL> __author__ = '<STR_LIT>' <EOL> def acq_max ( ac , gp , y_max , bounds ) : <EOL> """<STR_LIT>""" <EOL> x_max = bounds [ : , <NUM_LIT:0> ] <EOL> max_acq = None <EOL> x_tries = np . random . uniform ( bounds [ : , <NUM_LIT:0> ] , bounds [ : , <NUM_LIT:1> ] , <EOL> size = ( <NUM_LIT:100> , bounds . shape [ <NUM_LIT:0> ] ) ) <EOL> for x_try in x_tries : <EOL> res = minimize ( lambda x : - ac ( x . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) , gp = gp , y_max = y_max ) , <EOL> x_try . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) , <EOL> bounds = bounds , <EOL> method = "<STR_LIT>" ) <EOL> if max_acq is None or - res . fun >= max_acq : <EOL> x_max = res . x <EOL> max_acq = - res . fun <EOL> return np . clip ( x_max , bounds [ : , <NUM_LIT:0> ] , bounds [ : , <NUM_LIT:1> ] ) <EOL> class BayesianOptimization ( object ) : <EOL> def __init__ ( self , f , pbounds , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . pbounds = pbounds <EOL> self . keys = list ( pbounds . keys ( ) ) <EOL> self . dim = len ( pbounds ) <EOL> self . bounds = [ ] <EOL> for key in self . pbounds . keys ( ) : <EOL> self . bounds . append ( self . pbounds [ key ] ) <EOL> self . bounds = np . asarray ( self . bounds ) <EOL> self . f = f <EOL> self . initialized = False <EOL> self . init_points = [ ] <EOL> self . x_init = [ ] <EOL> self . y_init = [ ] <EOL> self . X = None <EOL> self . Y = None <EOL> self . i = <NUM_LIT:0> <EOL> self . gp = GaussianProcess ( theta0 = np . random . uniform ( <NUM_LIT> , <NUM_LIT> , self . dim ) , <EOL> thetaL = <NUM_LIT> * np . ones ( self . dim ) , <EOL> thetaU = <NUM_LIT> * np . ones ( self . dim ) , <EOL> random_start = <NUM_LIT:30> ) <EOL> self . util = None <EOL> self . plog = PrintLog ( self . keys ) <EOL> self . res = { } <EOL> self . res [ '<STR_LIT>' ] = { '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None } <EOL> self . res [ '<STR_LIT:all>' ] = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> self . verbose = verbose <EOL> def init ( self , init_points ) : <EOL> """<STR_LIT>""" <EOL> l = [ np . random . uniform ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] , size = init_points ) for x in self . bounds ] <EOL> self . init_points += list ( map ( list , zip ( * l ) ) ) <EOL> y_init = [ ] <EOL> for x in self . init_points : <EOL> y_init . append ( self . f ( ** dict ( zip ( self . keys , x ) ) ) ) <EOL> if self . verbose : <EOL> self . plog . print_step ( x , y_init [ - <NUM_LIT:1> ] ) <EOL> self . init_points += self . x_init <EOL> y_init += self . y_init <EOL> self . X = np . asarray ( self . init_points ) <EOL> self . Y = np . asarray ( y_init ) <EOL> self . initialized = True <EOL> def explore ( self , points_dict ) : <EOL> """<STR_LIT>""" <EOL> param_tup_lens = [ ] <EOL> for key in self . keys : <EOL> param_tup_lens . append ( len ( list ( points_dict [ key ] ) ) ) <EOL> if all ( [ e == param_tup_lens [ <NUM_LIT:0> ] for e in param_tup_lens ] ) : <EOL> pass <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> all_points = [ ] <EOL> for key in self . keys : <EOL> all_points . append ( points_dict [ key ] ) <EOL> self . init_points = list ( map ( list , zip ( * all_points ) ) ) <EOL> def initialize ( self , points_dict ) : <EOL> """<STR_LIT>""" <EOL> for target in points_dict : <EOL> self . y_init . append ( target ) <EOL> all_points = [ ] <EOL> for key in self . keys : <EOL> all_points . append ( points_dict [ target ] [ key ] ) <EOL> self . x_init . append ( all_points ) <EOL> def set_bounds ( self , new_bounds ) : <EOL> """<STR_LIT>""" <EOL> self . pbounds . update ( new_bounds ) <EOL> for row , key in enumerate ( self . pbounds . keys ( ) ) : <EOL> self . bounds [ row ] = self . pbounds [ key ] <EOL> def maximize ( self , <EOL> init_points = <NUM_LIT:5> , <EOL> n_iter = <NUM_LIT> , <EOL> acq = '<STR_LIT>' , <EOL> kappa = <NUM_LIT> , <EOL> xi = <NUM_LIT:0.0> , <EOL> ** gp_params ) : <EOL> """<STR_LIT>""" <EOL> self . plog . reset_timer ( ) <EOL> self . util = UtilityFunction ( kind = acq , kappa = kappa , xi = xi ) <EOL> if not self . initialized : <EOL> if self . verbose : <EOL> self . plog . print_header ( ) <EOL> self . init ( init_points ) <EOL> y_max = self . Y . max ( ) <EOL> self . gp . set_params ( ** gp_params ) <EOL> ur = unique_rows ( self . X ) <EOL> self . gp . fit ( self . X [ ur ] , self . Y [ ur ] ) <EOL> x_max = acq_max ( ac = self . util . utility , <EOL> gp = self . gp , <EOL> y_max = y_max , <EOL> bounds = self . bounds ) <EOL> if self . verbose : <EOL> self . plog . print_header ( initialization = False ) <EOL> for i in range ( n_iter ) : <EOL> pwarning = False <EOL> if np . any ( ( self . X - x_max ) . sum ( axis = <NUM_LIT:1> ) == <NUM_LIT:0> ) : <EOL> x_max = np . random . uniform ( self . bounds [ : , <NUM_LIT:0> ] , <EOL> self . bounds [ : , <NUM_LIT:1> ] , <EOL> size = self . bounds . shape [ <NUM_LIT:0> ] ) <EOL> pwarning = True <EOL> self . X = np . vstack ( ( self . X , x_max . reshape ( ( <NUM_LIT:1> , - <NUM_LIT:1> ) ) ) ) <EOL> self . Y = np . append ( self . Y , self . f ( ** dict ( zip ( self . keys , x_max ) ) ) ) <EOL> ur = unique_rows ( self . X ) <EOL> self . gp . fit ( self . X [ ur ] , self . Y [ ur ] ) <EOL> if self . Y [ - <NUM_LIT:1> ] > y_max : <EOL> y_max = self . Y [ - <NUM_LIT:1> ] <EOL> x_max = acq_max ( ac = self . util . utility , <EOL> gp = self . gp , <EOL> y_max = y_max , <EOL> bounds = self . bounds ) <EOL> if self . verbose : <EOL> self . plog . print_step ( self . X [ - <NUM_LIT:1> ] , self . Y [ - <NUM_LIT:1> ] , warning = pwarning ) <EOL> self . i += <NUM_LIT:1> <EOL> self . res [ '<STR_LIT>' ] = { '<STR_LIT>' : self . Y . max ( ) , <EOL> '<STR_LIT>' : dict ( zip ( self . keys , <EOL> self . X [ self . Y . argmax ( ) ] ) ) <EOL> } <EOL> self . res [ '<STR_LIT:all>' ] [ '<STR_LIT>' ] . append ( self . Y [ - <NUM_LIT:1> ] ) <EOL> self . res [ '<STR_LIT:all>' ] [ '<STR_LIT>' ] . append ( dict ( zip ( self . keys , self . X [ - <NUM_LIT:1> ] ) ) ) <EOL> if self . verbose : <EOL> self . plog . print_summary ( ) </s>
