clear all
close all
clc
set(0,'defaultTextInterpreter','latex');


%% FILE DESCRIPTION

% This file is used to classify a population of road transport missions
% according to the GTA and UFD systems.

% [1] L Romano, P Johannesson, E Nordström, F Bruzelius, R Andersson, and B
% Jacobson. "A classification method of road transport missions and
% applications using the operating cycle format". IEEE Access 10, (2022).
% Available from: https://doi.org/10.1109/ACCESS.2022.3188872.

% [2] L Romano, M Godio, P Johannesson, F Bruzelius, T Ghandriz, and B
% Jacobson. "Development of an operating cycle for long-haul heavy-duty
% trucks: theory, analysis and applications". Submitted for publication.

% Please, cite the above references when using this script for purposes of
% academic or industrial research


%% LOAD SIMPLE VARIABLES

sOC = load('C:\Users\luigir\Desktop\sOC for TRD\distributionsGenerated_SOC.mat');
sOC = sOC.sOC;

P_y_max2 = sOC.P_y_max2;
P_y_max3 = sOC.P_y_max3;
P_y_max6 = sOC.P_y_max6;
P_y_max9 = sOC.P_y_max9;
P_kappa = sOC.P_kappa;
Lh = sOC.L_h;
NC = sOC.N_C*10;
Ns = sOC.Lambda_s*100;
mean_N_s = mean(sOC.Lambda_s);
meanSpeed = sOC.V_hat;
Distance = sOC.Ltot;
UFD_dist = [];
UFD_topo = [];
UFD_speed = [];
GTA_speed = [];

mean_P_y_max2 = mean(P_y_max2);
mean_P_y_max3 = mean(P_y_max3);
mean_P_y_max6 = mean(P_y_max6);
mean_P_y_max9 = mean(P_y_max9);
mean_L_h = mean(Lh);
mean_P_kappa = mean(P_kappa)
mean_NC = mean(NC);
mean_meanSpeed = mean(meanSpeed);


%% COMPUTE PMFs

for i=1:length(Distance)
    if Distance(i) < 0.5
        UFD_dist(i) = 1;
    elseif Distance(i) <= 5
        UFD_dist(i) = 2;
    elseif Distance(i) <= 50
        UFD_dist(i) = 3;
    else
        UFD_dist(i) = 4;
    end
end
GTA_dist = UFD_dist;

for i=1:length(P_y_max2)
    if P_y_max2(i) > 0.8
        UFD_topo(i) = 1;
    elseif P_y_max2(i) > 0.6
        UFD_topo(i) = 3;
    else
        UFD_topo(i) = 4;
    end
end

for i=1:length(P_y_max3)
    if P_y_max3(i) >= 0.98
        GTA_topo(i) = 1;
    elseif P_y_max6(i) >= 0.98
        GTA_topo(i) = 2;
    elseif P_y_max9(i) > 0.98
        GTA_topo(i) = 3;
    else
        GTA_topo(i) = 4;
    end
end

for i=1:length(meanSpeed)
    if meanSpeed(i) <= 50
        GTA_speed(i) = 1;
    elseif meanSpeed(i) <= 80
        GTA_speed(i) = 2;
    else
        GTA_speed(i) = 3;
    end
end

for i=1:length(meanSpeed)
    if meanSpeed(i) <= 50
        UFD_speed(i) = 1;
    elseif meanSpeed(i) <= 70
        UFD_speed(i) = 2;
    elseif meanSpeed(i) <= 90
        UFD_speed(i) = 3;
    else
        UFD_speed(i) = 4;
    end
end

for i=1:length(P_kappa)
    if P_kappa(i) >= 0.8
        GTA_curv(i) = 1;
    else
        GTA_curv(i) = 2;
    end
end

for i=1:length(NC)
    if NC(i) <= 2
        UFD_curv(i) = 1;
    else
        UFD_curv(i) = 2;
    end
end


UFD_topoDist = [UFD_topo', UFD_dist'];
GTA_topoDist = [GTA_topo', GTA_dist'];

UFD_speedDist = [UFD_speed', UFD_dist'];
GTA_speedDist = [GTA_speed', GTA_dist'];

UFD_curvDist = [UFD_curv', UFD_dist'];
GTA_curvDist = [GTA_curv', GTA_dist'];


%% FIGURES

figure(1)
subplot(1,2,1)
hist3(GTA_topoDist,'Ctrs',{1:1:4 1:1:4}, 'FaceColor', [0.00,0.45,0.74])
set(gca,'TickLabelInterpreter','latex')
box on
xlabel('Road grade')
ylabel('Mission length')
axis tight square
axis([-inf inf -inf inf 0 5000])
xticks([1 2 3 4])
xticklabels({'\texttt{FLAT}','\texttt{P-FLAT}', ....
    '\texttt{HILLY}', '\texttt{V-HILLY}' })
yticks([1 2 3 4])
yticklabels({'\texttt{STOP\&GO}','\texttt{LOCAL}', ....
    '\texttt{REGIONAL}', '\texttt{L-DISTANCE}' })
title('\textbf{GTA}')
subplot(1,2,2)
hist3(UFD_topoDist,'Ctrs',{1:1:4 1:1:4}, 'FaceColor', [0.85,0.33,0.10])
set(gca,'TickLabelInterpreter','latex')
box on
% axis([0 2 0 inf])
xlabel('Road grade')
ylabel('Mission length')
axis tight square
axis([-inf inf -inf inf 0 5000])
xticks([1 2 3 4])
xticklabels({'\texttt{FLAT}','\texttt{P-FLAT}', ....
    '\texttt{HILLY}', '\texttt{V-HILLY}' })
yticks([1 2 3 4])
yticklabels({'\texttt{STOP\&GO}','\texttt{LOCAL}', ....
    '\texttt{REGIONAL}', '\texttt{L-DISTANCE}' })
title('\textbf{UFD}')

N_GTA_topo = hist3(GTA_topoDist,'Ctrs',{1:1:4 1:1:4});
N_UFD_topo = hist3(UFD_topoDist,'Ctrs',{1:1:4 1:1:4});

figure(2)
subplot(1,2,1)
hist3(GTA_speedDist,'Ctrs',{1:1:4 1:1:4}, 'FaceColor', [0.00,0.45,0.74])
set(gca,'TickLabelInterpreter','latex')
box on
xlabel('Legal speed')
ylabel('Mission length')
axis tight square
axis([-inf inf -inf inf 0 5000])
xticks([1 2 3 4])
xticklabels({'\texttt{LOW}','\texttt{MODERATE}', ....
    '\texttt{HIGH}', '\texttt{V-HIGH}' })
yticks([1 2 3 4])
yticklabels({'\texttt{STOP\&GO}','\texttt{LOCAL}', ....
    '\texttt{REGIONAL}', '\texttt{L-DISTANCE}' })
title('\textbf{GTA}')
subplot(1,2,2)
hist3(UFD_speedDist,'Ctrs',{1:1:4 1:1:4}, 'FaceColor', [0.85,0.33,0.10])
set(gca,'TickLabelInterpreter','latex')
box on
% axis([0 2 0 inf])
xlabel('Legal speed')
ylabel('Mission length')
axis tight square
axis([-inf inf -inf inf 0 5000])
xticks([1 2 3 4])
xticklabels({'\texttt{LOW}','\texttt{MODERATE}', ....
    '\texttt{HIGH}', '\texttt{V-HIGH}' })
yticks([1 2 3 4])
yticklabels({'\texttt{STOP\&GO}','\texttt{LOCAL}', ....
    '\texttt{REGIONAL}', '\texttt{L-DISTANCE}' })
title('\textbf{UFD}')

N_GTA_speed = hist3(GTA_speedDist,'Ctrs',{1:1:4 1:1:4});
N_UFD_speed = hist3(UFD_speedDist,'Ctrs',{1:1:4 1:1:4});

figure(3)
subplot(1,2,1)
hist3(GTA_curvDist,'Ctrs',{1:1:2 1:1:4}, 'FaceColor', [0.00,0.45,0.74])
set(gca,'TickLabelInterpreter','latex')
box on
xlabel('Curviness')
ylabel('Mission length')
axis tight square
axis([-inf inf -inf inf 0 5000])
xticks([1 2])
xticklabels({'\texttt{LOW}','\texttt{HIGH}'})
yticks([1 2 3 4])
yticklabels({'\texttt{STOP\&GO}','\texttt{LOCAL}', ....
    '\texttt{REGIONAL}', '\texttt{L-DISTANCE}' })
title('\textbf{GTA}')
subplot(1,2,2)
hist3(UFD_curvDist,'Ctrs',{1:1:2 1:1:4}, 'FaceColor', [0.85,0.33,0.10])
set(gca,'TickLabelInterpreter','latex')
box on
xlabel('Curviness')
ylabel('Mission length')
axis tight square
axis([-inf inf -inf inf 0 5000])
xticks([1 2])
xticklabels({'\texttt{LOW}','\texttt{HIGH}' })
yticks([1 2 3 4])
yticklabels({'\texttt{STOP\&GO}','\texttt{LOCAL}', ....
    '\texttt{REGIONAL}', '\texttt{L-DISTANCE}' })
title('\textbf{UFD}')

N_GTA_curv = hist3(GTA_curvDist,'Ctrs',{1:1:2 1:1:4});
N_UFD_curv = hist3(UFD_curvDist,'Ctrs',{1:1:2 1:1:4});


























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































