def test_grid_design(self): init_points_count = 3 samples = initial_design('grid', self.design_space, init_points_count) self.assertEqual(len(samples), init_points_count) self.assert_samples_against_space(samples) init_points_count = 1000 samples = initial_design('grid', self.design_space, init_points_count) self.assertEqual(len(samples), init_points_count) self.assert_samples_against_space(samples)
def test_duplicate_with_ignored_and_pending(self): space = [{ 'name': 'var_1', 'type': 'continuous', 'domain': (-3, 1), 'dimensionality': 1 }, { 'name': 'var_2', 'type': 'discrete', 'domain': (0, 1, 2, 3) }, { 'name': 'var_3', 'type': 'categorical', 'domain': (0, 1) }] design_space = Design_space(space) np.random.seed(666) number_points = 5 zipped_X = initial_design("random", design_space, number_points) pending_zipped_X = initial_design("random", design_space, number_points) ignored_zipped_X = initial_design("random", design_space, number_points) d = DuplicateManager(design_space, zipped_X, pending_zipped_X, ignored_zipped_X) duplicate_in_pending_state = np.atleast_2d( pending_zipped_X[0, :].copy()) assert d.is_zipped_x_duplicate(duplicate_in_pending_state) assert d.is_unzipped_x_duplicate( design_space.unzip_inputs(duplicate_in_pending_state)) duplicate_in_ignored_state = np.atleast_2d( ignored_zipped_X[0, :].copy()) assert d.is_zipped_x_duplicate(duplicate_in_ignored_state) assert d.is_unzipped_x_duplicate( design_space.unzip_inputs(duplicate_in_ignored_state))
def test_random_design_with_bandit_only(self): space = [self.bandit_variable] self.design_space = Design_space(space) initial_points_count = 3 samples = initial_design('random', self.design_space, initial_points_count) self.assertEqual(len(samples), initial_points_count)
def test_random_design_with_constraints(self): constraints = [{'name': 'const_1', 'constraint': 'x[:,0]**2 - 1'}] self.design_space = Design_space(self.space, constraints=constraints) initial_points_count = 10 samples = initial_design('random', self.design_space, initial_points_count) self.assert_samples_against_space(samples) self.assertTrue((samples[:, 0]**2 - 1 < 0).all())
def test_grid_design_with_multiple_continuous_variables(self): self.space.extend([{ 'name': 'var_5', 'type': 'continuous', 'domain': (0, 5), 'dimensionality': 2 }, { 'name': 'var_6', 'type': 'continuous', 'domain': (-5, 5), 'dimensionality': 1 }]) self.design_space = Design_space(self.space) init_points_count = 10 samples = initial_design('grid', self.design_space, init_points_count) self.assertEqual(len(samples), 1) init_points_count = 100 samples = initial_design('grid', self.design_space, init_points_count) self.assertEqual(len(samples), 3**4)
def test_nonrandom_designs_with_constrains(self): constraints = [{'name': 'const_1', 'constraint': 'x[:,0]**2 - 1'}] self.design_space = Design_space(self.space, constraints=constraints) initial_points_count = 10 with self.assertRaises(InvalidConfigError): initial_design('grid', self.design_space, initial_points_count) with self.assertRaises(InvalidConfigError): initial_design('latin', self.design_space, initial_points_count) with self.assertRaises(InvalidConfigError): initial_design('sobol', self.design_space, initial_points_count)
def test_duplicate(self): space = [{ 'name': 'var_1', 'type': 'continuous', 'domain': (-3, 1), 'dimensionality': 1 }, { 'name': 'var_2', 'type': 'discrete', 'domain': (0, 1, 2, 3) }, { 'name': 'var_3', 'type': 'categorical', 'domain': (0, 1) }] design_space = Design_space(space) np.random.seed(666) number_points = 5 zipped_X = initial_design("random", design_space, number_points) d = DuplicateManager(design_space, zipped_X) duplicate = np.atleast_2d(zipped_X[0, :].copy()) assert d.is_zipped_x_duplicate(duplicate) assert d.is_unzipped_x_duplicate(design_space.unzip_inputs(duplicate)) non_duplicate = np.array([[-2.5, 2., 0.]]) for x in zipped_X: assert not np.all(non_duplicate == x) assert not d.is_zipped_x_duplicate(non_duplicate) assert not d.is_unzipped_x_duplicate( design_space.unzip_inputs(non_duplicate))
def get_samples(self, n_samples, log_p_function, burn_in_steps=50): samples = initial_design('latin', self.space, n_samples) samples_log = np.array([[i] for i in range(n_samples)]) return samples, samples_log
def test_random_design(self): init_points_count = 10 samples = initial_design('random', self.design_space, init_points_count) self.assertEqual(len(samples), init_points_count) self.assert_samples_against_space(samples)