Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cutoff_too_large(self):
target = lambda x: x**2
taf = ExecuteTAFuncDict(ta=target, stats=self.stats)
self.assertRaises(ValueError, taf.run, config=2, cutoff=65536)
def test_eval_challenger_2(self):
"""
test eval_challenger with runtime objective and adaptive capping
"""
def target(x):
time.sleep(1.5)
return (x['a'] + 1) / 1000.
taf = ExecuteTAFuncDict(ta=target, stats=self.stats, run_obj="runtime")
taf.runhistory = self.rh
intensifier = SuccessiveHalving(
tae_runner=taf, stats=self.stats,
traj_logger=TrajLogger(output_dir=None, stats=self.stats),
rng=np.random.RandomState(12345), deterministic=True, cutoff=1,
instances=[1, 2], initial_budget=1, max_budget=2, eta=2, instance_order=None)
for i in range(2):
self.rh.add(config=self.config1, cost=.001, time=0.001,
status=StatusType.SUCCESS, instance_id=i + 1, seed=0,
additional_info=None)
intensifier._update_stage()
# config2 should be capped and config1 should still be the incumbent
def setUp(self):
self.cs = ConfigurationSpace()
self.cs.add_hyperparameter(UniformFloatHyperparameter(
name="x1", lower=1, upper=10, default_value=2)
)
self.scenario = Scenario({'cs': self.cs, 'run_obj': 'quality',
'output_dir': ''})
self.ta = ExecuteTAFuncDict(lambda x: x["x1"]**2)
def test_eval_challenger(self):
"""
since hyperband uses eval_challenger and get_next_challenger of the internal successive halving,
we don't test these method extensively
"""
def target(x):
return 0.1
taf = ExecuteTAFuncDict(ta=target, stats=self.stats)
taf.runhistory = self.rh
intensifier = Hyperband(
tae_runner=taf, stats=self.stats,
traj_logger=TrajLogger(output_dir=None, stats=self.stats),
rng=np.random.RandomState(12345), deterministic=True, run_obj_time=False,
instances=[None], initial_budget=0.5, max_budget=1, eta=2)
self.assertFalse(hasattr(intensifier, 's'))
# Testing get_next_challenger - get next configuration
config, _ = intensifier.get_next_challenger(challengers=[self.config2, self.config3],
chooser=None, run_history=self.rh)
self.assertEqual(intensifier.s, intensifier.s_max)
self.assertEqual(config, self.config2)
self.assertGreaterEqual(rval[2], 0.0)
self.assertEqual(rval[3], dict())
target = lambda x, seed: (x ** 2, {'key': seed})
taf = ExecuteTAFuncDict(ta=target, stats=self.stats)
rval = taf.run(config=2, instance='test')
self.assertFalse(taf._accepts_instance)
self.assertTrue(taf._accepts_seed)
self.assertEqual(rval[0], StatusType.SUCCESS)
self.assertEqual(rval[1], 4)
self.assertGreaterEqual(rval[2], 0.0)
self.assertEqual(rval[3], {'key': 12345})
target = lambda x, seed, instance: (x ** 2, {'key': seed,
'instance': instance})
taf = ExecuteTAFuncDict(ta=target, stats=self.stats)
rval = taf.run(config=2, instance='test')
self.assertTrue(taf._accepts_instance)
self.assertTrue(taf._accepts_seed)
self.assertEqual(rval[0], StatusType.SUCCESS)
self.assertEqual(rval[1], 4)
self.assertGreaterEqual(rval[2], 0.0)
self.assertEqual(rval[3], {'key': 12345, 'instance': 'test'})
def test_eval_challenger_3(self):
"""
test eval_challenger for updating to next stage and shuffling instance order every run
"""
def target(x: Configuration, instance: str):
return (x['a'] + int(instance)) / 1000.
taf = ExecuteTAFuncDict(ta=target, stats=self.stats, run_obj="quality")
taf.runhistory = self.rh
intensifier = SuccessiveHalving(
tae_runner=taf, stats=self.stats,
traj_logger=TrajLogger(output_dir=None, stats=self.stats),
rng=np.random.RandomState(12345), run_obj_time=False,
instances=[0, 1], instance_order='shuffle', eta=2,
deterministic=True, cutoff=1)
intensifier._update_stage()
self.assertEqual(intensifier.inst_seed_pairs, [(0, 0), (1, 0)])
config, _ = intensifier.get_next_challenger(challengers=[self.config1], chooser=None, run_history=self.rh)
inc, _ = intensifier.eval_challenger(challenger=config,
incumbent=None,
'par_factor': scenario.par_factor,
'cost_for_crash': scenario.cost_for_crash,
'abort_on_first_run_crash': scenario.abort_on_first_run_crash
}
if tae_runner_kwargs is not None:
tae_def_kwargs.update(tae_runner_kwargs)
if 'ta' not in tae_def_kwargs:
tae_def_kwargs['ta'] = scenario.ta
if tae_runner is None:
tae_def_kwargs['ta'] = scenario.ta
tae_runner = ExecuteTARunOld(**tae_def_kwargs)
elif inspect.isclass(tae_runner):
tae_runner = tae_runner(**tae_def_kwargs)
elif callable(tae_runner):
tae_def_kwargs['ta'] = tae_runner
tae_runner = ExecuteTAFuncDict(**tae_def_kwargs)
else:
raise TypeError("Argument 'tae_runner' is %s, but must be "
"either None, a callable or an object implementing "
"ExecuteTaRun. Passing 'None' will result in the "
"creation of target algorithm runner based on the "
"call string in the scenario file."
% type(tae_runner))
# Check that overall objective and tae objective are the same
if tae_runner.run_obj != scenario.run_obj:
raise ValueError("Objective for the target algorithm runner and "
"the scenario must be the same, but are '%s' and "
"'%s'" % (tae_runner.run_obj, scenario.run_obj))
# initialize intensification
intensifier_def_kwargs = {
max_steps=scenario.sls_max_steps,
n_steps_plateau_walk=scenario.sls_n_steps_plateau_walk)
# initialize tae_runner
# First case, if tae_runner is None, the target algorithm is a call
# string in the scenario file
if tae_runner is None:
tae_runner = ExecuteTARunOld(ta=scenario.ta,
stats=self.stats,
run_obj=scenario.run_obj,
runhistory=runhistory,
par_factor=scenario.par_factor,
cost_for_crash=scenario.cost_for_crash)
# Second case, the tae_runner is a function to be optimized
elif callable(tae_runner):
tae_runner = ExecuteTAFuncDict(ta=tae_runner,
stats=self.stats,
run_obj=scenario.run_obj,
memory_limit=scenario.memory_limit,
runhistory=runhistory,
par_factor=scenario.par_factor,
cost_for_crash=scenario.cost_for_crash)
# Third case, if it is an ExecuteTaRun we can simply use the
# instance. Otherwise, the next check raises an exception
elif not isinstance(tae_runner, ExecuteTARun):
raise TypeError("Argument 'tae_runner' is %s, but must be "
"either a callable or an instance of "
"ExecuteTaRun. Passing 'None' will result in the "
"creation of target algorithm runner based on the "
"call string in the scenario file."
% type(tae_runner))
Constructor
Arguments
---------
cost_oracle: typing.Mapping[str,float]
cost of oracle per instance
'''
super().__init__(**kwargs)
self.cost_oracle = cost_oracle
if tae is ExecuteTARunAClib:
self.runner = ExecuteTARunAClib(**kwargs)
elif tae is ExecuteTARunOld:
self.runner = ExecuteTARunOld(**kwargs)
elif tae is ExecuteTAFuncDict:
self.runner = ExecuteTAFuncDict(**kwargs)
elif tae is ExecuteTAFuncArray:
self.runner = ExecuteTAFuncArray(**kwargs)
else:
raise Exception('TAE not supported')
local_search = LocalSearch(acquisition_function,
scenario.cs)
# initialize tae_runner
# First case, if tae_runner is None, the target algorithm is a call
# string in the scenario file
if tae_runner is None:
tae_runner = ExecuteTARunOld(ta=scenario.ta,
stats=self.stats,
run_obj=scenario.run_obj,
runhistory=runhistory,
par_factor=scenario.par_factor,
cost_for_crash=scenario.cost_for_crash)
# Second case, the tae_runner is a function to be optimized
elif callable(tae_runner):
tae_runner = ExecuteTAFuncDict(ta=tae_runner,
stats=self.stats,
run_obj=scenario.run_obj,
memory_limit=scenario.memory_limit,
runhistory=runhistory,
par_factor=scenario.par_factor,
cost_for_crash=scenario.cost_for_crash)
# Third case, if it is an ExecuteTaRun we can simply use the
# instance. Otherwise, the next check raises an exception
elif not isinstance(tae_runner, ExecuteTARun):
raise TypeError("Argument 'tae_runner' is %s, but must be "
"either a callable or an instance of "
"ExecuteTaRun. Passing 'None' will result in the "
"creation of target algorithm runner based on the "
"call string in the scenario file."
% type(tae_runner))