
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
tic
m.DataPolicy = 'prohibit';  % prohibit free
m.MarketStruct = 'competition';  % competition oligopoly monopoly 
m.Privacy = 'nonePrivacy';  % nonePrivacy privacyExternality

m.t_cu = 0;       % 国家U单边最优关税
m.tau_cu = m.d_cu*(1+m.t_cu);

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;
        ub = [];   lb = 0;   opt0 = 0;
        [opt_Case_uc, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');      
        m.t_uc = opt_Case_uc;
        m.tau_uc = m.d_uc*(1+m.t_uc);
    
        [wel1,Case11] = func_eqm_iter(m);   
        Case1_uc.t_uc(i,j) = m.t_uc;
        Case1_uc.wel_u(i,j) = Case11.wel_u;
    end
end


m.t_uc = 0;       % 国家C单边最优关税
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;
        ub = [];   lb = 0;   opt0 = 0;
        [opt_Case_cu, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_cu(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');      
        m.t_cu = opt_Case_cu;
        m.tau_cu = m.d_cu*(1+m.t_cu);
    
        [wel1,Case11] = func_eqm_iter(m);   
        Case1_cu.t_cu(i,j) = m.t_cu;
        Case1_cu.wel_c(i,j) = Case11.wel_c;
    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;       % 国家U单边最优关税
m.tau_cu = m.d_cu*(1+m.t_cu);

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;
        ub = [];   lb = 0;   opt0 = 0;
        [opt_Case_uc, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');      
        m.t_uc = opt_Case_uc;
        m.tau_uc = m.d_uc*(1+m.t_uc);
    
        [wel2,Case22] = func_eqm_iter(m);   
        Case2_uc.t_uc(i,j) = m.t_uc;
        Case2_uc.wel_u(i,j) = Case22.wel_u;
    end
end

m.t_uc = 0;       % 国家C单边最优关税
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;
        ub = [];   lb = 0;   opt0 = 0;
        [opt_Case_cu, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_cu(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');      
        m.t_cu = opt_Case_cu;
        m.tau_cu = m.d_cu*(1+m.t_cu);
    
        [wel2,Case22] = func_eqm_iter(m);   
        Case2_cu.t_cu(i,j) = m.t_cu;
        Case2_cu.wel_c(i,j) = Case22.wel_c;
    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\TariffUnilateral_Tech.mat", "Case1_cu", "Case2_cu", "Case1_uc", "Case2_uc","beta","B","m")

