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

JJ = 21;
dis = 0.05;
beta_ini = 0; 

%% The equilibrium
% Case 1: competition + prohibit

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

for i = 1:JJ
    m.beta_c = beta_ini + dis*(i-1);
    m.beta_u = m.beta_c; 

    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,1) = m.t_cu;
    Case1.t_uc(i,1) = m.t_uc;
    Case1.wel_c(i,1) = Case11.wel_c;
    Case1.wel_u(i,1) = Case11.wel_u;
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 i = 1:JJ
    m.beta_c = beta_ini + dis*(i-1);
    m.beta_u = m.beta_c; 

    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,1) = m.t_cu;
    Case2.t_uc(i,1) = m.t_uc;
    Case2.wel_c(i,1) = Case22.wel_c;
    Case2.wel_u(i,1) = Case22.wel_u;
end

% Case 3: monopoly + prohibit
m.DataPolicy = 'prohibit';  % prohibit free
m.MarketStruct = 'monopoly';  % 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 i = 1:JJ
    m.beta_c = beta_ini + dis*(i-1);
    m.beta_u = m.beta_c; 

    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);

    [wel3,Case33] = func_eqm_iter(m);   
    Case3.t_cu(i,1) = m.t_cu;
    Case3.t_uc(i,1) = m.t_uc;
    Case3.wel_c(i,1) = Case33.wel_c;
    Case3.wel_u(i,1) = Case33.wel_u;
end


% Case 4: monopoly + free
m.DataPolicy = 'free';  % prohibit free
m.MarketStruct = 'monopoly';  % 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 i = 1:JJ
    m.beta_c = beta_ini + dis*(i-1);
    m.beta_u = m.beta_c; 

    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);

    [wel4,Case44] = func_eqm_iter(m);   
    Case4.t_cu(i,1) = m.t_cu;
    Case4.t_uc(i,1) = m.t_uc;
    Case4.wel_c(i,1) = Case44.wel_c;
    Case4.wel_u(i,1) = Case44.wel_u;
end

beta = beta_ini:dis:beta_ini + dis*(i-1);
beta = beta';

save ("data\TariffNash.mat", "Case1", "Case2", "Case3", "Case4","beta","m")
