%% GRADE initialization

%% control parameters for GRADE algorithm:
% parameter controling population size = pool_rate * dim
pool_rate = 10.0;
% radioactivity control the amount of new solutions created by mutation
radioactivity = 0.2;
% cross_limit control the distance of new solution from its parent created by cross operator
cross_limit = 1.0;  
%% other options
% choose if you want to display resunts during optimization
displayconf = 1; %set 1 for on and 0 for off
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if displayconf
    display( ' ' );
    display(sprintf( 'GRADE cofiguration:' ));
    display(sprintf( '   pool_rate = %g', pool_rate ));
    display(sprintf( '   radioactivity = %g', radioactivity ));
    display(sprintf( '   cross_limit = %g', cross_limit ));
    display( ' ' );
end

mutation_rate_limit = 1.0;
SelectedSize = pool_rate*Dim;
PoolSize = 2*SelectedSize;

generation = 0;
fitness_call = 0;
Force = zeros(PoolSize,1);
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

bsf = zeros(Dim,1);
btg = zeros(Dim,1);
bsf_value = 999999999999 ; 
btg_value = 999999999999 ; 

%% EVALUATE_GENERATION 
if displayconf
    display( sprintf( 'Fitness call    BTG value    BSF value', fitness_call, btg_value) );
end
for i = 1:PoolSize;
    if Return_to_domain == 1
        for k = 1:Dim 
            if CH(i,k) < Domain(k,1) CH(i,k) = Domain(k,1); end
            if CH(i,k) > Domain(k,2) CH(i,k) = Domain(k,2); end
        end
    end 

    Force(i) = F( (CH(i,:))' );
    fitness_call = fitness_call+1; 
    if Force(i) < btg_value
        btg_value = Force(i);
        btg(:)=CH(i,:);  
    end 
    
end 
if btg_value < bsf_value 
    bsf(:) = btg(:);
    bsf_value = btg_value; 
    Feval()
end 
if displayconf
    display( sprintf( '%12d   %10g   %10g', fitness_call, btg_value, bsf_value) );
end

%% SELECT 
ActualSize=PoolSize;
while ActualSize > SelectedSize
    i1 = randsample(ActualSize,1); 
    i2 = randsample(ActualSize-1,1)+1; 
    if i1 == i2 i2 = i2-1; end 
    if Force(i1) < Force(i2) dead = i2;  
    else dead = i1; end
    last = ActualSize; 
    CH(dead,:) = CH(last,:); 
    Force(dead) = Force(last); 
    ActualSize = ActualSize-1; 
end

%% author: Anicka Kucerova, Czech Technical University in Prague
%% version 0.0.3
%% 23 Mar 2009