% DE3GriewangkND.m
% Script to test the use of DE for the n-dimensional Griewangk function.
% Minimization using differential evolution of R. Storn.
% R. Storn, "Diferential Evolution - A Simple and Efficient Heuristic
% Strategy for Global Optimization over Continuous Spaces",
% J. Global Optimization, vol. 11, p.341-359 (1997).
% The present Matlab program is based on the one by Rainer Storn
% (http://www.icsi.berkeley.edu/~storn/code.htm), which was 
% improved by Ken Price, Arnold Neumaier and Jim Van Zandt
% The present program:
% (1) constructs the trial vectors more efficiently
% (2) makes the program loopless except for the main iteration loop:
%     key point: operate on the entire population in "parallel"
% (3) requires only one copy of the population
% (4) uses identifier names that reflect the matrix nature of the variables
% (5) implements only scheme: DE/rand/1/bin for constructing trial vectors
% K. Ming Leung, 10/06/03, 10/23/03

clear all;
time = cputime;
fcn = 'GriewangkND'; 
n = 10;         % 10 dimensional Griewangk function
Xmax = 40*ones(n,1); 
Xmin = -Xmax;
f = 0.5; c = 0.25; np = 20;  % good convergence for n=10 (nfe=14000)
%f = 0.75; c = 0.25; np = 20; 

tol = 1e-11;    % tolerance in function value achieved
%tol = 1e-9;     % tolerance in norm of solution vector
itMax = 20000;  % maximum number of generations

%[Bestmem,bestval,nfe,DATA] = DE33avg_(fcn,Xmin,Xmax,f,c,np,tol,itMax);
%[Bestmem,bestval,nfe,DATA] = DE33_(fcn,Xmin,Xmax,f,c,np,tol,itMax);
[Bestmem,bestval,nfe,DATA] = DE33F(fcn,Xmin,Xmax,f,c,np,tol,itMax);

fprintf('Best vector = [\n');
fprintf('                  %20.16f\n', Bestmem);
fprintf('              ]\n');
fprintf('Best function value = %10.4g\n', bestval);
fprintf('Number of function evaluations = %4d\n', nfe);

runtime = cputime - time,

semilogy(DATA(:,1),DATA(:,2:end),'LineWidth',1.5);
legend('\itBest cost','Average cost','Change in best cost','Change in average cost',3);%,'FontSize',14,'FontWeight','bold');
xlabel('Generation','FontSize',16,'FontWeight','bold');
ylabel('Function Value','FontSize',16,'FontWeight','bold');

