BayesOpt
gpmltest.m
1 % Bayesian optimization using GPML and Finker's Direct
2 %
3 % We assume that both files are loaded in the path
4 %
5 % Author: Ruben Martinez-Cantin <rmcantin@unizar.es>
6 %
7 
8 
9 clear all, close all
10 addpath('../testfunctions')
11 seed = 197; randn('seed',seed), rand('seed',seed)
12 plotting = 0;
13 
14 f = @ackley;
15 criteria = @nei;
16 bounds = ones(2,1)*[-32.768, 32.768];
17 
18 % Set initial set of points using latin hypercube sampling
19 nInit = 30;
20 nIter = 270;
21 xtr = lhsu(bounds(:,1),bounds(:,2),nInit);
22 ytr = zeros(size(xtr,1),1);
23 for i=1:size(xtr,1)
24  ytr(i) = f(xtr(i,:));
25 end;
26 
27 start_t = cputime;
28 
29 % setup the GP
30 cov = {@covMaterniso,3};
31 sf = 1; ell = 0.4; hyp0.cov = log([ell;sf]);
32 mean = 'meanZero';
33 sn = 0.005; hyp0.lik = log(sn);
34 lik = 'likGauss'; inf = 'infExact';
35 
36 % Only learn the parameters once before doing optimization
37 Ncg = 50;
38 hyp = minimize(hyp0,'gp', -Ncg, inf, mean, cov, lik, xtr, ytr); % opt hypers
39 
40 % set up DIRECT
41 opts.maxevals = 1000;
42 opts.maxits = 1000;
43 opts.showits = 0;
44 Problem.f = criteria;
45 
46 times = zeros(nIter,1);
47 
48 for i = 1:nIter
49 
50  fprintf(1,'- Iter %i of %i \n', i,nIter);
51  [minval, newX, hist] = Direct(Problem, bounds, opts, hyp, inf, mean,...
52  cov, lik, xtr, ytr);
53  newY = f(newX);
54  xtr = [xtr; newX'];
55  ytr = [ytr; newY];
56 
57  times(i) = cputime - start_t;
58 
59  fprintf(1,'New evaluation point. y = %2.4f \n', newY);
60  disp(newX')
61 
62  if plotting
63  % =====================================================================
64  % PLOT RESULTS =====================================================
65  % =====================================================================
66  x = 0:0.001:1;
67  for j = 1:length(x)
68  [crit(j), ymu(j), ys(j)] = feval(criteria, x(j), hyp, inf,...
69  mean, cov, lik, xtr, ytr );
70  end
71 
72  lw = 2;
73  h = figure(1);
74  clf;hold on;
75 
76  subplot(3,1,[1 2]); hold on;
77  title('Bayesian Optimization');
78  plot(xtr,ytr,'ko','LineWidth',lw);
79  plot(newX,newY,'ro','LineWidth',lw);
80 
81  plot(x,f(x), 'b','LineWidth',lw);
82  plot(x, ymu, 'k','LineWidth',lw);
83  plot(x, ymu+2*ys, 'k:','LineWidth',lw);
84  plot(x, ymu-2*ys, 'k:','LineWidth',lw);
85  avalue = axis; axis([0 1 avalue(3) avalue(4)]);
86 
87  subplot(3,1,3); hold on;
88  title('Criteria');
89  bar(x,-crit,'k');
90  avalue = axis; axis([0 1 avalue(3) avalue(4)]);
91 
92  pause(0.2);
93  end
94 end
95 
96 save('log_aiken_finkel.mat',times)
97 
98 minY = min(ytr);
99 
100 
101 
102 
103