Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
if random_configuration_chooser_kwargs is not None:
rand_conf_chooser_kwargs.update(random_configuration_chooser_kwargs)
if random_configuration_chooser is None:
if 'prob' not in rand_conf_chooser_kwargs:
rand_conf_chooser_kwargs['prob'] = scenario.rand_prob
random_configuration_chooser = ChooserProb(**rand_conf_chooser_kwargs)
elif inspect.isclass(random_configuration_chooser):
random_configuration_chooser = random_configuration_chooser(**rand_conf_chooser_kwargs)
elif not isinstance(random_configuration_chooser, RandomConfigurationChooser):
raise ValueError("random_configuration_chooser has to be"
" a class or object of RandomConfigurationChooser")
# reset random number generator in config space to draw different
# random configurations with each seed given to SMAC
scenario.cs.seed(rng.randint(MAXINT))
# initial Trajectory Logger
traj_logger = TrajLogger(output_dir=self.output_dir, stats=self.stats)
# initial EPM
types, bounds = get_types(scenario.cs, scenario.feature_array)
model_def_kwargs = {
'types': types,
'bounds': bounds,
'instance_features': scenario.feature_array,
'seed': rng.randint(MAXINT),
'pca_components': scenario.PCA_DIM,
}
if model_kwargs is not None:
model_def_kwargs.update(model_kwargs)
if model is None:
num_run, rng = self._get_rng(rng=rng)
# reset random number generator in config space to draw different
# random configurations with each seed given to SMAC
scenario.cs.seed(rng.randint(MAXINT))
# initial Trajectory Logger
traj_logger = TrajLogger(
output_dir=scenario.output_dir, stats=self.stats)
# initial EPM
types, bounds = get_types(scenario.cs, scenario.feature_array)
if model is None:
model = RandomForestWithInstances(types=types, bounds=bounds,
instance_features=scenario.feature_array,
seed=rng.randint(MAXINT),
pca_components=scenario.PCA_DIM)
# initial acquisition function
if acquisition_function is None:
if scenario.run_obj == "runtime":
acquisition_function = LogEI(model=model)
else:
acquisition_function = EI(model=model)
# inject model if necessary
if acquisition_function.model is None:
acquisition_function.model = model
# initialize optimizer on acquisition function
local_search = LocalSearch(acquisition_function,
scenario.cs)
# initialize tae_runner
Initial incumbent configuration
"""
if initial_incumbent.origin is None:
initial_incumbent.origin = 'Initial design'
# add this incumbent right away to have an entry to time point 0
self.traj_logger.add_entry(train_perf=2 ** 31,
incumbent_id=1,
incumbent=initial_incumbent)
rand_inst = self.rng.choice(self.scenario.train_insts)
if self.scenario.deterministic:
initial_seed = 0
else:
initial_seed = self.rng.randint(0, constants.MAXINT)
try:
status, cost, runtime, _ = self.tae_runner.start(
initial_incumbent,
instance=rand_inst,
cutoff=self.scenario.cutoff,
seed=initial_seed,
instance_specific=self.scenario.instance_specific.get(rand_inst,
"0"))
except FirstRunCrashedException as err:
if self.scenario.abort_on_first_run_crash:
raise err
else:
# TODO make it possible to add the failed run to the runhistory
if self.scenario.run_obj == "quality":
cost = self.scenario.cost_for_crash
def __init__(self, ta, stats=None, runhistory=None, run_obj:str="quality",
memory_limit:int=None, par_factor:int=1,
cost_for_crash:float=float(MAXINT),
abort_on_first_run_crash: bool=False,
use_pynisher:bool=True):
super().__init__(ta=ta, stats=stats, runhistory=runhistory,
run_obj=run_obj, par_factor=par_factor,
cost_for_crash=cost_for_crash)
"""
Abstract class for having a function as target algorithm
Parameters
----------
ta : callable
Function (target algorithm) to be optimized.
stats: Stats()
stats object to collect statistics about runtime and so on
runhistory: RunHistory
"""
if logger is None:
logger = logging.getLogger('GetRNG')
# initialize random number generator
if rng is not None and not isinstance(rng, (int, np.random.RandomState)):
raise TypeError('Argument rng accepts only arguments of type None, int or np.random.RandomState, '
'you provided %s.' % str(type(rng)))
if run_id is not None and not isinstance(run_id, int):
raise TypeError('Argument run_id accepts only arguments of type None, int or np.random.RandomState, '
'you provided %s.' % str(type(run_id)))
if rng is None and run_id is None:
# Case that both are None
logger.debug('No rng and no run_id given: using a random value to initialize run_id.')
rng = np.random.RandomState()
run_id = rng.randint(MAXINT)
elif rng is None and isinstance(run_id, int):
logger.debug('No rng and no run_id given: using run_id %d as seed.', run_id)
rng = np.random.RandomState(seed=run_id)
elif isinstance(rng, int):
if run_id is None:
run_id = rng
else:
pass
rng = np.random.RandomState(seed=rng)
elif isinstance(rng, np.random.RandomState):
if run_id is None:
run_id = rng.randint(MAXINT)
else:
pass
else:
raise ValueError('This should not happen! Please contact the developers! Arguments: rng=%s of type %s and '
def __init__(self, tae_runner: ExecuteTARun, stats: Stats,
traj_logger: TrajLogger, rng: np.random.RandomState,
instances: typing.List[str],
instance_specifics: typing.Mapping[str, np.ndarray]=None,
cutoff: int=MAX_CUTOFF, deterministic:bool=False,
run_obj_time: bool=True,
always_race_against: Configuration=None,
run_limit: int=MAXINT,
use_ta_time_bound: bool=False,
minR: int=1, maxR: int=2000,
adaptive_capping_slackfactor: float=1.2,
min_chall: int=2):
self.logger = logging.getLogger(
self.__module__ + "." + self.__class__.__name__)
self.stats = stats
self.traj_logger = traj_logger
# general attributes
if instances is None:
instances = []
self.instances = set(instances)
if instance_specifics is None:
self.instance_specifics = {}
else:
self.logger.debug("No run for incumbent found")
max_runs = 0
inc_inst = set([x[0] for x in inc_inst if x[1] == max_runs])
available_insts = (self.instances - inc_inst)
# if all instances were used n times, we can pick an instances
# from the complete set again
if not self.deterministic and not available_insts:
available_insts = self.instances
# Line 6 (Line 5 is further down...)
if self.deterministic:
next_seed = 0
else:
next_seed = self.rs.randint(low=0, high=MAXINT,
size=1)[0]
if available_insts:
# Line 5 (here for easier code)
next_instance = self.rs.choice(list(available_insts))
# Line 7
self.logger.debug("Add run of incumbent")
status, cost, dur, res = self.tae_runner.start(
config=incumbent,
instance=next_instance,
seed=next_seed,
cutoff=self.cutoff,
instance_specific=self.instance_specifics.get(next_instance, "0"))
self._ta_time += dur
self._num_run += 1
else:
configuration specifying "model", "acq_func" and "y_transform"
Returns
-------
typing.Tuple[AbstractAcquisitionFunction,
AbstractEPM,
AbstractRunHistory2EPM]
"""
types, bounds = get_types(self.config_space, instance_features=self.scenario.feature_array)
if conf["model"] == "RF":
model = RandomForestWithInstances(
types=types,
bounds=bounds,
instance_features=self.scenario.feature_array,
seed=self.rng.randint(MAXINT),
pca_components=conf.get("pca_dim", self.scenario.PCA_DIM),
# TODO add log-space to the inner hyperparameter sampling procedure
log_y=conf.get("log_y", self.scenario.transform_y in ["LOG", "LOGS"]),
num_trees=conf.get("num_trees", self.scenario.rf_num_trees),
do_bootstrapping=conf.get("do_bootstrapping", self.scenario.rf_do_bootstrapping),
ratio_features=conf.get("ratio_features", self.scenario.rf_ratio_features),
min_samples_split=int(conf.get("min_samples_to_split", self.scenario.rf_min_samples_split)),
min_samples_leaf=int(conf.get("min_samples_in_leaf", self.scenario.rf_min_samples_leaf)),
max_depth=int(conf.get("max_depth", self.scenario.rf_max_depth)),
)
elif conf["model"] == "GP":
cov_amp = 2
n_dims = len(types)
initial_ls = np.ones([n_dims])
exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims)
X matrix with configuartion x features for all observed samples
y: np.array
y matrix with all observations
types: np.array
types of X cols -- necessary to train our RF implementation
"""
if rng is None:
rng = np.random.RandomState(42)
if impute_inactive_parameters:
runhistory = force_finite_runhistory(runhistory)
types, bounds = get_types(scenario.cs, scenario.feature_array)
if logger is not None:
logger.debug("Types: " + str(types) + ", Bounds: " + str(bounds))
model = RandomForestWithInstances(scenario.cs, types, bounds, rng.randint(MAXINT))
params = scenario.cs.get_hyperparameters()
num_params = len(params)
run_obj = scenario.run_obj
if run_obj == "runtime":
# if we log the performance data,
# the RFRImputator will already get
# log transform data from the runhistory
cutoff = np.log10(scenario.cutoff)
threshold = np.log10(scenario.cutoff *
scenario.par_factor)
imputor = RFRImputator(rng=rng,
cutoff=cutoff,
Returns
-------
int, np.random.RandomState
"""
# initialize random number generator
if rng is None:
self.logger.debug('no rng given, using default seed of 1')
num_run = 1
rng = np.random.RandomState(seed=num_run)
elif isinstance(rng, int):
num_run = rng
rng = np.random.RandomState(seed=rng)
elif isinstance(rng, np.random.RandomState):
num_run = rng.randint(MAXINT)
rng = rng
else:
raise TypeError('Unknown type %s for argument rng. Only accepts '
'None, int or np.random.RandomState' % str(type(rng)))
return num_run, rng