Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from smac.utils.merge_foreign_data import merge_foreign_data
from smac.utils.io.cmd_reader import truthy as _is_truthy
from smac.utils.io.input_reader import InputReader
from smac.runhistory.runhistory import RunHistory
from smac.optimizer.objective import average_cost
from smac.tae.execute_ta_run import StatusType
in_reader = InputReader()
if sys.version_info[0] == 2:
import mock
else:
from unittest import mock
class InitFreeScenario(Scenario):
def __init__(self):
pass
class ScenarioTest(unittest.TestCase):
def setUp(self):
logging.basicConfig()
self.logger = logging.getLogger(
self.__module__ + '.' + self.__class__.__name__)
self.logger.setLevel(logging.DEBUG)
base_directory = os.path.split(__file__)[0]
base_directory = os.path.abspath(
os.path.join(base_directory, '..', '..'))
def test_get_runs(self):
''' test if the runs are generated as expected '''
scen = Scenario(self.scen_fn,
cmd_options={'run_obj': 'quality',
'train_insts' : self.train_insts,
'test_insts': self.test_insts})
scen.instance_specific = self.inst_specs
validator = Validator(scen, self.trajectory, self.rng)
# Get multiple configs
self.maxDiff=None
expected = [_Run(config='config1', inst='3', seed=1608637542, inst_specs='three'),
_Run(config='config2', inst='3', seed=1608637542, inst_specs='three'),
_Run(config='config1', inst='3', seed=1273642419, inst_specs='three'),
_Run(config='config2', inst='3', seed=1273642419, inst_specs='three'),
_Run(config='config1', inst='4', seed=1935803228, inst_specs='four'),
_Run(config='config2', inst='4', seed=1935803228, inst_specs='four'),
_Run(config='config1', inst='4', seed=787846414, inst_specs='four'),
_Run(config='config2', inst='4', seed=787846414, inst_specs='four'),
@author: Aaron Klein
'''
import logging
import numpy as np
from robo.task.branin import Branin
from robo.initial_design.init_random_uniform import init_random_uniform
from smac.smbo.smbo import SMBO
from smac.scenario.scenario import Scenario
logging.basicConfig(level=logging.DEBUG)
scenario = Scenario()
task = Branin()
X = init_random_uniform(task.X_lower, task.X_upper, 20)
Y = task.evaluate(X)
instance_features = np.array([[1]])
smac = SMBO(scenario)
new_x = smac.choose_next(X, Y)
print new_x
def test_passed_runhistory_deterministic(self):
''' test if passed runhistory is in resulting runhistory '''
scen = Scenario(self.scen_fn,
cmd_options={'run_obj': 'quality',
'train_insts' : self.train_insts,
'deterministic' : True})
scen.instance_specific = self.inst_specs
validator = Validator(scen, self.trajectory, self.rng)
# Add a few runs and check, if they are correctly processed
old_configs = [entry["incumbent"] for entry in self.trajectory]
old_rh = RunHistory(average_cost)
for config in old_configs[:int(len(old_configs)/2)]:
old_rh.add(config, 1, 1, StatusType.SUCCESS, instance_id='0')
configs = validator._get_configs('all')
insts = validator._get_instances('train')
runs_w_rh = validator._get_runs(configs, insts, repetitions=2,
runhistory=old_rh)
runs_wo_rh = validator._get_runs(configs, insts, repetitions=2)
output directory names of a SMAC runs
Returns
-------
trajs: typing.List
list of trajectories
cs: ConfigurationSpace
'''
cwd = os.getcwd()
smac_out_path, smac_out_dn = os.path.split(smac_out_dns[0])
os.chdir(smac_out_path)
# use first run as reference
scenario_fn = os.path.join(smac_out_dn, "scenario.txt")
scenario = Scenario(scenario_fn, {"output_dir": ""})
os.chdir(cwd)
trajs = []
for dn in smac_out_dns:
traj = TrajLogger.read_traj_aclib_format(fn=os.path.join(dn, "traj_aclib2.json"),
cs=scenario.cs)
trajs.append(traj)
return trajs, scenario.cs
random seed for SMAC
Returns
-------
Configuration
best incumbent configuration found by SMAC
'''
wallclock_limit = autofolio_config.get("wallclock_limit", wallclock_limit)
runcount_limit = autofolio_config.get("runcount_limit", runcount_limit)
taf = functools.partial(self.called_by_smac, scenario=scenario)
max_fold = scenario.cv_data.max().max()
max_fold = int(max_fold)
ac_scenario = Scenario({"run_obj": "quality", # we optimize quality
"runcount-limit": runcount_limit,
"cs": self.cs, # configuration space
"deterministic": "true",
"instances": [[str(i)] for i in range(1, max_fold+1)],
"wallclock-limit": wallclock_limit,
"output-dir" : "" if not autofolio_config.get("output-dir",None) else autofolio_config.get("output-dir")
})
# necessary to use stats options related to scenario information
AC_Stats.scenario = ac_scenario
# Optimize
self.logger.info(
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
self.logger.info("Start Configuration")
self.logger.info(
args.out = ''.join(args.out)
# Read the data
data = pd.read_hdf(args.ed)
run_info = pd.read_hdf(args.id)
#traintest = pd.read_hdf(args.tt)
#with open(args.para, 'rb') as fp:
# para = json.load(fp)
#init_design = InitialDesign(cs=data['search_space'],
# init_budget=0.1*data['n_iter'],
# rng=run_info['run_rng'],
# traj_logger=None,
# ta_run_limit=100)
scenario = Scenario({"run_obj": "quality", # optimize for solution quality
#"runcount-limit": data['n_iter'], # max. number of function evaluations;
"wallclock-limit": data['n_iter'],
"cs": data['search_space'],
"deterministic": "true",
"output_dir": "/scratch/mdeen/SMAC_output/" + run_info['run_name'],
"shared_model": False,
"input_psmac_dirs": "/scratch/mdeen/SMAC_output/" + run_info['run_name'],
"abort_on_first_run_crash": "false"
})
def score_cfg(cfg):
# Construct a new dictionary with parameters from the input configuration
# ! THIS POTENTIALLY HAS A BUG BUT PROBABLY NOT ACTUALLY ! #
if cfg is not None:
parameters = convert_cfg(cfg.get_dictionary())
formatter = logging.Formatter(
"%(asctime)s:%(levelname)s:%(name)s:%(message)s",
"%Y-%m-%d %H:%M:%S")
logger_handler.setFormatter(formatter)
root_logger.addHandler(logger_handler)
# remove default handler
root_logger.removeHandler(root_logger.handlers[0])
# Create defaults
rh = None
initial_configs = None
stats = None
incumbent = None
# Create scenario-object
scen = Scenario(args.scenario_file, [])
self.cs = scen.cs
if args.mode == "SMAC":
optimizer = SMAC(
scenario=scen,
rng=np.random.RandomState(args.seed),
runhistory=rh,
initial_configurations=initial_configs,
stats=stats,
restore_incumbent=incumbent,
run_id=args.seed)
elif args.mode == "ROAR":
optimizer = ROAR(
scenario=scen,
rng=np.random.RandomState(args.seed),
runhistory=rh,
def get_random_search_object_callback(
scenario_dict,
seed,
ta,
backend,
metalearning_configurations,
runhistory,
):
"""Random search."""
scenario_dict['input_psmac_dirs'] = backend.get_smac_output_glob()
scenario_dict['minR'] = len(scenario_dict['instances'])
scenario_dict['initial_incumbent'] = 'RANDOM'
scenario = Scenario(scenario_dict)
return ROAR(
scenario=scenario,
rng=seed,
tae_runner=ta,
runhistory=runhistory,
run_id=seed,
)