def test_ei(): np.random.seed(0) ackley = Ackley(dim=1) X = np.expand_dims([-15, -10, 0, 1, 20], axis=1) fX = np.array([ackley.eval(x) for x in X]) gp = GPRegressor(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub) gp.add_points(X, fX) # Find the global optimizer of EI x_true = -1.7558 x_next = ei_ga(X=X, Xpend=None, dtol=0.0, ei_tol=0, fX=fX, num_pts=1, opt_prob=ackley, surrogate=gp) assert np.isclose(x_next, x_true, atol=1e-2) # Find the optimizer at least distance 5 from other points x_true = 10.6656 x_next = ei_ga(X=X, Xpend=None, dtol=5.0, ei_tol=0, fX=fX, num_pts=1, opt_prob=ackley, surrogate=gp) assert np.isclose(x_next, x_true, atol=1e-2)
def test_srbf(): np.random.seed(0) ackley = Ackley(dim=1) X = np.expand_dims([-15, -10, 0, 1, 20], axis=1) fX = np.array([ackley.eval(x) for x in X]) gp = GPRegressor(dim=1) gp.add_points(X, fX) # Find the next point with w = 0.25 x_true = 10.50 x_next = candidate_uniform( num_pts=1, X=X, Xpend=None, fX=fX, num_cand=10000, surrogate=gp, opt_prob=ackley, weights=[0.25]) assert np.isclose(x_next, x_true, atol=1e-2) x_next = candidate_srbf( num_pts=1, X=X, Xpend=None, fX=fX, num_cand=10000, surrogate=gp, opt_prob=ackley, weights=[0.25], sampling_radius=0.5) assert np.isclose(x_next, x_true, atol=1e-2) # Find the next point with w = 0.75 x_true = -3.395 x_next = candidate_uniform( num_pts=1, X=X, Xpend=None, fX=fX, num_cand=10000, surrogate=gp, opt_prob=ackley, weights=[0.75]) assert np.isclose(x_next, x_true, atol=1e-2) x_next = candidate_srbf( num_pts=1, X=X, Xpend=None, fX=fX, num_cand=10000, surrogate=gp, opt_prob=ackley, weights=[0.75], sampling_radius=0.5) assert np.isclose(x_next, x_true, atol=1e-2)
def test_ei(): np.random.seed(0) ackley = Ackley(dim=1) X = np.expand_dims([-15, -10, 0, 1, 20], axis=1) fX = np.array([ackley.eval(x) for x in X]) gp = GPRegressor(dim=1) gp.add_points(X, fX) # Find the global optimizer of EI x_true = -3.0556 x_next = expected_improvement_ga( X=X, Xpend=None, dtol=0.0, ei_tol=0, fX=fX, num_pts=1, opt_prob=ackley, surrogate=gp) assert np.isclose(x_next, x_true, atol=1e-2) x_next = expected_improvement_uniform( X=X, Xpend=None, dtol=0.0, ei_tol=0, fX=fX, num_pts=1, opt_prob=ackley, surrogate=gp, num_cand=10000) assert np.isclose(x_next, x_true, atol=1e-2) # Find the optimizer at least distance 5 from other points x_true = 11.14 x_next = expected_improvement_ga( X=X, Xpend=None, dtol=5.0, ei_tol=0, fX=fX, num_pts=1, opt_prob=ackley, surrogate=gp) assert np.isclose(x_next, x_true, atol=1e-2) x_next = expected_improvement_uniform( X=X, Xpend=None, dtol=5.0, ei_tol=0, fX=fX, num_pts=1, opt_prob=ackley, surrogate=gp, num_cand=10000) assert np.isclose(x_next, x_true, atol=1e-2)
def test_lcb_serial(): max_evals = 50 gp = GPRegressor(dim=ackley.dim) slhd = SymmetricLatinHypercube( dim=ackley.dim, num_pts=2*(ackley.dim+1)) # Create a strategy and a controller controller = SerialController(ackley.eval) controller.strategy = LCBStrategy( max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=True) controller.run() check_strategy(controller)
def test_lcb_async(): max_evals = 50 gp = GPRegressor(dim=ackley.dim) slhd = SymmetricLatinHypercube( dim=ackley.dim, num_pts=2*(ackley.dim+1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = LCBStrategy( max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=True, batch_size=None) for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) controller.launch_worker(worker) controller.run() check_strategy(controller)
def example_lower_confidence_bounds(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_lower_confidence_bounds.log"): os.remove("./logfiles/example_lower_confidence_bounds.log") logging.basicConfig( filename="./logfiles/example_lower_confidence_bounds.log", level=logging.INFO) num_threads = 4 max_evals = 100 hart6 = Hartman6() gp = GPRegressor(dim=hart6.dim) slhd = SymmetricLatinHypercube(dim=hart6.dim, num_pts=2 * (hart6.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = LCBStrategy(max_evals=max_evals, opt_prob=hart6, exp_design=slhd, surrogate=gp, asynchronous=True) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(max_evals)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(slhd.__class__.__name__)) print("Surrogate: {}".format(gp.__class__.__name__)) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, hart6.eval) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))
def test_gp(): X = make_grid(30) # Make uniform grid with 30 x 30 points gp = GPRegressor(dim=2, lb=np.zeros(2), ub=np.ones(2)) assert isinstance(gp, Surrogate) fX = f(X) gp.add_points(X, fX) # Derivative at random points np.random.seed(0) Xs = np.random.rand(10, 2) fhx = gp.predict(Xs) fx = f(Xs) assert np.max(np.abs(fx - fhx)) < 1e-2 # Derivative at previous points # Reset the surrogate gp.reset() assert gp.num_pts == 0 and gp.dim == 2
def run(self): """ Run the optimization @return: Nothing """ self.problem = VoltageOptimizationProblem( self.circuit, self.options, self.max_iter, callback=self.progress_signal.emit) # # (1) Optimization problem # # print(data.info) # # # (2) Experimental design # # Use a symmetric Latin hypercube with 2d + 1 samples # exp_des = SymmetricLatinHypercube(dim=self.problem.dim, npts=2 * self.problem.dim + 1) # # # (3) Surrogate model # # Use a cubic RBF interpolant with a linear tail # surrogate = RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=self.max_eval) # # # (4) Adaptive sampling # # Use DYCORS with 100d candidate points # adapt_samp = CandidateDYCORS(data=self, numcand=100 * self.dim) # # # Use the serial controller (uses only one thread) # controller = SerialController(self.objfunction) # # # (5) Use the sychronous strategy without non-bound constraints # strategy = SyncStrategyNoConstraints(worker_id=0, # data=self, # maxeval=self.max_eval, # nsamples=1, # exp_design=exp_des, # response_surface=surrogate, # sampling_method=adapt_samp) # # controller.strategy = strategy # # # Run the optimization strategy # result = controller.run() # # # Print the final result # print('Best value found: {0}'.format(result.value)) # print('Best solution found: {0}'.format(np.array_str(result.params[0], max_line_width=np.inf, precision=5, # suppress_small=True))) num_threads = 4 surrogate_model = GPRegressor(dim=self.problem.dim) sampler = SymmetricLatinHypercube(dim=self.problem.dim, num_pts=2 * (self.problem.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=self.max_iter, opt_prob=self.problem, exp_design=sampler, surrogate=surrogate_model, asynchronous=True, batch_size=num_threads) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(self.max_iter)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(sampler.__class__.__name__)) print("Surrogate: {}".format(surrogate_model.__class__.__name__)) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, self.problem.eval) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=4, suppress_small=True))) self.solution = result.params[0] # Extract function values from the controller self.optimization_values = np.array( [o.value for o in controller.fevals]) # send the finnish signal self.progress_signal.emit(0.0) self.progress_text.emit('Done!') self.done_signal.emit()
def pysot_cube(objective, n_trials, n_dim, with_count=False, method=None, design=None): """ Minimize :param objective: :param n_trials: :param n_dim: :param with_count: :return: """ logging.getLogger('pySOT').setLevel(logging.ERROR) num_threads = 1 asynchronous = True max_evals = n_trials gp = GenericProblem(dim=n_dim, objective=objective) if design == 'latin': exp_design = LatinHypercube(dim=n_dim, num_pts=2 * (n_dim + 1)) elif design == 'symmetric': exp_design = SymmetricLatinHypercube(dim=n_dim, num_pts=2 * (n_dim + 1)) elif design == 'factorial': exp_design = TwoFactorial(dim=n_dim) else: raise ValueError('design should be latin, symmetric or factorial') # Create a strategy and a controller # SRBFStrategy, EIStrategy, DYCORSStrategy,RandomStrategy, LCBStrategy controller = ThreadController() if method.lower() == 'srbf': surrogate = RBFInterpolant(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim), kernel=CubicKernel(), tail=LinearTail(n_dim)) controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'ei': surrogate = GPRegressor(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim)) controller.strategy = EIStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'dycors': surrogate = RBFInterpolant(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim), kernel=CubicKernel(), tail=LinearTail(n_dim)) controller.strategy = DYCORSStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'lcb': surrogate = GPRegressor(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim)) controller.strategy = LCBStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'random': controller.strategy = RandomStrategy(max_evals=max_evals, opt_prob=gp) else: raise ValueError("Didn't recognize method passed to pysot") # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, gp.eval) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() best_x = result.params[0].tolist() return (result.value, best_x, gp.feval_count) if with_count else (result.value, best_x)
def setup_backend( self, params, strategy="SRBF", surrogate="RBF", design=None, ): self.opt_problem = BBoptOptimizationProblem(params) design_kwargs = dict(dim=self.opt_problem.dim) _coconut_case_match_to_1 = design _coconut_case_match_check_1 = False if _coconut_case_match_to_1 is None: _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = EmptyExperimentalDesign(**design_kwargs) if not _coconut_case_match_check_1: if _coconut_case_match_to_1 == "latin_hypercube": _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = LatinHypercube(num_pts=2 * (self.opt_problem.dim + 1), **design_kwargs) if not _coconut_case_match_check_1: if _coconut_case_match_to_1 == "symmetric_latin_hypercube": _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = SymmetricLatinHypercube( num_pts=2 * (self.opt_problem.dim + 1), **design_kwargs) if not _coconut_case_match_check_1: if _coconut_case_match_to_1 == "two_factorial": _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = TwoFactorial(**design_kwargs) if not _coconut_case_match_check_1: _coconut_match_set_name_design_cls = _coconut_sentinel _coconut_match_set_name_design_cls = _coconut_case_match_to_1 _coconut_case_match_check_1 = True if _coconut_case_match_check_1: if _coconut_match_set_name_design_cls is not _coconut_sentinel: design_cls = _coconut_case_match_to_1 if _coconut_case_match_check_1 and not (callable(design_cls)): _coconut_case_match_check_1 = False if _coconut_case_match_check_1: self.exp_design = design_cls(**design_kwargs) if not _coconut_case_match_check_1: raise TypeError( "unknown experimental design {_coconut_format_0!r}".format( _coconut_format_0=(design))) surrogate_kwargs = dict(dim=self.opt_problem.dim, lb=self.opt_problem.lb, ub=self.opt_problem.ub) _coconut_case_match_to_2 = surrogate _coconut_case_match_check_2 = False if _coconut_case_match_to_2 == "RBF": _coconut_case_match_check_2 = True if _coconut_case_match_check_2: self.surrogate = RBFInterpolant( kernel=LinearKernel() if design is None else CubicKernel(), tail=ConstantTail(self.opt_problem.dim) if design is None else LinearTail(self.opt_problem.dim), **surrogate_kwargs) if not _coconut_case_match_check_2: if _coconut_case_match_to_2 == "GP": _coconut_case_match_check_2 = True if _coconut_case_match_check_2: self.surrogate = GPRegressor(**surrogate_kwargs) if not _coconut_case_match_check_2: _coconut_match_set_name_surrogate_cls = _coconut_sentinel _coconut_match_set_name_surrogate_cls = _coconut_case_match_to_2 _coconut_case_match_check_2 = True if _coconut_case_match_check_2: if _coconut_match_set_name_surrogate_cls is not _coconut_sentinel: surrogate_cls = _coconut_case_match_to_2 if _coconut_case_match_check_2 and not (callable(surrogate_cls)): _coconut_case_match_check_2 = False if _coconut_case_match_check_2: self.surrogate = surrogate_cls(**surrogate_kwargs) if not _coconut_case_match_check_2: raise TypeError("unknown surrogate {_coconut_format_0!r}".format( _coconut_format_0=(surrogate))) strategy_kwargs = dict(max_evals=sys.maxsize, opt_prob=self.opt_problem, exp_design=self.exp_design, surrogate=self.surrogate, asynchronous=True, batch_size=1) _coconut_case_match_to_3 = strategy _coconut_case_match_check_3 = False if _coconut_case_match_to_3 == "SRBF": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = SRBFStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: if _coconut_case_match_to_3 == "EI": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = EIStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: if _coconut_case_match_to_3 == "DYCORS": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = DYCORSStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: if _coconut_case_match_to_3 == "LCB": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = LCBStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: _coconut_match_set_name_strategy_cls = _coconut_sentinel _coconut_match_set_name_strategy_cls = _coconut_case_match_to_3 _coconut_case_match_check_3 = True if _coconut_case_match_check_3: if _coconut_match_set_name_strategy_cls is not _coconut_sentinel: strategy_cls = _coconut_case_match_to_3 if _coconut_case_match_check_3 and not (callable(strategy_cls)): _coconut_case_match_check_3 = False if _coconut_case_match_check_3: self.strategy = strategy_cls(**strategy_kwargs) if not _coconut_case_match_check_3: raise TypeError("unknown strategy {_coconut_format_0!r}".format( _coconut_format_0=(strategy)))
class PySOTBackend(StandardBackend): """The pySOT backend uses pySOT for black box optimization.""" backend_name = "pySOT" implemented_funcs = ("choice", "randrange", "uniform") strategy = None @override def setup_backend( self, params, strategy="SRBF", surrogate="RBF", design=None, ): self.opt_problem = BBoptOptimizationProblem(params) design_kwargs = dict(dim=self.opt_problem.dim) _coconut_case_match_to_1 = design _coconut_case_match_check_1 = False if _coconut_case_match_to_1 is None: _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = EmptyExperimentalDesign(**design_kwargs) if not _coconut_case_match_check_1: if _coconut_case_match_to_1 == "latin_hypercube": _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = LatinHypercube(num_pts=2 * (self.opt_problem.dim + 1), **design_kwargs) if not _coconut_case_match_check_1: if _coconut_case_match_to_1 == "symmetric_latin_hypercube": _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = SymmetricLatinHypercube( num_pts=2 * (self.opt_problem.dim + 1), **design_kwargs) if not _coconut_case_match_check_1: if _coconut_case_match_to_1 == "two_factorial": _coconut_case_match_check_1 = True if _coconut_case_match_check_1: self.exp_design = TwoFactorial(**design_kwargs) if not _coconut_case_match_check_1: _coconut_match_set_name_design_cls = _coconut_sentinel _coconut_match_set_name_design_cls = _coconut_case_match_to_1 _coconut_case_match_check_1 = True if _coconut_case_match_check_1: if _coconut_match_set_name_design_cls is not _coconut_sentinel: design_cls = _coconut_case_match_to_1 if _coconut_case_match_check_1 and not (callable(design_cls)): _coconut_case_match_check_1 = False if _coconut_case_match_check_1: self.exp_design = design_cls(**design_kwargs) if not _coconut_case_match_check_1: raise TypeError( "unknown experimental design {_coconut_format_0!r}".format( _coconut_format_0=(design))) surrogate_kwargs = dict(dim=self.opt_problem.dim, lb=self.opt_problem.lb, ub=self.opt_problem.ub) _coconut_case_match_to_2 = surrogate _coconut_case_match_check_2 = False if _coconut_case_match_to_2 == "RBF": _coconut_case_match_check_2 = True if _coconut_case_match_check_2: self.surrogate = RBFInterpolant( kernel=LinearKernel() if design is None else CubicKernel(), tail=ConstantTail(self.opt_problem.dim) if design is None else LinearTail(self.opt_problem.dim), **surrogate_kwargs) if not _coconut_case_match_check_2: if _coconut_case_match_to_2 == "GP": _coconut_case_match_check_2 = True if _coconut_case_match_check_2: self.surrogate = GPRegressor(**surrogate_kwargs) if not _coconut_case_match_check_2: _coconut_match_set_name_surrogate_cls = _coconut_sentinel _coconut_match_set_name_surrogate_cls = _coconut_case_match_to_2 _coconut_case_match_check_2 = True if _coconut_case_match_check_2: if _coconut_match_set_name_surrogate_cls is not _coconut_sentinel: surrogate_cls = _coconut_case_match_to_2 if _coconut_case_match_check_2 and not (callable(surrogate_cls)): _coconut_case_match_check_2 = False if _coconut_case_match_check_2: self.surrogate = surrogate_cls(**surrogate_kwargs) if not _coconut_case_match_check_2: raise TypeError("unknown surrogate {_coconut_format_0!r}".format( _coconut_format_0=(surrogate))) strategy_kwargs = dict(max_evals=sys.maxsize, opt_prob=self.opt_problem, exp_design=self.exp_design, surrogate=self.surrogate, asynchronous=True, batch_size=1) _coconut_case_match_to_3 = strategy _coconut_case_match_check_3 = False if _coconut_case_match_to_3 == "SRBF": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = SRBFStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: if _coconut_case_match_to_3 == "EI": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = EIStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: if _coconut_case_match_to_3 == "DYCORS": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = DYCORSStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: if _coconut_case_match_to_3 == "LCB": _coconut_case_match_check_3 = True if _coconut_case_match_check_3: self.strategy = LCBStrategy(**strategy_kwargs) if not _coconut_case_match_check_3: _coconut_match_set_name_strategy_cls = _coconut_sentinel _coconut_match_set_name_strategy_cls = _coconut_case_match_to_3 _coconut_case_match_check_3 = True if _coconut_case_match_check_3: if _coconut_match_set_name_strategy_cls is not _coconut_sentinel: strategy_cls = _coconut_case_match_to_3 if _coconut_case_match_check_3 and not (callable(strategy_cls)): _coconut_case_match_check_3 = False if _coconut_case_match_check_3: self.strategy = strategy_cls(**strategy_kwargs) if not _coconut_case_match_check_3: raise TypeError("unknown strategy {_coconut_format_0!r}".format( _coconut_format_0=(strategy))) @override def tell_data(self, new_data, new_losses): """Special method that allows fast updating of the backend with new examples.""" points, values = self.opt_problem.get_points_values( new_data, new_losses) for i in range(points.shape[0]): X = np.copy(points[i, :]) self.strategy.X = np.vstack((self.strategy.X, X)) self.strategy._X = np.vstack((self.strategy._X, X)) self.strategy.fX = np.vstack((self.strategy.fX, values[i])) self.strategy._fX = np.vstack((self.strategy._fX, values[i])) assert self.surrogate is self.strategy.surrogate, ( self.surrogate, self.strategy.surrogate) self.surrogate.add_points(X, values[i]) @override def get_next_values(self): """Special method to get the next set of values to evaluate.""" assert self.strategy._X.shape[0] > 0, self.strategy._X assert self.surrogate.num_pts > 0, self.surrogate.num_pts while True: proposal = self.strategy.propose_action() assert proposal, proposal if proposal.action == "terminate": proposal.accept() elif proposal.action == "eval": self.opt_problem.eval(*proposal.args) self.strategy.pending_evals -= 1 self.strategy.remove_pending(proposal.args[0]) break else: proposal.reject() assert self.opt_problem.got_values is not None, "pySOT optimization produced no values" return self.opt_problem.got_values