function Avg_Cost = PFA_a_cost(S0,sample_path,P_high,P_low)

% number of scenario & time horizon
n_scenario = size(sample_path.D,1);
time = size(sample_path.D,2);

% Initial state
S = S0.S;
rate_d = S0.rate_d*ones(n_scenario,1);
rate_c = S0.rate_c*ones(n_scenario,1);
R_max = S0.R_max;
    %initial states
    R = S.R*ones(n_scenario,1);
    D = S.D*ones(n_scenario,1);
    E = S.E*ones(n_scenario,1);
    P = S.P*ones(n_scenario,1);

%% The simple policy based on specific parameters for low and high prices.

u.WD = [];
u.GD = [];
u.RD = [];
u.WR = [];
u.GR = [];
x.R = [];
x.D = [];
x.E = [];
x.P = [];
x.C = [];
for t = 1:time
    
    %policy => control variable
    WD = min(D,E);
    comp_h = P>P_high;
    RD = comp_h.*min(min(D-WD,rate_d),R)*S0.eff_d;
    GD = D-WD-RD;
    WR = min(min(E-WD,rate_c),R_max-R);
    comp_l = P<P_low;
    GR = comp_l.*min(rate_c-WR,R_max-R-WR);
    C = P.*(GD+GR);
    
    %Data storage
    u.WD = [u.WD, WD];
    u.GD = [u.GD, GD];
    u.RD = [u.RD, RD];
    u.WR = [u.WR, WR];
    u.GR = [u.GR, GR];
    x.R = [x.R, R];
    x.D = [x.D, D];
    x.E = [x.E, E];
    x.P = [x.P, P];
    x.C = [x.C, C];

    %update states
    R = R+S0.eff_c*(WR+GR)-RD;
    D = sample_path.D(1:n_scenario,t);
    E = sample_path.E(1,t)*ones(n_scenario,1);
    P = sample_path.P(1:n_scenario,t);
end
% Cost
Cost = sum(x.P.*(u.GD+u.GR),2);
for k = 1:n_scenario
    Avg_Cost_cum(k) = mean(Cost(1:k));
end
Avg_Cost = mean(Cost);

end

