def test_round_optimum_shapes(self): space = [{ 'name': 'var1', 'type': 'continuous', 'domain': (-1, 1), 'dimensionality': 1 }] with self.assertRaises(ValueError): design_space = Design_space(space) design_space.round_optimum([[[0.0]]]) with self.assertRaises(ValueError): design_space = Design_space(space) design_space.round_optimum(np.array([[[0.0]]])) with self.assertRaises(ValueError): design_space = Design_space(space) design_space.round_optimum(np.array([[0.0], [2.0]])) # Next couple of tests are intentionally very simple # as they just verify that exception is not thrown for the given input shape design_space = Design_space(space) rounded = design_space.round_optimum([0.0]) self.assertEqual(rounded[0], 0.0) rounded = design_space.round_optimum(np.array([0.0])) self.assertEqual(rounded[0], 0.0) rounded = design_space.round_optimum([[0.0]]) self.assertEqual(rounded[0], 0.0) rounded = design_space.round_optimum(np.array([[0.0]])) self.assertEqual(rounded[0], 0.0)
def test_round_optimum_shapes(self): space = [{'name': 'var1', 'type': 'continuous', 'domain':(-1,1), 'dimensionality': 1}] with self.assertRaises(ValueError): design_space = Design_space(space) design_space.round_optimum([[[0.0]]]) with self.assertRaises(ValueError): design_space = Design_space(space) design_space.round_optimum(np.array([[[0.0]]])) with self.assertRaises(ValueError): design_space = Design_space(space) design_space.round_optimum(np.array([[0.0], [2.0]])) # Next couple of tests are intentionally very simple # as they just verify that exception is not thrown for the given input shape design_space = Design_space(space) rounded = design_space.round_optimum([0.0]) self.assertEqual(rounded[0], 0.0) rounded = design_space.round_optimum(np.array([0.0])) self.assertEqual(rounded[0], 0.0) rounded = design_space.round_optimum([[0.0]]) self.assertEqual(rounded[0], 0.0) rounded = design_space.round_optimum(np.array([[0.0]])) self.assertEqual(rounded[0], 0.0)
def get_GP_optimum(obj): """ Finds the optimal design by maximising the mean of the surrogate probabilistic GP model. Parameters ---------- obj: GPyOpt object The GPyOpt object with a surrogate probabilistic model. """ # Define space space = Design_space(obj.domain, obj.constraints) bounds = space.get_bounds() # Specify Optimizer --- L-BFGS optimizer = OptLbfgs(space.get_bounds(), maxiter=1000) # Do the optimisation x, _ = optimizer.optimize( x0=obj.x_opt, f=lambda d: fun_dfun(obj, space, d)[0], f_df=lambda d: fun_dfun(obj, space, d), ) # TODO: MULTIPLE RE-STARTS FROM PREVIOUS BEST POINTS # Round values if space is discrete xtest = space.round_optimum(x)[0] if space.indicator_constraints(xtest): opt = xtest else: # Rounding mixed things up, so need to look at neighbours # Compute neighbours to optimum idx_comb = np.array( list(itertools.product([-1, 0, 1], repeat=len(bounds)))) opt_combs = idx_comb + xtest # Evaluate GP_evals = list() combs = list() for idx, d in enumerate(opt_combs): cons_check = space.indicator_constraints(d)[0][0] bounds_check = indicator_boundaries(bounds, d)[0][0] if cons_check * bounds_check == 1: pred = obj.model.predict(d)[0][0][0] GP_evals.append(pred) combs.append(d) else: pass idx_opt = np.where(GP_evals == np.min(GP_evals))[0][0] opt = combs[idx_opt] return opt
def assert_round_optimum(self, space, test_cases): design_space = Design_space(space) for test_case in test_cases: rounded = design_space.round_optimum(np.array(test_case['in'])) self.assertTrue(np.array_equal(rounded, np.array(test_case['out'])))
def assert_round_optimum(self, space, test_cases): design_space = Design_space(space) for test_case in test_cases: rounded = design_space.round_optimum(np.array(test_case['in'])) self.assertTrue(np.array_equal(rounded, np.array(test_case['out'])))