clc;  clear;  close
load ("data/Parameters.mat",'m');

II = 21;
dis1 = 0.05;
beta_ini = 0; 

JJ = 3;
dis2 = 0.5;
B_ini = 0.5;

%% The equilibrium
% Case 1: competition + prohibit

m.DataPolicy = 'prohibit';  % prohibit free
m.MarketStruct = 'competition';  % competition oligopoly monopoly 
m.Privacy = 'nonePrivacy';  % nonePrivacy privacyExternality

for j = 1:JJ
    for i = 1:II
        m.beta_c = beta_ini + dis1*(i-1);
        m.beta_u = m.beta_c; 
        m.B_c = B_ini + dis2*(j-1);
        m.B_u = 1;
    
        tol = 1e-12;  diff = 1;  ww = 1;  cc = 0;
        ub = [];  lb = 0;  opt_tar = zeros(2,1);
        while diff > tol && cc < 100000
    
            % U.S. optimal tariffs on Chinese goods
            opt0 = opt_tar(1);
            m.t_cu = opt_tar(2);
        
            [opt_us, wel_us, ~] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
            m.t_uc = opt_us;
        
            opt0 = opt_tar(2);
            [opt_cn, wel_cn, ~] =  knitromatlab(@(opt)func_obj_t_cu(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
        
            opt_tar_new = [opt_us; opt_cn];
            diff = max(abs(opt_tar - opt_tar_new));
            cc = cc+1;
            % disp([diff,cc])     
            opt_tar = ww*opt_tar_new + (1-ww)*opt_tar;
        end
    
        m.t_cu = opt_cn;
        m.t_uc = opt_us;
        m.tau_cu = m.d_cu*(1+m.t_cu);
        m.tau_uc = m.d_uc*(1+m.t_uc);
    
        [wel1,Case11] = func_eqm_iter(m);   
        Case1.t_cu(i,j) = m.t_cu;
        Case1.t_uc(i,j) = m.t_uc;
        Case1.wel_c(i,j) = Case11.wel_c;
        Case1.wel_u(i,j) = Case11.wel_u;
    end
end

% Case 2: competition + free
m.DataPolicy = 'free';  % prohibit free
m.MarketStruct = 'competition';  % competition oligopoly monopoly 
m.Privacy = 'nonePrivacy';  % nonePrivacy privacyExternality

m.t_cu = 0;
m.t_uc = 0;
m.tau_cu = m.d_cu*(1+m.t_cu);
m.tau_uc = m.d_uc*(1+m.t_uc);
for j = 1:JJ
    for i = 1:II
        m.beta_c = beta_ini + dis1*(i-1);
        m.beta_u = m.beta_c; 
        m.B_c = B_ini + dis2*(j-1);
        m.B_u = 1;
    
        tol = 1e-12;  diff = 1;  ww = 1;  cc = 0;
        ub = [];  lb = 0;  opt_tar = zeros(2,1);
        while diff > tol && cc < 100000
    
            % U.S. optimal tariffs on Chinese goods
            opt0 = opt_tar(1);
            m.t_cu = opt_tar(2);
        
            [opt_us, wel_us, ~] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
            m.t_uc = opt_us;
        
            opt0 = opt_tar(2);
            [opt_cn, wel_cn, ~] =  knitromatlab(@(opt)func_obj_t_cu(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
        
            opt_tar_new = [opt_us; opt_cn];
            diff = max(abs(opt_tar - opt_tar_new));
            cc = cc+1;
            % disp([diff,cc])     
            opt_tar = ww*opt_tar_new + (1-ww)*opt_tar;
        end
    
        m.t_cu = opt_cn;
        m.t_uc = opt_us;
        m.tau_cu = m.d_cu*(1+m.t_cu);
        m.tau_uc = m.d_uc*(1+m.t_uc);
    
        [wel1,Case22] = func_eqm_iter(m);   
        Case2.t_cu(i,j) = m.t_cu;
        Case2.t_uc(i,j) = m.t_uc;
        Case2.wel_c(i,j) = Case22.wel_c;
        Case2.wel_u(i,j) = Case22.wel_u;
    end
end

beta = beta_ini:dis1:beta_ini + dis1*(i-1);
beta = beta';
B = B_ini:dis2:B_ini+ dis2*(j-1);
B = B';
save ("data\TariffNash_Tech.mat", "Case1", "Case2","beta","B","m")

