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

%% The equilibrium: nash tariff and welfare
% Case 1: competition + prohibit
tic
m.DataPolicy = 'prohibit';  % prohibit free
m.MarketStruct = 'competition';  % competition oligopoly monopoly 
m.Privacy = 'nonePrivacy';  % nonePrivacy privacyExternality

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_uc = opt_us;
m.t_cu = opt_cn;
m.tau_uc = m.d_uc*(1+m.t_uc);
m.tau_cu = m.d_cu*(1+m.t_cu);
[wel1,Case1] = func_eqm_iter(m);
Case1.t_uc_opt = opt_us; 
Case1.t_cu_opt = opt_cn;  


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

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_uc = opt_us;
m.t_cu = opt_cn;
m.tau_uc = m.d_uc*(1+m.t_uc);
m.tau_cu = m.d_cu*(1+m.t_cu);
[wel2,Case2] = func_eqm_iter(m);
Case2.t_uc_opt = opt_us; 
Case2.t_cu_opt = opt_cn;  


% Case 3: monopoly + prohibit
m.DataPolicy = 'prohibit';  % prohibit free
m.MarketStruct = 'competition';  % competition oligopoly monopoly 
m.Privacy = 'nonePrivacy';  % nonePrivacy privacyExternality
m.B_c = 0.00001;            % 此情形类似于垄断模式。极端情形下无法求解最优时的次优选择。

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_uc = opt_us;
m.t_cu = opt_cn;
m.tau_uc = m.d_uc*(1+m.t_uc);
m.tau_cu = m.d_cu*(1+m.t_cu);
[wel3,Case3] = func_eqm_iter(m);
Case3.t_uc_opt = opt_us; 
Case3.t_cu_opt = opt_cn;  


% Case 4: monopoly + free
m.DataPolicy = 'free';  % prohibit free
m.MarketStruct = 'monopoly';  % competition oligopoly monopoly 
m.Privacy = 'nonePrivacy';  % nonePrivacy privacyExternality
m.B_c = 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_uc = opt_us;
m.t_cu = opt_cn;
m.tau_uc = m.d_uc*(1+m.t_uc);
m.tau_cu = m.d_cu*(1+m.t_cu);
[wel4,Case4] = func_eqm_iter(m);
Case4.t_uc_opt = opt_us; 
Case4.t_cu_opt = opt_cn;  
toc


%% Table 2
nash = [Case1.t_cu_opt; Case1.t_uc_opt; ...
        Case2.t_cu_opt; Case2.t_uc_opt; ...
        Case3.t_cu_opt; Case3.t_uc_opt; ...
        Case4.t_cu_opt; Case4.t_uc_opt];
wel = [Case1.wel_c; Case1.wel_u; ...
       Case2.wel_c; Case2.wel_u; ...
       Case3.wel_c; Case3.wel_u; ...
       Case4.wel_c; Case4.wel_u];
Pi = [Case1.Pi_c; Case1.Pi_u; ...
      Case2.Pi_c; Case2.Pi_u; ...
      Case3.Pi_c; Case3.Pi_u; ...
      Case4.Pi_c; Case4.Pi_u];
M = [Case1.M_c; Case1.M_u; ...
     Case2.M_c; Case2.M_u; ...
     Case3.M_c; Case3.M_u; ...
     Case4.M_c; Case4.M_u];
R = [Case1.R_c; Case1.R_u; ...
     Case2.R_c; Case2.R_u; ...
     Case3.R_c; Case3.R_u; ...
     Case4.R_c; Case4.R_u];
w = [Case1.w_c; Case1.w_u; ...
     Case2.w_c; Case2.w_u; ...
     Case3.w_c; Case3.w_u; ...
     Case4.w_c; Case4.w_u];
P = [Case1.P_c; Case1.P_u; ...
     Case2.P_c; Case2.P_u; ...
     Case3.P_c; Case3.P_u; ...
     Case4.P_c; Case4.P_u];

result = reshape([round(nash,2),round(wel,2),round(Pi,2),round(M,2),round(R,2),round(w,2),round(P,2)],[8,7]);
Table1 = array2table(result, 'VariableNames', {'纳什关税','福利', '商品利润', '数据利润', '关税','工资', '价格指数'});
writetable(Table1, 'result/表IV2.xlsx', 'WriteVariableNames', true);

fprintf('\n=================================================================\n');
fprintf('         表IV2 不同数据中间商市场结构以及不同数据政策下的纳什关税与福利\n');
fprintf('=================================================================\n\n');
disp(Table1);


























