19 #define _USE_MATH_DEFINES 27 extern "C" int LAPACKE_dposv(
int matrix_layout,
char uplo,
int n,
int nrhs,
double* A,
int LDA,
double* B,
int LDB);
36 double* params = (
double*)void_params;
38 return params[0]*sin(2*parameters[0] + params[1]) + params[2]*sin(parameters[0] + params[3] ) + params[4];
49 double* params = (
double*)void_params;
50 grad[0] = 2*params[0]*cos(2*parameters[0] + params[1]) + params[2]*cos(parameters[0] + params[3] );
97 std::random_device rd;
100 gen = std::mt19937(rd());
115 std::normal_distribution<> distrib_real(0, M_PI/4);
118 double random = distrib_real(
gen);
119 parameters_new[idx] = x[idx] + random;
123 x_prev.push_back(parameters_new);
124 f_prev.push_back(f_random);
154 for (
int idx=0; idx<(
int)
x_prev.size();idx++){
159 x_prev.push_back(solution_guess);
197 int samples_n = (
int)instance->
x_prev.size();
200 for (
int idx=0; idx<samples_n; idx++){
204 double sigma2_n = 0.0;
206 double sigma_n = std::sqrt(std::fabs(sigma2_n));
213 double deltax_max = (deltax>0.) ? deltax:0.0;
215 double EI = deltax_max + sigma_n*
pdf(deltax,sigma_n) - fabs(deltax)*
cdf(deltax,sigma_n);
225 for (
int idx=0;idx<samples;idx++){
232 for (
int idx=0;idx<samples;idx++){
233 mu_n = mu_n + x0[idx]*cov_x[idx];
237 memset(x0.
get_data(), M_PI, samples*
sizeof(double) );
240 for (
int idx=0;idx<mu_rhs.
cols;idx++){
241 sigma2_n = sigma2_n + x0[idx] * cov_x[idx];
244 sigma2_n = -1.0*(sigma2_n -
kernel(x,x));
258 dist = dist + (x0[idx]-x1[idx])*(x0[idx]-x1[idx]);
261 sigma01 =
alpha0*std::exp(-1.*std::sin(2*dist)*std::sin(2*dist)) + 1e-6;
268 return 1/sigma/std::sqrt(2*M_PI)*std::exp(0.5*(mu/sigma)*(mu/sigma));
272 return 0.5+0.5*std::erf(mu/sigma/std::sqrt(2));
276 return -1.0/sigma*grad_sigma*
pdf(mu,sigma) -
pdf(mu,sigma)*(grad_mu*sigma - mu*grad_sigma)/sigma/sigma;
284 double* data_new = covariance_new.
get_data();
292 covariance_new[samples-1+idx*samples] = cov_new[idx];
296 covariance_new[samples*samples-1] =
alpha0;
306 dist = dist + (x0[idx]-x[idx])*(x0[idx]-x[idx]);
308 dist = std::sqrt(dist);
309 double cost_func_base =
alpha0*std::exp(-1.*dist) + 1e-6;
311 for (
int grad_idx=0; grad_idx<
variable_num; grad_idx++){
312 double x1 = x[grad_idx];
313 grad[grad_var*variable_num + grad_idx] = 0.;
315 if (grad_idx == idx &&
self ==
true){
316 grad[grad_var*variable_num + grad_idx] = 0.;
319 grad[grad_var*variable_num + grad_idx] = grad[grad_var*variable_num + grad_idx] - cost_func_base*(2.*x1-2.*x0[idx]);
336 for (
int idx=0;idx<samples;idx++){
339 sigma2_rhs[idx] = M_PI;
343 for (
int idx=0; idx<samples; idx++){
344 mu_n = mu_n + mu_rhs[idx]*cov_x[idx];
346 mu_n = std::fabs(mu_n);
347 for (
int grad_idx=0; grad_idx<
variable_num; grad_idx++){
348 for (
int idx=0;idx<samples;idx++){
349 grad_mu[grad_idx] = grad_mu[grad_idx] + cov_x_grad[idx*variable_num + grad_idx]*mu_rhs[idx];
358 for (
int idx=0;idx<samples;idx++){
359 sigma2_n = sigma2_n + sigma2_rhs[idx] * cov_x[idx];
361 sigma2_n =
kernel(x,x)-sigma2_n;
365 for (
int grad_idx=0; grad_idx<
variable_num; grad_idx++){
367 for (
int idx=0;idx<samples;idx++){
368 b[idx] = cov_x_grad[idx*variable_num+grad_idx];
371 grad_sigma[grad_idx] = cov_self_grad[grad_idx];
372 for (
int idx=0;idx<samples;idx++){
373 grad_sigma[grad_idx] = grad_sigma[grad_idx] - 2.*cov_x_grad[idx*variable_num + grad_idx];
375 grad_sigma[grad_idx] = grad_sigma[grad_idx]/std::sqrt(sigma2_n)/2;
384 double deltax_max = (deltax>0.) ? deltax:0.0;
386 double pdf_mu =
pdf(mu_n,sigma_n);
387 double cdf_mu =
cdf(mu_n,sigma_n);
388 *f = -1.*(deltax_max + sigma_n*pdf_mu - fabs(deltax)*cdf_mu);
391 double deltax_grad = grad_mu[idx];
392 double deltax_max_grad = (deltax_grad>0.) ? grad_mu[idx] : 0.0;
393 double grad_rhs = -1.*std::fabs(deltax_grad)*cdf_mu - std::fabs(deltax)*pdf_mu*(grad_mu[idx]*sigma_n-mu_n*grad_sigma[idx])/sigma_n/sigma_n;
394 double grad_lhs = grad_sigma[idx]*pdf_mu + sigma_n*
grad_pdf(mu_n,sigma_n,grad_mu[idx],grad_sigma[idx]);
402 int samples_n = (
int)instance->
x_prev.size();
407 for (
int grad_idx=0; grad_idx<samples_n; grad_idx++){
410 cov_x[grad_idx] = cov_new;
416 instance->
kernel_combined(x_bfgs, x_bfgs, placeholder,cov_self_grad,0,
true);
425 double sigma_n = std::sqrt(sigma2_n);
450 std::random_device rd;
453 gen = std::mt19937(rd());
parameter_num
[set adaptive gate structure]
void conjugate_gradient(Matrix_real A, Matrix_real b, Matrix_real &x0, double tol)
std::vector< double > f_prev
Matrix_real copy() const
Call to create a copy of the matrix.
Matrix_real covariance
covariance matrix
A class implementing the BayesOpt algorithm as seen in: https://browse.arxiv.org/pdf/1807.02811.pdf.
double HS_partial_optimization_problem(Matrix_real parameters, void *void_params)
???????????????
double Start_Optimization(Matrix_real &x, int max_iterations_in)
double expected_improvement(double mu_n, double sigma_n)
double grad_pdf(double mu, double sigma, double grad_mu, double grad_sigma)
scalar * get_data() const
Call to get the pointer to the stored data.
double Start_Optimization(Matrix_real &x, int max_iterations_in)
double alpha0
amplitude of the kernel
int cols
The number of columns.
long maximal_iterations
maximal count of iterations during the optimization
void * meta_data
additional data needed to evaluate the cost function
double mu_0
constant for the mean function
double Start_Optimization(Matrix_real &x, long max_iter)
Bayes_Opt(double(*f_pointer)(Matrix_real, void *), void *meta_data_in)
Constructor of the class.
Bayes_Opt_Beam(double(*f_pointer)(Matrix_real, void *), void *meta_data_in, int start_in, Matrix_real parameters_original_in)
double kernel(Matrix_real x0, Matrix_real x1)
double num_precision
numerical precision used in the calculations
int size() const
Call to get the number of the allocated elements.
A class implementing the Powells-algorithm as seen in: https://academic.oup.com/comjnl/article-abstra...
void update_covariance(Matrix_real cov_new)
double(* costfnc)(Matrix_real x, void *params)
function pointer to evaluate the cost function and its gradient vector
void HS_partial_optimization_problem_grad(Matrix_real parameters, void *void_params, Matrix_real &grad)
???????????????
std::vector< Matrix_real > x_prev
previous parameters
double cdf(double mu, double sigma)
static double optimization_problem(Matrix_real x_Beam, void *void_instance)
double pdf(double mu, double sigma)
Header file for commonly used functions and wrappers to CBLAS functions.
~Bayes_Opt()
Destructor of the class.
int LAPACKE_dposv(int matrix_layout, char uplo, int n, int nrhs, double *A, int LDA, double *B, int LDB)
void calculate_conditional_distribution(Matrix_real x, Matrix_real cov_x, double &mu_n, double &sigma2_n)
static void optimization_problem_combined(Matrix_real x, void *void_instance, double *f0, Matrix_real &grad)
void HS_partial_optimization_problem_combined(Matrix_real parameters, void *void_params, double *f0, Matrix_real &grad)
???????????????
void expected_improvement_combined(double mu_n, double sigma_n, Matrix_real &grad_mu, Matrix_real &grad_sigma, double *f, Matrix_real &grad)
void kernel_combined(Matrix_real x0, Matrix_real x, double &f, Matrix_real &grad, int grad_var, bool self)
int variable_num
number of independent variables in the problem
void * meta_data
additional data needed to evaluate the cost function
void calculate_conditional_distribution_combined(Matrix_real x, Matrix_real cov_x, Matrix_real cov_x_grad, Matrix_real cov_self_grad, double &mu_n, double &sigma2_n, Matrix_real &grad_mu, Matrix_real &grad_sigma)
Class to store data of complex arrays and its properties.
static double optimization_problem(Matrix_real x_Powell, void *void_instance)
double(* costfnc)(Matrix_real x, void *params)
function pointer to evaluate the cost function and its gradient vector