<s> '''<STR_LIT>''' <EOL> import scipy <EOL> import numpy as np <EOL> from base_maximizer import BaseMaximizer <EOL> class GradientAscent ( BaseMaximizer ) : <EOL> def __init__ ( self , objective_function , X_lower , X_upper , <EOL> n_random = <NUM_LIT:1000> , n_restarts = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> self . n_restarts = n_restarts <EOL> self . n_random = n_random <EOL> super ( GradientAscent , self ) . __init__ ( objective_function , <EOL> X_lower , X_upper ) <EOL> def _acquisition_fkt_wrapper ( self , x ) : <EOL> fx , gx = self . objective_func ( x , derivative = True ) <EOL> return - fx [ <NUM_LIT:0> ] , - gx [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> def maximize ( self , rng = None ) : <EOL> """<STR_LIT>""" <EOL> if rng is None : <EOL> rng = np . random . RandomState ( <NUM_LIT> ) <EOL> random_points = rng . uniform ( <EOL> self . X_lower , self . X_upper , ( self . n_random , self . X_lower . shape [ <NUM_LIT:0> ] ) ) <EOL> fvals = np . zeros ( [ self . n_random ] ) <EOL> for i in range ( self . n_random ) : <EOL> fvals [ i ] = self . objective_func ( random_points [ i ] ) <EOL> best = fvals . argsort ( ) [ - self . n_restarts : ] <EOL> x_opt = np . zeros ( [ self . n_restarts , self . X_lower . shape [ <NUM_LIT:0> ] ] ) <EOL> fval = np . zeros ( [ self . n_restarts ] ) <EOL> for i in range ( self . n_restarts ) : <EOL> start_point = random_points [ best [ i ] ] <EOL> res = scipy . optimize . fmin_l_bfgs_b ( <EOL> self . _acquisition_fkt_wrapper , start_point , bounds = zip ( <EOL> self . X_lower , self . X_upper ) ) <EOL> x_opt [ i ] = res [ <NUM_LIT:0> ] <EOL> fval [ i ] = res [ <NUM_LIT:1> ] <EOL> best = np . argmax ( fval ) <EOL> return np . array ( [ x_opt [ best ] ] ) </s>
