exec ex_swarm.sci; exec buscarnd.sci; exec swarm.sci;
exec ag_r.sci; exec direct.sci; exec swarm_stopcr.sci;

[xo,Ot,nS]=buscarnd('ex_swarm',[0 0],-1,1,100,[-1 -1],[1 1],[],[],[],[2 2]);
//[xo,Ot,nS]=swarm('ex_swarm',[0 0],0,[1 1],[-1 -1],[1 1],[],200,27,[],1,1);
//[xo,Ot,nS]=ag_r('ex_swarm',2,[-1 -1],[1 1],1e-4,1e-4,200,1,20,80,10,3,2,10,1,20);

// Para achar os dois melhores otimos
//[xo,Ot,nS]=buscarnd('ex_swarm',[0 0],0,2,20,[-1 -1],[1 1],[],[],[],[2 2]);

// Se colocar o terceiro argumento como -1 nos dois primeiros algoritmos, entao faz o grafico.

N = 100;

tic
for i=1:N,
  [xo,Ot(i),nS(i)]=buscarnd('ex_swarm',[0 0],0,1,100,[-1 -1],[1 1],[],[],[],[2 2]);
end
tb=toc
sum(nS)/N
nb=find(Ot < -17);
size(nb,2)
figure(2); hist(Ot); legend('buscarnd');

tic
for i=1:N,
  [xo,Ots(i),nSs(i)]=swarm('ex_swarm',[0 0],0,[1 1],[-1 -1],[1 1],[2 2],200,27,[],1,1);
end
ts=toc
sum(nSs)/N
ns=find(Ots < -17);
size(ns,2)
figure(3); hist(Ots); legend('swarm');

tic
for i=1:N,
   [xo,Otg(i),nSg(i)]=ag_r('ex_swarm',2,[-1 -1],[1 1],1e-4,1e-4,200,1,20,80,10,3,2,10,1,20);
end
tg=toc
sum(nSg)/N
ng=find(Otg < -17);
size(ng,2)
figure(4); hist(Otg); legend('genetico');

bounds = [-1 1;-1 1];
options.testflag  = 1; options.globalmin = -17.9602;
//options.testflag  = 0; // mínimo global desconhecido
options.showits   = 0;
options.tol       = 1e-2; // percentual
Problem.f = 'ex_swarm';

tic
   [Otd,xo,ht] = direct(Problem,bounds,options);
td=toc
figure(5); plot(ht(:,2),ht(:,3))
xlabel('Fcn Evals');
ylabel('f{x}'); legend('Direct');

tic
for i=1:N,
  [xo,Otsc(i),nSsc(i),bank]=swarm_stopcr('ex_swarm',[0 0],0,[1 1],[-1 -1],[1 1],[2 2],200,27,[],1,1,0.95,1e-6);
end
tsc=toc
sum(nSsc)/N
nsc=find(Otsc < -17);
size(nsc,2)
figure(6); hist(Otsc); legend('swarm crit');
