1 % Bayesian optimization
using GPML and Finker
's Direct 3 % We assume that both files are loaded in the path 5 % Author: Ruben Martinez-Cantin <rmcantin@unizar.es> 10 addpath('../testfunctions
') 11 seed = 197; randn('seed
',seed), rand('seed
',seed) 15 bounds = ones(2,1)*[-32.768, 32.768]; 17 % Set initial set of points using latin hypercube sampling 20 xtr = lhsu(bounds(:,1),bounds(:,2),nInit); 21 ytr = zeros(size(xtr,1),1); 29 cov = {@covMaterniso,3}; 30 sf = 1; ell = 0.4; hyp0.cov = log([ell;sf]); 32 sn = 0.005; hyp0.lik = log(sn); 33 lik = 'likGauss
'; inf = 'infExact
'; 35 % Only learn the parameters once before doing optimization 37 hyp = minimize(hyp0,'gp
', -Ncg, inf, mean, cov, lik, xtr, ytr); % opt hypers 41 opt.algorithm = NLOPT_GN_DIRECT_L; 42 opt.lower_bounds = bounds(:,1); 43 opt.upper_bounds = bounds(:,2); 46 % opts.maxevals = 1000; 49 % Problem.f = criteria; 51 times = zeros(nIter,1); 55 fprintf(1,'- Iter %i of %i \n', i,nIter);
56 %[minval, newX, hist] = Direct(Problem, bounds, opts, hyp, inf, mean,...
57 % cov, lik, xtr, ytr);
58 criteria = (@(x) nei(x,hyp,inf,mean,cov,lik,xtr,ytr));
59 opt.min_objective = criteria;
61 [newX, minval, retcode] = nlopt_optimize(opt, [0 0]);
66 times(i) = cputime - start_t;
68 fprintf(1,
'New evaluation point. y = %2.4f \n', newY);
72 % ===================================================================== 73 % PLOT RESULTS ===================================================== 74 % ===================================================================== 77 [crit(j), ymu(j), ys(j)] = feval(criteria, x(j) ); 85 subplot(3,1,[1 2]); hold on; 86 title('Bayesian Optimization
'); 87 plot(xtr,ytr,'ko
','LineWidth
',lw); 88 plot(newX,newY,'ro
','LineWidth
',lw); 90 plot(x,yh, 'b
','LineWidth
',lw); 91 plot(x, ymu, 'k
','LineWidth
',lw); 92 plot(x, ymu+2*ys, 'k:
','LineWidth
',lw); 93 plot(x, ymu-2*ys, 'k:
','LineWidth
',lw); 94 avalue = axis; axis([0 1 avalue(3) avalue(4)]); 96 subplot(3,1,3); hold on; 99 avalue = axis; axis([0 1 avalue(3) avalue(4)]); 105 save('log_aiken.mat
',times)