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

JJ = 21;
dis = 0.037;
mu_ini = 1.25; 

%% 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.mu_c = mu_ini + dis*(i-1);
    m.mu_u = m.mu_c; 

    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,1) = m.t_cu;
    Case1_cu.wel_c(i,1) = Case11.wel_c;
    Case1_cu.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

for i = 1:JJ
    m.mu_c = mu_ini + dis*(i-1);
    m.mu_u = m.mu_c; 

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

for i = 1:JJ  
    m.mu_c = mu_ini + dis*(i-1);
    m.mu_u = m.mu_c; 

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

    [wel3,Case33] = func_eqm_iter(m);   
    Case3_cu.t_cu(i,1) = m.t_cu;
    Case3_cu.wel_c(i,1) = Case33.wel_c;
    Case3_cu.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

for i = 1:JJ
    m.mu_c = mu_ini + dis*(i-1);
    m.mu_u = m.mu_c; 

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

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

mu = mu_ini:dis:mu_ini + dis*(i-1);
mu = mu';

save ("data\TariffUnilateral_CU.mat", "Case1_cu", "Case2_cu", "Case3_cu", "Case4_cu","mu","m")

