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_construct_intensifier(self):
class DummyIntensifier(Intensifier):
pass
rng = np.random.RandomState(42)
smbo = SMAC4AC(self.scenario)
self.assertIsInstance(smbo.solver.intensifier, Intensifier)
self.assertIsNot(smbo.solver.intensifier.rs, rng)
smbo = SMAC4AC(self.scenario, rng=rng)
self.assertIsInstance(smbo.solver.intensifier, Intensifier)
self.assertIs(smbo.solver.intensifier.rs, rng)
smbo = SMAC4AC(self.scenario, intensifier_kwargs={'maxR': 987})
self.assertEqual(smbo.solver.intensifier.maxR, 987)
smbo = SMAC4AC(
self.scenario, intensifier=DummyIntensifier, intensifier_kwargs={'maxR': 987},
)
self.assertIsInstance(smbo.solver.intensifier, DummyIntensifier)
self.assertEqual(smbo.solver.intensifier.maxR, 987)
# Check for construction failure on wrong argument
with self.assertRaisesRegex(Exception, 'got an unexpected keyword argument'):
SMAC4AC(self.scenario, intensifier_kwargs={'dummy': 0.1})
def test_construct_intensifier(self):
class DummyIntensifier(Intensifier):
pass
rng = np.random.RandomState(42)
smbo = SMAC4AC(self.scenario)
self.assertIsInstance(smbo.solver.intensifier, Intensifier)
self.assertIsNot(smbo.solver.intensifier.rs, rng)
smbo = SMAC4AC(self.scenario, rng=rng)
self.assertIsInstance(smbo.solver.intensifier, Intensifier)
self.assertIs(smbo.solver.intensifier.rs, rng)
smbo = SMAC4AC(self.scenario, intensifier_kwargs={'maxR': 987})
self.assertEqual(smbo.solver.intensifier.maxR, 987)
smbo = SMAC4AC(
self.scenario, intensifier=DummyIntensifier, intensifier_kwargs={'maxR': 987},
)
self.assertIsInstance(smbo.solver.intensifier, DummyIntensifier)
self.assertEqual(smbo.solver.intensifier.maxR, 987)
# Check for construction failure on wrong argument
with self.assertRaisesRegex(Exception, 'got an unexpected keyword argument'):
SMAC4AC(self.scenario, intensifier_kwargs={'dummy': 0.1})
def test_choose_next_2(self):
# Test with a single configuration in the runhistory!
def side_effect(X):
return np.mean(X, axis=1).reshape((-1, 1))
def side_effect_predict(X):
m, v = np.ones((X.shape[0], 1)), None
return m, v
smbo = SMAC4AC(self.scenario, rng=1).solver
smbo.incumbent = self.scenario.cs.sample_configuration()
smbo.runhistory = RunHistory(aggregate_func=average_cost)
smbo.runhistory.add(smbo.incumbent, 10, 10, 1)
smbo.model = mock.Mock(spec=RandomForestWithInstances)
smbo.model.predict_marginalized_over_instances.side_effect = side_effect_predict
smbo.acquisition_func._compute = mock.Mock(spec=RandomForestWithInstances)
smbo.acquisition_func._compute.side_effect = side_effect
X = smbo.rng.rand(10, 2)
Y = smbo.rng.rand(10, 1)
challengers = smbo.choose_next(X, Y)
# Convert challenger list (a generator) to a real list
challengers = [c for c in challengers]
self.assertEqual(smbo.model.train.call_count, 1)
def test_construct_random_configuration_chooser(self):
rng = np.random.RandomState(42)
smbo = SMAC4AC(self.scenario)
self.assertIsInstance(smbo.solver.random_configuration_chooser, ChooserProb)
self.assertIsNot(smbo.solver.random_configuration_chooser, rng)
smbo = SMAC4AC(self.scenario, rng=rng)
self.assertIsInstance(smbo.solver.random_configuration_chooser, ChooserProb)
self.assertIs(smbo.solver.random_configuration_chooser.rng, rng)
smbo = SMAC4AC(self.scenario, random_configuration_chooser_kwargs={'rng': rng})
self.assertIsInstance(smbo.solver.random_configuration_chooser, ChooserProb)
self.assertIs(smbo.solver.random_configuration_chooser.rng, rng)
smbo = SMAC4AC(self.scenario, random_configuration_chooser_kwargs={'prob': 0.1})
self.assertIsInstance(smbo.solver.random_configuration_chooser, ChooserProb)
self.assertEqual(smbo.solver.random_configuration_chooser.prob, 0.1)
smbo = SMAC4AC(
self.scenario,
random_configuration_chooser=ChooserCosineAnnealing,
random_configuration_chooser_kwargs={'prob_max': 1, 'prob_min': 0.1, 'restart_iteration': 10},
)
self.assertIsInstance(smbo.solver.random_configuration_chooser, ChooserCosineAnnealing)
# Check for construction failure on wrong argument
with self.assertRaisesRegex(Exception, 'got an unexpected keyword argument'):
SMAC4AC(self.scenario, random_configuration_chooser_kwargs={'dummy': 0.1})
def test_choose_next_empty_X_2(self):
smbo = SMAC4AC(self.scenario, rng=1).solver
X = np.zeros((0, 2))
Y = np.zeros((0, 1))
challengers = smbo.choose_next(X, Y)
x = [c for c in challengers]
self.assertEqual(len(x), 1)
self.assertIsInstance(x[0], Configuration)
def test_rf_comp_builder(self):
seed = 42
smbo = SMAC4AC(self.scenario, rng=seed).solver
conf = {"model": "RF", "acq_func": "EI"}
acqf, model = smbo._component_builder(conf)
self.assertTrue(isinstance(acqf, EI))
self.assertTrue(isinstance(model, RandomForestWithInstances))
def test_construct_acquisition_function(self):
rng = np.random.RandomState(42)
smbo = SMAC4AC(self.scenario)
self.assertIsInstance(smbo.solver.acquisition_func, EI)
smbo = SMAC4AC(self.scenario, rng=rng)
self.assertIsInstance(smbo.solver.acquisition_func.model, RandomForestWithInstances)
self.assertEqual(smbo.solver.acquisition_func.model.seed, 1935803228)
smbo = SMAC4AC(self.scenario, acquisition_function_kwargs={'par': 17})
self.assertIsInstance(smbo.solver.acquisition_func, EI)
self.assertEqual(smbo.solver.acquisition_func.par, 17)
smbo = SMAC4AC(self.scenario, acquisition_function=LCB, acquisition_function_kwargs={'par': 19})
self.assertIsInstance(smbo.solver.acquisition_func, LCB)
self.assertEqual(smbo.solver.acquisition_func.par, 19)
# Check for construction failure on wrong argument
with self.assertRaisesRegex(Exception, 'got an unexpected keyword argument'):
SMAC4AC(self.scenario, acquisition_function_kwargs={'dummy': 0.1})
def test_choose_next_w_empty_rh(self):
seed = 42
smbo = SMAC4AC(self.scenario, rng=seed).solver
smbo.runhistory = RunHistory(aggregate_func=average_cost)
X = self.scenario.cs.sample_configuration().get_array()[None, :]
Y = self.branin(X)
self.assertRaisesRegex(
ValueError,
'Runhistory is empty and the cost value of the incumbent is '
'unknown.',
smbo.choose_next,
**{"X": X, "Y": Y}
)
x = next(smbo.choose_next(X, Y, incumbent_value=0.0)).get_array()
assert x.shape == (2,)
def test_choose_next_empty_X_2(self):
epm_chooser = SMAC4AC(self.scenario, rng=1).solver.epm_chooser
challengers = epm_chooser.choose_next()
x = [c for c in challengers]
self.assertEqual(len(x), 1)
self.assertIsInstance(x[0], Configuration)
def validate_incs(self, incs: np.ndarray):
"""
Validation
"""
solver = SMAC4AC(scenario=self.scenario, rng=self.rng, run_id=MAXINT, **self.kwargs)
self.logger.info('*' * 120)
self.logger.info('Validating')
new_rh = solver.validate(config_mode=incs,
instance_mode=self.val_set,
repetitions=1,
use_epm=False,
n_jobs=self.n_optimizers)
return self._get_mean_costs(incs, new_rh)