% runs an entire experiment for benchmarking MY_OPTIMIZER
% on the noise-free testbed. fgeneric.m and benchmarks.m
% must be in the path of Matlab/Octave
% CAPITALIZATION indicates code adaptations to be made

global noaa;
global lowfe;
global maxfunevals;
global dim;

addpath('D:\Dokumenty\!!Rok2005\CI\BBOB\bbob.v10.2\matlab');  % should point to fgeneric.m etc.
datapath = 'D:\Dokumenty\!!Rok2005\CI\BBOB\bbob.v10.2\matlab\datadm100000xdim-101-130-np250cd02-2D-3D-5D-10D-20D-40D\';  % different folder for each experiment
opt.algName = 'Differential Migration';
opt.comments = 'comparison with restarted CMA-ES';
maxfunevals = '100000 * dim'; % 10*dim is a short test-experiment taking a few minutes 
                          % INCREMENT maxfunevals successively to larger value(s)
minfunevals = 'dim + 2';  % PUT MINIMAL SENSIBLE NUMBER OF EVALUATIONS for a restart
maxrestarts = 1e4;        % SET to zero for an entirely deterministic algorithm

more off;  % in octave pagination is on by default

t0 = clock;
rand('state', sum(100 * t0));

%for dim = [2,3,5,10,20,40]  % small dimensions first, for CPU reasons
%for dim = [2,3,10,40]  % small dimensions first, for CPU reasons
for dim = [5,20]  % small dimensions first, for CPU reasons
  for ifun = benchmarks('FunctionIndices')  % or benchmarksnoisy(...)
%  for ifun = benchmarksnoisy('FunctionIndices')  % or benchmarksnoisy(...)
%    for iinstance = [1:15]  % first 15 function instances
    for iinstance = [1:5]  % first 15 function instances
      fgeneric('initialize', ifun, iinstance, datapath, opt); 

      % independent restarts until maxfunevals or ftarget is reached
      for restarts = 0:maxrestarts
        MY_OPTIMIZER_DM('fgeneric', dim, fgeneric('ftarget'), ...
                     eval(maxfunevals) - fgeneric('evaluations'));
        if fgeneric('fbest') < fgeneric('ftarget') || ...
           fgeneric('evaluations') + eval(minfunevals) > eval(maxfunevals)
          break;
        end
        if lowfe == 1
%            disp('lowfe');
          break;
        end
      end

      disp(sprintf(['  f%d in %d-D, instance %d: FEs=%d with %d restarts,' ...
                    ' fbest-ftarget=%.4e, elapsed time [h]: %.2f'], ...
                   ifun, dim, iinstance, ...
                   fgeneric('evaluations'), ...
                   restarts, ...
                   fgeneric('fbest') - fgeneric('ftarget'), ...
                   etime(clock, t0)/60/60));

      fgeneric('finalize');
    end
    disp(['      date and time: ' num2str(clock, ' %.0f')]);
  end
  disp(sprintf('---- dimension %d-D done ----', dim));
end

