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

JJ = 41;
dis = 0.025;

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

for i = 1:JJ
    m.B_c = 0.5 + dis*(i-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,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.B_c = 0.5 + dis*(i-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,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

B_c = 0.5:dis:0.5+dis*(JJ-1);
B_c = B_c';

%% Figure：算力和福利
warning('off','all')

set(0, 'defaulttextinterpreter','latex')
set(0, 'defaultAxesTickLabelInterpreter','latex'); 
set(0, 'defaultLegendInterpreter','latex');
set(groot,'defaultLineLineWidth',2); 

TightPlot.ColumeNumber = 2; 
TightPlot.RowNumber = 2;
TightPlot.GapW = 0.1;
TightPlot.GapH = 0.15;
TightPlot.MarginsLower = 0.15;
TightPlot.MarginsUpper = 0.1;
TightPlot.MarginsLeft = 0.1;
TightPlot.MarginsRight = 0.1;

picture1 = figure('color',[1 1 1]);
p = tight_subplot(TightPlot.ColumeNumber,TightPlot.RowNumber,...
    [TightPlot.GapH TightPlot.GapW],...
    [TightPlot.MarginsLower TightPlot.MarginsUpper],...
    [TightPlot.MarginsLeft TightPlot.MarginsRight]); 

axes(p(1));
line(B_c,Case1.t_cu, 'Color', 'k', 'LineStyle', '-', 'LineWidth', 1.25);
hold on;
scatter(B_c, Case1.t_uc, 6, '^', 'k');
hold off;
set(gca, 'XTickLabelMode', 'auto');
set(gca,'yTickLabel',num2str(get(gca,'yTick')','%.2f'));
xlabel('$ B_{C} $','Interpreter','latex', 'FontSize',10);
ylabel('纳什关税','interpreter','latex', 'FontSize',10);
title(p(1),'（a）竞争/禁止流动','FontSize',10)

axes(p(2));
line(B_c,Case2.t_cu, 'Color', 'k', 'LineStyle', '-', 'LineWidth', 1.25);
hold on;
scatter(B_c, Case2.t_uc, 6, '^', 'k');
hold off;
set(gca, 'XTickLabelMode', 'auto');
set(gca,'yTickLabel',num2str(get(gca,'yTick')','%.2f'));
xlabel('$ B_{C} $','Interpreter','latex', 'FontSize',10);
ylabel('纳什关税','interpreter','latex', 'FontSize',10);
title(p(2),'（b）竞争/自由流动','FontSize',10)

axes(p(3));
line(B_c,Case1.wel_c, 'Color', 'k', 'LineStyle', '-', 'LineWidth', 1.25);
hold on;
scatter(B_c, Case1.wel_u, 6, '^', 'k');
hold off;
set(gca, 'XTickLabelMode', 'auto');
set(gca,'yTickLabel',num2str(get(gca,'yTick')','%.2f'));
xlabel('$ B_{C} $','Interpreter','latex', 'FontSize',10);
ylabel('福利','interpreter','latex', 'FontSize',10);
title(p(3),'（c）竞争/禁止流动','FontSize',10)

axes(p(4));
line(B_c,Case2.wel_c, 'Color', 'k', 'LineStyle', '-', 'LineWidth', 1.25);
hold on;
scatter(B_c, Case2.wel_u, 6, '^', 'k');
hold off;
set(gca, 'XTickLabelMode', 'auto');
set(gca,'yTickLabel',num2str(get(gca,'yTick')','%.2f'));
xlabel('$ B_{C} $','Interpreter','latex', 'FontSize',10);
ylabel('福利','interpreter','latex', 'FontSize',10);
title(p(4),'（d）竞争/自由流动','FontSize',10)

handle = legend({'国家 C', '国家 U','最优关税'});
set(handle,'Position', [0.45 0.0000001 0.1 0.05],'Orientation','horizon','Interpreter','latex','FontSize',8)

fprintf('\n=================================================================\n');
fprintf('         图III9 国家𝐶不同算力水平下的纳什关税和相应福利（𝜌 = 0.9）\n');
fprintf('=================================================================\n\n');
print(picture1,'result/图III9.png','-r600','-djpeg');

