
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

ub = [];   lb = 0;   opt0 = 0;
[opt_Case1, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
Case1.t_uc_opt = opt_Case1;         % optimal tariff and welfare
m.t_uc = opt_Case1;
m.tau_uc = m.d_uc*(1+m.t_uc);
[wel1,Case11] = func_eqm_iter(m);
Case1.wel_c_opt = Case11.wel_c;
Case1.wel_u_opt = Case11.wel_u;


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

ub = [];   lb = 0;   opt0 = 0;
[opt_Case2, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
Case2.t_uc_opt = opt_Case2;         % optimal tariff and welfare
m.t_uc = opt_Case2;
m.tau_uc = m.d_uc*(1+m.t_uc);
[wel2,Case22] = func_eqm_iter(m);
Case2.wel_c_opt = Case22.wel_c;
Case2.wel_u_opt = Case22.wel_u;


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

ub = [];   lb = 0;   opt0 = 0;
[opt_Case3, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
Case3.t_uc_opt = opt_Case3;         % optimal tariff and welfare
m.t_uc = opt_Case3;
m.tau_uc = m.d_uc*(1+m.t_uc);
[wel3,Case33] = func_eqm_iter(m);
Case3.wel_c_opt = Case33.wel_c;
Case3.wel_u_opt = Case33.wel_u;


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

ub = [];   lb = 0;   opt0 = 0;
[opt_Case4, fval, exitflag] =  knitromatlab(@(opt)func_obj_t_uc(opt,m), opt0, [], [], [], [], lb, ub, [], [], [], 'ktropt_singlestart.opt');
Case4.t_uc_opt = opt_Case4;         % optimal tariff and welfare
m.t_uc = opt_Case4;
m.tau_uc = m.d_uc*(1+m.t_uc);
[wel4,Case44] = func_eqm_iter(m);
Case4.wel_c_opt = Case44.wel_c;
Case4.wel_u_opt = Case44.wel_u;
toc

save("data/Counterfactual_TariffUnilateral_UC.mat",'Case1','Case2','Case3','Case4');
