function best_control=choose_best_control(H_tilde,C,H_tilde0,k,apriori,regulator_parameters,candidates,compare_parameters)
num_of_candidates=regulator_parameters.num_of_candidates;
n_0=compare_parameters.n0;
realization1=zeros(num_of_candidates,n_0); 

H0=add_H_tilde0(regulator_parameters,H_tilde);

%generovani realizaci ztrat
for i=1:num_of_candidates
    C(:,k)=candidates(:,i);
    for j=1:n_0
        realization1(i,j)=generate_realization(apriori,H0,k,H_tilde,C,regulator_parameters);
    end
end
mean_values=mean(realization1,2);

%RSSS
if (compare_parameters.RSSS)
    h=compare_parameters.rinott;
    t=compare_parameters.student;
    variances=sum((realization1-mean_values*ones(1,n_0)).^2,2)/(n_0-1);
    W=t*sqrt((variances*ones(1,num_of_candidates)+ones(num_of_candidates,1)*variances')/n_0);

    pass=ones(num_of_candidates,1); %pass(i)=1 => i-ty kandidat jde do 2 faze
    for i=1:size(candidates)
        for j=1:size(candidates)
            if (mean_values(i)>mean_values(j)+max(0,W(i,j)-compare_parameters.delta)) %hledam minimum
                pass(i)=false;
                break;
            end
        end
    end
    
    if sum(pass)>1
        %druha faze RSSS
        %sum(pass)    
        for i=1:size(candidates)
            if (pass(i))
                n_1=max(1,(-floor(-variances(i)*(h/compare_parameters.delta)^2))-n_0);
                n_1=min(100,n_1);
                realization2=zeros(1,n_1);
                C(index)=candidates(i);
                for j=1:n_1
                    realization2(j)=generate_realization(apriori,index,H_tilde,C,horizont);
                end
                mean_values(i)=mean_values(i)/(1+n_1/n_0)+mean(realization2)/(1+n_0/n_1);
            end
        end
    end
end

%volba kadnidata s nejmensi prumernou ztratou
[min_val min_index]=min(mean_values);
best_control=candidates(min_index);
end