function [new_pop,new_chrom,new_value,info]=super_cerveza(t,step,pop,chrom,value,ind_str,objs_str,fitinfo,factor1,step1,factor2,step2)

% SUPER_CERVEZA	Function that modifies individuals of the population adding/substrancting a factor in each gene
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS,INFO]= SUPER_CERVEZA(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR1,STEP1,
%																					FACTOR2,STEP2)
%
% It modifies the profile of the population, adding/substracting a factor for each of the genes iteratively, and
% selecting among the current individual and the two created the best one.
% FACTOR is an array which stores the factor of modification (FACTOR1 for the Time, FACTOR2 for the temperature). 
% The first element will be used when T/STEP=1, the second
% when T/STEP=2, ... and so on (STEP1 is refered to the Time and
% STEP2 to the temperature). If T/STEP>length(FACTOR) then the last element of FACTOR will be applied
% WHICHONES is used for indicating which are the genes to modifify. When no provided, it will be applied to all the
% genes of each individual
%
% See also GA_SUPER

% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 9: El Aprendizaje con Algoritmos Genéticos
% Autores: Bonifacio de Andres y Toro

%
changed=0;
if mod(t,step2)==0
   [new_pop,new_chrom,new_value,info]=super_cervezaTemp(t,step2,pop,chrom,value,ind_str,objs_str,fitinfo,factor2);
   changed=1;
elseif mod(t,step1)==0
   [new_pop,new_chrom,new_value,info]=super_cervezaTime(t,step1,pop,chrom,value,ind_str,objs_str,fitinfo,factor1);
   changed=1;
end

if changed==0
   new_pop=pop;
   new_chrom=chrom;
   new_value=value;
   info=[];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [new_pop,new_chrom,new_value,info]=super_cervezaTime(t,step,pop,chrom,value,ind_str,objs_str,fitinfo,factor,whichones)

% SUPER_VLPROFILE	Function that modifies individuals of the population adding/substrancting a factor in each gene
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS]=      SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR)
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS]=      SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR,
%																					WHICHONES)
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS,INFO]= SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR)
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS,INFO]= SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR,
%																					WHICHONES)
%
% It modifies the profile of the population, adding/substracting a factor for each of the genes iteratively, and
% selecting among the current individual and the two created the best one.
% FACTOR is an array which stores the factor of modification. The first element will be used when T/STEP=1, the second
% when T/STEP=2, ... and so on. If T/STEP>length(FACTOR) then the last element of FACTOR will be applied
% WHICHONES is used for indicating which are the genes to modifify. When no provided, it will be applied to all the
% genes of each individual
% WHICH_M when returned, is a matrix with the same dimension as POP with POP(i,j):
%		0  -> gene j has not been changed in the individual i
%		1  -> gene j has changed by addition in the individual i
%		-1 -> gene j has changed by substraction in the individual i
%
% Important: This function can only be applied to gene POPULATION whose gene representation is a numberic matrix
%				 It does not use nitching, to select the best ones.
%
% See also GA_SUPER
%
disp(sprintf('superTime %d',t));
chrom_coding=iscell(chrom);
factor=factor(:);
t1=t/step;
if t1>length(factor)
   factorusado=factor(end);
else
   factorusado=factor(t1);
end

param=is_fields(ind_str,3);
limit=param{1}{1};

if nargin==9
	obtain=1   ;
else
   obtain=0;
   jb=whichones
   climitb=length(whichones)
end

new_pop=pop;
new_value=value;
new_chrom=chrom;
[r,c]=size(pop);

info=cell(r,1);

for l=1:r
   if obtain
      climit=size(new_pop{l},2);
      j=1:climit;
   end
   new_popl=new_pop{l};
   new_chroml=new_chrom(l);
   new_valuel=new_value(l,:);
   infol=zeros(1,climit);
   for p=1:climit
      if j(p)<size(new_popl,2)
         timet=sum(new_popl(2,:));
         %aux_pop={new_popl;new_popl;new_popl};
          aux_pop={new_popl;new_popl};
			restar=min(factorusado,timet-limit(1));
         aux_pop{2}(2,j(p))=aux_pop{2}(2,j(p))-restar;		
         howm=2;
         if aux_pop{2}(2,j(p))<=0
            restar=restar+aux_pop{2}(2,j(p));
            aux_pop{2}=[aux_pop{2}(:,1:j(p)-1),aux_pop{2}(:,j(p)+1:end)];
            next=j(p);
         else
            next=j(p)+1;
         end
         if next<size(aux_pop{2},2)
            aux_pop{3}=aux_pop{2};
            aux_pop{3}(2,next)=aux_pop{3}(2,next)+restar;
            howm=3;
         end
                 
         aux_pop(2:howm)=ga_repair(t,aux_pop(2:howm),ind_str);
         chrom_aux=[new_chroml;ga_evalgen(t,aux_pop(2:howm),ind_str,chrom_coding)];
   		value_aux=[new_valuel;ga_evalchrom(t,chrom_aux(2:howm),objs_str)];
         fitness_aux=ga_fitness(t,value_aux,fitinfo.obj,fitinfo.fitfun,fitinfo.fitparam);

         [m,ind]=max(fitness_aux);
 
         if ind==2
            infol(j(p))=-1;
         elseif ind==3
      		infol(j(p))=1;
         end
         new_popl=aux_pop{ind};
         new_chroml=chrom_aux(ind);
         new_valuel=value_aux(ind,:);
         if (0)
         disp(sprintf('In super %d %d value: (ind=%d)',l,j(p),ind));
         disp(sprintf('%f ',new_valuel));
        % show_bestn(t,aux_pop,chrom_aux,value_aux,fitness_aux,3);

         end
      else
         break;
      end
   end
   new_pop{l}=new_popl;
   new_chom(l)=new_chroml;
   new_value(l,:)=new_valuel;   
   info{l}=infol;
end

         
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [new_pop,new_chrom,new_value,info]=super_cervezaTemp(t,step,pop,chrom,value,ind_str,objs_str,fitinfo,factor,whichones)

% SUPER_VLPROFILE	Function that modifies individuals of the population adding/substrancting a factor in each gene
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS]=      SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR)
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS]=      SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR,
%																					WHICHONES)
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS,INFO]= SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR)
%
% [NEW_POP,NEW_CHROM,NEW_VALUE,NEW_FITNESS,INFO]= SUPER_VLPROFILE(T,STEP,POP,CHROM,VALUE,IND_STR,OBJS_STR,FITINFO,FACTOR,
%																					WHICHONES)
%
% It modifies the profile of the population, adding/substracting a factor for each of the genes iteratively, and
% selecting among the current individual and the two created the best one.
% FACTOR is an array which stores the factor of modification. The first element will be used when T/STEP=1, the second
% when T/STEP=2, ... and so on. If T/STEP>length(FACTOR) then the last element of FACTOR will be applied
% WHICHONES is used for indicating which are the genes to modifify. When no provided, it will be applied to all the
% genes of each individual
% WHICH_M when returned, is a matrix with the same dimension as POP with POP(i,j):
%		0  -> gene j has not been changed in the individual i
%		1  -> gene j has changed by addition in the individual i
%		-1 -> gene j has changed by substraction in the individual i
%
% Important: This function can only be applied to gene POPULATION whose gene representation is a numberic matrix
%				 It does not use nitching, to select the best ones.
%
% See also GA_SUPER
%
disp(sprintf('superTemp %d',t));
chrom_coding=iscell(chrom);
factor=factor(:);
t1=t/step;
if t1>length(factor)
   factorusado=factor(end);
else
   factorusado=factor(t1);
end

if nargin==9
	obtain=1   ;
else
   obtain=0;
   jb=whichones
   climitb=length(whichones)
end

new_pop=pop;
new_value=value;
new_chrom=chrom;
[r,c]=size(pop);

info=cell(r,1);

for l=1:r
   if obtain
      climit=size(new_pop{l},2);
      j=1:climit;
   end
   new_popl=new_pop{l};
   new_chroml=new_chrom(l);
   new_valuel=new_value(l,:);
   infol=zeros(1,climit);
   for p=1:climit
      if j(p)<=size(new_popl,2)
         aux_pop={new_popl;new_popl;new_popl};
         aux_pop{2}(1,j(p))=aux_pop{2}(1,j(p))-factorusado;		
         aux_pop{3}(1,j(p))=aux_pop{3}(1,j(p))+factorusado;      
         aux_pop(2:3)=ga_repair(t,aux_pop(2:3),ind_str);
   		chrom_aux=[new_chroml;ga_evalgen(t,aux_pop(2:3),ind_str,chrom_coding)];
   		value_aux=[new_valuel;ga_evalchrom(t,chrom_aux(2:3),objs_str)];
         fitness_aux=ga_fitness(t,value_aux,fitinfo.obj,fitinfo.fitfun,fitinfo.fitparam);
         [m,ind]=max(fitness_aux);
 
         if ind==2
            infol(j(p))=-1;
         elseif ind==3
      		infol(j(p))=1;
         end
         new_popl=aux_pop{ind};
         new_chroml=chrom_aux(ind);
         new_valuel=value_aux(ind,:);
         if (0)
         disp(sprintf('In super %d %d value: (ind=%d)',l,j(p),ind));
         disp(sprintf('%f ',new_valuel));
        % show_bestn(t,aux_pop,chrom_aux,value_aux,fitness_aux,3);

         end
      else
         break;
      end
   end
   new_pop{l}=new_popl;
   new_chom(l)=new_chroml;
   new_value(l,:)=new_valuel;   
   info{l}=infol;
end

         



