unit CEC08_Benchmark;
interface
{$W-,M-,Q-,I-}

uses sysutils, math;
Const
     nb_eval=25;
     Swarmsize=10;
     granul=0.01;
     E=2.7182818284590452353602874713526625;
     PI=3.1415926535897932384626433832795029;
     TPI=2*3.1415926535897932384626433832795029;
     f_bias : array [0..5] of double=(-450.0 , -450.0 , 390.0 , -330.0 , -180.0 , -140.0);
Type
    h=array [0..10-1] of double;
    tt=array of double;  tta=array of integer;
    ttt=array of array of double;
    y=record
       eval:double; nEval:integer; time:string;
       g1, g50, g100, g500,g1000,g1200:double; g3000,g4000,g5000:double;
       g10000:double;g25000:double; g50000,g70000:double;g100000:double;g250000:double;g500000,g1000000,g2500000,g5000000:double;
      end;
    sauv=record
          PrevPos:h; PrevVel:h;pos:h;vel:h;
         end;
    sauv1=record
         fonction: array of sauv;
         end;
  Str=record
       pos:double;
       vel:double; PrevVel:double; PrevPos:double;
      End;
  fly=record
       num:integer; deplacement:array of Str; Neighbor:array of integer;
       BestDeplacement:array of Str; WorstDeplacement:array of Str;
       Fitness:double; BestFitness:double;
      End;

 Var SwarmPop:array [1..swarmsize] of fly;
    Gbest, FE, generation, fdejon:integer; pas,InertWW,pas2:array of double;
    biase,error, InertW, DebInertW, FinInertW, MaxVel, MinVel, c1, c2: double;
    tabsauv:array[0..nb_eval-1] of y; eval4,eval5,eval5_2,eval5_6,eval6,eval6_2:double;
    sphere,schwefel,rosenbrock,rastrigin,griewank,ackley,schwefel2_22,schwefel1_2,
    extended_f10,bohachevsky,schaffer,hybrid_15,hybrid_19:array of double;
    Dim, taille_group, nb_group:integer;
    k1,k2,k3:integer; u,v,temperature:double; f:textfile; limitInf,limitSup:integer;
    moy,epsilon1,epsilon2,epsilon3:double;

Function Shifted_Sphere(x:array of str):double; Function Shifted_Sphere1(dime:integer;x:array of double):double;
Function Schwefel_Problem(x:array of str):double;
Function Shifted_Rosenbrock(x:array of str):double; Function Shifted_Rosenbrock1(dime:integer;x:array of double):double;
Function Shifted_Rastrigin(x:array of str):double; Function Shifted_Rastrigin1(dime:integer;x:array of double):double;
Function Shifted_Griewank(x:array of str):double; Function Shifted_Griewank1(dime:integer;x:array of double):double;
Function Shifted_Ackley(x:array of str):double;
Function f_Schwefel2_22(x:array of str):extended;
Function f_Schwefel2_22NoDesplazamiento1(dime:integer;x:array of double):double;
Function f_Schwefel1_2(x:array of str):double;
Function f_10(x:double; y:double):double;
Procedure divideFunctions(x:array of str; var part1:array of double; var part2:array of double; m:double; var psize1:integer; var psize2:integer);
Procedure divideFunctions1(x:array of double; var part1:array of double; var part2:array of double; m:double; var psize1:integer; var psize2:integer);
Function Extended_f_10(x:array of str):double;
Function Extended_f_10NoDesplazamiento1(dime:integer;x:array of double):double;
Function f_Bohachevsky(x:array of str):double;
Function f_BohachevskyNoDesplazamiento1(dime:integer;x:array of double):double;
Function f_Schaffer(x:array of str):double;
Function f_Hybrid_12(x:array of str):double;
Function f_Hybrid_13(x:array of str):double;
Function f_Hybrid_14(x:array of str):double;
Function f_Hybrid_15(x:array of str):double;
Function f_Hybrid_16(x:array of str):double;
Function f_Hybrid_17(x:array of str):double;
Function f_Hybrid_18(x:array of str):double;
Function f_Hybrid_19(x:array of str):extended;
 // --------------------------------
Procedure Initialise();
procedure Tri();
Procedure Init_Spop();
Procedure Evaluation();
procedure Correction1(index:integer;j:integer);
Procedure Update();
function Arret(simul:integer):boolean;

Implementation

Procedure Initialise();
 var i,j,k,t,index:integer;   s,res:double;
begin
 Dim:=1000;
 taille_group:=500; nb_group:=2;  //dernier version
 setlength(sphere,Dim);setlength(schwefel,Dim); setlength(rosenbrock,Dim); setlength(rastrigin,Dim);
 setlength(griewank,Dim); setlength(ackley,Dim);
 setlength(schwefel2_22,Dim);  setlength(schwefel1_2,Dim); setlength(extended_f10,Dim);
 setlength(bohachevsky,Dim); setlength(schaffer,Dim);  setlength(hybrid_15,Dim);setlength(hybrid_19,Dim);

 epsilon1:=1e-2; epsilon2:=1e-6; epsilon3:=9e-10; biase:=0;
 setlength(pas,nb_group); setlength(pas2,nb_group);setlength(InertWW,nb_group);
 case fdejon of
  1: begin u:=-100; v:=100; biase:=f_bias[0];
      assignfile(f,'C:\datafiles08\Sphere.txt'); reset(f);
      for i:=0 to Dim-1 do Read(f,sphere[i]);  closefile(f);
     end;
  2: begin u:=-100; v:=100; biase:=f_bias[1];
      assignfile(f,'C:\datafiles08\Schwefel.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,schwefel[i]); closefile(f);
     end;
  3: begin u:=-100; v:=100; biase:=f_bias[2];
      assignfile(f,'C:\datafiles08\Rosenbrock.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,rosenbrock[i]); closefile(f);
     end;
  4: begin u:=-5; v:=5;  biase:=f_bias[3];
      assignfile(f,'C:\datafiles08\Rastrigin.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,rastrigin[i]); closefile(f);
     end;
  5: begin u:=-600; v:=600;  biase:=f_bias[4];
      assignfile(f,'C:\datafiles08\Griewank.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,griewank[i]); closefile(f);
     end;
  6: begin u:=-32; v:=32; biase:=f_bias[5];
      assignfile(f,'C:\datafiles08\Ackley.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,ackley[i]); closefile(f);
     end;
  7: begin u:=-10; v:=10;
      assignfile(f,'C:\datafiles08\Schwefel2_22.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,schwefel2_22[i]); closefile(f);
     end;
  8: begin u:=-65.536; v:=65.536;
      assignfile(f,'C:\datafiles08\Schwefel1_2.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,schwefel1_2[i]); closefile(f);
     end;
  9: begin u:=-100; v:=100;
      assignfile(f,'C:\datafiles08\Extended_f10.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,extended_f10[i]); closefile(f);
     end;
 10: begin u:=-15; v:=15;
      assignfile(f,'C:\datafiles08\Bohachevsky.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,bohachevsky[i]); closefile(f);
     end;
 11: begin u:=-100; v:=100;
      assignfile(f,'C:\datafiles08\Schaffer.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,schaffer[i]); closefile(f);
     end;
 12: begin u:=-100; v:=100; end;
 13: begin u:=-100; v:=100; end;
 14: begin u:=-5; v:=5; end;
 15: begin u:=-10; v:=10;
      assignfile(f,'C:\datafiles08\Hybrid_15.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,hybrid_15[i]); closefile(f);
     end;
 16: begin u:=-100; v:=100; end;
 17: begin u:=-100; v:=100; end;
 18: begin u:=-5; v:=5; end;
 19: begin u:=-10; v:=10;
      assignfile(f,'C:\datafiles08\Hybrid_19.txt'); reset(f);
      for i:=0 to Dim-1 do read(f,hybrid_19[i]); closefile(f);
     end;
  end;
end;

Function Shifted_Sphere(x:array of str):double;  //Fonction: F1
 var i:integer; z,f:double;
begin
 f:=0;
 for i:=0 to dim-1 do begin z := x[i].pos - sphere[i]; f := f + (z * z); end;
 Shifted_Sphere:=f + f_bias[0];
end;

Function Shifted_Sphere1(dime:integer;x:array of double):double;
 var i:integer; z,f:double;
begin
 f:=0; for i:=0 to dime-1 do begin z := x[i] - sphere[i]; f := f + (z * z); end;
 Shifted_Sphere1:=f + f_bias[0];
end;

Function Schwefel_Problem(x:array of str):double; //Fonction: F2
 var i:integer;   z,F: double ;
begin
 F:=abs(x[0].pos); for i:=1 to dim-1 do begin  z := x[i].pos - schwefel[i]; F := max(F , abs(z)); end;
 Schwefel_Problem:=F + f_bias[1];
end;

Function Shifted_Rosenbrock(x:array of str):double;  //Fonction: F3
  var i:integer; z:array of double; F:double;
begin
 setlength(z,dim); F := 0;
 for i:=0 to dim-1 do z[i] := x[i].pos - rosenbrock[i] + 1;
 for i:=0 to dim-2 do F := F + 100*( power((power(z[i],2)-z[i+1]) , 2) ) + power((z[i]-1) , 2);
 Shifted_Rosenbrock:= F + f_bias[2];
end;
Function Shifted_Rosenbrock1(dime:integer;x:array of double):double;
  var i:integer; z:array of double; F:double;
begin
 setlength(z,dime); F := 0; for i:=0 to dime-1 do z[i] := x[i] - rosenbrock[i] + 1;
 for i:=0 to dime-2 do F := F + 100*( power((power(z[i],2)-z[i+1]) , 2) ) + power((z[i]-1) , 2);
 Shifted_Rosenbrock1:= F + f_bias[2];
end;

Function Shifted_Rastrigin(x:array of str):double;  //Fonction: F4
  var i:integer; z,F:double;
begin
 F:=0;
 for i:=0 to dim-1 do begin z := x[i].pos - rastrigin[i]; F := F + ( power(z,2) - 10*cos(2*pi*z) + 10);end;
 Shifted_Rastrigin:= F + f_bias[3];
end;

Function Shifted_Rastrigin1(dime:integer;x:array of double):double;
  var i:integer; z,F:double;
begin
 F:=0;for i:=0 to dime-1 do begin z := x[i] - rastrigin[i]; F := F + ( power(z,2) - 10*cos(2*pi*z) + 10);end;
 Shifted_Rastrigin1:= F + f_bias[3];
end;

Function Shifted_Griewank(x:array of str):double; //Fonction: F5
 var i:integer; z,F1,F2:double;
begin
 F1:=0; F2:=1; for i:=0 to dim-1 do
 begin z := x[i].pos - griewank[i];F1 := F1 + ( power(z,2) / 4000 );F2 := F2 * ( cos(z/sqrt(i+1)));end;
 Shifted_Griewank:=(F1 - F2 + 1 + f_bias[4]);
end;

Function Shifted_Griewank1(dime:integer;x:array of double):double;
 var i:integer; z,F1,F2:double;
begin
 F1:=0; F2:=1; for i:=0 to dime-1 do
 begin z := x[i] - griewank[i];F1 := F1 + ( power(z,2) / 4000 );F2 := F2 * ( cos(z/sqrt(i+1)));end;
 Shifted_Griewank1:=(F1 - F2 + 1 + f_bias[4]);
end;

Function Shifted_Ackley(x:array of str):double;  //Fonction: F6
 var i:integer; z,Sum1,Sum2,F:double;
begin
 Sum1 := 0; Sum2 := 0; F := 0;
 for i:=0 to dim-1 do begin z := x[i].pos - ackley[i];Sum1 := Sum1 + power(z , 2 );Sum2 := Sum2 + cos(2*pi*z);end;
 F := -20*exp(-0.2*sqrt(Sum1/dim)) -exp(Sum2/dim) + 20 + E + f_bias[5];
 Shifted_Ackley:=F;
end;

Function f_Schwefel2_22(x:array of str):extended;   //Fonction: F7
  var  i,K:integer; sum, currentGen, prod:extended;
begin
 sum := 0.0; prod := 1.0; i:=0;
 while (i<=dim-1) do begin
 currentGen := abs(x[i].pos-schwefel2_22[i]); sum := sum + currentGen; prod := prod * currentGen; i:=i+1;
 end;
 f_Schwefel2_22:=sum + prod;
end;

Function f_Schwefel2_22NoDesplazamiento1(dime:integer;x:array of double):double;
  var  i:integer; sum, currentGen, prod:double;
begin
 sum := 0.0; prod := 1.0;
 for i := 0 to dime-1 do begin currentGen := abs(x[i]); sum := sum + currentGen; prod := prod*currentGen; end;
 f_Schwefel2_22NoDesplazamiento1:=sum + prod;
end;

Function f_Schwefel1_2(x:array of str):double;  //Fonction: F8
  var i:integer; Sum, Val:double;
begin
  Sum:=0.0; Val:=0.0;
	for i := 0 to dim-1 do begin Val := Val + (x[i].pos-schwefel1_2[i]); Sum := Sum + (Val * Val);end;
	f_Schwefel1_2:=Sum;
end;

Function f_10(x:double; y:double):double;
  var p, z, t:double;
begin
 p:=(x*x+y*y);  z:=power(p, 0.25); t:=sin(50.0*power(p, 0.1)); t:=t*t+1.0;
 f_10:=z*t;
end;

Function Extended_f_10(x:array of str):double;   //Fonction: F9
  var i:integer; suma:double;
begin
 suma:=0.0; for i:=0 to dim-2 do suma:=suma + f_10(x[i].pos-extended_f10[i], x[i+1].pos-extended_f10[i+1]);
 suma :=suma + f_10(x[dim-1].pos-extended_f10[dim-1], x[0].pos-extended_f10[0]);
 Extended_f_10:=suma;
end;

Function Extended_f_10NoDesplazamiento1(dime:integer;x:array of double):double;
  var i:integer; suma:double;
begin
 suma:=0.0; for i:=0 to dime-2 do suma:=suma + f_10(x[i], x[i+1]);
 suma :=suma + f_10(x[dime-1], x[0]);
 Extended_f_10NoDesplazamiento1:=suma;
end;

Function f_Bohachevsky(x:array of str):double;   //Fonction: F10
  var sum,currentGen,nextGen:double; i:integer; c1,c2:double;
begin
 sum := 0.0; currentGen := x[0].pos-bohachevsky[0];
 for i := 1 to dim-1 do
 begin
  nextGen := x[i].pos-bohachevsky[i]; sum := sum + currentGen * currentGen + 2.0 * nextGen * nextGen;
  c1:=cos(3.0 * PI * currentGen); c2:=cos(4.0 * PI * nextGen);
  sum := sum + 0.7 -(0.3*c1+0.4*c2);
  currentGen := nextGen;
 end;
 f_Bohachevsky:=sum;
end;

Function f_BohachevskyNoDesplazamiento1(dime:integer;x:array of double):double;
  var sum,currentGen,nextGen:double; i:integer;
begin
 sum := 0.0; currentGen := x[0];
 for i := 1 to dime-1 do
 begin
  nextGen := x[i]; sum := sum + currentGen * currentGen + 2.0 * nextGen * nextGen;
  c1:=cos(3.0 * PI * currentGen); c2:=cos(4.0 * PI * nextGen);
  sum := sum + 0.7 -(0.3*c1+0.4*c2);
  currentGen := nextGen;
 end;
 f_BohachevskyNoDesplazamiento1:=sum;
end;

Function f_Schaffer(x:array of str):double;  //Fonction: F11
  var i:integer; sum, aux, aux2,currentGen, nextGen:double;
begin
 sum := 0.0; currentGen := x[0].pos-schaffer[0];  currentGen := currentGen * currentGen;
 for i := 1 to dim-1 do
 begin
  nextGen := x[i].pos-schaffer[i]; nextGen := nextGen * nextGen; aux := currentGen + nextGen;
  currentGen := nextGen; aux2 := sin(50.0 * power(aux, 0.1));
  sum := sum + power(aux, 0.25) * (aux2 * aux2 + 1.0);
 end;
 f_Schaffer:=sum;
end;

Procedure divideFunctions(x:array of str; var part1:array of double; var part2:array of double; m:double; var psize1:integer; var psize2:integer);
  var   shared,rest, i, total,temp:integer;  partrest:array of double; tr:boolean;
begin
  setlength(partrest,dim);
  if (m <= 0.5) then begin for i:=0 to dim-1 do partrest[i] := part2[i]; end
                     else begin for i:=0 to dim-1 do partrest[i] := part1[i]; m := 1-m; end;
  shared := floor(dim*m);  rest := 2*shared;
  for i := 0 to shared-1 do begin part1[i] := x[i*2].pos; part2[i]:= x[i*2+1].pos; end;
  total := dim-shared;
  for i := 0 to (total-shared)-1 do partrest[i+shared] := x[i+rest].pos;
  psize1 := shared;  psize2 := dim-shared; i:=0; tr:=true;
  while ((i<=dim-1)and tr) do begin if (partrest[i]<>part1[i]) then tr:=false; i:=i+1; end;
  if tr then begin temp := psize1;	psize1 := psize2;	psize2 := temp; end;
end;

Procedure divideFunctions1(x:array of double; var part1:array of double; var part2:array of double; m:double; var psize1:integer; var psize2:integer);
  var   shared,rest, i, total,temp:integer;  partrest:array of double; tr:boolean;
begin
  setlength(partrest,dim);
  if (m <= 0.5) then begin for i:=0 to dim-1 do partrest[i] := part2[i]; end
                     else begin for i:=0 to dim-1 do partrest[i] := part1[i]; m := 1-m; end;
  shared := floor(dim*m);  rest := 2*shared;
  for i := 0 to shared-1 do begin part1[i] := x[i*2]; part2[i]:= x[i*2+1]; end;
  total := dim-shared;
  for i := 0 to (total-shared)-1 do partrest[i+shared] := x[i+rest];
  psize1 := shared;  psize2 := dim-shared; i:=0; tr:=true;
  while ((i<=dim-1)and tr) do begin if (partrest[i]<>part1[i]) then tr:=false; i:=i+1; end;
  if tr then begin temp := psize1;	psize1 := psize2;	psize2 := temp; end;
end;

Function f_Hybrid_12(x:array of str):double;       //Fonction: F12
  var  part1, part2:array of double; size1, size2:integer; f1,f2:double;
begin
  setlength(part1,dim);  setlength(part2,dim);
  divideFunctions(x,part1, part2, 0.25, size1, size2);
  f1:=Extended_f_10NoDesplazamiento1(size1, part1);
  f2:=Shifted_Sphere1(size2,part2)-f_bias[0]; // assert(f1 >= 0); assert(f2 >= 0);
  f_Hybrid_12:=f1+f2;
end;

Function f_Hybrid_13(x:array of str):double;      //Fonction: F13
 var part1,part2:array of double; size1, size2:integer; f1, f2:double;
begin
 setlength(part1,dim);  setlength(part2,dim);
 divideFunctions(x, part1, part2, 0.25, size1, size2);
 f1:=Extended_f_10NoDesplazamiento1(size1, part1);
 f2:=Shifted_Rosenbrock1(size2,part2)-f_bias[2];
 f_Hybrid_13:=f1+f2;
end;

Function f_Hybrid_14(x:array of str):double;     //Fonction: F14
  var part1,part2:array of double; size1, size2:integer; f1, f2:double;
begin
 setlength(part1,dim);  setlength(part2,dim);
 divideFunctions(x, part1, part2, 0.25, size1, size2);
 f1:=Extended_f_10NoDesplazamiento1(size1, part1);
 f2:=Shifted_Rastrigin1(size2, part2)-f_bias[3];
 f_Hybrid_14:=f1+f2;
end;

Function f_Hybrid_15(x:array of str):double;         //Fonction: F15
  var part1,part2:array of double; i,size1, size2:integer; f1, f2:double; desp:array of double;
begin
 setlength(part1,dim);  setlength(part2,dim); setlength(desp,Dim);
 for i := 0 to dim-1 do desp[i] := x[i].pos - hybrid_15[i];
 divideFunctions1(desp, part1, part2, 0.25, size1, size2);
 f1:=f_BohachevskyNoDesplazamiento1(size1, part1);
 f2:=f_Schwefel2_22NoDesplazamiento1(size2, part2);
 f_Hybrid_15:=f1+f2;
end;

Function f_Hybrid_16(x:array of str):double;       //Fonction: F16
  var part1,part2:array of double; size1, size2:integer; f1, f2:double;
begin
 setlength(part1,dim);  setlength(part2,dim);
 divideFunctions(x, part1, part2, 0.5, size1, size2);
 f1:=Extended_f_10NoDesplazamiento1(size1, part1);
 f2:=Shifted_Sphere1(size2, part2)-f_bias[0];
 f_Hybrid_16:=f1+f2;
end;

Function f_Hybrid_17(x:array of str):double;      //Fonction: F17
  var part1,part2:array of double; size1, size2:integer; f1, f2:double;
begin
  setlength(part1,dim);  setlength(part2,dim);
  divideFunctions(x, part1, part2, 0.75, size1, size2);
  f1:=Extended_f_10NoDesplazamiento1(size1, part1);
  f2:=Shifted_Rosenbrock1(size2, part2)-f_bias[2];  //assert(f1 >= 0);  assert(f2 >= 0);
  f_Hybrid_17:=f1+f2;
end;

Function f_Hybrid_18(x:array of str):double;    //Fonction: F18
  var part1,part2:array of double; size1, size2:integer; f1, f2:double;
begin
 setlength(part1,dim);  setlength(part2,dim);
 divideFunctions(x, part1, part2, 0.75, size1, size2);
 f1:=Extended_f_10NoDesplazamiento1(size1, part1);  //  assert(f1 >= 0);
 f2:=Shifted_Rastrigin1(size2, part2)-f_bias[3];  //assert(f2 >= 0);
 f_Hybrid_18:=f1+f2;
end;

Function f_Hybrid_19(x:array of str):extended;   //Fonction: F19
  var part1,part2:array of double;  i,size1, size2:integer; f1, f2:extended;desp:array of double;
begin
 setlength(part1,dim);  setlength(part2,dim); setlength(desp,dim);
 for i := 0 to dim-1 do desp[i] := x[i].pos - hybrid_19[i];
 divideFunctions1(desp, part1, part2, 0.75, size1, size2);
 f1:=f_BohachevskyNoDesplazamiento1(size1, part1);
 f2:=f_Schwefel2_22NoDesplazamiento1(size2, part2);
 f_Hybrid_19:=f1+f2;
end;
 // ************* End Benchmark's Functions *************

Procedure Tri;
 var i,j:integer; a:double; b:integer; c:string;
begin
 for i:=0 to nb_eval-2 do
 begin
  for j:=i+1 to nb_eval-1 do
  begin
    if tabsauv[j].eval<tabsauv[i].eval then
    begin
     a:=tabsauv[i].eval; tabsauv[i].eval:=tabsauv[j].eval; tabsauv[j].eval:=a;
     c:=tabsauv[i].time; tabsauv[i].time:=tabsauv[j].time; tabsauv[j].time:=c;
     a:=tabsauv[i].g1; tabsauv[i].g1:=tabsauv[j].g1; tabsauv[j].g1:=a;
     a:=tabsauv[i].g50; tabsauv[i].g50:=tabsauv[j].g50; tabsauv[j].g50:=a;
     a:=tabsauv[i].g100; tabsauv[i].g100:=tabsauv[j].g100; tabsauv[j].g100:=a;
     a:=tabsauv[i].g500; tabsauv[i].g500:=tabsauv[j].g500; tabsauv[j].g500:=a;
     a:=tabsauv[i].g1000; tabsauv[i].g1000:=tabsauv[j].g1000; tabsauv[j].g1000:=a;
     a:=tabsauv[i].g1200; tabsauv[i].g1200:=tabsauv[j].g1200; tabsauv[j].g1200:=a;
     a:=tabsauv[i].g3000; tabsauv[i].g3000:=tabsauv[j].g3000; tabsauv[j].g3000:=a;
     a:=tabsauv[i].g4000; tabsauv[i].g4000:=tabsauv[j].g4000; tabsauv[j].g4000:=a;
     a:=tabsauv[i].g5000; tabsauv[i].g5000:=tabsauv[j].g5000; tabsauv[j].g5000:=a;
     a:=tabsauv[i].g10000; tabsauv[i].g10000:=tabsauv[j].g10000; tabsauv[j].g10000:=a;
     a:=tabsauv[i].g25000; tabsauv[i].g25000:=tabsauv[j].g25000; tabsauv[j].g25000:=a;
     a:=tabsauv[i].g50000; tabsauv[i].g50000:=tabsauv[j].g50000; tabsauv[j].g50000:=a;
     a:=tabsauv[i].g70000; tabsauv[i].g70000:=tabsauv[j].g70000; tabsauv[j].g70000:=a;
     a:=tabsauv[i].g100000; tabsauv[i].g100000:=tabsauv[j].g100000; tabsauv[j].g100000:=a;
     a:=tabsauv[i].g250000; tabsauv[i].g250000:=tabsauv[j].g250000; tabsauv[j].g250000:=a;
     a:=tabsauv[i].g500000; tabsauv[i].g500000:=tabsauv[j].g500000; tabsauv[j].g500000:=a;
     a:=tabsauv[i].g1000000; tabsauv[i].g1000000:=tabsauv[j].g1000000; tabsauv[j].g1000000:=a;
     a:=tabsauv[i].g2500000; tabsauv[i].g500000:=tabsauv[j].g2500000; tabsauv[j].g2500000:=a;
     a:=tabsauv[i].g5000000; tabsauv[i].g5000000:=tabsauv[j].g5000000; tabsauv[j].g5000000:=a;
    end;
  end;
 end;
end;

//-------------------------------
Procedure Init_Spop();
 var index, j : integer;
     val : double;
begin
 For index := 1 To Swarmsize do
 begin
  SwarmPop[index].num := index;
  setlength(SwarmPop[index].deplacement,Dim);
  setlength(SwarmPop[index].Bestdeplacement,Dim);
  setlength(SwarmPop[index].Worstdeplacement,Dim);
   For j := 0 To Dim - 1 do
   begin
    SwarmPop[index].Deplacement[j].pos :=u+(v-u)*random;
    SwarmPop[index].BestDeplacement[j].pos :=SwarmPop[index].Deplacement[j].pos;
    SwarmPop[index].Deplacement[j].vel :=MinVel+(MaxVel-MinVel)*random;
    SwarmPop[index].BestDeplacement[j].vel :=SwarmPop[index].Deplacement[j].vel;
  end; // i
 end;
End;

  //-----------------------------
Procedure Evaluation();
var index,j,k:integer; s:double;
begin
 For index := 1 To Swarmsize do
 begin
  case fdejon of
  1:  SwarmPop[index].Fitness:=Shifted_Sphere(SwarmPop[index].deplacement);
  2:  SwarmPop[index].Fitness:=Schwefel_Problem(SwarmPop[index].deplacement);
  3:  SwarmPop[index].Fitness:=Shifted_Rosenbrock(SwarmPop[index].deplacement);
  4:  SwarmPop[index].Fitness:=Shifted_Rastrigin(SwarmPop[index].deplacement);
  5:  SwarmPop[index].Fitness:=Shifted_Griewank(SwarmPop[index].deplacement);
  6:  SwarmPop[index].Fitness:=Shifted_Ackley(SwarmPop[index].deplacement);
  7:  SwarmPop[index].Fitness:=f_Schwefel2_22(SwarmPop[index].deplacement);
  8:  SwarmPop[index].Fitness:=f_Schwefel1_2(SwarmPop[index].deplacement);
  9:  SwarmPop[index].Fitness:=Extended_f_10(SwarmPop[index].deplacement);
 10:  SwarmPop[index].Fitness:=f_Bohachevsky(SwarmPop[index].deplacement);
 11:  SwarmPop[index].Fitness:=f_Schaffer(SwarmPop[index].deplacement);
 12:  SwarmPop[index].Fitness:=f_Hybrid_12(SwarmPop[index].deplacement);
 13:  SwarmPop[index].Fitness:=f_Hybrid_13(SwarmPop[index].deplacement);
 14:  SwarmPop[index].Fitness:=f_Hybrid_14(SwarmPop[index].deplacement);
 15:  SwarmPop[index].Fitness:=f_Hybrid_15(SwarmPop[index].deplacement);
 16:  SwarmPop[index].Fitness:=f_Hybrid_16(SwarmPop[index].deplacement);
 17:  SwarmPop[index].Fitness:=f_Hybrid_17(SwarmPop[index].deplacement);
 18:  SwarmPop[index].Fitness:=f_Hybrid_18(SwarmPop[index].deplacement);
 19:  SwarmPop[index].Fitness:=f_Hybrid_19(SwarmPop[index].deplacement);
 end;
  If (generation = 1) Then SwarmPop[index].BestFitness := SwarmPop[index].Fitness;
  If ((generation = 1) And (index = 1)) Then Gbest := 1;
  If (SwarmPop[index].Fitness < SwarmPop[index].BestFitness) Then
  begin
   SwarmPop[index].BestFitness := SwarmPop[index].Fitness;
    For j := 0 To Dim - 1 do
    begin
     SwarmPop[index].BestDeplacement[j].PrevPos:=SwarmPop[index].BestDeplacement[j].pos;
     SwarmPop[index].BestDeplacement[j].pos := SwarmPop[index].deplacement[j].pos;
     SwarmPop[index].BestDeplacement[j].PrevVel:=SwarmPop[index].BestDeplacement[j].vel;
     SwarmPop[index].BestDeplacement[j].vel := SwarmPop[index].deplacement[j].vel;
    end;
  End;
  If (SwarmPop[index].BestFitness < SwarmPop[Gbest].BestFitness) Then Gbest := index;
 end;
end;
//----------------------
procedure Correction1(index:integer;j:integer);
 var  valid_sup:boolean;   k1:integer; k:double;
begin
valid_sup:=false;
 while (not valid_sup) do
 begin
   if ((SwarmPop[index].Deplacement[j].pos<=v)and (SwarmPop[index].Deplacement[j].pos>=u))then valid_sup:=true
   else valid_sup:=false;
   if (not valid_sup) then SwarmPop[index].Deplacement[j].pos:=u+(v-u)*random;
 end;
end;

  //************************
Procedure Update();
 Var index, i, j, limitInf, limitSup : integer;
begin
 For index := 1 To Swarmsize do
 begin
   limitInf:=0;  limitSup:=limitInf+(taille_group-1);
   For j := limitInf To limitSup do
   begin
     InertWW[0] := (Exp(InertWW[0])/(100));
     pas[0] :=  1/Ln(2*(30)) * RandG(0,1) * ln(sqrt(1000+1));
     SwarmPop[index].Deplacement[j].vel :=(InertWW[0])*SwarmPop[index].Deplacement[j].vel
     +(SwarmPop[index].BestDeplacement[j].pos-SwarmPop[index].Deplacement[j].pos)
     +((SwarmPop[Gbest].BestDeplacement[j].pos) - (SwarmPop[index].Deplacement[j].pos));
   end;
   For j := limitInf To limitSup do
   begin
     SwarmPop[index].deplacement[j].pos :=((SwarmPop[index].deplacement[j].pos) + (pas[0]*(SwarmPop[index].deplacement[j].vel)));
     Correction1(index,j);
   end;
   For j := limitInf To limitInf+(Dim-limitInf)-1 do
   begin
    InertWW[1] := -(Exp(InertWW[1])/(100));
    pas[1] :=  1/Ln(2*(30)) * RandG(0,1) * Ln(sqrt(1000+1));
    SwarmPop[index].Deplacement[j].vel :=(InertWW[1])*SwarmPop[index].Deplacement[j].vel
     +(SwarmPop[index].BestDeplacement[j].pos-SwarmPop[index].Deplacement[j].pos)
     +((SwarmPop[Gbest].BestDeplacement[j].pos) - (SwarmPop[index].Deplacement[j].pos));
   end;
   For j := limitInf To limitInf+(Dim-limitInf)-1 do
    begin
     SwarmPop[index].deplacement[j].pos :=0.999*(SwarmPop[index].deplacement[j].pos) + (pas[1]*(SwarmPop[index].deplacement[j].vel));
     Correction1(index,j);
   End;
 End; //index
End;

  //------------------------------------------
function Arret(simul:integer):boolean;
   var i,j:integer; trouve,tr:boolean;
begin
 if (generation=FE) then Arret:=true else Arret:=false;
end;
end.
///--------------




