def gpb_from_func_caller(func_caller, worker_manager, max_capital, is_mf, mode=None, acq=None, mf_strategy=None, domain_add_max_group_size=-1, options=None, reporter='default'): """ GP Bandit optimisation from a utils.function_caller.FunctionCaller instance. domain_add_max_group_size indicates whether we should use an additive model or not. If its negative, then use a non-additive model. If its positive, then use an additive model with maximum group size as given. If zero, then use the default in options. """ # pylint: disable=too-many-branches reporter = get_reporter(reporter) if is_mf: if isinstance(func_caller.fidel_space, domains.EuclideanDomain) and \ isinstance(func_caller.domain, domains.EuclideanDomain): optimiser_constructor = EuclideanGPBandit dflt_list_of_options = get_all_mf_gp_bandit_args_from_gp_args( euclidean_mf_gp_args) else: if isinstance(func_caller.domain, domains.EuclideanDomain): optimiser_constructor = EuclideanGPBandit dflt_list_of_options = get_all_gp_bandit_args_from_gp_args( euclidean_gp_args) else: raise ValueError('Random optimiser not implemented for domain of type %s.'%( \ type(func_caller.domain))) # Load options if options is None: reporter = get_reporter(reporter) options = load_options(dflt_list_of_options, reporter=reporter) if acq is not None: options.acq = acq if mode is not None: options.mode = mode if mf_strategy is not None: options.mf_strategy = mf_strategy # Additive model if domain_add_max_group_size >= 0: if is_mf: options.domain_use_additive_gp = True if domain_add_max_group_size > 0: options.domain_add_max_group_size = domain_add_max_group_size else: options.use_additive_gp = True if domain_add_max_group_size > 0: options.add_max_group_size = domain_add_max_group_size # create optimiser and return optimiser = optimiser_constructor(func_caller, worker_manager, is_mf=is_mf, options=options, reporter=reporter) return optimiser.optimise(max_capital)
def _get_ga_optimiser_args(cls, obj, num_evals, mutation_op, init_pool, init_pool_vals=None, expects_inputs_to_be_iterable=True): """ Returns arguments for the optimiser. """ if expects_inputs_to_be_iterable: def _obj_wrap(_obj, _x): """ A wrapper for the optimiser for GA. """ ret = _obj([_x]) return ret[0] def _get_obj_wrap(_obj): """ Returns an optimiser for GA. """ return lambda x: _obj_wrap(_obj, x) obj = _get_obj_wrap(obj) if init_pool_vals is None: init_pool_vals = [obj(nn) for nn in init_pool] reporter = get_reporter('silent') options = load_options(ga_opt_args, reporter=reporter) options.pre_eval_points = init_pool options.pre_eval_vals = init_pool_vals options.pre_eval_true_vals = init_pool_vals options.num_mutations_per_epoch = int( np.clip(3 * np.sqrt(num_evals), 5, 100)) # print 'GA opt args: ', num_evals, options.num_mutations_per_epoch return obj, options, reporter, mutation_op
def nasbot(func_caller, worker_manager, budget, tp_comp=None, mode=None, init_pool=None, acq='hei', options=None, reporter='default'): """ NASBOT optimisation from a function caller. """ nn_type = func_caller.domain.nn_type if options is None: reporter = get_reporter(reporter) options = load_options(all_nasbot_args, reporter=reporter) if acq is not None: options.acq = acq if mode is not None: options.mode = mode if tp_comp is None: tp_comp = get_default_otmann_distance(nn_type, 1.0) # Initial queries if not hasattr(options, 'pre_eval_points') or options.pre_eval_points is None: if init_pool is None: init_pool = get_initial_pool(nn_type) options.get_initial_points = lambda n: init_pool[:n] return (NASBOT(func_caller, worker_manager, tp_comp, options=options, reporter=reporter)).optimise(budget)
def __init__(self, X, Y, kernel, mean_func, noise_var, build_posterior=True, reporter=None, handle_non_psd_kernels=''): """ Constructor. """ super(GP, self).__init__() _check_feature_label_lengths_and_format(X, Y) self.X = X self.Y = Y self.kernel = kernel self.mean_func = mean_func self.noise_var = noise_var self.reporter = get_reporter(reporter) self.handle_non_psd_kernels = handle_non_psd_kernels # Some derived attribues. self.num_tr_data = len(self.Y) # Initialise other attributes we will need. self.L = None self.alpha = None self.K_trtr = None self.K_trtr_wo_noise = None self._set_up() self.in_debug_mode = False # Build posterior if necessary if build_posterior: self.build_posterior()
def __init__(self, func_caller, worker_manager, mutation_op, crossover_op=None, options=None, reporter=None): """ Constructor. mutation_op: A function which takes in a list of objects and modifies them. crossover_op: A function which takes in two objects and performs a cross-over operation. So far we have not implemented cross-over but included here in case we want to include it in the future. For other arguments, see BlackboxOptimiser """ if options is None: reporter = get_reporter(reporter) options = load_options(ga_opt_args, reporter=reporter) super(GAOptimiser, self).__init__(func_caller, worker_manager, model=None, options=options, reporter=reporter) self.mutation_op = mutation_op self.crossover_op = crossover_op self.to_eval_points = []
def optimize_chemist(func_caller, worker_manager, budget, mode=None, init_pool=None, acq='hei', options=None, reporter='default'): """ Chemist optimization from a function caller. """ if options is None: reporter = get_reporter(reporter) options = load_options(all_chemist_args, reporter=reporter) # TODO: what is this option? if acq is not None: options.acq = acq if mode is not None: options.mode = mode # Initial queries if not hasattr(options, 'pre_eval_points') or options.pre_eval_points is None: if init_pool is None: init_pool = get_initial_pool() options.get_initial_points = lambda n: init_pool[:n] return (Chemist(func_caller, worker_manager, options=options, reporter=reporter)).optimise(budget)
def __init__(self, experiment_name, num_experiments, save_file_name, save_file_extension='', reporter='default', random_seed_val='time'): """ Constructor. random_seed_val: If None we will not change the random seed. If it is 'time' we will set it to a time based value. If it is an int, we will set the seed to that value. """ self.experiment_name = experiment_name self.num_experiments = num_experiments if save_file_extension == '': save_file_parts = save_file_name.split('.') save_file_name = save_file_parts[0] save_file_extension = save_file_parts[1] self.save_file_extension = save_file_extension self.save_file_full_name = save_file_name + '.' + self.save_file_extension self.pickle_file_name = save_file_name + '.p' self.reporter = get_reporter(reporter) self.to_be_saved = Namespace(experiment_name=self.experiment_name) self.data_not_to_be_mat_saved = [] self.data_not_to_be_pickled = [] # We will need these going forward. self.experiment_iter = 0 # Set the random seed if random_seed_val is not None: if random_seed_val == 'time': random_seed_val = int(time() * 100) % 100000 self.reporter.writeln('Setting random seed to %d.' % (random_seed_val)) np.random.seed(random_seed_val) random.seed(random_seed_val)
def __init__(self, constraint_checker=None, options=None, reporter=None): """ Constructor. """ self.reporter = get_reporter(reporter) self.constraint_checker = constraint_checker if options is None: options = load_options(nn_modifier_args) self.options = options
def main(): """ Main function. """ options = load_options(all_mf_gp_bandit_args) prob, options = get_problem_parameters(options) # Set other variables all_methods = NONFINITE_METHODS + FINITE_METHODS method_options = {key: options for key in all_methods} noisy_str = 'noiseless' if not NOISY else 'noisy%0.3f' % ( prob.mfof.noise_var) save_file_prefix = '%s-%s-p%d-d%d' % (prob.experiment_name, noisy_str, prob.mfof.fidel_dim, prob.mfof.domain_dim) reporter = get_reporter('default') experimenter = MFOptExperimenter(prob.experiment_name, prob.mfof, prob.max_capital, all_methods, prob.num_experiments, SAVE_RESULTS_DIR, save_file_prefix=save_file_prefix, method_options=method_options, method_reporter=reporter, reporter=reporter) experimenter.run_experiments()
def mf_random_optimiser_from_func_caller(func_caller, worker_manager, max_capital, mode, options=None, reporter='default', *args, **kwargs): """ Creates a MF EuclideanRandomOptimiser Object and optimises the function. """ reporter = get_reporter(reporter) if isinstance(func_caller.domain, domains.EuclideanDomain) and \ isinstance(func_caller.fidel_space, domains.EuclideanDomain): optimiser_constructor = MFEuclideanRandomOptimiser dflt_list_of_options = mf_euclidean_random_optimiser_args else: raise ValueError( ('MF Random optimiser not implemented for (domain, fidel_space) ' + 'of types (%s, %s).') % (type(func_caller.domain), type(func_caller.fidel_space))) # Load options if options is None: options = load_options(dflt_list_of_options) options.mode = mode # Create optimiser optimiser = optimiser_constructor(func_caller, worker_manager, options=options, reporter=reporter, *args, **kwargs) # optimise and return return optimiser.optimise(max_capital)
def optimise_with_method_on_func_caller(method, func_caller, worker_manager, max_capital, options=None, reporter='default'): """ A wrapper that can be used to call all optimisation method. """ if options is None: from gp_bandit import all_gp_bandit_args reporter = get_reporter(reporter) options = load_options(all_gp_bandit_args, reporter=reporter) # The mode should be specified in the first three letters of the method argument options.mode = method[0:3] if method in ['synRAND', 'asyRAND']: return random_optimiser_from_func_caller(func_caller, worker_manager, max_capital, options=options, reporter=reporter) elif method in [ 'asyTS', 'synTS', 'asyHTS', 'asyUCB', 'asyBUCB', 'synBUCB', 'synUCBPE', 'asyEI' ]: from gp_bandit import gpb_from_func_caller options.acq = method[3:] return gpb_from_func_caller(func_caller, worker_manager, max_capital, options=options, reporter='default') else: raise ValueError('Unknown method %s!' % (method))
def _get_euc_gpb_arguments(cls, num_dims): """ Returns options for GP Bandits on a Euclidean space. """ reporter = reporters.get_reporter('silent') gpb_args = gp_bandit.get_all_gp_bandit_args_from_gp_args(all_simple_gp_args) options = load_options(gpb_args, reporter=reporter) options.get_initial_points = lambda n: np.random.random((n, num_dims)) options.num_init_evals = 20 return options
def __init__(self, options, reporter='default'): """ Constructor. """ super(GPFitter, self).__init__() self.reporter = get_reporter(reporter) if isinstance(options, list): options = load_options(options, 'GP', reporter=self.reporter) self.options = options self._set_up()
def maximise_function(func, max_capital, domain=None, domain_bounds=None, config=None, options=None, hp_tune_criterion='post_sampling', hp_tune_method='slice', init_capital=None, init_capital_frac=None, num_init_evals=20): """ Maximizes a function given a function and domain bounds of the hyperparameters and returns optimal value and optimal point. """ reporter = get_reporter('default') if options is None: euc_gpb_args = get_all_gp_bandit_args_from_gp_args(euclidean_gp_args) options = load_options(euc_gpb_args) options.gpb_hp_tune_criterion = hp_tune_criterion options.gpb_post_hp_tune_method = hp_tune_method options.init_capital = init_capital options.init_capital_frac = init_capital_frac options.num_init_evals = num_init_evals # Check for Domains if domain is None: if config is not None: param_spec = config_parser(options.config) domain = create_domain(param_spec['domain']) elif domain_bounds is not None: domain = domains.EuclideanDomain(domain_bounds) else: raise ValueError( 'Domain or path to config file or domain_bounds have to be given.' ) # Create worker manager and function caller worker_manager = SyntheticWorkerManager(num_workers=1, time_distro='caller_eval_cost') if isinstance(domain, domains.EuclideanDomain): func_caller = EuclideanFunctionCaller(func, domain, vectorised=False) else: func_caller = FunctionCaller(func, domain) # Create GPBandit opbject and run optimiser gpb = EuclideanGPBandit(func_caller, worker_manager, reporter=reporter, options=options) opt_val, opt_pt, history = gpb.optimise(max_capital) opt_pt = func_caller.get_raw_domain_coords(opt_pt) history.curr_opt_points = [ func_caller.get_raw_domain_coords(pt) for pt in history.curr_opt_points ] history.query_points = [ func_caller.get_raw_domain_coords(pt) for pt in history.query_points ] return opt_val, opt_pt, history
def _get_optimiser_args(self, optimiser_args=chemist.all_chemist_args): """ Returns the options and reporter. """ reporter = get_reporter('default') options = load_options(optimiser_args, reporter=reporter) options.pre_eval_points = self.ga_init_pool options.pre_eval_vals = self.ga_init_vals options.pre_eval_true_vals = self.ga_init_vals options_clone = deepcopy(options) return options, options_clone, reporter
def __init__(self, X, Y, options=None, reporter=None, *args, **kwargs): """ Constructor. """ self.X = X self.Y = Y self.reporter = get_reporter(reporter) self.num_data = len(X) if options is None: options = load_options(mol_gp_args, 'GPFitter', reporter=reporter) super(MolGPFitter, self).__init__(options, *args, **kwargs)
def prep_optimiser_args(obj_func, optimiser_args): """ Returns the options and reporter. """ reporter = get_reporter('default') options = load_options(optimiser_args, reporter=reporter) options.pre_eval_points = get_initial_pool() options.pre_eval_vals = [obj_func(mol) for mol in options.pre_eval_points] options.pre_eval_true_vals = options.pre_eval_vals options_clone = deepcopy(options) return options, options_clone, reporter
def __init__(self, X, Y, options=None, reporter=None): """ Constructor. """ self.dim = len(X[0]) reporter = get_reporter(reporter) if options is None: options = load_options(euclidean_gp_args, 'EuclideanGP', reporter=reporter) super(EuclideanGPFitter, self).__init__(X, Y, options, reporter)
def __init__(self, ZZ, XX, YY, options=None, reporter=None): """ Constructor. options should either be a Namespace, a list or None. """ reporter = get_reporter(reporter) if options is None: options = load_options(euclidean_mf_gp_args, 'MF-GP', reporter) self.fidel_dim = len(ZZ[0]) self.domain_dim = len(XX[0]) self.input_dim = self.fidel_dim + self.domain_dim super(EuclideanMFGPFitter, self).__init__(ZZ, XX, YY, options, reporter)
def __init__(self, ZZ, XX, YY, options=None, reporter=None): """ Constructor. """ reporter = get_reporter(reporter) if isinstance(options, list): options = load_options(options, 'MFGP', reporter=self.reporter) self.ZZ = ZZ self.XX = XX self.YY = YY self.num_tr_data = len(self.YY) ZX = get_ZX_from_ZZ_XX(ZZ, XX) super(MFGPFitter, self).__init__(ZX, YY, options, reporter)
def __init__(self, func_caller, worker_manager, options=None, reporter=None): """ Constructor. """ self.reporter = get_reporter(reporter) if options is None: options = load_options(all_gp_bandit_args, reporter=reporter) super(GPBandit, self).__init__(func_caller, worker_manager, options, self.reporter)
def get_options_and_reporter(method, init_points, init_vals): """ Returns the options and reporter. """ reporter = get_reporter('default') if method in ['GA', 'randGA']: options = load_options(ga_optimiser.ga_opt_args, reporter=reporter) else: raise ValueError('Unknown method %s.' % (method)) options.pre_eval_points = init_points options.pre_eval_vals = init_vals options.pre_eval_true_vals = init_vals return options, reporter
def __init__(self, mf_opt_func, options=None, reporter=None): """ Constructor. """ self.reporter = get_reporter(reporter) if options is None: options = load_options(all_mf_gp_bandit_args, reporter=reporter) self.options = options # Set up mfgp and mfof attributes self.mfof = mf_opt_func # mfof refers to an MFOptFunction object. self.mfgp = None # Other set up self._set_up()
def __init__(self, func_caller, worker_manager, options=None, reporter=None): """ Constructor. """ self.reporter = get_reporter(reporter) if options is None: options = load_options(blackbox_opt_args, reporter=reporter) super(RandomOptimiser, self).__init__(func_caller, worker_manager, options, self.reporter)
def __init__(self, X, Y, options, reporter='default'): """ Constructor. """ super(GPFitter, self).__init__() assert len(X) == len(Y) self.reporter = get_reporter(reporter) if isinstance(options, list): options = load_options(options, 'GP', reporter=self.reporter) self.options = options self.X = X self.Y = Y self.num_data = len(X) self._set_up()
def __init__(self, func_caller, worker_manager, options=None, reporter=None): if options is None: reporter = get_reporter(reporter) options = load_options(all_chemist_args, reporter=reporter) super(Chemist, self).__init__(func_caller, worker_manager, options=options, reporter=reporter)
def __init__(self, ZZ, XX, YY, options=None, reporter=None): """ Constructor. options should either be a Namespace, a list or None""" reporter = get_reporter(reporter) if options is None: options = load_options(all_mf_gp_args, 'MF-GP', reporter) self.ZZ = ZZ self.XX = XX self.YY = YY self.ZX = np.concatenate((self.ZZ, self.XX), axis=1) self.fidel_dim = self.ZZ.shape[1] self.domain_dim = self.XX.shape[1] self.input_dim = self.fidel_dim + self.domain_dim super(MFGPFitter, self).__init__(options, reporter)
def test_instantiation(self): """ Tests instantiation of the optimiser. """ optimiser = self._child_instantiate_optimiser( self.func_caller, self.worker_manager_3, options=None, reporter=reporters.get_reporter('silent')) self.report('Instantiated %s object.' % (type(optimiser))) for attr in dir(optimiser): if not attr.startswith('_'): self.report( 'optimiser.%s = %s' % (attr, str(getattr(optimiser, attr))), 'test_result')
def random_optimise_from_args(func_caller, worker_manager, max_capital, mode=None, options=None, reporter='default'): """ Random otpimisation from a utils.function_caller.FunctionCaller instance. """ if options is None: reporter = get_reporter(reporter) options = load_options(random_opt_args, reporter=reporter) options.mode = mode return (RandomOptimiser(func_caller, worker_manager, options, reporter)).optimise(max_capital)
def mfgpb_from_mfoptfunc(mf_opt_func, max_capital, acq=None, options=None, reporter='default'): """ MF GP Bandit optimisation with an mf_func.MFOptFunction object. """ # if not isinstance(mf_opt_func, MFOptFunction): # raise ValueError('mf_opt_func should be a mf_func.MFOptFunction instance.') if acq is not None: if options is None: reporter = get_reporter(reporter) options = load_options(all_mf_gp_bandit_args, reporter=reporter) options.acq = acq return (MFGPBandit(mf_opt_func, options, reporter)).optimise(max_capital)