clear all;
clc;


Map_datasize=0 %Note 2:
Convensional_datasize=0    %Note 2:
MEC_datasize=0 %Note 2:

Map_tolerable=0 %Note 1:
Convensional_tolerable=0    %Note 1:
MEC_tolerable=0 %Note 1:
Map_time=0;     %Note 1:
Convensional_time=0     %Note 1:
MEC_time=0              %Note 1:


Map_speed=0;     %Note 4:
Convensional_speed=0     %Note 4:
MEC_speed=0              %Note 4:
velocity_speed=1         %Note 4:

MAP_Total_alpha10= 0; %Note 3:
MAP_Total_alpha20= 0; %Note 3:
MAP_Total_alpha30= 0; %Note 3:
MAP_Total_beta20=  0; %Note 3:
MAP_Total_beta40=0; %Note 3:
MAP_Total_beta60=0; %Note 3:

Total_SystemCost_MAP=0;
Total_SystemCost_Convensional= 0    
Total_SystemCost_MEC= 0;

MAP_aggregate= 0;
Convensional_aggregate= 0; 
MEC_aggregate= 0;
Total_costalpha_zero_aggregate=0;
Total_costgamma_zero_aggregate=0;
e=1
f=1
%for tolerable=1:4  % Note 1: This loop is just for one graph to calculate the t_max for 1 to 10. If you are not using this graph(Tolerable Time) then delete this loop. and last end and 3 line before end, and also the bar graph of tolerable time.
%for datasize=5:5:50 %Note 2: This loop is just for one graph to calculate the datasize from 5 to 50. If you are not using this graph(datasize of task) then delete this loop. and last end and 3 line before end, and also the last graph data size.
for speed=11:3:23  % Note 4: This loop is just for one graph to calculate the Speed from 30km/h to 70 km/h. If you are not using this graph(Velocity) then delete this loop. and last end and last end and 3 line before end, and also the bar graph of velocity.
    Total_SystemCost_MAP=0  % Note 4:
    Total_SystemCost_Convensional=0   % Note 4:
    Total_SystemCost_MEC=0   % Note 4:

observations=100
for k=1:observations
    N=10       % Total no. of resource hungry vehicle
    for i=1:N  % Total no. of resource hungry vehicle
        V_n_location(i)= randi([0 2000],1,1)  % Source vehicles location
        V_n_capacity(i)= randi([10^6 2*(10^8)],1,1) % source vehicle total computational capacity
        fV_n(i)= randi([1 50],1,1) % source vehicle available computational capacity
        %V_n_speed(i)=randi([10 20],1,1)                  % source vehicle speed
        V_n_speed(i)= speed                                %Note 4: RHVs speed between 40km/h to 80km/h
       %%%%%%%%%%%%%This is the original setting from where the rest of
        %%%%%%%%%%%%%the graph generated%%%%%%%%%%%%%%%%%%%5
        D_n(i)= randi([10 50],1,1)            %  Data size in Kbits
        B_n(i)= D_n(i)*2           % computation cycles required to complete the task in Mega cycles with respect to data size D_n
        t_max(i)=B_n(i)/1.5    % Total tolerable time in seconds according to B_n
%        
%%%%%%%%%This is for graph when we are taking according to tolerable time
%%%%%%%%%t_max
       
%         t_max(i)=tolerable;             % Note 1: This statement is only written when we are showing graph according to t_max. Otherwise we use the upper statement and make it as a comment.
%         B_n(i)= t_max(i)*1.5            % Note 1:
%         D_n(i)= B_n(i)/2                % Note 1: 
%         x=[1 4 8 12]
%         D_n(i)= 25            %  Data size in Kbits         % Note 1:
%         B_n(i)= D_n(i)*2            % Note 1:
%         t_max(i)=x(tolerable);         % Note 1:

%%%%%%%%%This is for graph when we are taking according to Data size%%%%%
%         D_n(i)= datasize           % Note 2: This statement is only written when we are showing graph according to datasize. Otherwise we use the upper statement and make it as a comment.
%         B_n(i)= D_n(i)*2           % Note 2:
%         t_max(i)=B_n(i)/1.5        % Note 2:
    
    end

    %for Other vehicles location and capacity
    M=10;       % Total no. of other vehicles
    for j=1:M  % Total no. of other vehicles
        V_i_location(j)= randi([0 2000],1,1)    % other vehicles location Which may or may not be Resource rich vehicles.
        V_i_capacity(j)= randi([10^6 2*(10^8)],1,1) % other vehicle computational capacity
        %V_i_speed(j)=randi([10 20],1,1)                  % other vehicle speed
        V_i_speed(j)=17                    %Note 4: for Fixing RRV speed to 60 km\h

    end
    %for VEC location and capacity
    loc=400;
    for rsu=1:5
        rsu_location(rsu)=loc;  % other vehicles location Which may or may not be Resource rich vehicles.
        rsu_capacity= 8*10^8;  % computational capacity of VEC servers. in Cycles/s
        loc=loc+400;
    end
    RT_VEC=randi([5 10],1,5) % so far just created later this is a queue which is kept increasing as the no. of vehicles grow.  
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                                %Different Functions
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %VEC transmission
    V_n_size= length(V_n_location)
    [ R_V2I_final,t_V2I_stay ]= VEC_trans(V_n_location,V_n_speed, V_n_size)
    %V2V transmission
    V_i_size= length(V_i_location)
    [ R_V2V_final]= V2V_trans(V_n_location, V_n_size, V_i_location, V_i_size)
    %V2V Stay time
    [ V2V_stay, fV_i, fV_other,Available_resources ]= V2V_staytime (V_n_location, V_n_speed, V_n_size, V_i_location, V_i_speed, V_i_size)
    %Local Computation
    [Cost_local, Time_local,alpha, Total_Cost_local]=local_comp (fV_n,B_n,D_n, V_n_size, t_max)
    %Nearby vehicle Computation
    [ Cost_nearby, Time_nearby, beta, minValues,minCost,Total_Cost_nearby] = nearby_comp(B_n,D_n, V_n_size, t_max, V2V_stay, fV_i,R_V2V_final)
    %VEC Server Computation
    [Cost_VEC, Time_VEC,Total_Cost_VEC] = VEC_comp(B_n,D_n, V_n_size, t_max,alpha, beta, t_V2I_stay, R_V2I_final,rsu_capacity,V_n_location, V_i_location, fV_i,Available_resources,RT_VEC)
    %Convensional Algorithm
    [Cost_Convensional,Total_Cost_Convensional]= Convensional_Algo(B_n,D_n, V_n_size, t_max,alpha, t_V2I_stay, R_V2I_final,rsu_capacity,V_n_location, V_i_location, fV_i,Available_resources,RT_VEC)
    % For Beta zero.
    [Total_Cost_nearby_MEC]= Cost_betazero_VEC_comp(B_n,D_n, V_n_size, t_max,alpha, t_V2I_stay, R_V2I_final,rsu_capacity,V_n_location, V_i_location, fV_i,Available_resources,RT_VEC)
    %For taking apha is equal to zero
    %[Cost_alphazero] = Cost_alphazero_VEC_comp(B_n,D_n, V_n_size, t_max,alpha, beta, t_V2I_stay, R_V2I_final,rsu_capacity,V_n_location, V_i_location, fV_i,Available_resources, RT_VEC)
    %For taking gama is equal to zero
    [Total_Cost_nearby_gamazero,beta_rest] = nearby_comp_gamazero(B_n,D_n, V_n_size, t_max, V2V_stay, fV_i,R_V2V_final,alpha)
    %For taking specific values of alpha i.e. alph=0.1, 0.2, 0.3 %Note 3:
    [alpha_10,alpha_20,alpha_30,Total_Cost_local_10,Total_Cost_local_20,Total_Cost_local_30] = local_comp_Specific_alpha (fV_n,B_n,D_n, V_n_size, t_max)
    %For taking specific values of beta i.e. beta=0.2, 0.4, 0.6     %Note 3:
    [beta_20,beta_40, beta_60,Total_Cost_nearby_20,Total_Cost_nearby_40,Total_Cost_nearby_60] = nearby_comp_Specific_beta(B_n,D_n, V_n_size, t_max, V2V_stay, fV_i,R_V2V_final)
    %for taking specific value of gama according to the values of alpha and
    %beta i.e. alph=0.1, 0.2, 0.3 and beta=0.2, 0.4, 0.6        %Note 3:
%    [Total_Cost_VEC_gamma_a10, Total_Cost_VEC_gamma_a20, Total_Cost_VEC_gamma_a30, Total_Cost_VEC_gamma_b20, Total_Cost_VEC_gamma_b40, Total_Cost_VEC_gamma_b60] = VEC_comp_Specific (alpha, beta,alpha_10,alpha_20,alpha_30, beta_20,beta_40, beta_60, B_n,D_n, V_n_size, t_max, t_V2I_stay, R_V2I_final,rsu_capacity,V_n_location, V_i_location, fV_i,Available_resources, RT_VEC)
    % For MEC-partial offloading Technique.
    [Total_MECCost_nearby,beta_MEC_nearby]= MEC_Algo_nearby(B_n,D_n, V_n_size, t_max, V2V_stay, fV_i,R_V2V_final)
    [Total_Cost_MEC] = MEC_Algo_VEC(B_n,D_n, V_n_size, t_max,alpha, beta_MEC_nearby, t_V2I_stay, R_V2I_final,rsu_capacity,V_n_location, V_i_location, fV_i,Available_resources,RT_VEC)

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Total Cost%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    Cost_total_indivisual=Cost_local+Cost_nearby+Cost_VEC     %our proposed algorithm for Indivisual vehicles
    Total_SystemCost_MAP= Total_Cost_local+Total_Cost_nearby+Total_Cost_VEC; %Our proposed algorithm Total system cost. 
    Cost_MEC=Cost_local+Cost_nearby                %Comparison of algorithm
    Total_SystemCost_Convensional= Total_Cost_local+Total_Cost_Convensional      %Conventional algorithm where only local and MEC computation take place    
    Total_SystemCost_MEC= Total_Cost_local+Total_MECCost_nearby+ Total_Cost_MEC
    
%   Total_costalpha_zero= Total_Cost_nearby_MEC+ Cost_alphazero     %where alpha is zero
    Total_costgamma_zero= Total_Cost_local+Total_Cost_nearby_gamazero        %where gama is zero

    %%%%%%%%%%%%For taking the average of the simulations%%%%%%%%%%%%%%%%%
    
    MAP_aggregate= MAP_aggregate+Total_SystemCost_MAP; %Our proposed algorithm Total system cost. Taking average 
    Convensional_aggregate= Convensional_aggregate+Total_SystemCost_Convensional      %Conventional algorithm where only local and MEC computation take place. Taking average     
    MEC_aggregate= MEC_aggregate+Total_SystemCost_MEC                 %Taking average for MEC algorithm
%    Total_costalpha_zero_aggregate= Total_costalpha_zero_aggregate+Total_costalpha_zero % Cost When Alpha is zero
    Total_costgamma_zero_aggregate= Total_costgamma_zero_aggregate+Total_costgamma_zero
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                                  %Note 3
%     MAP_Total_alpha10= MAP_Total_alpha10+(Total_Cost_local_10+Total_Cost_nearby+Total_Cost_VEC_gamma_a10); %Note 3:
%     MAP_Total_alpha20= MAP_Total_alpha20+(Total_Cost_local_20+Total_Cost_nearby+Total_Cost_VEC_gamma_a20); %Note 3:
%     MAP_Total_alpha30= MAP_Total_alpha30+(Total_Cost_local_30+Total_Cost_nearby+Total_Cost_VEC_gamma_a30); %Note 3:
    
%     MAP_Total_beta20=MAP_Total_beta20+(Total_Cost_local+Total_Cost_nearby_20+Total_Cost_VEC_gamma_b20); %Note 3:
%     MAP_Total_beta40=MAP_Total_beta40+(Total_Cost_local+Total_Cost_nearby_40+Total_Cost_VEC_gamma_b40); %Note 3:
%     MAP_Total_beta60=MAP_Total_beta60+(Total_Cost_local+Total_Cost_nearby_60+Total_Cost_VEC_gamma_b60); %Note 3:
   
%     Map_tolerable=(MAP_aggregate/observations)      %Note 3:
     
%     Map_time(e)=Map_tolerable(end)               %Note 3:
%     e=e+1;                                          %Note 3:
%     MAP_aggregate=0
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
end
%     Map_tolerable=(MAP_aggregate/observations)      %Note 1:
%     Convensional_tolerable=(Convensional_aggregate/observations)    %Note 1:
%     MEC_tolerable=(MEC_aggregate/observations)      %Note 1:
%     
%     Map_time(e)=Map_tolerable(end)               %Note 1:
%     Convensional_time(e)=Convensional_tolerable(end)         %Note 1:
%     MEC_time(e)=MEC_tolerable(end)
%     e=e+1;                                          %Note 1:
%     MAP_aggregate=0
%     Convensional_aggregate=0
%     MEC_aggregate=0
% end     %Note 1:

%     Map_datasize_initial=(MAP_aggregate/observations)       %Note 2:
%     Convensional_datasize_initial=(Convensional_aggregate/observations)    %Note 2:
%     MEC_datasize_initial=(MEC_aggregate/observations)   %Note 2: 
%     
%     Map_datasize(f)=Map_datasize_initial(end)               %Note 2:
%     Convensional_datasize(f)=Convensional_datasize_initial(end)         %Note 2:
%     MEC_datasize(f)=MEC_datasize_initial(end)
%     f=f+1;                                          %Note 2:
%     MAP_aggregate=0
%     Convensional_aggregate=0
%     MEC_aggregate=0
%     
% end     %Note 2:
%%%%%%%%%%%%%%%%%%%%%%%Note 4:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
    
    Map_tolerable=(MAP_aggregate/observations)%Note 4:
    Map_speed(velocity_speed)=Map_tolerable(end)             %Note 4:
    MEC_tolerable=(MEC_aggregate/observations) %Note 4:
    MEC_speed(velocity_speed)= MEC_tolerable(end)  %Note 4:
    Convensional_tolerable=(Convensional_aggregate/observations)%Note 4:
    Convensional_speed(velocity_speed) = Convensional_tolerable(end)   %Note 4:
    velocity_speed=velocity_speed+1                 %Note 4:
    MAP_aggregate=0;        %Note 4:
    MEC_aggregate=0;        %Note 4:
    Convensional_aggregate=0;       %Note 4:
end       %Note 4:

MAP_Total_alpha10_final = (MAP_Total_alpha10/observations); %Note 3:
MAP_Total_alpha20_final = (MAP_Total_alpha20/observations); %Note 3:
MAP_Total_alpha30_final = (MAP_Total_alpha30/observations); %Note 3:
MAP_Total_beta20_final  = (MAP_Total_beta20/observations);  %Note 3:
MAP_Total_beta40_final  = (MAP_Total_beta40/observations);  %Note 3:
MAP_Total_beta60_final  = (MAP_Total_beta60/observations);  %Note 3:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
               %Plots: This section is reserved for Plots only
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure
%plot(1:N,Cost_total(1:N),'-ro','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)
plot(1:N,(MAP_aggregate(1:N)/observations),'-ro','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[0.9290 0.6940 0.1250], 'MarkerSize',10)
hold on;
%figure
plot(1:N,(MEC_aggregate(1:N)/observations),'-ks','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)
%figure
plot(1:N,(Convensional_aggregate(1:N)/observations),'-ms','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)
xlabel('No. of Vehicles')
ylabel('Average Offloading Cost')
%legend('Shortest Queue', 'V2I Direct', 'Awarely-Balanced')
title('System Cost')
hold off;

%Separate Plot for proposed Algorithm.
figure
plot(1:N,(MAP_aggregate(1:N)/observations),'-rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[0.9290 0.6940 0.1250], 'MarkerSize',10)

%putting alpha, beta, and gamma =0 one by one to see the difference.
figure
plot(1:N,(Total_costalpha_zero_aggregate(1:N)/observations),'-r+','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)      % Where alpha is zero.
hold on;
plot(1:N,(Convensional_aggregate(1:N)/observations),'-mo','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)      % Where beta is zero.
plot(1:N,(Total_costgamma_zero_aggregate(1:N)/observations),'-k+','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)      % Where gamma is zero.
hold off;      

%%%%%%%%This graph is for tolerable time increases from 1 to 10.%%%%%%%%%%%
x=[Map_time(1) MEC_time(1) Convensional_time(1); Map_time(2) MEC_time(2) Convensional_time(2); Map_time(3) MEC_time(3) Convensional_time(3); Map_time(4) MEC_time(4) Convensional_time(4)]
bar(x)      %Note 1:


figure %Note 1:
plot(4:4:16,(Map_time(1:4)),'-b*','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[0.9290 0.6940 0.1250], 'MarkerSize',10)
hold on;
plot(4:4:16,(MEC_time(1:4)),'-rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)
plot(4:4:16,(Convensional_time(1:4)),'-mo','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)
%xlabel('Data size of the Task')
ylabel('Average Offloading Cost')
legend('MAP', 'MEC Partial', 'Conventional')
%title('System Cost')
hold off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure %Note 2:
plot(5:5:50,(Map_datasize(1:10)),'-b*','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[0.9290 0.6940 0.1250], 'MarkerSize',10)
hold on;
plot(5:5:50,(MEC_datasize(1:10)),'-rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)
plot(5:5:50,(Convensional_datasize(1:10)),'-mo','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',[.49 .5 .63], 'MarkerSize',10)
xlabel('Data size of the Task')
ylabel('Average Offloading Cost')
legend('MAP', 'MEC Partial', 'Conventional')
%title('System Cost')
hold off;


%%%%%%%%%%%%%%This bar graph is accoding to the specified values of alpha and
%%%%%%%%%%%%%%beta i.e. alph=0.1, 0.2, 0.3 and beta=0.2, 0.4, 0.6
figure %Note 3: 
x=[ MAP_Total_alpha10_final(1) MAP_Total_alpha20_final(1) MAP_Total_alpha30_final(1) MAP_Total_beta20_final(1) MAP_Total_beta40_final(1) MAP_Total_beta60_final(1); MAP_Total_alpha10_final(2) MAP_Total_alpha20_final(2) MAP_Total_alpha30_final(2) MAP_Total_beta20_final(2) MAP_Total_beta40_final(2) MAP_Total_beta60_final(2); MAP_Total_alpha10_final(3) MAP_Total_alpha20_final(3) MAP_Total_alpha30_final(3) MAP_Total_beta20_final(3) MAP_Total_beta40_final(3) MAP_Total_beta60_final(3); MAP_Total_alpha10_final(4) MAP_Total_alpha20_final(4) MAP_Total_alpha30_final(4) MAP_Total_beta20_final(4) MAP_Total_beta40_final(4) MAP_Total_beta60_final(4); MAP_Total_alpha10_final(5) MAP_Total_alpha20_final(5) MAP_Total_alpha30_final(5) MAP_Total_beta20_final(5) MAP_Total_beta40_final(5) MAP_Total_beta60_final(5); MAP_Total_alpha10_final(6) MAP_Total_alpha20_final(6) MAP_Total_alpha30_final(6) MAP_Total_beta20_final(6) MAP_Total_beta40_final(6) MAP_Total_beta60_final(6); MAP_Total_alpha10_final(7) MAP_Total_alpha20_final(7) MAP_Total_alpha30_final(7) MAP_Total_beta20_final(7) MAP_Total_beta40_final(7) MAP_Total_beta60_final(7); MAP_Total_alpha10_final(8) MAP_Total_alpha20_final(8) MAP_Total_alpha30_final(8) MAP_Total_beta20_final(8) MAP_Total_beta40_final(8) MAP_Total_beta60_final(8); MAP_Total_alpha10_final(9) MAP_Total_alpha20_final(9) MAP_Total_alpha30_final(9) MAP_Total_beta20_final(9) MAP_Total_beta40_final(9) MAP_Total_beta60_final(9); MAP_Total_alpha10_final(10) MAP_Total_alpha20_final(10) MAP_Total_alpha30_final(10) MAP_Total_beta20_final(10) MAP_Total_beta40_final(10) MAP_Total_beta60_final(10)
]  % From RHV 1 to 10
h=bar(x)
xlabel('No. of Resource Hungry Vehicles')
ylabel('Total Offloading Cost')
legend(h,{'?=0.1','?=0.2','?=0.3','?=0.2','?=0.4','?=0.6'})


%%%%%%%%%%%%%%This bar graph is accoding to the Speed of RHVs, i.e., 30km/h to
%%%%%%%%%%%%%%70km/h and 50km/h fixed for RRVs %%%%%%%%%%%%%%%%%%%%%
figure %Note 4: 
x=[Map_speed(1) MEC_speed(1) Convensional_speed(1); Map_speed(2) MEC_speed(2) Convensional_speed(2); Map_speed(3) MEC_speed(3) Convensional_speed(3); Map_speed(4) MEC_speed(4) Convensional_speed(4); Map_speed(5) MEC_speed(5) Convensional_speed(5)]
% x=[Map_speed(1) MEC_speed(1) Convensional_speed(1); Map_speed(2) MEC_speed(2) Convensional_speed(2); Map_speed(3) MEC_speed(3) Convensional_speed(3); Map_speed(4) MEC_speed(4) Convensional_speed(4)]
bar(x)      %Note 4:
xlabel('Velocity of RHVs Km/h') %Note 4:
ylabel('Total Offloading Cost') %Note 4:
legend('MAP', 'MEC Partial', 'Conventional') %Note 4:   ............this is the mat file "T100_NM10_Speed8t020.mat"


