Commits

Ruben Martinez-Cantin committed 3ef4cb0

Testing GPML+NLOPT

Comments (0)

Files changed (6)

include/bayesoptbase.hpp

     };
 
     
-    NonParametricProcess* getSurrogateModel()
+    inline NonParametricProcess* getSurrogateModel()
     { return mGP.get(); };
 
     int setSurrogateModel();    
      * surrogate, etc.)
      */
     int __init__();
+
     CriteriaFactory mCFactory;
     randEngine mEngine;
   };

matlab/bayesopt.m

 %        [xmin, fmin] = bayesopt('function_name', nDimensions, params,
 %                                lowerBound, upperBound) 
 %
+%        [xmin, fmin] = bayesoptdisc(@function_handler, validset, params)
+%        [xmin, fmin] = bayesoptdisc('function_name', validset, params)
+%
 %
 % nDimensions is the number of dimensions of the query vector.
 %
-% Params is a struct which may have the fields 
-%      n_iterations           Maximum BayesOpt evaluations (budget) 
-%      n_inner_iterations     Maximum inner optimizer evaluations 
-%      n_init_samples         Number of samples before optimization 
-%      verbose_level          Verbose level 
-%      theta                  Vector with kernel hyperparameters 
-%      alpha, beta, delta     Inv-Gamma-Normal hyperparameters 
-%      noise                  Observation noise 
-%      s_name            Name of the surrogate function 
-%      k_name            Name of the kernel function 
-%      c_name            Name of the criterion 
-%      m_name            Name of the mean function 
+% Params is a struct which have the same fields as the C/C++ interface 
+%   (see include/parameters.h)
 %
 % lowerBound and upperBound should be a nDim x 1 or 1 x nDim vectors with
 %      the lower and upper bound for each component. (optional, default 0-1)
 %
+% validset is the set of discrete points for discrete optimization
 %
-%-----------------------------------------------------------------------------
-%   This file is part of BayesOptimization, an efficient C++ library for 
-%   Bayesian optimization.
+% 
+% -------------------------------------------------------------------------
+%    This file is part of BayesOpt, an efficient C++ library for 
+%    Bayesian optimization.
 %
-%   Copyright (C) 2011 Ruben Martinez-Cantin <rmcantin@unizar.es>
-% 
-%   BayesOptimization is free software: you can redistribute it and/or modify
-%   it under the terms of the GNU General Public License as published by
-%   the Free Software Foundation, either version 3 of the License, or
-%   (at your option) any later version.
+%    Copyright (C) 2011-2013 Ruben Martinez-Cantin <rmcantin@unizar.es>
 %
-%   BayesOptimization is distributed in the hope that it will be useful,
-%   but WITHOUT ANY WARRANTY; without even the implied warranty of
-%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-%   GNU General Public License for more details.
+%    BayesOpt is free software: you can redistribute it and/or modify it 
+%    under the terms of the GNU General Public License as published by
+%    the Free Software Foundation, either version 3 of the License, or
+%    (at your option) any later version.
 %
-%   You should have received a copy of the GNU General Public License
-%   along with BayesOptimization.  If not, see <http://www.gnu.org/licenses/>.
-%-----------------------------------------------------------------------------
+%    BayesOpt is distributed in the hope that it will be useful, but 
+%    WITHOUT ANY WARRANTY; without even the implied warranty of
+%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+%    GNU General Public License for more details.
+%
+%    You should have received a copy of the GNU General Public License
+%    along with BayesOpt.  If not, see <http://www.gnu.org/licenses/>.
+% ------------------------------------------------------------------------
+%
 

matlab/gpml_comparison/gpmlnlopt.m

+% Bayesian optimization using GPML and Finker's Direct
+% 
+% We assume that both files are loaded in the path
+%
+% Author: Ruben Martinez-Cantin <rmcantin@unizar.es>
+%
+
+
+clear all, close all
+addpath('../testfunctions')
+seed = 197; randn('seed',seed), rand('seed',seed)
+plotting = 0;
+
+f = @ackley;
+bounds = ones(2,1)*[-32.768, 32.768];
+
+% Set initial set of points using latin hypercube sampling
+nInit = 30;   
+nIter = 270;
+xtr = lhsu(bounds(:,1),bounds(:,2),nInit);
+ytr = zeros(size(xtr,1),1);
+for i=1:size(xtr,1)
+    ytr(i) = f(xtr(i,:));
+end;
+
+start_t = cputime;
+
+% setup the GP
+cov = {@covMaterniso,3}; 
+sf = 1; ell = 0.4;  hyp0.cov  = log([ell;sf]);
+mean = 'meanZero';       
+sn = 0.005;          hyp0.lik  = log(sn);
+lik = 'likGauss';     inf = 'infExact';
+
+% Only learn the parameters once before doing optimization
+Ncg = 50;
+hyp = minimize(hyp0,'gp', -Ncg, inf, mean, cov, lik, xtr, ytr); % opt hypers
+
+
+% set up DIRECT
+opt.algorithm = NLOPT_GN_DIRECT_L;
+opt.lower_bounds = bounds(:,1);
+opt.upper_bounds = bounds(:,2);
+
+opt.maxeval = 1000;
+% opts.maxevals = 1000;
+% opts.maxits = 1000;
+% opts.showits = 0;
+% Problem.f = criteria;
+
+times = zeros(nIter,1);
+
+for i = 1:nIter
+    
+    fprintf(1,'- Iter %i of %i \n', i,nIter);
+    %[minval, newX, hist] = Direct(Problem, bounds, opts, hyp, inf, mean,...
+    %                              cov, lik, xtr, ytr);
+    criteria = (@(x) nei(x,hyp,inf,mean,cov,lik,xtr,ytr));
+    opt.min_objective = criteria;
+    
+    [newX, minval, retcode] = nlopt_optimize(opt, [0 0]);
+    newY = f(newX);
+    xtr = [xtr; newX];    
+    ytr = [ytr; newY];
+    
+    times(i) = cputime - start_t;
+    
+    fprintf(1,'New evaluation point. y = %2.4f \n', newY);
+    disp(newX')
+
+    if plotting
+        % =====================================================================
+        % PLOT RESULTS    =====================================================
+        % =====================================================================
+        x = 0:0.001:1;
+        for j = 1:length(x)
+            [crit(j), ymu(j), ys(j)] = feval(criteria, x(j) );
+            yh(j) = f(x(j));
+        end
+    
+        lw = 2;
+        h = figure(1);
+        clf;hold on;
+    
+        subplot(3,1,[1 2]);   hold on;
+        title('Bayesian Optimization');
+        plot(xtr,ytr,'ko','LineWidth',lw);
+        plot(newX,newY,'ro','LineWidth',lw);
+    
+        plot(x,yh, 'b','LineWidth',lw);
+        plot(x, ymu, 'k','LineWidth',lw);
+        plot(x, ymu+2*ys, 'k:','LineWidth',lw);
+        plot(x, ymu-2*ys, 'k:','LineWidth',lw);
+        avalue = axis;   axis([0 1 avalue(3) avalue(4)]);
+    
+        subplot(3,1,3); hold on;
+        title('Criteria');
+        bar(x,-crit,'k');
+        avalue = axis;   axis([0 1 avalue(3) avalue(4)]);
+    
+        pause(0.2);
+    end
+end
+
+save('log_aiken.mat',times)
+
+minY = min(ytr);
+
+
+
+
+

matlab/gpml_comparison/gpmltest.m

     end
 end
 
-save('log_aiken.mat',times)
+save('log_aiken_finkel.mat',times)
 
 minY = min(ytr);
 

matlab/gpml_comparison/neinlopt.m

+function [ei, varargout] = nei(query, hyp, inf, mean, cov,...
+                               lik, xtr, ytr, params )
+% NEGEXPIMPROVEMENT Compute the *negative* expected improvement.
+%
+% DIRECT will call this with the query vector as a column, but we want
+% it to be a row.  Make sure we can call it both ways.
+
+if (size(query,1) > size(query,2))
+   query = query.';
+end
+
+[ymu, ys2] = gp(hyp, inf, mean, cov, lik, xtr, ytr, query);  % predict
+
+
+s = sqrt(ys2);             % Predicted std
+yres = min(ytr) - ymu;     % Predicted error
+ynorm = yres/s;            % Normalize error
+
+ei = yres * normcdf(ynorm) + s * normpdf(ynorm);
+ei = max([0 ei]);
+
+% DIRECT search for a minimum, and we want the maximum expected
+% improvement, so...
+ei = -ei;
+
+varargout{1} = ymu;    varargout{2} = s;
 %    This file is part of BayesOpt, an efficient C++ library for 
 %    Bayesian optimization.
 %
-%    Copyright (C) 2011-2012 Ruben Martinez-Cantin <rmcantin@unizar.es>
+%    Copyright (C) 2011-2013 Ruben Martinez-Cantin <rmcantin@unizar.es>
 %
 %    BayesOpt is free software: you can redistribute it and/or modify it 
 %    under the terms of the GNU General Public License as published by
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.