% 1st round to find a potential solution of GMM problem. 

clear;
lb = [0,0,-Inf,-Inf,-Inf,-Inf,-Inf,-Inf,-Inf,-Inf];
ub = Inf*ones(1,10);
options = optimset('TolX',10^-9,'MaxFunEvals',2500,'TolFun',10^-9,'Algorithm','interior-point');

x0_1 = [0.8, 0.9];
x0_2 = [500, 2000];
x0_3 = [-50000, 0] ; % const
x0_4 = [-10000, 0, 10000]; % logasset_a
x0_5 = [-1000, 0, 1000];%[-5000, 0, 5000]; % logasset_a_2
x0_6 = [-70000, 0, 20000];%[-5000, 0, 5000]; % npf_a
x0_7= [-50000, 0, 50000]; %roa_annual
x0_8= [-50000, 0, 50000]; % realEst_a
x0_9= 0;%[-10, 0, 10]; % House
x0_10= 0;%[-10, 0, 10]; % Senate


N1 = length(x0_1);
N2 = length(x0_2);
N3 = length(x0_3);
N4 = length(x0_4);
N5 = length(x0_5);
N6 = length(x0_6);
N7 = length(x0_7);
N8 = length(x0_8);
N9 = length(x0_9);
N10 = length(x0_10);
length_all = N1*N2*N3*N4*N5*N6*N7*N8*N9*N10;
x_opt = NaN(length_all,10);
x0_opt = NaN(length_all,10);
fval_opt = NaN(length_all,1);
exitflag_opt = NaN(length_all,1);
AA = []; b = []; Aeq = []; beq = []; nonlcon = [];

tic
for i_1 = 1:N1
    for i_2 = 1:N2
        for i_3 = 1:N3
            for i_4 = 1:N4
                for i_5 = 1:N5
                    for i_6 = 1:N6
                        for i_7 = 1:N7
                            for i_8 = 1:N8
                                for i_9 = 1:N9
                                    for i_10 = 1:N10
                                        x0 = [x0_1(i_1), x0_2(i_2), x0_3(i_3), x0_4(i_4), x0_5(i_5), x0_6(i_6), x0_7(i_7), x0_8(i_8), x0_9(i_9), x0_10(i_10)];                                    
                                        [x,fval,exitflag] = fmincon(@param_obj_KLW,x0,AA,b,Aeq,beq,lb,ub,nonlcon,options);
                                        Index = (N2*N3*N4*N5*N6*N7*N8*N9*N10)*(i_1-1)+(N3*N4*N5*N6*N7*N8*N9*N10)*(i_2-1)+(N4*N5*N6*N7*N8*N9*N10)*(i_3-1)...
                                            +(N5*N6*N7*N8*N9*N10)*(i_4-1)+(N6*N7*N8*N9*N10)*(i_5-1)+(N7*N8*N9*N10)*(i_6-1)+(N8*N9*N10)*(i_7-1) + (N9*N10)*(i_8-1)...
                                            + N10*(i_9-1) + i_10;
                                        x0_opt(Index,:) = x0;
                                        x_opt(Index,:) = x;
                                        fval_opt(Index,:) = fval;
                                        exitflag_opt(Index,:) = exitflag;
                                        if exitflag > 0
                                            [i_1,i_2,i_3,i_4,i_5,i_6,i_7,i_8,i_9,i_10]
                                            [fval, x]
                                        end
                                        A = 500;
                                        if Index == A
                                            fval_1st = fval_opt(1:Index,:); x_opt_1st = x_opt(1:Index,:); x0_opt_1st = x0_opt(1:Index,:); exitflag_opt_1st = exitflag_opt(1:Index,:);
                                            save('C:/Users/LoweryR/Documents/My Dropbox/UND_research_Banking/EstBank/Est/param/GMM1st_051013_I_v28_1st.mat',...
                                                'fval_1st','x_opt_1st','x0_opt_1st','exitflag_opt_1st');
                                        elseif Index == 2*A 
                                            fval_2nd = fval_opt(1*A+1:Index,:); x_opt_2nd = x_opt(1*A+1:Index,:); x0_opt_2nd = x0_opt(1*A+1:Index,:); exitflag_opt_2nd = exitflag_opt(1*A+1:Index,:);
                                            save('C:/Users/LoweryR/Documents/My Dropbox/UND_research_Banking/EstBank/Est/param/GMM1st_051013_I_v28_2nd.mat',...
                                                'fval_2nd','x_opt_2nd','x0_opt_2nd','exitflag_opt_2nd');
                                        elseif Index == 3*A 
                                            fval_3rd = fval_opt(2*A+1:Index,:); x_opt_3rd = x_opt(2*A+1:Index,:); x0_opt_3rd = x0_opt(2*A+1:Index,:); exitflag_opt_3rd = exitflag_opt(2*A+1:Index,:);
                                            save('C:/Users/LoweryR/Documents/My Dropbox/UND_research_Banking/EstBank/Est/param/GMM1st_051013_I_v28_3rd.mat',...
                                                'fval_3rd','x_opt_3rd','x0_opt_3rd','exitflag_opt_3rd');
                                        elseif Index == length_all 
                                            fval_4th = fval_opt(3*A+1:Index,:); x_opt_4th = x_opt(3*A+1:Index,:); x0_opt_4th = x0_opt(3*A+1:Index,:); exitflag_opt_4th = exitflag_opt(3*A+1:Index,:);
                                            save('C:/Users/LoweryR/Documents/My Dropbox/UND_research_Banking/EstBank/Est/param/GMM1st_051013_I_v28_4th.mat',...
                                                'fval_4th','x_opt_4th','x0_opt_4th','exitflag_opt_4th');
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
    end
end
    toc
save('C:/Users/LoweryR/Documents/My Dropbox/UND_research_Banking/EstBank/Est/param/GMM1st_051013_I_v28.mat','x0_opt','x_opt','fval_opt','exitflag_opt');