clear all
clc
close all

load('P00_woTES.mat')
A00_woTES = Accumu_mean;
AC00_woTES = operation_cost;
load('P01_simple.mat')
A01_PFA = Avg_Cost_cum;
AC01_PFA = Cost;
load('P02_MPC.mat')
A02_stMPC = Avg_cost_cum;
AC02_stMPC = Cost2;
load('P03_CFA_R07_result.mat')
A03_CFA_g = Avg_cost_cum;
AC03_CFA = Cost2;
load('P04_D_scn.mat')
A04_scnMPC_D = Avg_cost_cum;
AC04_smpc_d = Cost2;
load('P04_P_scn.mat')
A04_scnMPC_P = Avg_cost_cum;
AC04_smpc_p = Cost2;
load('P05_Opt.mat') 
A05_opt = Avg_cost_cum;
AC05_opt = T_Cost




%%
mx = max(max([A00_woTES; A01_PFA; A02_stMPC; A03_CFA_g; A04_scnMPC_D; A04_scnMPC_P; A05_opt]))
mn = min(min([A00_woTES; A01_PFA; A02_stMPC; A03_CFA_g; A04_scnMPC_D; A04_scnMPC_P; A05_opt]))

figure()


subplot(1,7,1)
plot(A00_woTES)
hold on
plot(ones(1,length(A00_woTES))*A00_woTES(500),'--')
xlabel('Simulation number')
ylabel('Average cost [NOK]')
title('W/O TES')
ylim([10^4 8*10^5 ])


subplot(1,7,2)
plot(A01_PFA)
hold on
plot(ones(1,length(A01_PFA))*A01_PFA(500),'--')
xlabel('Simulation number')
title('PFA')
ylim([10^4 8*10^5 ])


subplot(1,7,3)
plot(A02_stMPC)
hold on
plot(ones(1,length(A02_stMPC))*A02_stMPC(500),'--')
xlabel('Simulation number')
title('St.MPC')
ylim([10^4 8*10^5 ])

subplot(1,7,4)
plot(A03_CFA_g)
hold on
plot(ones(1,length(A03_CFA_g))*A03_CFA_g(500),'--')
xlabel('Simulation number')
title('CFA')
ylim([10^4 8*10^5 ])

subplot(1,7,5)
plot(A04_scnMPC_D)
hold on
plot(ones(1,length(A04_scnMPC_D))*A04_scnMPC_D(500),'--')
xlabel('Simulation number')
title('Scn.MPC on $Q^{D}$')
ylim([10^4 8*10^5 ])


subplot(1,7,6)
plot(A04_scnMPC_P)
hold on
plot(ones(1,length(A04_scnMPC_P))*A04_scnMPC_P(500),'--')
xlabel('Simulation number')
title('Scn.MPC on $p$')
ylim([10^4 8*10^5 ])

subplot(1,7,7)
plot(A05_opt)
hold on
plot(ones(1,length(A05_opt))*A05_opt(500),'--')
xlabel('Simulation number')
title('Optimum')
ylim([10^4 8*10^5 ])
xlabel('Simulation number')

%% in one plot
figure()



plot(A00_woTES)
hold on
%plot(ones(1,length(A00_woTES))*A00_woTES(500),'--')
%xlabel('Simulation number')

%title('W/O TES')

hold on
plot(A01_PFA)
hold on
%plot(ones(1,length(A01_PFA))*A01_PFA(500),'--')
%xlabel('Simulation number')
%title('PFA')

hold on
plot(A02_stMPC)
hold on
%plot(ones(1,length(A02_stMPC))*A02_stMPC(500),'--')
%xlabel('Simulation number')
%title('St.MPC')

hold on
plot(A03_CFA_g)
hold on
%plot(ones(1,length(A03_CFA_g))*A03_CFA_g(500),'--')
%xlabel('Simulation number')
%title('CFA')

hold on
plot(A04_scnMPC_D)
hold on
%plot(ones(1,length(A04_scnMPC_D))*A04_scnMPC_D(500),'--')
%xlabel('Simulation number')
%title('Scn.MPC on $Q^{D}$')

hold on
plot(A04_scnMPC_P)
hold on
%plot(ones(1,length(A04_scnMPC_P))*A04_scnMPC_P(500),'--')
%xlabel('Simulation number')
%title('Scn.MPC on $p$')

hold on
plot(A05_opt)
hold on
%plot(ones(1,length(A05_opt))*A05_opt(500),'--')
%xlabel('Simulation number')
%title('Optimum')
ylim([4*10^5 9*10^5 ])
xlabel('Simulation number','FontSize',15)
ylabel('Average cost [NOK]','FontSize',15)
legend('System without TES','Simple Policy','Certainty equivalence MPC','Parmetrically modified MPC', 'Scenario-based MPC on price','Scenario-based MPC on demand','Optimum')
set(gca,'FontSize',15)
%% calculation

% Relative_Cost1.P00_WO_TES = (A00_woTES(500)-A05_opt(500))/A05_opt(500);
% Relative_Cost1.P01_PFA =(A01_PFA(500)-A05_opt(500))/A05_opt(500);
% Relative_Cost1.P02_MPC =(A02_stMPC(500)-A05_opt(500))/A05_opt(500);
% Relative_Cost1.P03_CFA =(A03_CFA_g(500)-A05_opt(500))/A05_opt(500);
% Relative_Cost1.P04_Pscn =(A04_scnMPC_P(500)-A05_opt(500))/A05_opt(500);
% Relative_Cost1.P04_Dscn = (A04_scnMPC_D(500)-A05_opt(500))/A05_opt(500);
% Relative_Cost1.P05_Opt = (A05_opt(500)-A05_opt(500))/A05_opt(500)

Relative_Cost2.P00_WO_TES = A00_woTES(500)/A05_opt(500);
Relative_Cost2.P01_PFA =A01_PFA(500)/A05_opt(500);
Relative_Cost2.P02_MPC =A02_stMPC(500)/A05_opt(500);
Relative_Cost2.P03_CFA =A03_CFA_g(500)/A05_opt(500);
Relative_Cost2.P04_Pscn =A04_scnMPC_P(500)/A05_opt(500);
Relative_Cost2.P04_Dscn = A04_scnMPC_D(500)/A05_opt(500);
Relative_Cost2.P05_Opt = A05_opt(500)/A05_opt(500)

%%
PPP.cost00 = A00_woTES(500);
PPP.cost01 = A01_PFA(500);
PPP.cost02 = A02_stMPC(500);
PPP.cost03 = A03_CFA_g(500);
PPP.cost04_p = A04_scnMPC_P(500);
PPP.cost04_d = A04_scnMPC_D(500);
PPP.cost05 = A05_opt(500)
%% each simulation

min_c = min(min([AC00_woTES; AC01_PFA; AC02_stMPC; AC03_CFA; AC04_smpc_d; AC04_smpc_p; AC05_opt]))
max_c = max(max([AC00_woTES; AC01_PFA; AC02_stMPC; AC03_CFA; AC04_smpc_d; AC04_smpc_p; AC05_opt]))

figure()
stairs(AC00_woTES,'--','color', 'black')
hold on
stairs(AC01_PFA,'--o')
hold on
stairs(AC02_stMPC)
hold on
stairs(AC03_CFA)
hold on
stairs(AC04_smpc_d)
hold on
stairs(AC04_smpc_p)
hold on
stairs(AC05_opt,':')
xlim([0 100])
xlabel('Simulation number')
ylabel('Operation cost [NOK]')
legend('W/O TES', 'PFA', 'St.MPC', 'CFA', 'Scn.MPC(D)', 'Scn.MPC(P)', 'Opt')

%% each simulation

min_c = min(min([AC00_woTES; AC01_PFA; AC02_stMPC; AC03_CFA; AC04_smpc_d; AC04_smpc_p; AC05_opt]))
max_c = max(max([AC00_woTES; AC01_PFA; AC02_stMPC; AC03_CFA; AC04_smpc_d; AC04_smpc_p; AC05_opt]))
Font = 15
Line = 1.1
figure()
stairs(AC00_woTES,'--','color', 'black','LineWidth',Line)
hold on
stairs(AC01_PFA,'--o','LineWidth',Line)
hold on
stairs(AC02_stMPC,'LineWidth',Line)
hold on
stairs(AC03_CFA,'LineWidth',Line)
hold on
stairs(AC04_smpc_d,'LineWidth',Line)
hold on
stairs(AC04_smpc_p,'LineWidth',Line)
hold on
stairs(AC05_opt,':','LineWidth',Line)
xlim([1 10])
xlabel('Simulation number','FontSize',Font)
ylabel('Operation cost [NOK]','FontSize',Font)
legend('W/O TES', 'PFA', 'St.MPC', 'CFA', 'Scn.MPC(D)', 'Scn.MPC(P)', 'Opt')
set(gca,'FontSize',Font)

%% each simulation

min_c = min(min([AC00_woTES; AC01_PFA; AC02_stMPC; AC03_CFA; AC04_smpc_d; AC04_smpc_p; AC05_opt]))
max_c = max(max([AC00_woTES; AC01_PFA; AC02_stMPC; AC03_CFA; AC04_smpc_d; AC04_smpc_p; AC05_opt]))
Font = 15
Line = 1.1
figure()
plot(AC00_woTES,'--','color', 'black','LineWidth',Line)
hold on
plot(AC01_PFA,'--o','LineWidth',Line)
hold on
plot(AC02_stMPC,'LineWidth',Line)
hold on
plot(AC03_CFA,'LineWidth',Line)
hold on
plot(AC04_smpc_d,'LineWidth',Line)
hold on
plot(AC04_smpc_p,'LineWidth',Line)
hold on
plot(AC05_opt,':','LineWidth',Line)
xlim([0 10])
xlabel('Simulation number','FontSize',Font)
ylabel('Operation cost [NOK]','FontSize',Font)
legend('W/O TES', 'PFA', 'St.MPC', 'CFA', 'Scn.MPC(D)', 'Scn.MPC(P)', 'Opt')
set(gca,'FontSize',Font)

%% test
C10 = AC01_PFA < AC00_woTES;
C10s = sum(C10)
C12 = AC01_PFA < AC02_stMPC;
C12s = sum(C12)
C13 = AC01_PFA < AC03_CFA;
C13s = sum(C13)
C14d = AC01_PFA < AC04_smpc_d;
C14ds = sum(C14d)
C14p = AC01_PFA < AC04_smpc_p;
C14ps = sum(C14p)
C15 = AC01_PFA < AC05_opt;
C15s = sum(C15)

