function finalgenetic2()

clc
tic
% Populationsize
% Make sure this is an even number, preferably dividable by 4!
n = 12;

% Number of params
nparam = 8;

% Generation number
gener = 1;

% The number of generations that will be tested per generation
anglespergen = 5;

% Mutation probability
mutprob = 1 / (2 * n * nparam);

% Possible parameter ranges:
%exc = 10*rand(n,1) + 10*ones(n,1);
exc = 45*rand(n,1);
%exc = [11;      18;     19.0982;    10.01;  16.092;  12];
%exc = [11;      10.872615;     11.0982;    10.01;  12.092;  12];

% Possible parameter ranges:
%inh = 10*rand(n,1) + 3*ones(n,1);
inh = 45*rand(n,1);
%inh = [8.5; 3.9887;     6.345;      12.6;   3.23;    9 ];\

% Possible parameter ranges:
%tadapt = 50*rand(n,1) + 20*ones(n,1);
tadapt = 100*rand(n,2);

%0-10
tmemb = 10*rand(n,2);

%0.0-0.01
ginc = 6*rand(n,2) / 100;

results = zeros(n,1);
params = [exc inh tadapt tmemb ginc results];


    
genresults = zeros(n,anglespergen);
for k=1:anglespergen
    soundangle = pi*rand(1,1) / 3;

    % Test current generation
    for i=1:n
        genresults(i,k) = mostparams(params(i,1:nparam),soundangle);
    end       
end
    
params(:,nparam+1) = mean(genresults,2);

cont = 1;

while cont
    
    % Display current generation's results
    disp(sprintf('\nGeneration %d', gener))
   % disp(sprintf('Cricket No. \t  Exc.Str. \t     Inh.Str. \t     tAdapt \t     tMemb \t     Ginc \t     timeresult'))
   % disp(sprintf('--------------------------------------------------------------------------------------------------------'))
   % for i=1:n
   %     disp(sprintf('      %d \t     %f \t     %f \t     %f \t     %f \t     %f \t     %f', i, params(i,:)));% params(i,1), params(i,2), params(i,3), params(i,nparam+1)));
   % end
    %pause

    gener = gener + 1;
    
    % Reproduction operation
    
    middle = median(params(:,nparam+1));
    counter = 0;
    index = zeros(1,n/2);
    for i=1:n
        % Find indices of best crickets
        for k=1:n/2
            if (index(k) == 0) | (params(i,nparam+1) <= params(index(k),nparam+1))
                for j=n/2:-1:k+1
                    index(j) = index(j-1);
                end
                index(k) = i;
                break
            end
        end
    end
    for i=1:n/2
        % Store parameters of best crickets twice
        params2(i,:) = params(index(i),:);
        params2(i+n/2,:) = params2(i,:);
    end
    
    
    % Cross-over operation
    
    params(:,1) = params2(:,1);
    for i=1:2:n-1
        % find random crossover position
        crosspos = ceil((nparam-1)*rand(1,1));
        
        params(i,1:crosspos) = params2(i,1:crosspos);
        params(i+1,1:crosspos) = params2(i+1,1:crosspos);
        
        params(i,crosspos+1:nparam) = params2(i+1,crosspos+1:nparam);
        params(i+1,crosspos+1:nparam) = params2(i,crosspos+1:nparam);
    end
    
    
    % Mutation operation
    
    probs = rand(1,n*nparam);
    for i=1:n*nparam
        if probs(1,i) < mutprob
            if i/n <= 2
                params(n-mod(i,n),ceil(i/n)) = 45*rand(1,1);
            elseif i/n <= 4
                params(n-mod(i,n),ceil(i/n)) = 100*rand(1,1);
            elseif i/n <= 6
                params(n-mod(i,n),ceil(i/n)) = 10*rand(1,1);
            else
                params(n-mod(i,n),ceil(i/n)) = 6*rand(1,1) / 100;
            end
        end
    end
    
    genresults = zeros(n,anglespergen);
    for k=1:anglespergen
        soundangle = pi*rand(1,1) / 3;

        % Test current generation
        for i=1:n
            genresults(i,k) = mostparams(params(i,1:nparam),soundangle);
        end       
    end
    
    params(:,nparam+1) = mean(genresults,2);
    
    cont = 0;
    for i=2:n
        for j=1:nparam
            if params(i,j) ~= params(1,j) 
                cont = 1;
                break;
            end
        end
    end      
end

toc

% Display current generation's results
disp(sprintf('Generation = %d', gener))
disp(sprintf('Populationsize = %d', n))
disp(sprintf('Excitatory strength = %f', params(1,1)))
disp(sprintf('Inhibitory strength = %f', params(1,2)))
disp(sprintf('tAdapt AN neurons = %f', params(1,3)))
disp(sprintf('tAdapt Motor neurons = %f', params(1,4)))
disp(sprintf('tMemb AN neurons = %f', params(1,5)))
disp(sprintf('tMemb Motor neurons = %f', params(1,6)))
disp(sprintf('Ginc AN neurons = %f', params(1,7)))
disp(sprintf('Ginc Motor neurons = %f', params(1,8)))