function new_pop=crossing_cerveza(t,pop1,pop2,crossp,ind_str)

% CROSSING_CERVEZA	It crosses two populations of variable length by the given crossing points 
% 
% NEW_POP=CROSSING_cerveza(T,POP1,POP2,CROSSP,IND_STR)
%
% IND_STR is the individual structure and T the generation number, and although they are not used in this function 
% they should keep on existing to be used as a cross-performing method.
%
% See also GA_CROSS, IS_NEW, 


% 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

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

newpop1=pop1;
newpop2=pop2;
[r,c]=size(pop1);
for i=1:r
   l=length(crossp{i});
   pop{1}=pop1{i};
   pop{2}=pop2{i};
   crosspi=round(cumsum(crossp{i},2)/dt);
   time{1}=round(cumsum(pop{1}(2,:))/dt);
   time{2}=round(cumsum(pop{2}(2,:))/dt);
   npop1=[];
   npop2=[];
   j=1;
   while j<l
      a1=rem(j-1,2)+1;
      a2=~rem(j-1,2)+1;
      m1=find(time{a1}<crosspi(a1,j)); 
      m2=find(time{a2}<crosspi(a2,j)); 
      if ~isempty(m1)
         m1=[m1,m1(end)+1];
      else
         m1=1;
      end
      if ~isempty(m2)
         m2=[m2,m2(end)+1];
      else
         m2=1;
      end
      l1=length(m1);
      l2=length(m2);
      npop1(:,end+1:end+l1)=pop{a1}(:,m1);
      npop2(:,end+1:end+l2)=pop{a2}(:,m2);
      
      difvalue=time{a1}(m1(end))-crosspi(a1,j);
      if difvalue>0
         npop1(2,end)=npop1(2,end)-difvalue*dt;
         valuec=(pop{a1}(1,m1(end)+1)-pop{a1}(1,m1(end)))*npop1(2,end)/pop{a1}(2,m1(end))+pop{a1}(1,m1(end));
         pop{a1}=pop{a1}(:,m1(end):end);
         pop{a1}(1,1)=valuec;
         pop{a1}(2,1)=difvalue*dt;
         time{a1}=time{a1}(m1(end):end);
      else
         pop{a1}=pop{a1}(:,m1(end)+1:end);
         time{a1}=time{a1}(m1(end)+1:end);
      end
      
      difvalue=time{a2}(m2(end))-crosspi(a2,j);
      if difvalue>0
         npop2(2,end)=npop2(2,end)-difvalue*dt;
         valuec=(pop{a2}(1,m2(end)+1)-pop{a2}(1,m2(end)))*npop2(2,end)/pop{a2}(2,m2(end))+pop{a2}(1,m2(end));
         pop{a2}=pop{a2}(:,m2(end):end);
         pop{a2}(1,1)=valuec;
         pop{a2}(2,1)=difvalue*dt;
         time{a2}=time{a2}(m2(end):end);
      else
         pop{a2}=pop{a2}(:,m2(end)+1:end);
         time{a2}=time{a2}(m2(end)+1:end);
      end      
      j=j+1;
   end
   a1=rem(j-1,2)+1;
   a2=~rem(j-1,2)+1;
   npop1(:,end+1:end+size(pop{a1},2))=pop{a1};
   npop2(:,end+1:end+size(pop{a2},2))=pop{a2};
   newpop1{i}=npop1;
   newpop2{i}=npop2;

end
  
new_pop=[newpop1;newpop2];
