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 test_invalid_constraint(self): space = [{'name': 'var_1', 'type': 'continuous', 'domain':(-5, 5), 'dimensionality': 1}] constraints = [{'name': 'const_1', 'constraint': 'x[:,20]**2 - 1'}] x = np.array([[0]]) design_space = Design_space(space, constraints=constraints) with self.assertRaises(Exception): design_space.indicator_constraints(x)
def test_invalid_constraint(self): space = [{'name': 'var_1', 'type': 'continuous', 'domain':(-5, 5), 'dimensionality': 1}] constraints = [{'name': 'const_1', 'constrain': 'x[:,20]**2 - 1'}] x = np.array([[0]]) design_space = Design_space(space, constraints=constraints) with self.assertRaises(Exception): design_space.indicator_constraints(x)
def test_indicator_constraints(self): space = [{'name': 'var_1', 'type': 'continuous', 'domain':(-5, 5), 'dimensionality': 1}] constraints = [ {'name': 'const_1', 'constraint': 'x[:,0]**2 - 1'}] x = np.array([[0], [0.5], [4], [-0.2], [-5]]) expected_indicies = np.array([[1], [1], [0], [1], [0]]) design_space = Design_space(space, constraints=constraints) I_x = design_space.indicator_constraints(x) self.assertTrue(np.array_equal(expected_indicies, I_x))
def test_indicator_constraints(self): space = [{'name': 'var_1', 'type': 'continuous', 'domain':(-5, 5), 'dimensionality': 1}] constraints = [ {'name': 'const_1', 'constrain': 'x[:,0]**2 - 1'}] x = np.array([[0], [0.5], [4], [-0.2], [-5]]) expected_indicies = np.array([[1], [1], [0], [1], [0]]) design_space = Design_space(space, constraints=constraints) I_x = design_space.indicator_constraints(x) self.assertTrue(np.array_equal(expected_indicies, I_x))