Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
scen = Scenario("test/test_files/scenario.txt")
rh = RunHistory(average_cost)
rh.load_json("test/test_files/output/runhistory.json", scen.cs)
traj_fn = "test/test_files/output/traj_aclib2.json"
trajectory = TrajLogger.read_traj_aclib_format(fn=traj_fn, cs=scen.cs)
train = scen.train_insts
analyzer = Analyzer(scen, rh, train, "test/test_files/tmp")
default = scen.cs.get_default_configuration()
incumbent = trajectory[-1]["incumbent"]
self.default_cost = analyzer.get_cost_per_instance(default,
aggregate=np.mean)
self.inc_cost = analyzer.get_cost_per_instance(incumbent,
aggregate=np.mean)
self.plot = Plotter()
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
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
# directory, create a new scenario with an extended budget:
new_scenario = Scenario(orig_scen_dict,
cmd_options={'runcount_limit': 50, # overwrite these args
'output_dir' : 'restored'})
# We load the runhistory, ...
rh_path = os.path.join(old_output_dir, "runhistory.json")
runhistory = RunHistory(aggregate_func=None)
runhistory.load_json(rh_path, new_scenario.cs)
# ... stats, ...
stats_path = os.path.join(old_output_dir, "stats.json")
stats = Stats(new_scenario)
stats.load(stats_path)
# ... and trajectory.
traj_path = os.path.join(old_output_dir, "traj_aclib2.json")
trajectory = TrajLogger.read_traj_aclib_format(
fn=traj_path, cs=new_scenario.cs)
incumbent = trajectory[-1]["incumbent"]
# Now we can initialize SMAC with the recovered objects and restore the
# state where we left off. By providing stats and a restore_incumbent, SMAC
# automatically detects the intention of restoring a state.
smac = SMAC(scenario=new_scenario,
runhistory=runhistory,
stats=stats,
restore_incumbent=incumbent,
run_id=1)
# Because we changed the output_dir, we might want to copy the old
# trajectory-file (runhistory and stats will be complete, but trajectory is
# written sequentially)
new_traj_path = os.path.join(new_scenario.output_dir, "run_1", "traj_aclib2.json")
shutil.copy(traj_path, new_traj_path)
self.scen_fn = os.path.join(folder, 'scenario.txt')
self.rh_fn = os.path.join(folder, 'runhistory.json')
self.traj_fn = os.path.join(folder, 'traj_aclib2.json')
self.traj_old_fn = os.path.join(folder, 'traj_old.csv')
# Create Scenario (disable output_dir to avoid cluttering)
scen_dict = in_reader.read_scenario_file(self.scen_fn)
scen_dict['output_dir'] = ""
with changedir(ta_exec_dir):
self.scen = Scenario(scen_dict)
# Load runhistory and trajectory
self.runhistory = RunHistory(average_cost)
self.runhistory.update_from_json(self.rh_fn, self.scen.cs)
self.traj = TrajLogger.read_traj_aclib_format(fn=self.traj_fn,
cs=self.scen.cs)
incumbent = self.traj[-1]['incumbent']
self.train_inst = self.scen.train_insts
self.test_inst = self.scen.test_insts
# Initialize SMAC-object
super().__init__(scenario=self.scen, runhistory=self.runhistory)
#restore_incumbent=incumbent)
# TODO use restore, delete next line
self.solver.incumbent = incumbent
if (not run_1_existed) and os.path.exists('run_1'):
shutil.rmtree('run_1')
repetitions: int
number of repetitions in nondeterministic algorithms (in
deterministic will be fixed to 1)
use_epm: bool
whether to use an EPM instead of evaluating all runs with the TAE
n_jobs: int
number of parallel processes used by joblib
Returns
-------
runhistory: RunHistory
runhistory containing all specified runs
"""
if isinstance(config_mode, str):
traj_fn = os.path.join(self.scenario.output_dir_for_this_run, "traj_aclib2.json")
trajectory = TrajLogger.read_traj_aclib_format(fn=traj_fn, cs=self.scenario.cs)
else:
trajectory = None
if self.scenario.output_dir_for_this_run:
new_rh_path = os.path.join(self.scenario.output_dir_for_this_run, "validated_runhistory.json")
else:
new_rh_path = None
validator = Validator(self.scenario, trajectory, self.rng)
if use_epm:
new_rh = validator.validate_epm(config_mode=config_mode,
instance_mode=instance_mode,
repetitions=repetitions,
runhistory=self.runhistory,
output_fn=new_rh_path)
else:
new_rh = validator.validate(config_mode, instance_mode, repetitions,
v = float(v)
else:
v = v
##############################################
config_dict[k] = v
config = Configuration(configuration_space=cs, values=config_dict)
config.origin = "External Trajectory"
return config
TrajLogger._convert_dict_to_config = alternative_configuration_recovery
traj_fn = os.path.join(self.folder, 'traj_aclib2.json')
if not os.path.isfile(traj_fn):
traj_fn = self.get_glob_file(self.folder, 'traj_aclib2.json')
traj = TrajLogger.read_traj_aclib_format(fn=traj_fn, cs=cs)
return traj
scenario_fn = os.path.join(smac_out_dns[0], "scenario.txt")
scenario = Scenario(scenario_fn, {"output_dir": ""})
smac = SMAC(scenario=scenario)
rh = smac.solver.runhistory
rh.load_json(os.path.join(smac_out_dns[0], "runhistory.json"), cs=scenario.cs)
for dn in smac_out_dns[1:]:
try:
rh.update_from_json(fn=os.path.join(dn, "runhistory.json"), cs=scenario.cs)
except json.decoder.JSONDecodeError:
print("Failed to read %s" %(os.path.join(dn, "runhistory.json")))
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 smac, trajs