%% CERAF check for new local extreme  

if ( lastbtg-btg_value > Prec )
    lastchange=generation;
    lastbtg = btg_value;
end

if ( ( generation-lastchange ) > quiet_generation ) 
    n_extreme = n_extreme+1;
    Eloc(n_extreme,:) = btg(:);
    Eval(n_extreme) = btg_value;
    for i=1:Dim Erad(n_extreme,i) = radius_rate*(Domain(i,2)-Domain(i,1)); end
    btg_value=999999999999;
    lastbtg=999999999999;
    lastchange=generation;
    
    CH = rand(PoolSize,Dim);
    for i = 1:PoolSize
        for j = 1:Dim
            CH(i,j) = CH(i,j)*(Domain(j,2)-Domain(j,1))+Domain(j,1);
        end
    end

    new_ceraf=0;
    monsters=PoolSize;

    if cerafdisplayconf 
        display( sprintf( '%d extreme marked at %dth generation: (%g) with zone: [%g,%g]', n_extreme, generation, Eloc(n_extreme,1), Eloc(n_extreme,1)-Erad(n_extreme,1), Eloc(n_extreme,1)+Erad(n_extreme,1) ));
    end
    
    if save_extremes == 1
        f = fopen(Efile,'a');
        fprintf( f, '%d %g', fitness_call, btg_value );
        for i = 1:Dim fprintf( f, ' %g', btg(i) ); end
        fprintf( f, '\n' );
        fclose( f );
    end
else
    new_ceraf=1;
end

%% CERAF check for new solution in radioactive zone to be killed

i = new_ceraf*SelectedSize+1;
ikilled = PoolSize+1;
nik = 0;
while i<=PoolSize
    if i == ikilled     nik = nik + 1;  else     nik = 0;    end
    if nik > 10
        Erad(:,:) = Erad(:,:)*0.5; 
        nik = 0;
        display( sprintf( 'Almost whole domain is radioactive, zones are decreased by half in their radius, quiet parameter should be set higher!' ) );
    end      
        
    for j = 1:n_extreme
        r=0.0;
        for k = 1:Dim r = r + ((CH(i,k)-Eloc(j,k))/Erad(j,k))^2; end
        if r < 1 
            if i-SelectedSize > monsters
                for k = 1:Dim Erad(j,k)=Erad(j,k)*deact_rate; end
            end
            CH(i,:) = rand(1,Dim);
            for j = 1:Dim
                CH(i,j) = CH(i,j)*(Domain(j,2)-Domain(j,1))+Domain(j,1);
            end
            ikilled = i;
            i=i-1;
            killed=killed+1;
            break
        end
    end
    i=i+1;
end

%% author: Anicka Kucerova, Czech Technical University in Prague
%% version 0.0.5
%% 24 Mar 2009
   
