def test_objective_anchor_points_with_duplicate(self): space = [{ 'name': 'var_1', 'type': 'discrete', 'domain': (-1, 2) }, { 'name': 'var_2', 'type': 'discrete', 'domain': (0, 1) }, { 'name': 'var_3', 'type': 'categorical', 'domain': (0, 1) }] design_space = Design_space(space) np.random.seed(666) design_type = "random" dummy_objective = lambda X: np.sum(X * X, axis=1) generator = ObjectiveAnchorPointsGenerator(design_space, design_type, dummy_objective, num_samples=1000) initial_points = np.array([[-1, 1, 0], [-1, 1, 1]]) duplicate_manager = DuplicateManager(design_space, initial_points) # There is a total of 2x2x2=8 possible configurations, minus the 2 defined in initial_points solution = np.array([[-1., 0., 1., 0.], [-1., 0., 0., 1.], [2., 0., 1., 0.], [2., 0., 0., 1.], [2., 1., 1., 0.], [2., 1., 0., 1.]]) anchor_points = generator.get(num_anchor=6, duplicate_manager=duplicate_manager, unique=True) self.assertTrue(np.all(anchor_points == solution)) all_points = np.vstack( (initial_points, design_space.zip_inputs(solution))) duplicate_manager_with_all_points = DuplicateManager( design_space, all_points) # There aren't any more candidates to generate, hence the exception self.assertRaises( Exception, lambda: generator.get(num_anchor=1, duplicate_manager= duplicate_manager_with_all_points, unique=True))
def _compute_next_evaluations(self, pending_zipped_X=None, ignored_zipped_X=None): """ Computes the location of the new evaluation (optimizes the acquisition in the standard case). :param pending_zipped_X: matrix of input configurations that are in a pending state (i.e., do not have an evaluation yet). :param ignored_zipped_X: matrix of input configurations that the user black-lists, i.e., those configurations will not be suggested again. :return: """ ## --- Update the context if any self.acquisition.optimizer.context_manager = ContextManager( self.space, self.context) ### --- Activate de_duplication if self.de_duplication: duplicate_manager = DuplicateManager( space=self.space, zipped_X=self.X, pending_zipped_X=pending_zipped_X, ignored_zipped_X=ignored_zipped_X) else: duplicate_manager = None ### We zip the value in case there are categorical variables return self.space.zip_inputs( self.evaluator.compute_batch( duplicate_manager=duplicate_manager, context_manager=self.acquisition.optimizer.context_manager))
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 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 _compute_setting(self, pending_zipped_X, ignored_zipped_X): context_manager = ContextManager(self.subspace, self.context) # --- Update the context if any self.acquisition.optimizer.context_manager = context_manager # --- Activate de_duplication if self.de_duplication: duplicate_manager = DuplicateManager( space=self.subspace, zipped_X=self.X, pending_zipped_X=pending_zipped_X, ignored_zipped_X=ignored_zipped_X) else: duplicate_manager = None return context_manager, duplicate_manager
def _compute_next_evaluations(self, pending_zipped_X=None, ignored_zipped_X=None): # --- Update the context if any self.acquisition.optimizer.context_manager = ContextManager(self.subspace, self.context) # --- Activate de_duplication if self.de_duplication: duplicate_manager = DuplicateManager( space=self.subspace, zipped_X=self.X, pending_zipped_X=pending_zipped_X, ignored_zipped_X=ignored_zipped_X) else: duplicate_manager = None # We zip the value in case there are categorical variables suggested_ = self.subspace.zip_inputs(self.evaluator.compute_batch( duplicate_manager=duplicate_manager, context_manager=self.acquisition.optimizer.context_manager)) return suggested_