k = 14;

names = {
   'test_grooteman_14_series_multiple_DP'   ...
    };
%     'test_grooteman_02_multiple_DP',         ...
%     'test_grooteman_03_10term',              ...
%     'test_grooteman_04_convex',              ...
%     'test_grooteman_05_concave',             ...
%     'test_grooteman_06_saddle_surface',      ...
%     'test_grooteman_07_highly_nonlinear',    ...
%     'test_grooteman_08_highly_nonlinear',    ...
%     'test_grooteman_09_parallel',            ...
%     'test_grooteman_10_series',              ...
%     'test_grooteman_11_parallel2',           ...
%     'test_grooteman_12_series2',             ...
%     'test_grooteman_13_parallel3'            ...
%     'test_grooteman_14_series_multiple_DP'   ...

N = 100;
result = struct([]);
result(1).P_f_OO_ADIS = [];
result(1).Beta_OO_ADIS = [];
result(1).Calc_OO_ADIS = [];
result(1).err_abs_OO_ADIS = [];
result(1).err_OO_ADIS = [];
result(1).P_f_ADIS = [];
result(1).Beta_ADIS = [];
result(1).Calc_ADIS = [];
result(1).err_abs_ADIS = [];
result(1).err_ADIS = [];
result(1).Exact = [];

clc;
warning off
for i = 1:length(names)
    for j = 1:N
        try
            fname = ['seed_' num2str(j) '_' names{i}];
            
            cd(names{i});
            
            r = load(fname);
            
            if j == 1
                result.Exact = r.result.Exact.P_f;
%                 result.Exact = 1.46e-7;

            end
            
            result.P_f_OO_ADIS(j)       = r.result.OO_ADIS.Output.P_f;
            result.Beta_OO_ADIS(j)      = r.result.OO_ADIS.Output.Beta;
            result.Calc_OO_ADIS(j)      = r.result.OO_ADIS.Output.Calc;
            result.err_abs_OO_ADIS(j)   = abs((result.P_f_OO_ADIS(j) - result.Exact)/result.Exact)*100;
            result.err_OO_ADIS(j)       = (result.P_f_OO_ADIS(j) - result.Exact)/result.Exact*100;
            result.P_f_ADIS(j)          = r.result.ADIS.Output.P_f;
            result.Beta_ADIS(j)         = r.result.ADIS.Output.Beta;
            result.Calc_ADIS(j)         = r.result.ADIS.Output.Calc;
            result.err_abs_ADIS(j)      = abs((result.P_f_ADIS(j) - result.Exact)/result.Exact)*100;
            result.err_ADIS(j)          = (result.P_f_ADIS(j) - result.Exact)/result.Exact*100;
        catch e
            disp(e.message)
            result.P_f_OO_ADIS(j)       = NaN;
            result.Beta_OO_ADIS(j)      = NaN;
            result.Calc_OO_ADIS(j)      = NaN;
            result.err_abs_OO_ADIS(j)   = NaN;
            result.err_OO_ADIS(j)       = NaN;
            result.P_f_ADIS(j)          = NaN;
            result.Beta_ADIS(j)         = NaN;
            result.Calc_ADIS(j)         = NaN;
            result.err_abs_ADIS(j)      = NaN;
            result.err_ADIS(j)          = NaN;
        end
        cd ..
    end
end

err_oo  = result.err_abs_OO_ADIS';
err     = result.err_abs_ADIS';
calc_oo = result.Calc_OO_ADIS';
calc    = result.Calc_ADIS';

save(['err_oo_' num2str(k) '.txt'],'err_oo','-ascii')
save(['err_' num2str(k) '.txt'],'err','-ascii')
save(['calc_oo_' num2str(k) '.txt'],'calc_oo','-ascii')
save(['calc_' num2str(k) '.txt'],'calc','-ascii')

% ADIS_err        = nanmean(abs((result.P_f_ADIS - result.Exact)/result.Exact))*100
% OO_ADIS_err     = nanmean(abs((result.P_f_OO_ADIS - result.Exact)/result.Exact))*100
% ADIS_bias       = nanmean((result.P_f_ADIS - result.Exact)/result.Exact)*100
% OO_ADIS_bias    = nanmean((result.P_f_OO_ADIS - result.Exact)/result.Exact)*100
% ADIS_Pf         = nanmean(result.P_f_ADIS)
% OO_ADIS_Pf      = nanmean(result.P_f_OO_ADIS)
% ADIS_Pfstd      = 100*nanstd(result.P_f_ADIS)/nanmean(result.P_f_ADIS)
% OO_ADIS_Pfstd   = 100*nanstd(result.P_f_OO_ADIS)/nanmean(result.P_f_OO_ADIS)
% ADIS_Calc       = nanmean(result.Calc_ADIS)
% OO_ADIS_Calc    = nanmean(result.Calc_OO_ADIS)
% ADIS_Calcstd    = nanstd(result.Calc_ADIS)
% OO_ADIS_Calcstd = nanstd(result.Calc_OO_ADIS)
% ADIS_failed     = sum(isnan(result.P_f_ADIS))
% OO_ADIS_failed  = sum(isnan(result.P_f_OO_ADIS))

