
nvar=2;
printf("%d\n",nvar);
ub(1:nvar)=5.12;
lb(1:nvar)=-5.12;
options.PopulationSize=755;
options.PopInitRange=[-5.12;5.12];
options.Generations=650;
options.Vectorized="off";
options.UseParallel="never";
options.TimeLimit=200;
options.FitnessLimit=1e-6;
options.CrossoverFraction=0.8;
options.CrossoverFcn=@crossoverscattered;
options.MutationFcn={@mutationgaussian,1,1};
options.SelectionFcn=@selectionstochunif;
options.FitnessScalingFcn=@fitscalingrank;
options.EliteCount=2;
options.InitialPopulation=gacreationuniform(nvar,@N_Rastringin,options);
options.InitialScores=[N_Rastringin(options.InitialPopulation)]; 
for a=1:30
[x,fval,exitflag,output,population,score]=ga(@N_Rastringin,nvar,[],[],[],[],lb,ub,[],options);

gen(a,1)=output.generations;
t(a,1)=output.time;

ValIn(a,:)=x;
ValOut(a,1)=fval;

printf("%d\t%d\t%d\n",a,output.time,output.generations);
endfor
Refval=cat(2,[1:30]',gen,t);
Statistics=[nvar, options.PopulationSize,mean(t),mean(gen)];
Precision=cat(2,ValIn,ValOut);
printf("%d\t%d\t%d\t%d\t%d\t%d\n",Statistics(1,:),length(t(lt(t,options.TimeLimit))),length(gen(lt(gen,options.Generations))));
  Index=nvar;
  --Index;
  RefPop(:,Index)=[nvar,mean(t),mean(gen),min(t),max(t),min(gen),max(gen)];
  RefValTime(:,Index)=t;
  RefValGen(:,Index)=gen;
#{
fid=fopen("StatistikRastriginsfkt_ga.csv","w");
fprintf(fid,"%s,%d,%d\n%s,%d,%d","Median",Statistics(1,:),"Mittelwert",Statistics(2,:));
fclose("all");

csvwrite("Referenzwerte_maxPop10.csv",Refval);
csvwrite("maxPop10.csv",Statistics);
csvwrite("Praezision_maxPop10.csv",Precision);
#}
#{
hold;
grid;
PlotRastringin;
#set(gca,"xtick",-5.12:.6:5.12);
for i=1:100
plot3(ValIn(i,1),ValIn(i,2),ValOut(i),"y*","markersize",16);
endfor
for i=1:options.PopulationSize
  plot3(options.InitialPopulation(i,1),options.InitialPopulation(i,2),rastriginsfcn(options.InitialPopulation(i,:)),"r*","markersize",14);
endfor

hold;
#}