├── CM_ALG_Paper_Version_Analysis.m ├── EDF_Code.m ├── Frame_Based_QOS.m ├── Heterogeneous_Deadline_1.m ├── Heterogeneous_Deadline_2.m ├── Heterogeneous_Deadline_3.m ├── Heterogeneous_Deadline_4.m ├── LDF_First_EDF.m ├── README.md ├── RMG.m ├── SP_EDF_WDF_Comparison.m ├── Traffic Pattern RLPF.xlsx └── num_packet_arrival.m /CM_ALG_Paper_Version_Analysis.m: -------------------------------------------------------------------------------- 1 | % Comparison between CM, and Reduced Cost 2 | % Reduced cost is a policy when all the deadlines are one 3 | % In the reduced case, we know that scheduling the highest 4 | % weigth link with existing packet is the optimal policy 5 | % Complete paper approach 6 | 7 | clc; 8 | clear all; 9 | close all; 10 | 11 | tic; 12 | 13 | % L= 10; 14 | % temp_lam= rand(1,L); 15 | % temp_lam= temp_lam/sum(temp_lam); 16 | % lambda= round(temp_lam,3); 17 | 18 | % lambda= [ 0.23 0.20 0.16 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 19 | % % lambda= linspace(0.01,1,20); lambda= lambda/sum(lambda); lambda= round(lambda,3); 20 | % lambda= logspace(-2,0,10); lambda= lambda/sum(lambda); lambda= round(lambda,3); 21 | % lambda= [(1-0.9)/9*ones(1,9) 0.9]; 22 | 23 | % A random combination of lambda that does not maintain an increasing or 24 | % decreasing sequence 25 | % lambda= 0.1*ones(1,10); 26 | % lambda= [ 0.20 0.16 0.23 .01 0.14 0.09 0.07 0.04 0.02 0.02 ]; 27 | % lambda= 0.1*ones(1,10); 28 | % lambda= [0.54 0.3 0.15]; 29 | % lambda= [0.4 0.3 0.29]; 30 | % lambda= [0.4 0.3 0.2 0.1]; 31 | % lambda= [0.3 0.25 0.20 0.15 0.1]; 32 | % lambda= 1/5*ones(1,5); 33 | % lambda= [0.3 0.25 0.18 0.13 0.1 0.04]; 34 | % lambda= [0.5 0.4 0.1]; 35 | L= 10; 36 | lambda= ones(1,L)*1/L; 37 | % lambda= [ 0.7 0.3 ]; 38 | % lambda= [0.5 0.5]; 39 | % lambda= [0.8 0.8 0.8]; 40 | 41 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; % 10 Links 42 | % tao= [ 4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; % 20 Links 43 | % tao= [5 3 4]; % 3 Links 44 | 45 | L= length(lambda); 46 | temp_weight= rand(1,L); 47 | w= sort(temp_weight)/sum(temp_weight); % Ascending weight 48 | w= sort(temp_weight,'descend')/sum(temp_weight); % Descending weight 49 | w= round(w,4); 50 | 51 | % L= length(lambda); 52 | % w= 0.5.^(1:1:L); % 2.^(L:-1:1); 53 | % w= (L:-1:1); 54 | % w= w/sum(w); 55 | 56 | T= 10^5; 57 | 58 | tao_max= 2; 59 | 60 | for repeat= 1:5 61 | 62 | cost_CM= zeros(tao_max,1); 63 | cost_red= cost_CM; 64 | a= zeros(L,T); 65 | 66 | for j=1:L 67 | a(j,: )= rand(1,T)>(1-lambda(j)); 68 | end 69 | 70 | % disp('Appropriateness of the data'); 71 | % mean(a,2) 72 | % sum(mean(a,2)) 73 | 74 | for tao1= 1:tao_max 75 | 76 | tao1 77 | tao= tao1*ones(1,L); 78 | 79 | %%%%%%%%% Reduced Policy %%%%%%%%%%%% 80 | if tao1==1 81 | 82 | s= zeros(L,T); 83 | D1= zeros(L,1); 84 | 85 | for t=1:T 86 | 87 | if sum(a(:,t)) 88 | ind= find(a(:,t),1); 89 | s(ind,t)= 1; 90 | end 91 | 92 | end 93 | 94 | D1= sum(a,2)- sum(s,2); 95 | cost_red= w*D1/T./(1:tao_max)'; 96 | 97 | end 98 | 99 | 100 | 101 | %%%%%%%%%%%%% CM Policy %%%%%%%%%%%%%%%% 102 | 103 | sm= zeros(L,T); 104 | chain_res= [ ]; 105 | chain_link= [ ]; 106 | D2= zeros(L,1); 107 | 108 | for t= 1:T 109 | 110 | % Updating chain_link and chain_res on arrival 111 | 112 | if sum(a(:,t)) 113 | 114 | for j= 1:L 115 | 116 | if a(j,t) 117 | 118 | chain_link= sort([chain_link j+0.5]); 119 | place= find(chain_link==(j+0.5)); 120 | chain_res= [chain_res(1:place-1) tao(j) chain_res(place:end)]; 121 | chain_link(place)= j; 122 | 123 | end 124 | end 125 | 126 | end 127 | 128 | 129 | 130 | if sum(chain_res) 131 | 132 | % Finding max weight packets 133 | min_res= min(chain_res); 134 | max_res= max(chain_res); 135 | 136 | sched_res= [ ]; 137 | sched_link= [ ]; 138 | 139 | for i= min_res:max_res 140 | temp_ind= find(chain_res==i); 141 | sched_res= [sched_res chain_res(temp_ind)]; 142 | sched_link= [sched_link chain_link(temp_ind)]; 143 | 144 | temp= sortrows([sched_link;sched_res]'); 145 | sched_link= temp(:,1)'; 146 | sched_res= temp(:,2)'; 147 | 148 | most= min(i,length(sched_link)); 149 | % Drop calculation 150 | for link= sched_link(most+1:end) 151 | D2(link)= D2(link)+1; 152 | end 153 | 154 | % Schedulable set 155 | sched_link= sched_link(1:most); 156 | sched_res= sched_res(1:most); 157 | 158 | 159 | end 160 | 161 | 162 | % Finding packet to schedule 163 | [~,ind]= min(sched_res); 164 | sm(sched_link(ind),t)= 1; 165 | 166 | % Updating chain_link and chain_res 167 | sched_link(ind)= []; 168 | sched_res(ind)= []; 169 | 170 | chain_link= sched_link; 171 | chain_res= sched_res; 172 | 173 | chain_res= chain_res-1; 174 | 175 | end 176 | 177 | 178 | end 179 | 180 | cost_CM(tao1)= w*D2/T; 181 | QOS(:,tao1)= sum(sm,2)./sum(a,2); 182 | 183 | 184 | end % tao1 loop ends 185 | 186 | disp('QOS as deadline increases from 1:tao_max'); 187 | disp(QOS); 188 | per1= cost_CM./cost_red 189 | 190 | repeat 191 | 192 | disp('Weight transmission'); 193 | sum(QOS(:,2)'.*w.*lambda)/sum(w.*lambda) 194 | 195 | end % Repeat 3 times and average the cost, loop ends 196 | 197 | 198 | %% 199 | 200 | 201 | 202 | disp(' CM Reduced '); 203 | disp([cost_CM cost_red]); 204 | 205 | 206 | figure(1); 207 | plot(1:tao_max, cost_CM, '-o', 1:tao_max, cost_red,'-s', 'linewidth',2); 208 | legend('CM Cost', 'Reduced Cost'); xlabel('Deadlines'); ylabel('Average Cost'); 209 | grid on; 210 | title('Comparison of CM, and Reduced cost'); 211 | 212 | figure(2); 213 | per1= cost_CM./cost_red; 214 | plot(1:tao_max, per1,'-^','linewidth',2); 215 | grid on; 216 | xlabel('Deadlines'); ylabel('Ratio'); 217 | title('Ratio of CM to RED'); 218 | 219 | 220 | %% 221 | 222 | toc; 223 | 224 | % File_Name= 'EDF_SP_10_Links_10^6_All_3_Deadlines'; 225 | % 226 | % h = findobj('type','figure'); 227 | % n = length(h); 228 | % 229 | % mkdir(File_Name); 230 | % cd(File_Name); 231 | % for i=1:n 232 | % savefig(i,num2str(i)); 233 | % end 234 | % cd .. 235 | 236 | -------------------------------------------------------------------------------- /EDF_Code.m: -------------------------------------------------------------------------------- 1 | %%%% EDF algorithm, it does EDF with arbitrary tie braking 2 | %%%% Generates Deficit vs Time graph for EDF policy 3 | %%%% QOS graph is plotted at 10^6 times slots 4 | 5 | clc; 6 | % clear all; 7 | close all; 8 | 9 | tic; 10 | 11 | % File_Name= 'EDF_20_Links_10^6_All_3_Deadlines'; 12 | 13 | e= 3:6; 14 | DL= zeros(1,length(e)); 15 | 16 | for r= 1:length(e) 17 | 18 | T= 10^e(r); 19 | lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 20 | % lambda= [ 0.26 0.19 0.15 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 21 | % lambda= [0.4 0.35 0.24]; 22 | % lambda= [0.55 0.3 0.15]; 23 | % lambda= [0.9 0.9 0.9]; 24 | 25 | L= length(lambda); 26 | 27 | p= ones(1,L)*0.80; 28 | % p= [0.9 0.85 0.75]; 29 | % p= [0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.7 0.65 0.65]; % Delivery ratio 30 | % p= [0.9*ones(1,3) 0.8*ones(1,5) 0.75*ones(1,5) 0.65*ones(1,7) 0.6]; 31 | 32 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; 33 | % tao= [ 4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; 34 | tao= 2*ones(1,L); 35 | % tao= [5 3 4]; 36 | 37 | a= zeros(L,T); 38 | s= a; 39 | ED= a; 40 | prior= a; 41 | D1= a; 42 | D= a; 43 | 44 | 45 | for j=1:L 46 | a(j,: )= rand(1,T)>(1-lambda(j)); 47 | end 48 | 49 | disp('Appropriateness of the data'); 50 | mean(a,2) 51 | sum(mean(a,2)) 52 | 53 | 54 | % Stack of deadlines 55 | z= zeros(sum(tao),T); 56 | 57 | for t=1:T 58 | 59 | for j= 1:L 60 | 61 | start= sum(tao(1:j-1))+1; 62 | last= sum(tao(1:j-1))+tao(j); 63 | jth_link_index= start:last; 64 | 65 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 66 | z(jth_link_index,t)= [0;temp(1:end-1)]; 67 | 68 | if a(j,t)==1 69 | z(start,t)= tao(j); 70 | end 71 | 72 | if sum(z(jth_link_index,t)) 73 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 74 | end 75 | 76 | end 77 | 78 | 79 | deadlines= ED(:,t); 80 | min_deadline= min(deadlines(deadlines>0)); 81 | if (min_deadline) 82 | prior(find(ED( :,t)== min_deadline),t)= 1; 83 | end 84 | 85 | prior_ind= 0; 86 | if sum(prior( :,t)) 87 | prior_ind= find(prior( :,t)); 88 | end 89 | 90 | %%%%% Deficit calculation 91 | 92 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 93 | 94 | if sum(prior_ind) 95 | chosen= datasample(prior_ind,1); 96 | s(chosen,t)= 1; 97 | D(chosen,t)= max(0,D(chosen,t)-1); 98 | 99 | start= sum(tao(1:chosen-1))+1; 100 | last= sum(tao(1:chosen-1))+tao(chosen); 101 | jth_link_index= start:last; 102 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 103 | end 104 | 105 | 106 | %%%% Drop calculation 107 | D1( :,t)= D1( :,max(1,t-1)); 108 | 109 | for j= 1:L 110 | 111 | start= sum(tao(1:j-1))+1; 112 | last= sum(tao(1:j-1))+tao(j); 113 | jth_link_index= start:last; 114 | 115 | if z(last,t)==1 && s(j,t)==0 116 | D1(j,t)= D1(j,t)+1; 117 | end 118 | 119 | end 120 | 121 | 122 | 123 | end 124 | 125 | 126 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 127 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 128 | 129 | disp('Delivery ratio- aggregate'); 130 | sum(sum(s))/sum(sum(a)) 131 | 132 | disp('Delivery ratio- linkwise'); 133 | EDF= sum(s,2)./sum(a,2) 134 | 135 | disp('Difference of arrival and schedule'); 136 | sum(a,2)-sum(s,2) 137 | disp('Total drop '); 138 | sum(sum(a,2)-sum(s,2)) 139 | 140 | disp('Cumulative drop at the end'); 141 | D1(:,T) 142 | sum(D1(:,T)) 143 | 144 | disp('Cumulative deficit at the end'); 145 | D(:,T) 146 | DL(r)= sum(D(:,T)); 147 | 148 | end 149 | 150 | mu= sum(s,2)/T; 151 | [mu lambda'] 152 | mu-lambda' 153 | 154 | 155 | 156 | %% 157 | 158 | figure(1) 159 | % plot(e, DL, 'k+', 'LineWidth',2); hold on; 160 | plot(e, DL, 'LineWidth',2); hold on; 161 | % ylim([1 500]); 162 | xlabel('10^e'); 163 | ylabel('Deficit'); 164 | title('Deficit Comparison for EDF'); 165 | grid on; 166 | 167 | figure(2); 168 | plot(1:L, EDF, 'k+', 'LineWidth',2); hold on; 169 | plot(1:L, p, 'LineWidth',2) 170 | legend('EDF', 'Given'); 171 | 172 | plot(1:L,EDF, 'LineWidth',2); hold on; 173 | xlabel('Links'); 174 | ylabel('Achieved delivery ratio'); 175 | title('QOS Comparison of EDF policy'); 176 | grid on; 177 | 178 | toc; 179 | 180 | h = findobj('type','figure'); 181 | n = length(h); 182 | 183 | mkdir(File_Name); 184 | cd(File_Name); 185 | for i=1:n 186 | savefig(i,num2str(i)); 187 | end 188 | cd .. 189 | 190 | 191 | 192 | figure(8); hold on; 193 | plot(1:20, backup_1,'k', 1:20, backup_3,'g', 'LineWidth',2); 194 | legend('Given','LDF+EDF', 'EDF+LDF','Randomized','LDF','EDF'); 195 | 196 | figure(1); hold on; 197 | plot(1:20, backup_1,'k', 1:20, backup_3,'g', 'LineWidth',2); 198 | legend('Given','LDF+EDF', 'EDF+LDF','Randomized','LDF','EDF'); 199 | 200 | 201 | 202 | -------------------------------------------------------------------------------- /Frame_Based_QOS.m: -------------------------------------------------------------------------------- 1 | % There is a frame with tao_max time slots 2 | % Packet arrives on the first time slot in a frame with some probability 3 | % No packet arrives in the middle of the frame 4 | % Whatever packet arrives at the beginning of the frame, has to be scheduled 5 | % by the end of the frame 6 | 7 | clc; 8 | clear all; 9 | close all; 10 | 11 | T= 10^5; 12 | % lambda= [0.4 0.3 0.29]; 13 | % lambda= [0.4 0.3 0.15 0.09]; % 0.04]; 14 | % lambda= 0.1*ones(1,10); 15 | lambda= [ 0.23 0.20 0.16 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 16 | % lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 17 | 18 | % lambda= fliplr(lambda); 19 | L= length(lambda); 20 | w= 0.5.^(L:-1:1); 21 | 22 | a= [ ]; 23 | for j=1:L 24 | a(j,: )= rand(1,T)>(1-lambda(j)); 25 | end 26 | 27 | tao_max= 3; 28 | 29 | soj= 0; 30 | i=1; 31 | while i<= T-tao_max 32 | 33 | if sum(a(:,i)) 34 | 35 | backup= i+tao_max-1; 36 | 37 | j= i; 38 | while j<=min(backup, T) 39 | 40 | if sum(a(:,j)) 41 | 42 | backup= j+tao_max-1; 43 | end 44 | 45 | j= j+1; 46 | 47 | end 48 | 49 | soj= soj+(backup-i+1); 50 | i= backup; 51 | 52 | end 53 | 54 | i= i+1; 55 | 56 | end 57 | 58 | soj 59 | soj/T 60 | 61 | 62 | num_arrivals= ones(1,L); 63 | for i= tao_max+1:L % This are the links whose delivery ratio is being calculated 64 | 65 | 66 | 67 | temp= 0; 68 | for j= 0:tao_max-1 % This number of arrivals 69 | 70 | z= [ ]; 71 | z= nchoosek(1:i-1,j); % In this indices of links 72 | 73 | for n= 1:nchoosek(i-1,j) % In this indices of links 74 | arrive= z(n,: ); 75 | noarrive= setdiff(1:i-1,arrive); 76 | temp= temp+ prod(lambda(arrive))*prod(1-lambda(noarrive)); 77 | 78 | end 79 | 80 | end 81 | 82 | num_arrivals(i)= temp; 83 | 84 | end 85 | 86 | [(1:L)' num_arrivals'] 87 | 88 | disp('Cost of the frame based policy'); 89 | sum((1- num_arrivals).*w) 90 | 91 | % atot= sum(a); 92 | % z= zeros(1,L); 93 | % for i= 0:L 94 | % z(i+1)= length(find(atot==i)); 95 | % end 96 | % 97 | % 98 | % 99 | % disp(' #of Packets Prob Actual'); 100 | % [(0:L)' num_arrivals' z'/T] 101 | 102 | 103 | -------------------------------------------------------------------------------- /Heterogeneous_Deadline_1.m: -------------------------------------------------------------------------------- 1 | %%%% EDF+LDF Policy 2 | %%%% Generates Deficit vs Time graph 3 | %%%% Also generates QOS graph at 10^e times slots, e= [3 4 5 6]; 4 | 5 | clc; 6 | clear all; 7 | close all; 8 | 9 | tic; 10 | 11 | e= 3:6; 12 | DL= zeros(1,length(e)); 13 | 14 | for r= 1:length(e) 15 | 16 | T= 10^e(r); 17 | lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 18 | % lambda= [ 0.26 0.19 0.15 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 19 | % lambda= [0.4 0.35 0.24]; 20 | % lambda= [0.54 0.3 0.15]; 21 | % lambda= [0.9 0.9 0.9]; 22 | 23 | L= length(lambda); 24 | 25 | p= ones(1,L)*0.80; 26 | % p= [0.9 0.85 0.75]; 27 | % p= [0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.7 0.65 0.65]; % Delivery ratio 28 | % p= [0.9*ones(1,3) 0.8*ones(1,5) 0.75*ones(1,5) 0.65*ones(1,7) 0.6]; 29 | 30 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; 31 | % tao= [4*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 32 | % tao= [5*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 33 | tao= [ 4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; 34 | % tao= 2*ones(1,L); 35 | % tao= [4 2 3]; 36 | 37 | a= zeros(L,T); 38 | 39 | for j=1:L 40 | a(j,: )= rand(1,T)>(1-lambda(j)); 41 | end 42 | 43 | disp('Appropriateness of the data'); 44 | mean(a,2) 45 | sum(mean(a,2)) 46 | 47 | 48 | % Traffic Pattern 49 | % Traffic= zeros(L,T); 50 | % for j= 1:L 51 | % for t= 1:T 52 | % Traffic(j,min(t:t+tao(j)-1,T))= a(j,t)+Traffic(j,min(t:t+tao(j)-1,T)); 53 | % end 54 | % 55 | % end 56 | 57 | 58 | %%%% Deficit queue based schedule 59 | 60 | s= zeros(L,T); 61 | ED= s; 62 | D= s; 63 | D1= s; 64 | A= s; 65 | R= s; 66 | prior= s; 67 | A(:,1)= a(:,1); 68 | 69 | % Stack of deadlines 70 | z= zeros(sum(tao),T); 71 | 72 | % prior(:,1)= a(:,1); 73 | % if sum(a(:,1)) 74 | % s(datasample(find(a(:,1)),1),1)= 1; 75 | % end 76 | % 77 | % R(:,1)= s(:,1); 78 | 79 | 80 | for t= 1:T 81 | 82 | for j= 1:L 83 | 84 | start= sum(tao(1:j-1))+1; 85 | last= sum(tao(1:j-1))+tao(j); 86 | jth_link_index= start:last; 87 | 88 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 89 | z(jth_link_index,t)= [0;temp(1:end-1)]; 90 | 91 | if a(j,t)==1 92 | z(start,t)= tao(j); 93 | end 94 | 95 | if sum(z(jth_link_index,t)) 96 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 97 | end 98 | 99 | end 100 | 101 | 102 | deadlines= ED(:,t); 103 | min_deadline= min(deadlines(deadlines>0)); 104 | if (min_deadline) 105 | prior(find(ED( :,t)== min_deadline),t)= 1; 106 | end 107 | 108 | prior_ind= 0; 109 | if sum(prior( :,t)) 110 | prior_ind= find(prior( :,t)); 111 | end 112 | 113 | 114 | %%%% Deficit queue update method 115 | 116 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 117 | 118 | 119 | if sum(prior_ind) 120 | 121 | links_pack= zeros(length(prior_ind),2); 122 | 123 | for k= 1:length(prior_ind) 124 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 125 | end 126 | 127 | maximum_deficit= max(links_pack( :,1)); 128 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 129 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 130 | s(chosen,t)= 1; 131 | 132 | D(chosen,t)= max(0,D(chosen,t)-1); 133 | 134 | start= sum(tao(1:chosen-1))+1; 135 | last= sum(tao(1:chosen-1))+tao(chosen); 136 | jth_link_index= start:last; 137 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 138 | 139 | end 140 | 141 | 142 | %%%% Drop calculation 143 | 144 | if t>1 145 | for j= 1:L 146 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 147 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 148 | D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 149 | end 150 | end 151 | 152 | 153 | 154 | end 155 | 156 | 157 | 158 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 159 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 160 | 161 | disp('Delivery ratio- aggregate'); 162 | sum(sum(s))/sum(sum(a)) 163 | 164 | disp('Delivery ratio- linkwise'); 165 | deficit_based= sum(s,2)./sum(a,2) 166 | 167 | 168 | disp('Difference of arrival and schedule'); 169 | sum(a,2)-sum(s,2) 170 | disp('Total drop '); 171 | sum(sum(a,2)-sum(s,2)) 172 | disp('Cumulative deficit at the end'); 173 | DD= D(:,T) 174 | disp(sum(D(:,T))) 175 | 176 | disp('Cumulative drop at the end'); 177 | D1(:,T) 178 | sum(D1(:,T)) 179 | 180 | DL(r)= sum(D(:,T)); 181 | figure(r) 182 | plot(1:L, deficit_based, 'k+', 'LineWidth',2); hold on; 183 | plot(1:L, p, 'LineWidth',2) 184 | 185 | legend('LDF', 'Given'); 186 | 187 | xlabel('Links'); 188 | ylabel('Achieved delivery ratio'); 189 | title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 190 | grid on; 191 | 192 | 193 | end 194 | 195 | figure(r+1) 196 | plot(e, DL, 'k+', 'LineWidth',2); hold on; 197 | plot(e, DL, 'LineWidth',2); hold on; 198 | 199 | xlabel('10^e'); 200 | ylabel('Deficit'); 201 | title('Deficit Comparison for LDF'); 202 | grid on; 203 | 204 | 205 | 206 | toc; 207 | tic; 208 | 209 | -------------------------------------------------------------------------------- /Heterogeneous_Deadline_2.m: -------------------------------------------------------------------------------- 1 | %%%% Largest code, subsumes everything 2 | %%%% Comparison of EDF_LDF....Only LDF with arbitrary tie braking....Randomized schedule 3 | %%%% Improved drop calculation 4 | %%%% Generates Deficit vs Time graph 5 | %%%% Also generates QOS graph at 10^6 times slots 6 | 7 | clc; 8 | clear all; 9 | close all; 10 | 11 | tic; 12 | 13 | e= 3:6; 14 | DL= zeros(1,length(e)); 15 | DW= DL; 16 | DR= DL; 17 | % File_Name= 'EDF_LDF_RAND_Heter_Deadlines_20_Links_1_000_000_Slots'; 18 | 19 | for r= 1:length(e) 20 | 21 | T= 10^e(r); 22 | lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 23 | % lambda= [ 0.26 0.19 0.15 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 24 | % lambda= [0.4 0.35 0.24]; 25 | % lambda= [0.54 0.3 0.15]; 26 | % lambda= [0.9 0.9 0.9]; 27 | 28 | L= length(lambda); 29 | 30 | p= ones(1,L)*0.80; 31 | % p= [0.9 0.85 0.75]; 32 | % p= [0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.7 0.65 0.65]; % Delivery ratio 33 | % p= [0.9*ones(1,3) 0.8*ones(1,5) 0.75*ones(1,5) 0.65*ones(1,7) 0.6]; 34 | 35 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; 36 | % tao= [4*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 37 | % tao= [5*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 38 | % tao= [ 4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; 39 | tao= 2*ones(1,L); 40 | % tao= [4 2 3]; 41 | 42 | a= zeros(L,T); 43 | 44 | for j=1:L 45 | a(j,: )= rand(1,T)>(1-lambda(j)); 46 | end 47 | 48 | disp('Appropriateness of the data'); 49 | mean(a,2) 50 | sum(mean(a,2)) 51 | 52 | 53 | % Traffic Pattern 54 | % Traffic= zeros(L,T); 55 | % for j= 1:L 56 | % for t= 1:T 57 | % Traffic(j,min(t:t+tao(j)-1,T))= a(j,t)+Traffic(j,min(t:t+tao(j)-1,T)); 58 | % end 59 | % 60 | % end 61 | 62 | 63 | %%%% Deficit queue based schedule 64 | 65 | s= zeros(L,T); 66 | ED= s; 67 | D= s; 68 | D1= s; 69 | A= s; 70 | R= s; 71 | prior= s; 72 | A(:,1)= a(:,1); 73 | 74 | % Stack of deadlines 75 | z= zeros(sum(tao),T); 76 | 77 | % prior(:,1)= a(:,1); 78 | % if sum(a(:,1)) 79 | % s(datasample(find(a(:,1)),1),1)= 1; 80 | % end 81 | % 82 | % R(:,1)= s(:,1); 83 | 84 | 85 | for t= 1:T 86 | 87 | for j= 1:L 88 | 89 | start= sum(tao(1:j-1))+1; 90 | last= sum(tao(1:j-1))+tao(j); 91 | jth_link_index= start:last; 92 | 93 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 94 | z(jth_link_index,t)= [0;temp(1:end-1)]; 95 | 96 | if a(j,t)==1 97 | z(start,t)= tao(j); 98 | end 99 | 100 | if sum(z(jth_link_index,t)) 101 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 102 | end 103 | 104 | end 105 | 106 | 107 | deadlines= ED(:,t); 108 | min_deadline= min(deadlines(deadlines>0)); 109 | if (min_deadline) 110 | prior(find(ED( :,t)== min_deadline),t)= 1; 111 | end 112 | 113 | prior_ind= 0; 114 | if sum(prior( :,t)) 115 | prior_ind= find(prior( :,t)); 116 | end 117 | 118 | 119 | %%%% Deficit queue update method 120 | 121 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 122 | 123 | 124 | if sum(prior_ind) 125 | 126 | links_pack= zeros(length(prior_ind),2); 127 | 128 | for k= 1:length(prior_ind) 129 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 130 | end 131 | 132 | maximum_deficit= max(links_pack( :,1)); 133 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 134 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 135 | s(chosen,t)= 1; 136 | 137 | D(chosen,t)= max(0,D(chosen,t)-1); 138 | 139 | start= sum(tao(1:chosen-1))+1; 140 | last= sum(tao(1:chosen-1))+tao(chosen); 141 | jth_link_index= start:last; 142 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 143 | 144 | end 145 | 146 | 147 | D1( :,t)= D1( :,max(1,t-1)); 148 | 149 | for j= 1:L 150 | 151 | start= sum(tao(1:j-1))+1; 152 | last= sum(tao(1:j-1))+tao(j); 153 | jth_link_index= start:last; 154 | 155 | if z(last,t)==1 && s(j,t)==0 156 | D1(j,t)= D1(j,t)+1; 157 | end 158 | 159 | end 160 | 161 | 162 | %%%% Drop calculation 163 | 164 | if t>1 165 | for j= 1:L 166 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 167 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 168 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 169 | end 170 | end 171 | 172 | 173 | 174 | end 175 | 176 | 177 | 178 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 179 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 180 | 181 | disp('Delivery ratio- aggregate'); 182 | sum(sum(s))/sum(sum(a)) 183 | 184 | disp('Delivery ratio- linkwise'); 185 | deficit_based= sum(s,2)./sum(a,2) 186 | 187 | 188 | disp('Difference of arrival and schedule'); 189 | sum(a,2)-sum(s,2) 190 | disp('Total drop '); 191 | sum(sum(a,2)-sum(s,2)) 192 | disp('Cumulative deficit at the end'); 193 | DD= D(:,T) 194 | disp(sum(D(:,T))) 195 | 196 | disp('Cumulative drop at the end'); 197 | D1(:,T) 198 | sum(D1(:,T)) 199 | 200 | DL(r)= sum(D(:,T)); 201 | 202 | % figure(r) 203 | % plot(1:L, deficit_based, 'LineWidth',2); hold on; 204 | % plot(1:L, p, 'LineWidth',2); hold on; 205 | % 206 | % legend('LDF', 'Given'); 207 | % 208 | % xlabel('Links'); 209 | % ylabel('Achieved delivery ratio'); 210 | % title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 211 | % grid on; 212 | % 213 | % 214 | % %end 215 | % 216 | % figure(r+1) 217 | % plot(e, DL, 'k+', 'LineWidth',2); hold on; 218 | % plot(e, DL, 'LineWidth',2); hold on; 219 | % 220 | % xlabel('10^e'); 221 | % ylabel('Deficit'); 222 | % title('Deficit Comparison for LDF'); 223 | % grid on; 224 | 225 | %%%% Saving the figures 226 | 227 | % h = findobj('type','figure'); 228 | % n = length(h); 229 | % mkdir(File_Name); 230 | % cd(File_Name); 231 | % for i=1:n 232 | % savefig(i,num2str(i)); 233 | % end 234 | % cd .. 235 | 236 | toc; 237 | tic; 238 | 239 | 240 | %% 241 | %%%% Comparison of LDF+EDF and LDF without EDF 242 | 243 | % File_Name= 'Just_LDF_21_Links_Heter'; 244 | 245 | 246 | %%%% Deficit queue based schedule 247 | 248 | s= zeros(L,T); 249 | ED= s; 250 | D= s; 251 | D1= s; 252 | A= s; 253 | R= s; 254 | prior= s; 255 | A(:,1)= a(:,1); 256 | 257 | % Stack of deadlines 258 | z= zeros(sum(tao),T); 259 | 260 | % prior(:,1)= a(:,1); 261 | % if sum(a(:,1)) 262 | % s(datasample(find(a(:,1)),1),1)= 1; 263 | % end 264 | % 265 | % R(:,1)= s(:,1); 266 | 267 | 268 | for t= 1:T 269 | 270 | for j= 1:L 271 | 272 | start= sum(tao(1:j-1))+1; 273 | last= sum(tao(1:j-1))+tao(j); 274 | jth_link_index= start:last; 275 | 276 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 277 | z(jth_link_index,t)= [0;temp(1:end-1)]; 278 | 279 | if a(j,t)==1 280 | z(start,t)= tao(j); 281 | end 282 | 283 | if sum(z(jth_link_index,t)) 284 | prior(j,t)= 1; 285 | end 286 | 287 | end 288 | 289 | 290 | prior_ind= 0; 291 | if sum(prior( :,t)) 292 | prior_ind= find(prior( :,t)); 293 | end 294 | 295 | 296 | %%%% Deficit queue update method 297 | 298 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 299 | 300 | 301 | if sum(prior_ind) 302 | 303 | links_pack= zeros(length(prior_ind),2); 304 | 305 | for k= 1:length(prior_ind) 306 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 307 | end 308 | 309 | maximum_deficit= max(links_pack( :,1)); 310 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 311 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 312 | s(chosen,t)= 1; 313 | 314 | D(chosen,t)= max(0,D(chosen,t)-1); 315 | 316 | start= sum(tao(1:chosen-1))+1; 317 | last= sum(tao(1:chosen-1))+tao(chosen); 318 | jth_link_index= start:last; 319 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 320 | 321 | end 322 | 323 | 324 | D1( :,t)= D1( :,max(1,t-1)); 325 | 326 | for j= 1:L 327 | 328 | start= sum(tao(1:j-1))+1; 329 | last= sum(tao(1:j-1))+tao(j); 330 | jth_link_index= start:last; 331 | 332 | if z(last,t)==1 && s(j,t)==0 333 | D1(j,t)= D1(j,t)+1; 334 | end 335 | 336 | end 337 | 338 | 339 | %%%% Drop calculation 340 | 341 | if t>1 342 | for j= 1:L 343 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 344 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 345 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 346 | end 347 | end 348 | 349 | 350 | 351 | end 352 | 353 | 354 | 355 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 356 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 357 | 358 | disp('Delivery ratio- aggregate'); 359 | sum(sum(s))/sum(sum(a)) 360 | 361 | disp('Delivery ratio- linkwise'); 362 | deficit_based_without= sum(s,2)./sum(a,2) 363 | 364 | 365 | disp('Difference of arrival and schedule'); 366 | sum(a,2)-sum(s,2) 367 | disp('Total drop '); 368 | sum(sum(a,2)-sum(s,2)) 369 | disp('Cumulative deficit at the end'); 370 | DD= D(:,T) 371 | disp(sum(D(:,T))) 372 | 373 | disp('Cumulative drop at the end'); 374 | D1(:,T) 375 | sum(D1(:,T)) 376 | 377 | DW(r)= sum(D(:,T)); 378 | % figure(r) 379 | % plot(1:L, deficit_based_without, 'LineWidth',2); hold on; 380 | % plot(1:L, p, 'LineWidth',2); hold on; 381 | % 382 | % legend('LDF', 'Given'); 383 | % 384 | % xlabel('Links'); 385 | % ylabel('Achieved delivery ratio'); 386 | % title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 387 | % grid on; 388 | 389 | %%%% Take care of this end 390 | % %end 391 | 392 | % figure(r+1) 393 | % plot(e, DL, 'k+', 'LineWidth',2); hold on; 394 | % plot(e, DL, 'LineWidth',2); hold on; 395 | % 396 | % xlabel('10^e'); 397 | % ylabel('Deficit'); 398 | % title('Deficit Comparison for LDF'); 399 | % grid on; 400 | 401 | %%%% Saving the figures 402 | 403 | % h = findobj('type','figure'); 404 | % n = length(h); 405 | % mkdir(File_Name); 406 | % cd(File_Name); 407 | % for i=1:n 408 | % savefig(i,num2str(i)); 409 | % end 410 | % cd .. 411 | 412 | toc; 413 | tic; 414 | 415 | 416 | 417 | 418 | 419 | 420 | %% 421 | %%%% Completely Random 422 | 423 | % File_Name= 'Random_21_Links_Heter'; 424 | 425 | 426 | %%%% Deficit queue based schedule 427 | 428 | s= zeros(L,T); 429 | ED= s; 430 | D= s; 431 | D1= s; 432 | A= s; 433 | R= s; 434 | prior= s; 435 | A(:,1)= a(:,1); 436 | 437 | % Stack of deadlines 438 | z= zeros(sum(tao),T); 439 | 440 | % prior(:,1)= a(:,1); 441 | % if sum(a(:,1)) 442 | % s(datasample(find(a(:,1)),1),1)= 1; 443 | % end 444 | % 445 | % R(:,1)= s(:,1); 446 | 447 | 448 | for t= 1:T 449 | 450 | for j= 1:L 451 | 452 | start= sum(tao(1:j-1))+1; 453 | last= sum(tao(1:j-1))+tao(j); 454 | jth_link_index= start:last; 455 | 456 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 457 | z(jth_link_index,t)= [0;temp(1:end-1)]; 458 | 459 | if a(j,t)==1 460 | z(start,t)= tao(j); 461 | end 462 | 463 | if sum(z(jth_link_index,t)) 464 | prior(j,t)= 1; 465 | end 466 | 467 | end 468 | 469 | 470 | %%%% Deficit queue update method 471 | 472 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 473 | 474 | prior_ind= 0; 475 | if sum(prior( :,t)) 476 | prior_ind= find(prior( :,t)); 477 | chosen= datasample(prior_ind,1); 478 | 479 | s(chosen,t)= 1; 480 | D(chosen,t)= max(0,D(chosen,t)-1); % Deficit queue update 481 | 482 | start= sum(tao(1:chosen-1))+1; 483 | last= sum(tao(1:chosen-1))+tao(chosen); 484 | jth_link_index= start:last; 485 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 486 | 487 | end 488 | 489 | 490 | 491 | D1( :,t)= D1( :,max(1,t-1)); 492 | 493 | for j= 1:L 494 | 495 | start= sum(tao(1:j-1))+1; 496 | last= sum(tao(1:j-1))+tao(j); 497 | jth_link_index= start:last; 498 | 499 | if z(last,t)==1 && s(j,t)==0 500 | D1(j,t)= D1(j,t)+1; 501 | end 502 | 503 | end 504 | 505 | 506 | %%%% Drop calculation 507 | 508 | if t>1 509 | for j= 1:L 510 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 511 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 512 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 513 | end 514 | end 515 | 516 | 517 | 518 | end 519 | 520 | 521 | 522 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 523 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 524 | 525 | disp('Delivery ratio- aggregate'); 526 | sum(sum(s))/sum(sum(a)) 527 | 528 | disp('Delivery ratio- linkwise'); 529 | random= sum(s,2)./sum(a,2) 530 | 531 | 532 | disp('Difference of arrival and schedule'); 533 | sum(a,2)-sum(s,2) 534 | disp('Total drop '); 535 | sum(sum(a,2)-sum(s,2)) 536 | disp('Cumulative deficit at the end'); 537 | DD= D(:,T) 538 | disp(sum(D(:,T))) 539 | 540 | disp('Cumulative drop at the end'); 541 | D1(:,T) 542 | sum(D1(:,T)) 543 | 544 | DR(r)= sum(D(:,T)); 545 | % figure(r) 546 | % plot(1:L, random, 'LineWidth',2); hold on; 547 | % plot(1:L, p, 'LineWidth',2); hold on; 548 | % 549 | % legend('LDF', 'Given'); 550 | % 551 | % xlabel('Links'); 552 | % ylabel('Achieved delivery ratio'); 553 | % title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 554 | % grid on; 555 | 556 | %%%% Take care of this end 557 | % %end 558 | 559 | % figure(r+1) 560 | % plot(e, DL, 'k+', 'LineWidth',2); hold on; 561 | % plot(e, DL, 'LineWidth',2); hold on; 562 | % 563 | % xlabel('10^e'); 564 | % ylabel('Deficit'); 565 | % title('Deficit Comparison for LDF'); 566 | % grid on; 567 | 568 | %%%% Saving the figures 569 | 570 | % h = findobj('type','figure'); 571 | % n = length(h); 572 | % mkdir(File_Name); 573 | % cd(File_Name); 574 | % for i=1:n 575 | % savefig(i,num2str(i)); 576 | % end 577 | % cd .. 578 | 579 | toc; 580 | tic; 581 | 582 | end 583 | 584 | 585 | h = findobj('type','figure'); 586 | n = length(h); 587 | 588 | figure(n+1); 589 | plot(e,DL, e,DW, e,DR, 'linewidth',2); grid on; 590 | % ylim([1 500]); 591 | legend('EDF+LDF','LDF','Randomized'); 592 | xlabel('10^e'); 593 | ylabel('Deficit'); 594 | title('Cumulative Deficit Comparison Among Policies'); 595 | 596 | 597 | figure(n+2); 598 | plot(1:L,p,1:L,deficit_based,1:L,deficit_based_without,1:L, random, 'linewidth',2); 599 | legend('Given','EDF+LDF','LDF', 'Randomized'); 600 | grid on; 601 | xlabel('Links'); 602 | ylabel('QOS'); 603 | title('Achieved QOS Comparison Among Policies'); 604 | 605 | 606 | % File_Name= 'EDF_20_Links_10^6_All_3_Deadlines' 607 | mkdir(File_Name); 608 | cd(File_Name); 609 | for i=1:n 610 | savefig(i,num2str(i)); 611 | end 612 | cd .. 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | -------------------------------------------------------------------------------- /Heterogeneous_Deadline_3.m: -------------------------------------------------------------------------------- 1 | %%%% Comparison of EDF_LDF....LDF_EDF....Randomized schedule 2 | %%%% Improved drop calculation 3 | 4 | clc; 5 | clear all; 6 | close all; 7 | 8 | tic; 9 | 10 | e= 3:6; 11 | DL= zeros(1,length(e)); 12 | % File_Name= 'LDF_EDF_10_Links_Heter'; 13 | 14 | for r= 1:length(e) 15 | 16 | T= 10^e(r); 17 | % lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7) 0.01]; 18 | lambda= [ 0.26 0.19 0.15 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 19 | % lambda= [0.4 0.35 0.24]; 20 | % lambda= [0.54 0.3 0.15]; 21 | % lambda= [0.9 0.9 0.9]; 22 | 23 | L= length(lambda); 24 | 25 | p= ones(1,L)*0.80; 26 | % p= [0.9 0.85 0.75]; 27 | % p= [0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.7 0.65 0.65]; % Delivery ratio 28 | % p= [0.9*ones(1,3) 0.8*ones(1,5) 0.75*ones(1,5) 0.65*ones(1,7) 0.6]; 29 | 30 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; 31 | tao= [4*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 32 | % tao= [5*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 33 | % tao= [ 5*ones(1,2) 2*ones(1,5) 4*ones(1,5) 3*ones(1,9)]; 34 | % tao= 2*ones(1,L); 35 | % tao= [4 2 3]; 36 | 37 | a= zeros(L,T); 38 | 39 | for j=1:L 40 | a(j,: )= rand(1,T)>(1-lambda(j)); 41 | end 42 | 43 | disp('Appropriateness of the data'); 44 | mean(a,2) 45 | sum(mean(a,2)) 46 | 47 | 48 | % Traffic Pattern 49 | % Traffic= zeros(L,T); 50 | % for j= 1:L 51 | % for t= 1:T 52 | % Traffic(j,min(t:t+tao(j)-1,T))= a(j,t)+Traffic(j,min(t:t+tao(j)-1,T)); 53 | % end 54 | % 55 | % end 56 | 57 | 58 | %%%% Deficit queue based schedule 59 | 60 | s= zeros(L,T); 61 | ED= s; 62 | D= s; 63 | D1= s; 64 | A= s; 65 | R= s; 66 | prior= s; 67 | A(:,1)= a(:,1); 68 | 69 | % Stack of deadlines 70 | z= zeros(sum(tao),T); 71 | 72 | % prior(:,1)= a(:,1); 73 | % if sum(a(:,1)) 74 | % s(datasample(find(a(:,1)),1),1)= 1; 75 | % end 76 | % 77 | % R(:,1)= s(:,1); 78 | 79 | 80 | for t= 1:T 81 | 82 | for j= 1:L 83 | 84 | start= sum(tao(1:j-1))+1; 85 | last= sum(tao(1:j-1))+tao(j); 86 | jth_link_index= start:last; 87 | 88 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 89 | z(jth_link_index,t)= [0;temp(1:end-1)]; 90 | 91 | if a(j,t)==1 92 | z(start,t)= tao(j); 93 | end 94 | 95 | if sum(z(jth_link_index,t)) 96 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 97 | end 98 | 99 | end 100 | 101 | 102 | deadlines= ED(:,t); 103 | min_deadline= min(deadlines(deadlines>0)); 104 | if (min_deadline) 105 | prior(find(ED( :,t)== min_deadline),t)= 1; 106 | end 107 | 108 | prior_ind= 0; 109 | if sum(prior( :,t)) 110 | prior_ind= find(prior( :,t)); 111 | end 112 | 113 | 114 | %%%% Deficit queue update method 115 | 116 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 117 | 118 | 119 | if sum(prior_ind) 120 | 121 | links_pack= zeros(length(prior_ind),2); 122 | 123 | for k= 1:length(prior_ind) 124 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 125 | end 126 | 127 | maximum_deficit= max(links_pack( :,1)); 128 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 129 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 130 | s(chosen,t)= 1; 131 | 132 | D(chosen,t)= max(0,D(chosen,t)-1); 133 | 134 | start= sum(tao(1:chosen-1))+1; 135 | last= sum(tao(1:chosen-1))+tao(chosen); 136 | jth_link_index= start:last; 137 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 138 | 139 | end 140 | 141 | 142 | D1( :,t)= D1( :,max(1,t-1)); 143 | 144 | for j= 1:L 145 | 146 | start= sum(tao(1:j-1))+1; 147 | last= sum(tao(1:j-1))+tao(j); 148 | jth_link_index= start:last; 149 | 150 | if z(last,t)==1 && s(j,t)==0 151 | D1(j,t)= D1(j,t)+1; 152 | end 153 | 154 | end 155 | 156 | 157 | %%%% Drop calculation 158 | 159 | if t>1 160 | for j= 1:L 161 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 162 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 163 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 164 | end 165 | end 166 | 167 | 168 | 169 | end 170 | 171 | 172 | 173 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 174 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 175 | 176 | disp('Delivery ratio- aggregate'); 177 | sum(sum(s))/sum(sum(a)) 178 | 179 | disp('Delivery ratio- linkwise'); 180 | deficit_based= sum(s,2)./sum(a,2) 181 | 182 | 183 | disp('Difference of arrival and schedule'); 184 | sum(a,2)-sum(s,2) 185 | disp('Total drop '); 186 | sum(sum(a,2)-sum(s,2)) 187 | disp('Cumulative deficit at the end'); 188 | DD= D(:,T) 189 | disp(sum(D(:,T))) 190 | 191 | disp('Cumulative drop at the end'); 192 | D1(:,T) 193 | sum(D1(:,T)) 194 | 195 | DL(r)= sum(D(:,T)); 196 | figure(r) 197 | plot(1:L, deficit_based, 'k+', 'LineWidth',2); hold on; 198 | plot(1:L, p, 'LineWidth',2) 199 | 200 | legend('LDF', 'Given'); 201 | 202 | xlabel('Links'); 203 | ylabel('Achieved delivery ratio'); 204 | title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 205 | grid on; 206 | 207 | 208 | %%%% Take care of this end 209 | % end 210 | 211 | figure(r+1) 212 | plot(e, DL, 'k+', 'LineWidth',2); hold on; 213 | plot(e, DL, 'LineWidth',2); hold on; 214 | 215 | xlabel('10^e'); 216 | ylabel('Deficit'); 217 | title('Deficit Comparison for LDF'); 218 | grid on; 219 | 220 | %%%% Saving the figures 221 | 222 | h = findobj('type','figure'); 223 | n = length(h); 224 | mkdir(File_Name); 225 | cd(File_Name); 226 | for i=1:n 227 | savefig(i,num2str(i)); 228 | end 229 | cd .. 230 | 231 | toc; 232 | tic; 233 | 234 | 235 | %% 236 | %%%% Comparison of LDF+EDF and LDF without EDF 237 | 238 | File_Name= 'Just_LDF_10_Links_Heter'; 239 | 240 | 241 | %%%% Deficit queue based schedule 242 | 243 | s= zeros(L,T); 244 | ED= s; 245 | D= s; 246 | D1= s; 247 | A= s; 248 | R= s; 249 | prior= s; 250 | A(:,1)= a(:,1); 251 | 252 | % Stack of deadlines 253 | z= zeros(sum(tao),T); 254 | 255 | % prior(:,1)= a(:,1); 256 | % if sum(a(:,1)) 257 | % s(datasample(find(a(:,1)),1),1)= 1; 258 | % end 259 | % 260 | % R(:,1)= s(:,1); 261 | 262 | 263 | for t= 1:T 264 | 265 | for j= 1:L 266 | 267 | start= sum(tao(1:j-1))+1; 268 | last= sum(tao(1:j-1))+tao(j); 269 | jth_link_index= start:last; 270 | 271 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 272 | z(jth_link_index,t)= [0;temp(1:end-1)]; 273 | 274 | if a(j,t)==1 275 | z(start,t)= tao(j); 276 | end 277 | 278 | if sum(z(jth_link_index,t)) 279 | prior(j,t)= 1; 280 | end 281 | 282 | end 283 | 284 | 285 | prior_ind= 0; 286 | if sum(prior( :,t)) 287 | prior_ind= find(prior( :,t)); 288 | end 289 | 290 | 291 | %%%% Deficit queue update method 292 | 293 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 294 | 295 | 296 | if sum(prior_ind) 297 | 298 | links_pack= zeros(length(prior_ind),2); 299 | 300 | for k= 1:length(prior_ind) 301 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 302 | end 303 | 304 | maximum_deficit= max(links_pack( :,1)); 305 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 306 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 307 | s(chosen,t)= 1; 308 | 309 | D(chosen,t)= max(0,D(chosen,t)-1); 310 | 311 | start= sum(tao(1:chosen-1))+1; 312 | last= sum(tao(1:chosen-1))+tao(chosen); 313 | jth_link_index= start:last; 314 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 315 | 316 | end 317 | 318 | 319 | D1( :,t)= D1( :,max(1,t-1)); 320 | 321 | for j= 1:L 322 | 323 | start= sum(tao(1:j-1))+1; 324 | last= sum(tao(1:j-1))+tao(j); 325 | jth_link_index= start:last; 326 | 327 | if z(last,t)==1 && s(j,t)==0 328 | D1(j,t)= D1(j,t)+1; 329 | end 330 | 331 | end 332 | 333 | 334 | %%%% Drop calculation 335 | 336 | if t>1 337 | for j= 1:L 338 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 339 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 340 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 341 | end 342 | end 343 | 344 | 345 | 346 | end 347 | 348 | 349 | 350 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 351 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 352 | 353 | disp('Delivery ratio- aggregate'); 354 | sum(sum(s))/sum(sum(a)) 355 | 356 | disp('Delivery ratio- linkwise'); 357 | deficit_based_without= sum(s,2)./sum(a,2) 358 | 359 | 360 | disp('Difference of arrival and schedule'); 361 | sum(a,2)-sum(s,2) 362 | disp('Total drop '); 363 | sum(sum(a,2)-sum(s,2)) 364 | disp('Cumulative deficit at the end'); 365 | DD= D(:,T) 366 | disp(sum(D(:,T))) 367 | 368 | disp('Cumulative drop at the end'); 369 | D1(:,T) 370 | sum(D1(:,T)) 371 | 372 | DW(r)= sum(D(:,T)); 373 | figure(r) 374 | plot(1:L, deficit_based_without, 'k+', 'LineWidth',2); hold on; 375 | plot(1:L, p, 'LineWidth',2) 376 | 377 | legend('LDF', 'Given'); 378 | 379 | xlabel('Links'); 380 | ylabel('Achieved delivery ratio'); 381 | title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 382 | grid on; 383 | 384 | %%%% Take care of this end 385 | % end 386 | 387 | figure(r+1) 388 | plot(e, DL, 'k+', 'LineWidth',2); hold on; 389 | plot(e, DL, 'LineWidth',2); hold on; 390 | 391 | xlabel('10^e'); 392 | ylabel('Deficit'); 393 | title('Deficit Comparison for LDF'); 394 | grid on; 395 | 396 | %%%% Saving the figures 397 | 398 | h = findobj('type','figure'); 399 | n = length(h); 400 | mkdir(File_Name); 401 | cd(File_Name); 402 | for i=1:n 403 | savefig(i,num2str(i)); 404 | end 405 | cd .. 406 | 407 | toc; 408 | tic; 409 | 410 | 411 | 412 | 413 | 414 | 415 | %% 416 | %%%% Completely Random 417 | 418 | File_Name= 'Random_10_Links_Rand'; 419 | 420 | 421 | %%%% Deficit queue based schedule 422 | 423 | s= zeros(L,T); 424 | ED= s; 425 | D= s; 426 | D1= s; 427 | A= s; 428 | R= s; 429 | prior= s; 430 | A(:,1)= a(:,1); 431 | 432 | % Stack of deadlines 433 | z= zeros(sum(tao),T); 434 | 435 | % prior(:,1)= a(:,1); 436 | % if sum(a(:,1)) 437 | % s(datasample(find(a(:,1)),1),1)= 1; 438 | % end 439 | % 440 | % R(:,1)= s(:,1); 441 | 442 | 443 | for t= 1:T 444 | 445 | for j= 1:L 446 | 447 | start= sum(tao(1:j-1))+1; 448 | last= sum(tao(1:j-1))+tao(j); 449 | jth_link_index= start:last; 450 | 451 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 452 | z(jth_link_index,t)= [0;temp(1:end-1)]; 453 | 454 | if a(j,t)==1 455 | z(start,t)= tao(j); 456 | end 457 | 458 | if sum(z(jth_link_index,t)) 459 | prior(j,t)= 1; 460 | end 461 | 462 | end 463 | 464 | 465 | %%%% Deficit queue update method 466 | 467 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 468 | 469 | prior_ind= 0; 470 | if sum(prior( :,t)) 471 | prior_ind= find(prior( :,t)); 472 | chosen= datasample(prior_ind,1); 473 | 474 | s(chosen,t)= 1; 475 | D(chosen,t)= max(0,D(chosen,t)-1); % Deficit queue update 476 | 477 | start= sum(tao(1:chosen-1))+1; 478 | last= sum(tao(1:chosen-1))+tao(chosen); 479 | jth_link_index= start:last; 480 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 481 | 482 | end 483 | 484 | 485 | 486 | D1( :,t)= D1( :,max(1,t-1)); 487 | 488 | for j= 1:L 489 | 490 | start= sum(tao(1:j-1))+1; 491 | last= sum(tao(1:j-1))+tao(j); 492 | jth_link_index= start:last; 493 | 494 | if z(last,t)==1 && s(j,t)==0 495 | D1(j,t)= D1(j,t)+1; 496 | end 497 | 498 | end 499 | 500 | 501 | %%%% Drop calculation 502 | 503 | if t>1 504 | for j= 1:L 505 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 506 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 507 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 508 | end 509 | end 510 | 511 | 512 | 513 | end 514 | 515 | 516 | 517 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 518 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 519 | 520 | disp('Delivery ratio- aggregate'); 521 | sum(sum(s))/sum(sum(a)) 522 | 523 | disp('Delivery ratio- linkwise'); 524 | deficit_based_rand= sum(s,2)./sum(a,2) 525 | 526 | 527 | disp('Difference of arrival and schedule'); 528 | sum(a,2)-sum(s,2) 529 | disp('Total drop '); 530 | sum(sum(a,2)-sum(s,2)) 531 | disp('Cumulative deficit at the end'); 532 | DD= D(:,T) 533 | disp(sum(D(:,T))) 534 | 535 | disp('Cumulative drop at the end'); 536 | D1(:,T) 537 | sum(D1(:,T)) 538 | 539 | DR(r)= sum(D(:,T)); 540 | figure(r) 541 | plot(1:L, deficit_based_rand, 'k+', 'LineWidth',2); hold on; 542 | plot(1:L, p, 'LineWidth',2) 543 | 544 | legend('LDF', 'Given'); 545 | 546 | xlabel('Links'); 547 | ylabel('Achieved delivery ratio'); 548 | title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 549 | grid on; 550 | 551 | %%%% Take care of this end 552 | % end 553 | 554 | figure(r+1) 555 | plot(e, DL, 'k+', 'LineWidth',2); hold on; 556 | plot(e, DL, 'LineWidth',2); hold on; 557 | 558 | xlabel('10^e'); 559 | ylabel('Deficit'); 560 | title('Deficit Comparison for LDF'); 561 | grid on; 562 | 563 | %%%% Saving the figures 564 | 565 | h = findobj('type','figure'); 566 | n = length(h); 567 | mkdir(File_Name); 568 | cd(File_Name); 569 | for i=1:n 570 | savefig(i,num2str(i)); 571 | end 572 | cd .. 573 | 574 | toc; 575 | tic; 576 | 577 | end 578 | 579 | 580 | 581 | figure(7); 582 | plot(e,DL, e,DW, e,DR, 'linewidth',2); grid on; 583 | legend('LDF+EDF','LDF Without EDF','RANDOM'); 584 | xlabel('10^e'); 585 | ylabel('Deficit'); 586 | title('Cumulative Deficit Comparison Among Policies'); 587 | ylim([0 100]); 588 | 589 | figure(6); 590 | plot(1:L,p,1:L,deficit_based,1:L,deficit_based_without,1:L,deficit_based_rand,'linewidth',2); 591 | legend('Given','LDF+EDF','LDF Without EDF','RANDOM'); 592 | grid on; 593 | xlabel('Links'); 594 | ylabel('QOS'); 595 | title('Achieved QOS Comparison Among Policies'); 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | -------------------------------------------------------------------------------- /Heterogeneous_Deadline_4.m: -------------------------------------------------------------------------------- 1 | %%%% Comparison of EDF_LDF....Only LDF with arbitrary tie braking 2 | %%%% Improved drop calculation 3 | %%%% Generates QOS graph at 10^e times slots 4 | %%%% Generates Deficit vs Time graph 5 | %%%% Also generates QOS graph at 10^6 times slots 6 | 7 | clc; 8 | clear all; 9 | close all; 10 | 11 | tic; 12 | 13 | e= 3:5; 14 | DL= zeros(1,length(e)); 15 | DW= zeros(1,length(e)); 16 | % File_Name= 'Heter_Deadline_20_Links_1_000_000_Time_Slots'; 17 | 18 | for r= 1:length(e) 19 | 20 | T= 10^e(r); 21 | % lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 22 | % lambda= [ 0.23 0.20 0.16 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 23 | % lambda= ones(1,10)*0.7; 24 | lambda= [ 0.20 0.16 .01 0.14 0.09 0.23 0.07 0.04 0.02 0.02 ]; % An arbitrary lambda 25 | % lambda= [0.4 0.35 0.24]; 26 | % lambda= [0.54 0.3 0.15]; 27 | % lambda= [0.9 0.9 0.9]; 28 | 29 | L= length(lambda); 30 | lambda= fliplr(lambda); 31 | 32 | % p= [1.0000 0.9600 0.9344 0.9331 0.9144 0.9041 0.8802 0.8746 0.8716 0.8702]; % Required QOS for arbitrary lambda 33 | % p= [1.0000 0.9980 0.9940 0.9902 0.9826 0.9698 0.9545 0.9329 0.9116 0.8893]; % Required QOS for the flipped lambda 34 | % p= [1.0000 0.9540 0.9232 0.9035 0.8890 0.8810 0.8753 0.8723 0.8709 0.8694]; % Required QOS for the given lambda 35 | p= ones(1,L)*0.80; 36 | % p= [0.9 0.85 0.75]; 37 | % p= [0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.7 0.65 0.65]; % Delivery ratio 38 | % p= [0.9*ones(1,3) 0.8*ones(1,5) 0.75*ones(1,5) 0.65*ones(1,7)]; 39 | 40 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; 41 | % tao= [4*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 42 | % tao= [5*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 43 | % tao= [ 4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; 44 | tao= 2*ones(1,L); 45 | % tao= [4 2 3]; 46 | 47 | a= zeros(L,T); 48 | 49 | for j=1:L 50 | a(j,: )= rand(1,T)>(1-lambda(j)); 51 | end 52 | 53 | disp('Appropriateness of the data'); 54 | mean(a,2) 55 | sum(mean(a,2)) 56 | 57 | 58 | % Traffic Pattern 59 | % Traffic= zeros(L,T); 60 | % for j= 1:L 61 | % for t= 1:T 62 | % Traffic(j,min(t:t+tao(j)-1,T))= a(j,t)+Traffic(j,min(t:t+tao(j)-1,T)); 63 | % end 64 | % 65 | % end 66 | 67 | 68 | %%%% Deficit queue based schedule 69 | 70 | s= zeros(L,T); 71 | ED= s; 72 | D= s; 73 | D1= s; 74 | A= s; 75 | R= s; 76 | prior= s; 77 | A(:,1)= a(:,1); 78 | 79 | % Stack of deadlines 80 | z= zeros(sum(tao),T); 81 | 82 | % prior(:,1)= a(:,1); 83 | % if sum(a(:,1)) 84 | % s(datasample(find(a(:,1)),1),1)= 1; 85 | % end 86 | % 87 | % R(:,1)= s(:,1); 88 | 89 | 90 | %%%%% EDF+LDF 91 | 92 | for t= 1:T 93 | 94 | for j= 1:L 95 | 96 | start= sum(tao(1:j-1))+1; 97 | last= sum(tao(1:j-1))+tao(j); 98 | jth_link_index= start:last; 99 | 100 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 101 | z(jth_link_index,t)= [0;temp(1:end-1)]; 102 | 103 | if a(j,t)==1 104 | z(start,t)= tao(j); 105 | end 106 | 107 | if sum(z(jth_link_index,t)) 108 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 109 | end 110 | 111 | end 112 | 113 | 114 | deadlines= ED(:,t); 115 | min_deadline= min(deadlines(deadlines>0)); 116 | if (min_deadline) 117 | prior(find(ED( :,t)== min_deadline),t)= 1; 118 | end 119 | 120 | prior_ind= 0; 121 | if sum(prior( :,t)) 122 | prior_ind= find(prior( :,t)); 123 | end 124 | 125 | 126 | %%%% Deficit queue update method 127 | 128 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 129 | 130 | 131 | if sum(prior_ind) 132 | 133 | links_pack= zeros(length(prior_ind),2); 134 | 135 | for k= 1:length(prior_ind) 136 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 137 | end 138 | 139 | maximum_deficit= max(links_pack( :,1)); 140 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 141 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 142 | s(chosen,t)= 1; 143 | 144 | D(chosen,t)= max(0,D(chosen,t)-1); 145 | 146 | start= sum(tao(1:chosen-1))+1; 147 | last= sum(tao(1:chosen-1))+tao(chosen); 148 | jth_link_index= start:last; 149 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 150 | 151 | end 152 | 153 | 154 | D1( :,t)= D1( :,max(1,t-1)); 155 | 156 | for j= 1:L 157 | 158 | start= sum(tao(1:j-1))+1; 159 | last= sum(tao(1:j-1))+tao(j); 160 | jth_link_index= start:last; 161 | 162 | if z(last,t)==1 && s(j,t)==0 163 | D1(j,t)= D1(j,t)+1; 164 | end 165 | 166 | end 167 | 168 | 169 | %%%% Drop calculation 170 | 171 | if t>1 172 | for j= 1:L 173 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 174 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 175 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 176 | end 177 | end 178 | 179 | 180 | 181 | end 182 | 183 | 184 | 185 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 186 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 187 | 188 | disp('Delivery ratio- aggregate'); 189 | sum(sum(s))/sum(sum(a)) 190 | 191 | disp('Delivery ratio- linkwise'); 192 | deficit_based= sum(s,2)./sum(a,2) 193 | 194 | 195 | disp('Difference of arrival and schedule'); 196 | sum(a,2)-sum(s,2) 197 | disp('Total drop '); 198 | sum(sum(a,2)-sum(s,2)) 199 | disp('Cumulative deficit at the end'); 200 | DD= D(:,T) 201 | disp(sum(D(:,T))) 202 | 203 | disp('Cumulative drop at the end'); 204 | D1(:,T) 205 | sum(D1(:,T)) 206 | 207 | DL(r)= sum(D(:,T)); 208 | figure(r) 209 | plot(1:L, deficit_based, '+', 'LineWidth',2); hold on; 210 | plot(1:L, p, 'LineWidth',2); hold on; 211 | 212 | % legend('LDF', 'Given'); 213 | 214 | xlabel('Links'); 215 | ylabel('Achieved delivery ratio'); 216 | title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 217 | grid on; 218 | 219 | 220 | %%%% Take care of this end 221 | % end 222 | 223 | % figure(r+1) 224 | % plot(e, DL, '+', 'LineWidth',2); hold on; 225 | % plot(e, DL, 'LineWidth',2); hold on; 226 | % 227 | % xlabel('10^e'); 228 | % ylabel('Deficit'); 229 | % title('Deficit Comparison for LDF'); 230 | % grid on; 231 | 232 | 233 | 234 | % %%%% Saving the figures 235 | % 236 | % h = findobj('type','figure'); 237 | % n = length(h); 238 | % mkdir(File_Name); 239 | % cd(File_Name); 240 | % for i=1:n 241 | % savefig(i,num2str(i)); 242 | % end 243 | % cd .. 244 | % 245 | % toc; 246 | % tic; 247 | 248 | 249 | %% 250 | %%%% Only LDF with arbitrary tie braking 251 | 252 | 253 | 254 | % for r= 1:length(e) 255 | % 256 | % T= 10^e(r); 257 | 258 | %%%% Deficit queue based schedule 259 | 260 | s= zeros(L,T); 261 | ED= s; 262 | D= s; 263 | D1= s; 264 | A= s; 265 | R= s; 266 | prior= s; 267 | A(:,1)= a(:,1); 268 | 269 | % Stack of deadlines 270 | z= zeros(sum(tao),T); 271 | 272 | % prior(:,1)= a(:,1); 273 | % if sum(a(:,1)) 274 | % s(datasample(find(a(:,1)),1),1)= 1; 275 | % end 276 | % 277 | % R(:,1)= s(:,1); 278 | 279 | 280 | for t= 1:T 281 | 282 | for j= 1:L 283 | 284 | start= sum(tao(1:j-1))+1; 285 | last= sum(tao(1:j-1))+tao(j); 286 | jth_link_index= start:last; 287 | 288 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 289 | z(jth_link_index,t)= [0;temp(1:end-1)]; 290 | 291 | if a(j,t)==1 292 | z(start,t)= tao(j); 293 | end 294 | 295 | if sum(z(jth_link_index,t)) 296 | prior(j,t)= 1; 297 | end 298 | 299 | end 300 | 301 | 302 | prior_ind= 0; 303 | if sum(prior( :,t)) 304 | prior_ind= find(prior( :,t)); 305 | end 306 | 307 | 308 | %%%% Deficit queue update method 309 | 310 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 311 | 312 | 313 | if sum(prior_ind) 314 | 315 | links_pack= zeros(length(prior_ind),2); 316 | 317 | for k= 1:length(prior_ind) 318 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 319 | end 320 | 321 | maximum_deficit= max(links_pack( :,1)); 322 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 323 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 324 | s(chosen,t)= 1; 325 | 326 | D(chosen,t)= max(0,D(chosen,t)-1); 327 | 328 | start= sum(tao(1:chosen-1))+1; 329 | last= sum(tao(1:chosen-1))+tao(chosen); 330 | jth_link_index= start:last; 331 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 332 | 333 | end 334 | 335 | %%%% Drop calculation 336 | D1( :,t)= D1( :,max(1,t-1)); 337 | 338 | for j= 1:L 339 | 340 | start= sum(tao(1:j-1))+1; 341 | last= sum(tao(1:j-1))+tao(j); 342 | jth_link_index= start:last; 343 | 344 | if z(last,t)==1 && s(j,t)==0 345 | D1(j,t)= D1(j,t)+1; 346 | end 347 | 348 | end 349 | 350 | 351 | 352 | 353 | if t>1 354 | for j= 1:L 355 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 356 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 357 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 358 | end 359 | end 360 | 361 | 362 | end 363 | 364 | 365 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 366 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 367 | 368 | disp('Delivery ratio- aggregate'); 369 | sum(sum(s))/sum(sum(a)) 370 | 371 | disp('Delivery ratio- linkwise'); 372 | deficit_based_without= sum(s,2)./sum(a,2) 373 | 374 | 375 | disp('Difference of arrival and schedule'); 376 | sum(a,2)-sum(s,2) 377 | disp('Total drop '); 378 | sum(sum(a,2)-sum(s,2)) 379 | disp('Cumulative deficit at the end'); 380 | DD= D(:,T) 381 | disp(sum(D(:,T))) 382 | 383 | disp('Cumulative drop at the end'); 384 | D1(:,T) 385 | sum(D1(:,T)) 386 | 387 | DW(r)= sum(D(:,T)); 388 | figure(r) 389 | plot(1:L, deficit_based_without, '+', 'LineWidth',2); hold on; 390 | % plot(1:L, p, 'LineWidth',2) 391 | 392 | % legend('LDF','Given','LDF Without'); 393 | 394 | xlabel('Links'); 395 | ylabel('Achieved delivery ratio'); 396 | title(['QOS Comparison of LDF for time 10^' num2str(e(r))]); 397 | grid on; 398 | 399 | %%%% Take care of this end 400 | 401 | end 402 | 403 | % figure(r+1) 404 | % plot(e, DW, 'k+', 'LineWidth',2); hold on; 405 | % plot(e, DW, 'LineWidth',2); hold on; 406 | % 407 | % xlabel('10^e'); 408 | % ylabel('Deficit'); 409 | % title('Deficit Comparison for LDF'); 410 | % grid on; 411 | 412 | 413 | % end 414 | 415 | %% 416 | 417 | h = findobj('type','figure'); 418 | n = length(h); 419 | for i=1:n 420 | figure(i); 421 | legend('EDF-LDF','Given','LDF with Arbitrary'); 422 | end 423 | 424 | 425 | figure(n+1); 426 | plot(e,DL, e,DW, 'linewidth',2); grid on; 427 | legend('EDF-LDF','LDF with arbitrary'); 428 | xlabel('10^e'); 429 | ylabel('Deficit'); 430 | title('Cumulative Deficit Comparison Among Policies'); 431 | 432 | 433 | figure(n+2); 434 | plot(1:L,p,1:L,deficit_based,1:L,deficit_based_without,'linewidth',2); 435 | legend('Given','EDF-LDF','LDF with Arbitrary'); 436 | grid on; 437 | xlabel('Links'); 438 | ylabel('QOS'); 439 | title('Achieved QOS Comparison Among Policies'); 440 | 441 | 442 | 443 | 444 | %%% Saving the figures 445 | 446 | h = findobj('type','figure'); 447 | n = length(h); 448 | mkdir(File_Name); 449 | cd(File_Name); 450 | for i=1:n 451 | savefig(i,num2str(i)); 452 | end 453 | cd .. 454 | 455 | toc; 456 | 457 | 458 | 459 | -------------------------------------------------------------------------------- /LDF_First_EDF.m: -------------------------------------------------------------------------------- 1 | %%%% Comparison of EDF_LDF and LDF_EDF 2 | %%%% Improved drop calculation 3 | %%%% Generates QOS graph at 10^e times slots 4 | %%%% Generates Deficit vs Time graph 5 | %%%% Also generates QOS graph at 10^6 times slots 6 | 7 | %% 8 | clc; 9 | clear all; 10 | close all; 11 | 12 | tic; 13 | 14 | e= 3:6; 15 | DL= zeros(1,length(e)); 16 | DW= zeros(1,length(e)); 17 | % File_Name= 'LDF_THEN_EDF_Heter_Deadline_10_Links_1_000_000_Time_Slots'; 18 | 19 | for r= 1:length(e) 20 | 21 | T= 10^e(r); 22 | % lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 23 | lambda= [ 0.26 0.19 0.15 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 24 | % lambda= [0.4 0.35 0.24]; 25 | % lambda= [0.54 0.3 0.15]; 26 | % lambda= [0.9 0.9 0.9]; 27 | 28 | L= length(lambda); 29 | 30 | p= ones(1,L)*0.80; 31 | % p= [0.9 0.85 0.75]; 32 | % p= [0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.7 0.65 0.65]; % Delivery ratio 33 | % p= [0.9*ones(1,3) 0.8*ones(1,5) 0.75*ones(1,5) 0.65*ones(1,7)]; 34 | 35 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; 36 | % tao= [4*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 37 | % tao= [5*ones(1,3) 2*ones(1,4) 3*ones(1,3)]; 38 | % tao= [4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; 39 | tao= 3*ones(1,L); 40 | % tao= [4 2 3]; 41 | 42 | a= zeros(L,T); 43 | 44 | for j=1:L 45 | a(j,: )= rand(1,T)>(1-lambda(j)); 46 | end 47 | 48 | disp('Appropriateness of the data'); 49 | mean(a,2) 50 | sum(mean(a,2)) 51 | 52 | 53 | % Traffic Pattern 54 | % Traffic= zeros(L,T); 55 | % for j= 1:L 56 | % for t= 1:T 57 | % Traffic(j,min(t:t+tao(j)-1,T))= a(j,t)+Traffic(j,min(t:t+tao(j)-1,T)); 58 | % end 59 | % 60 | % end 61 | 62 | 63 | %%%% Deficit queue based schedule 64 | 65 | s= zeros(L,T); 66 | ED= s; 67 | D= s; 68 | D1= s; 69 | A= s; 70 | R= s; 71 | prior= s; 72 | A(:,1)= a(:,1); 73 | 74 | % Stack of deadlines 75 | z= zeros(sum(tao),T); 76 | 77 | % prior(:,1)= a(:,1); 78 | % if sum(a(:,1)) 79 | % s(datasample(find(a(:,1)),1),1)= 1; 80 | % end 81 | % 82 | % R(:,1)= s(:,1); 83 | 84 | 85 | for t= 1:T 86 | 87 | for j= 1:L 88 | 89 | start= sum(tao(1:j-1))+1; 90 | last= sum(tao(1:j-1))+tao(j); 91 | jth_link_index= start:last; 92 | 93 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 94 | z(jth_link_index,t)= [0;temp(1:end-1)]; 95 | 96 | if a(j,t)==1 97 | z(start,t)= tao(j); 98 | end 99 | 100 | if sum(z(jth_link_index,t)) 101 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 102 | end 103 | 104 | end 105 | 106 | 107 | deadlines= ED(:,t); 108 | min_deadline= min(deadlines(deadlines>0)); 109 | if (min_deadline) 110 | prior(find(ED( :,t)== min_deadline),t)= 1; 111 | end 112 | 113 | prior_ind= 0; 114 | if sum(prior( :,t)) 115 | prior_ind= find(prior( :,t)); 116 | end 117 | 118 | 119 | %%%% Deficit queue update method 120 | 121 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 122 | 123 | 124 | if sum(prior_ind) 125 | 126 | links_pack= zeros(length(prior_ind),2); 127 | 128 | for k= 1:length(prior_ind) 129 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 130 | end 131 | 132 | maximum_deficit= max(links_pack( :,1)); 133 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 134 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 135 | s(chosen,t)= 1; 136 | 137 | D(chosen,t)= max(0,D(chosen,t)-1); 138 | 139 | start= sum(tao(1:chosen-1))+1; 140 | last= sum(tao(1:chosen-1))+tao(chosen); 141 | jth_link_index= start:last; 142 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 143 | 144 | end 145 | 146 | 147 | D1( :,t)= D1( :,max(1,t-1)); 148 | 149 | for j= 1:L 150 | 151 | start= sum(tao(1:j-1))+1; 152 | last= sum(tao(1:j-1))+tao(j); 153 | jth_link_index= start:last; 154 | 155 | if z(last,t)==1 && s(j,t)==0 156 | D1(j,t)= D1(j,t)+1; 157 | end 158 | 159 | end 160 | 161 | 162 | %%%% Drop calculation 163 | 164 | if t>1 165 | for j= 1:L 166 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 167 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 168 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 169 | end 170 | end 171 | 172 | 173 | 174 | end 175 | 176 | 177 | 178 | %% 179 | 180 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 181 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 182 | 183 | disp('Delivery ratio- aggregate'); 184 | sum(sum(s))/sum(sum(a)) 185 | 186 | disp('Delivery ratio- linkwise'); 187 | deficit_based= sum(s,2)./sum(a,2) 188 | 189 | 190 | disp('Difference of arrival and schedule'); 191 | sum(a,2)-sum(s,2) 192 | disp('Total drop '); 193 | sum(sum(a,2)-sum(s,2)) 194 | disp('Cumulative deficit at the end'); 195 | DD= D(:,T) 196 | disp(sum(D(:,T))) 197 | 198 | disp('Cumulative drop at the end'); 199 | D1(:,T) 200 | sum(D1(:,T)) 201 | 202 | DL(r)= sum(D(:,T)); 203 | figure(r) 204 | plot(1:L, deficit_based, '+', 'LineWidth',2); hold on; 205 | plot(1:L, p, 'LineWidth',2); hold on; 206 | 207 | % legend('LDF', 'Given'); 208 | 209 | xlabel('Links'); 210 | ylabel('Achieved delivery ratio'); 211 | title(['QOS Comparison of EDF+LDF for time 10^' num2str(e(r))]); 212 | grid on; 213 | 214 | 215 | %%%% Take care of this end 216 | % end 217 | 218 | % figure(r+1) 219 | % plot(e, DL, '+', 'LineWidth',2); hold on; 220 | % plot(e, DL, 'LineWidth',2); hold on; 221 | % 222 | % xlabel('10^e'); 223 | % ylabel('Deficit'); 224 | % title('Deficit Comparison for LDF'); 225 | % grid on; 226 | 227 | 228 | 229 | % %%%% Saving the figures 230 | % 231 | % h = findobj('type','figure'); 232 | % n = length(h); 233 | % mkdir(File_Name); 234 | % cd(File_Name); 235 | % for i=1:n 236 | % savefig(i,num2str(i)); 237 | % end 238 | % cd .. 239 | % 240 | % toc; 241 | % tic; 242 | 243 | 244 | %% 245 | 246 | 247 | 248 | 249 | % for r= 1:length(e) 250 | % 251 | % T= 10^e(r); 252 | 253 | %%%% Deficit queue based schedule 254 | 255 | s= zeros(L,T); 256 | ED= s; 257 | DEDD= s; 258 | D= s; 259 | D1= s; 260 | A= s; 261 | R= s; 262 | prior= s; 263 | A(:,1)= a(:,1); 264 | 265 | % Stack of deadlines 266 | z= zeros(sum(tao),T); 267 | 268 | % prior(:,1)= a(:,1); 269 | % if sum(a(:,1)) 270 | % s(datasample(find(a(:,1)),1),1)= 1; 271 | % end 272 | % 273 | % R(:,1)= s(:,1); 274 | 275 | 276 | for t= 1:T 277 | 278 | for j= 1:L 279 | 280 | start= sum(tao(1:j-1))+1; 281 | last= sum(tao(1:j-1))+tao(j); 282 | jth_link_index= start:last; 283 | 284 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 285 | z(jth_link_index,t)= [0;temp(1:end-1)]; 286 | 287 | if a(j,t)==1 288 | z(start,t)= tao(j); 289 | end 290 | 291 | if sum(z(jth_link_index,t)) 292 | prior(j,t)= 1; 293 | end 294 | 295 | end 296 | 297 | 298 | % Links that a have a packet in the input 299 | prior_ind= 0; 300 | if sum(prior( :,t)) 301 | prior_ind= find(prior( :,t)); 302 | end 303 | 304 | 305 | %%%% Deficit queue update method 306 | 307 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-p(j))'; 308 | 309 | 310 | if sum(prior_ind) 311 | 312 | links_pack= zeros(length(prior_ind),2); 313 | 314 | for k= 1:length(prior_ind) 315 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 316 | end 317 | 318 | maximum_deficit= max(links_pack( :,1)); 319 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 320 | 321 | 322 | 323 | 324 | if length(max_def_ind)>1 325 | 326 | for j= links_pack(max_def_ind,2) % The links that have same deficit 327 | % Now find out which are EDD 328 | 329 | start= sum(tao(1:j-1))+1; 330 | last= sum(tao(1:j-1))+tao(j); 331 | jth_link_index= start:last; 332 | 333 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 334 | 335 | end 336 | 337 | 338 | deadlines= ED(:,t); 339 | min_deadline= min(deadlines(deadlines>0)); 340 | DEDD(find(ED( :,t)== min_deadline),t)= 1; % Largest deficit EDD packet 341 | 342 | 343 | DEDD_ind= 0; 344 | if sum(DEDD( :,t)) 345 | DEDD_ind= find(DEDD( :,t)); 346 | end 347 | 348 | else 349 | 350 | DEDD_ind= links_pack(max_def_ind,2); 351 | 352 | end 353 | 354 | 355 | chosen= datasample(DEDD_ind,1); % DEDD_ind are the links that have largest deficit and same residual time packet 356 | s(chosen,t)= 1; 357 | 358 | D(chosen,t)= max(0,D(chosen,t)-1); 359 | 360 | start= sum(tao(1:chosen-1))+1; 361 | last= sum(tao(1:chosen-1))+tao(chosen); 362 | jth_link_index= start:last; 363 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 364 | 365 | end 366 | 367 | %%%% Drop calculation 368 | D1( :,t)= D1( :,max(1,t-1)); 369 | 370 | for j= 1:L 371 | 372 | start= sum(tao(1:j-1))+1; 373 | last= sum(tao(1:j-1))+tao(j); 374 | jth_link_index= start:last; 375 | 376 | if z(last,t)==1 && s(j,t)==0 377 | D1(j,t)= D1(j,t)+1; 378 | end 379 | 380 | end 381 | 382 | 383 | 384 | 385 | if t>1 386 | for j= 1:L 387 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 388 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 389 | % D1(j,t)= A(j,t)-R(j,t); % Cumulative drop 390 | end 391 | end 392 | 393 | 394 | end 395 | 396 | 397 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 398 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 399 | 400 | disp('Delivery ratio- aggregate'); 401 | sum(sum(s))/sum(sum(a)) 402 | 403 | disp('Delivery ratio- linkwise'); 404 | deficit_based_without= sum(s,2)./sum(a,2) 405 | 406 | 407 | disp('Difference of arrival and schedule'); 408 | sum(a,2)-sum(s,2) 409 | disp('Total drop '); 410 | sum(sum(a,2)-sum(s,2)) 411 | disp('Cumulative deficit at the end'); 412 | DD= D(:,T) 413 | disp(sum(D(:,T))) 414 | 415 | disp('Cumulative drop at the end'); 416 | D1(:,T) 417 | sum(D1(:,T)) 418 | 419 | DW(r)= sum(D(:,T)); 420 | figure(r) 421 | plot(1:L, deficit_based_without, '+', 'LineWidth',2); hold on; 422 | % plot(1:L, p, 'LineWidth',2) 423 | 424 | % legend('LDF','Given','LDF Without'); 425 | 426 | xlabel('Links'); 427 | ylabel('Achieved delivery ratio'); 428 | title(['QOS Comparison of LDF+EDF for time 10^' num2str(e(r))]); 429 | grid on; 430 | 431 | %%%% Take care of this end 432 | 433 | end 434 | 435 | % figure(r+1) 436 | % plot(e, DW, 'k+', 'LineWidth',2); hold on; 437 | % plot(e, DW, 'LineWidth',2); hold on; 438 | % 439 | % xlabel('10^e'); 440 | % ylabel('Deficit'); 441 | % title('Deficit Comparison for LDF'); 442 | % grid on; 443 | 444 | 445 | % end 446 | 447 | h = findobj('type','figure'); 448 | n = length(h); 449 | for i=1:n 450 | figure(i); 451 | legend('EDF+LDF','Given','LDF+EDF'); 452 | end 453 | 454 | 455 | figure(n+1); 456 | plot(e,DL, e,DW, 'linewidth',2); grid on; 457 | legend('EDF+LDF','LDF+EDF'); 458 | xlabel('10^e'); 459 | ylabel('Deficit'); 460 | title('Cumulative Deficit Comparison Among Policies'); 461 | 462 | 463 | figure(n+2); 464 | plot(1:L,p,1:L,deficit_based,1:L,deficit_based_without,'linewidth',2); 465 | legend('Given','EDF+LDF','LDF+EDF'); 466 | grid on; 467 | xlabel('Links'); 468 | ylabel('QOS'); 469 | title('Achieved QOS Comparison Among Policies'); 470 | 471 | 472 | 473 | 474 | %%% Saving the figures 475 | 476 | h = findobj('type','figure'); 477 | n = length(h); 478 | mkdir(File_Name); 479 | cd(File_Name); 480 | for i=1:n 481 | savefig(i,num2str(i)); 482 | end 483 | cd .. 484 | 485 | toc; 486 | 487 | 488 | 489 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wireless-Network-Packet-Scheduling-Algorithms 2 | 3 | Real time internet packets are deadline constrained. The packets are weighted to characterize relative importance over one another. The goal is to find an algorithm that maximizes weighted packet transmission before deadline expiry or satisfies quality of service for each link. This work develops discrete time simulation programs for various packet scheduling algorithms. An illustration of packets and deadlines can be seen in this [Excel file](https://github.com/tashrifbillah/Wireless-Network-Packet-Scheduling-Algorithms/blob/master/Traffic%20Pattern%20RLPF.xlsx). 4 | 5 | The model under consideration is interference constrained communication links, could be collocated or ad-hoc. On the other hand, packet arrival process is a Bernoulli random variable, independent from link to link. Please see [the presentation](https://drive.google.com/file/d/1cS8rGfJcWyW1awyca1oilJBFBPH_8e7j/view?usp=sharing) for a comprehensive idea about the project. 6 | 7 | The following algorithms are studied- 8 | 9 | # EDF: Earliest Deadline First 10 | The algorithm prioritizes links having least remaining time packets. If two links have packets with the same least remaining time, then tie is broken giving priority to higher weight links. 11 | 12 | # LDF: Largest Deficit First 13 | Please see [the paper](http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=6923479) for definition of deficit evoloution process. The algorithm schedules packets from the link that has highest deficit. Any possible tie is broken according to class weights. 14 | 15 | Paper: On the Performance of Largest-Deficit-First for Scheduling Real-Time Traffic in Wireless Networks, Xiaohan Kang et al. 16 | 17 | 18 | # CMTO: Continuous Minloss Throughput Optimal 19 | The algorithm looks at the existing packets waiting in the queue. It then finds the schedule that minimizes the loss over the queued packets. Please see [the paper](https://link.springer.com/article/10.1023%2FA%3A1015890105072) for detailed description. 20 | 21 | Paper: Scheduling Multiclass Packet Streams to Minimize Weighted Loss, Robert L. Givan et al. 22 | 23 | # 3/4 Competitive Ratio Algorithm 24 | This algorithm is an improvement over the previous ones. It achieves 75% of the optimal weighted packet transmission. Please see [the paper](http://www.columbia.edu/~js1353/pubs/jlss.pdf) for details. 25 | 26 | Paper: Online Scheduling of Packets with Agreeable Deadlines, Lucasz Jez et al. 27 | 28 | # Code description 29 | Each code is developed in MATLAB. Each of them are standalone, meaning they can execute independently of others. Explanatory comments are made at the beginning of each code. Please follow the comments to understand the purpose of that particular code. 30 | 31 | # Input 32 | Each code requires the following inputs- packet arrival probability (lambda), deadline (tao), and class weight (w). Some sample inputs are already given there for understanding 33 | 34 | # Output 35 | Given the input, the code prints fraction of (weighted) packets transmitted, number of packets transmitted and evolved deficit. 36 | It also plots the fraction as a function of links. Another interesting plot shows the evolution of deficit as a function of time. 37 | 38 | Please see the [plots](https://drive.google.com/drive/folders/1lHjWMucX65sm5jy9EcQiDkIk0ojpWFxz?usp=sharing) we obtained in our experiment. 39 | 40 | 41 | -------------------------------------------------------------------------------- /RMG.m: -------------------------------------------------------------------------------- 1 | % Comparison between CM, and Reduced Cost 2 | % Complete paper approach 3 | 4 | clc; 5 | clear all; 6 | close all; 7 | 8 | tic; 9 | 10 | 'The only correct version' 11 | 12 | % L= 10; 13 | % temp_lam= rand(1,L); 14 | % temp_lam= temp_lam/sum(temp_lam); 15 | % lambda= round(temp_lam,3); 16 | 17 | 18 | % lambda= [ 0.23 0.20 0.16 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 19 | % % lambda= linspace(0.01,1,20); lambda= lambda/sum(lambda); lambda= round(lambda,3); 20 | % lambda= logspace(-2,0,10); lambda= lambda/sum(lambda); lambda= round(lambda,3); 21 | % lambda= [(1-0.9)/9*ones(1,9) 0.9]; 22 | 23 | % A random combination of lambda that does not maintain an increasing or 24 | % decreasing sequence 25 | % lambda= 0.1*ones(1,10); 26 | % lambda= [ 0.20 0.16 0.23 .01 0.14 0.09 0.07 0.04 0.02 0.02 ]; 27 | % lambda= 0.1*ones(1,10); 28 | % lambda= [0.54 0.3 0.15]; 29 | % lambda= [0.4 0.3 0.29]; 30 | % lambda= [0.4 0.3 0.2 0.1]; 31 | % lambda= [0.3 0.25 0.20 0.15 0.1]; 32 | % lambda= 1/5*ones(1,5); 33 | % lambda= [0.3 0.25 0.18 0.13 0.1 0.04]; 34 | % lambda= [0.5 0.4 0.1]; 35 | L= 10; 36 | lambda= ones(1,L)*1/L; 37 | lambda= ones(1,L)*0.9; 38 | % lambda= [ 0.7 0.3 ]; 39 | % lambda= [0.5 0.5]; 40 | % lambda= [0.8 0.8 0.8]; 41 | 42 | 43 | % lambda= [0.414 0.586]; % Two links, gamma_min 44 | % lambda= [ 0.1736 0.4068 0.4196]; % Three links, gamma_min 45 | % lambda= [0.0100 0.1837 0.3994 0.3868 0.0100 0.0100]; % Six links, gamma_min 46 | % lambda= [0.1646 0.3966 0.4188 0.0100 0.0100]; % 5 links, gamma_min 47 | % lambda= [0.1680 0.4014 0.4206 0.0100]; % 4 links, gamma_min 48 | % lambda= [ 0.1573 0.3760 0.3967 0.0100 0.0100 0.0100 0.0100 0.0100 0.0100 0.0100]; % 10 links, gamma_min 49 | % lambda= [0.0100 0.1786 0.3832 0.3681 0.0100 0.0100 0.0100 0.0100 0.0100 0.0100]; % 10 links, gamma_min 50 | % lambda= fliplr(lambda); 51 | % L= length(lambda); 52 | 53 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; % 10 Links 54 | % tao= [ 4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; % 20 Links 55 | % tao= [5 3 4]; % 3 Links 56 | 57 | L= length(lambda); 58 | % temp_weight= rand(1,L); 59 | % w= sort(temp_weight)/sum(temp_weight); % Ascending weight 60 | % w= sort(temp_weight,'descend')/sum(temp_weight); % Descending weight 61 | % w= round(w,4); 62 | 63 | % L= length(lambda); 64 | w= 0.8.^(1:1:L); % 2.^(L:-1:1); 65 | % w= [ 10 7 4.9 3.43 2.40 ]; 66 | % w= [10 8 6.4 5.12 4.1 ]; 67 | % w= [ 10 0.1 0.05 0.03 0.02]; 68 | % w= (L:-1:1); 69 | % w= w/sum(w); 70 | 71 | T= 10^5; 72 | 73 | tao_max= 2; 74 | OUR= [ ]; 75 | PAPER= [ ]; 76 | CM= [ ]; 77 | 78 | for repeat= 1:5 79 | 80 | cost_CM= zeros(tao_max,1); 81 | cost_red= cost_CM; 82 | a= zeros(L,T); 83 | 84 | for j=1:L 85 | a(j,: )= rand(1,T)>(1-lambda(j)); 86 | end 87 | 88 | % disp('Appropriateness of the data'); 89 | % mean(a,2) 90 | % sum(mean(a,2)) 91 | 92 | for tao1= tao_max 93 | 94 | tao1 95 | tao= tao1*ones(1,L); 96 | 97 | %%%%%%%%%%%% CM Policy %%%%%%%%%%%%%%%% 98 | 99 | QOS= [ ]; 100 | sm= zeros(L,T); 101 | chain_res= [ ]; 102 | chain_link= [ ]; 103 | D2= zeros(L,1); 104 | 105 | for t= 1:T 106 | 107 | % Updating chain_link and chain_res on arrival 108 | 109 | if sum(a(:,t)) 110 | 111 | for j= 1:L 112 | 113 | if a(j,t) 114 | 115 | chain_link= sort([chain_link j+0.5]); 116 | place= find(chain_link==(j+0.5)); 117 | chain_res= [chain_res(1:place-1) tao(j) chain_res(place:end)]; 118 | chain_link(place)= j; 119 | 120 | end 121 | end 122 | 123 | end 124 | 125 | 126 | 127 | if sum(chain_res) 128 | 129 | % Finding max weight packets 130 | min_res= min(chain_res); 131 | max_res= max(chain_res); 132 | 133 | sched_res= [ ]; 134 | sched_link= [ ]; 135 | 136 | for i= min_res:max_res 137 | temp_ind= find(chain_res==i); 138 | sched_res= [sched_res chain_res(temp_ind)]; 139 | sched_link= [sched_link chain_link(temp_ind)]; 140 | 141 | temp= sortrows([sched_link;sched_res]'); 142 | sched_link= temp(:,1)'; 143 | sched_res= temp(:,2)'; 144 | 145 | most= min(i,length(sched_link)); 146 | % Drop calculation 147 | for link= sched_link(most+1:end) 148 | D2(link)= D2(link)+1; 149 | end 150 | 151 | % Schedulable set 152 | sched_link= sched_link(1:most); 153 | sched_res= sched_res(1:most); 154 | 155 | 156 | end 157 | 158 | 159 | % Finding packet to schedule 160 | [~,ind]= min(sched_res); 161 | sm(sched_link(ind),t)= 1; 162 | 163 | % Updating chain_link and chain_res 164 | sched_link(ind)= []; 165 | sched_res(ind)= []; 166 | 167 | chain_link= sched_link; 168 | chain_res= sched_res; 169 | 170 | chain_res= chain_res-1; 171 | 172 | end 173 | 174 | 175 | end 176 | 177 | 178 | QOS(:,tao1)= sum(sm,2)./sum(a,2); 179 | % disp('CM Weight transmission'); 180 | CM= [CM sum(QOS(:,2)'.*w.*lambda)/sum(w.*lambda)]; 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | %%%%%%% Our Algorithm %%%%%%%%% 189 | 190 | sm= zeros(L,T); 191 | chain_res= [ ]; 192 | chain_link= [ ]; 193 | D2= zeros(L,1); 194 | QOS= [ ]; 195 | 196 | 197 | for t= 1:T 198 | 199 | % Updating chain_link and chain_res on arrival 200 | 201 | if sum(a(:,t)) 202 | 203 | for j= 1:L 204 | 205 | if a(j,t) 206 | 207 | chain_link= sort([chain_link j+0.5]); 208 | place= find(chain_link==(j+0.5)); 209 | chain_res= [chain_res(1:place-1) tao(j) chain_res(place:end)]; 210 | chain_link(place)= j; 211 | 212 | end 213 | end 214 | 215 | end 216 | 217 | 218 | 219 | if sum(chain_res) 220 | 221 | % Finding max weight packets 222 | min_res= min(chain_res); 223 | max_res= max(chain_res); 224 | 225 | sched_res= [ ]; 226 | sched_link= [ ]; 227 | 228 | for i= min_res:max_res 229 | temp_ind= find(chain_res==i); 230 | sched_res= [sched_res chain_res(temp_ind)]; 231 | sched_link= [sched_link chain_link(temp_ind)]; 232 | 233 | temp= sortrows([sched_link;sched_res]'); 234 | sched_link= temp(:,1)'; 235 | sched_res= temp(:,2)'; 236 | 237 | most= min(i,length(sched_link)); 238 | % Drop calculation 239 | for link= sched_link(most+1:end) 240 | D2(link)= D2(link)+1; 241 | end 242 | 243 | % Schedulable set 244 | sched_link= sched_link(1:most); 245 | sched_res= sched_res(1:most); 246 | 247 | 248 | end 249 | 250 | 251 | 252 | % RMG schedule 253 | % Finding packet to schedule 254 | [~,inde]= min(sched_res); 255 | indh= 1; 256 | 257 | if inde==indh 258 | ind= 1; 259 | else 260 | packet_e= sched_link(inde); 261 | packet_h= sched_link(1); 262 | 263 | p= 0.99; 264 | 265 | if w(inde)/w(indh)<1 && w(inde)/w(indh)>p 266 | 267 | prob= rand>1-p; 268 | % prob= rand>(1-w(packet_e)/w(packet_h)); 269 | 270 | 271 | 272 | if prob 273 | ind= inde; 274 | else 275 | ind= indh; 276 | if sched_res(inde)==1 277 | sched_res(inde)= [ ]; 278 | sched_link(inde)= [ ]; 279 | end 280 | end 281 | 282 | elseif w(inde)/w(indh)
0 283 | 284 | 285 | ind= indh; 286 | if sched_res(inde)==1 287 | sched_res(inde)= [ ]; 288 | sched_link(inde)= [ ]; 289 | end 290 | 291 | end 292 | 293 | end 294 | 295 | sm(sched_link(ind),t)= 1; 296 | 297 | % Updating chain_link and chain_res 298 | sched_link(ind)= []; 299 | sched_res(ind)= []; 300 | 301 | chain_link= sched_link; 302 | chain_res= sched_res; 303 | 304 | 305 | chain_res= chain_res-1; 306 | 307 | end 308 | 309 | end 310 | 311 | QOS(:,tao1)= sum(sm,2)./sum(a,2); 312 | % disp('Our Weight transmission'); 313 | OUR= [OUR sum(QOS(:,2)'.*w.*lambda)/sum(w.*lambda)]; 314 | 315 | 316 | 317 | 318 | 319 | %%%%%%%%%%%% Paper Algorithm %%%%%%%%%%%%%%% 320 | 321 | sm= zeros(L,T); 322 | chain_res= [ ]; 323 | chain_link= [ ]; 324 | D2= zeros(L,1); 325 | QOS= [ ]; 326 | 327 | for t= 1:T 328 | 329 | % Updating chain_link and chain_res on arrival 330 | 331 | if sum(a(:,t)) 332 | 333 | for j= 1:L 334 | 335 | if a(j,t) 336 | 337 | chain_link= sort([chain_link j+0.5]); 338 | place= find(chain_link==(j+0.5)); 339 | chain_res= [chain_res(1:place-1) tao(j) chain_res(place:end)]; 340 | chain_link(place)= j; 341 | 342 | end 343 | end 344 | 345 | end 346 | 347 | 348 | 349 | if sum(chain_res) 350 | 351 | % Finding max weight packets 352 | min_res= min(chain_res); 353 | max_res= max(chain_res); 354 | 355 | sched_res= [ ]; 356 | sched_link= [ ]; 357 | 358 | for i= min_res:max_res 359 | temp_ind= find(chain_res==i); 360 | sched_res= [sched_res chain_res(temp_ind)]; 361 | sched_link= [sched_link chain_link(temp_ind)]; 362 | 363 | temp= sortrows([sched_link;sched_res]'); 364 | sched_link= temp(:,1)'; 365 | sched_res= temp(:,2)'; 366 | 367 | most= min(i,length(sched_link)); 368 | % Drop calculation 369 | for link= sched_link(most+1:end) 370 | D2(link)= D2(link)+1; 371 | end 372 | 373 | % Schedulable set 374 | sched_link= sched_link(1:most); 375 | sched_res= sched_res(1:most); 376 | 377 | 378 | end 379 | 380 | 381 | 382 | % RMG schedule 383 | % Finding packet to schedule 384 | [~,inde]= min(sched_res); 385 | indh= 1; 386 | 387 | if inde==indh 388 | ind= 1; 389 | else 390 | packet_e= sched_link(inde); 391 | packet_h= sched_link(1); 392 | 393 | % prob= rand>0.26; 394 | prob= rand>(1-w(packet_e)/w(packet_h)); 395 | 396 | if prob 397 | ind= inde; 398 | else 399 | ind= indh; 400 | if sched_res(inde)==1 401 | sched_res(inde)= [ ]; 402 | sched_link(inde)= [ ]; 403 | end 404 | end 405 | 406 | end 407 | 408 | sm(sched_link(ind),t)= 1; 409 | 410 | % Updating chain_link and chain_res 411 | sched_link(ind)= []; 412 | sched_res(ind)= []; 413 | 414 | chain_link= sched_link; 415 | chain_res= sched_res; 416 | 417 | 418 | chain_res= chain_res-1; 419 | 420 | end 421 | 422 | end 423 | 424 | QOS(:,tao1)= sum(sm,2)./sum(a,2); 425 | % disp('Paper Weight transmission'); 426 | PAPER= [PAPER sum(QOS(:,2)'.*w.*lambda)/sum(w.*lambda)]; 427 | 428 | 429 | end % tao1 loop ends 430 | 431 | 432 | repeat 433 | 434 | end % Repeat 3 times and average the cost, loop ends 435 | 436 | 437 | N= length(CM); 438 | plot(1:N,CM,'-o', 1:N,OUR,'--^', 1:N, PAPER, '-s', 'linewidth',2); grid on; 439 | legend('CM', 'Probabilistic', 'RMG'); 440 | title('Ration of total packet weight transmission to total packet weight arrival'); 441 | xlabel('Iteration'); ylabel('Gain'); 442 | 443 | 444 | 445 | -------------------------------------------------------------------------------- /SP_EDF_WDF_Comparison.m: -------------------------------------------------------------------------------- 1 | %%%% Multiclass EDF algorithm, it does EDF with class weight based tie braking 2 | %%%% Static priority, it schedules a link with packet in descending order of factor= tao*lambda 3 | %%%% Generates Weighted loss vs Time graph for both the policies 4 | %%%% QOS graph is plotted at 10^6 times slots 5 | 6 | clc; 7 | clear all; 8 | close all; 9 | 10 | tic; 11 | 12 | % File_Name= 'EDF_SP_10_Links_10^6_All_3_Deadlines'; 13 | 14 | % lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 15 | lambda= [ 0.26 0.19 0.15 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 16 | % lambda= [0.4 0.35 0.24]; 17 | % lambda= [0.55 0.3 0.15]; 18 | % lambda= [0.9 0.9 0.9]; 19 | 20 | L= length(lambda); 21 | 22 | % tao= [3*ones(1,3) 1*ones(1,4) 2*ones(1,3)]; 23 | % tao= [ 4*ones(1,3) 1*ones(1,8) 3*ones(1,9)]; 24 | tao= 2*ones(1,L); 25 | % tao= [5 3 4]; 26 | 27 | temp_weight= rand(1,L); 28 | w= sort(temp_weight)/sum(temp_weight); % Ascending lambda, ascending weight 29 | % w= sort(temp_weight,'descend')/sum(temp_weight); % Ascending lambda, descending weight 30 | w= round(w,4); 31 | factor= 100*w.*lambda; 32 | factor= round(factor,4); 33 | [~, link_index]= sort(factor); 34 | static_priority(link_index)= 1:10; 35 | 36 | cost_sp= [ ]; 37 | cost_edf= [ ]; 38 | cost_w_deficit= [ ]; 39 | 40 | e= 3:6; 41 | for r= 1:length(e) 42 | 43 | T= 10^e(r); 44 | a= zeros(L,T); 45 | s= a; 46 | ED= a; 47 | prior= a; 48 | D1= a; 49 | 50 | 51 | for j=1:L 52 | a(j,: )= rand(1,T)>(1-lambda(j)); 53 | end 54 | 55 | disp('Appropriateness of the data'); 56 | mean(a,2) 57 | sum(mean(a,2)) 58 | 59 | 60 | %%%%%%%%% EDF Policy 61 | 62 | % Stack of deadlines 63 | z= zeros(sum(tao),T); 64 | 65 | for t=1:T 66 | 67 | for j= 1:L 68 | 69 | start= sum(tao(1:j-1))+1; 70 | last= sum(tao(1:j-1))+tao(j); 71 | jth_link_index= start:last; 72 | 73 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 74 | z(jth_link_index,t)= [0;temp(1:end-1)]; 75 | 76 | if a(j,t)==1 77 | z(start,t)= tao(j); 78 | end 79 | 80 | if sum(z(jth_link_index,t)) 81 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 82 | end 83 | 84 | end 85 | 86 | 87 | deadlines= ED(:,t); 88 | min_deadline= min(deadlines(deadlines>0)); 89 | if (min_deadline) 90 | prior(find(ED( :,t)== min_deadline),t)= 1; 91 | end 92 | 93 | prior_ind= 0; 94 | if sum(prior( :,t)) 95 | prior_ind= find(prior( :,t)); 96 | end 97 | 98 | 99 | if sum(prior_ind) 100 | temp_chosen= max(w(prior_ind)); 101 | chosen= find(w==temp_chosen); 102 | s(chosen,t)= 1; 103 | 104 | 105 | start= sum(tao(1:chosen-1))+1; 106 | last= sum(tao(1:chosen-1))+tao(chosen); 107 | jth_link_index= start:last; 108 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 109 | end 110 | 111 | 112 | %%%% Drop calculation 113 | D1( :,t)= D1( :,max(1,t-1)); 114 | 115 | for j= 1:L 116 | 117 | start= sum(tao(1:j-1))+1; 118 | last= sum(tao(1:j-1))+tao(j); 119 | jth_link_index= start:last; 120 | 121 | if z(last,t)==1 && s(j,t)==0 122 | D1(j,t)= D1(j,t)+1; 123 | end 124 | 125 | end 126 | 127 | 128 | 129 | end 130 | 131 | 132 | 133 | cost_edf= [cost_edf w*D1(:,T)/T]; 134 | 135 | 136 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 137 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 138 | 139 | disp('Delivery ratio- aggregate'); 140 | sum(sum(s))/sum(sum(a)) 141 | 142 | disp('Delivery ratio- linkwise'); 143 | EDF= sum(s,2)./sum(a,2) 144 | 145 | disp('Difference of arrival and schedule'); 146 | sum(a,2)-sum(s,2) 147 | disp('Total drop '); 148 | sum(sum(a,2)-sum(s,2)) 149 | 150 | disp('Cumulative drop at the end'); 151 | D1(:,T) 152 | sum(D1(:,T)) 153 | 154 | 155 | 156 | 157 | 158 | %%%%%%%% Static priority policy 159 | s= zeros(L,T); 160 | ED= s; 161 | prior= s; 162 | D1= s; 163 | z= zeros(sum(tao),T); 164 | 165 | for t=1:T 166 | 167 | for j= 1:L 168 | 169 | start= sum(tao(1:j-1))+1; 170 | last= sum(tao(1:j-1))+tao(j); 171 | jth_link_index= start:last; 172 | 173 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 174 | z(jth_link_index,t)= [0;temp(1:end-1)]; 175 | 176 | if a(j,t)==1 177 | z(start,t)= tao(j); 178 | end 179 | 180 | if sum(z(jth_link_index,t)) 181 | prior(j,t)= 1; 182 | end 183 | 184 | end 185 | 186 | % Links with packets 187 | prior_ind= 0; 188 | if sum(prior( :,t)) 189 | prior_ind= find(prior( :,t)); 190 | end 191 | 192 | 193 | if sum(prior_ind) 194 | % chosen= datasample(prior_ind,1); 195 | temp_chosen= max(static_priority(prior_ind)); 196 | chosen= find(static_priority==temp_chosen); 197 | s(chosen,t)= 1; 198 | 199 | start= sum(tao(1:chosen-1))+1; 200 | last= sum(tao(1:chosen-1))+tao(chosen); 201 | jth_link_index= start:last; 202 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 203 | end 204 | 205 | 206 | %%%% Drop calculation 207 | D1( :,t)= D1( :,max(1,t-1)); 208 | 209 | for j= 1:L 210 | 211 | start= sum(tao(1:j-1))+1; 212 | last= sum(tao(1:j-1))+tao(j); 213 | jth_link_index= start:last; 214 | 215 | if z(last,t)==1 && s(j,t)==0 216 | D1(j,t)= D1(j,t)+1; 217 | end 218 | 219 | end 220 | 221 | 222 | 223 | end 224 | 225 | cost_sp= [cost_sp w*D1(:,T)/T]; 226 | 227 | 228 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 229 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 230 | 231 | disp('Delivery ratio- aggregate'); 232 | sum(sum(s))/sum(sum(a)) 233 | 234 | disp('Delivery ratio- linkwise'); 235 | SP= sum(s,2)./sum(a,2) 236 | 237 | disp('Difference of arrival and schedule'); 238 | sum(a,2)-sum(s,2) 239 | disp('Total drop '); 240 | sum(sum(a,2)-sum(s,2)) 241 | 242 | disp('Cumulative drop at the end'); 243 | D1(:,T) 244 | sum(D1(:,T)) 245 | 246 | 247 | 248 | 249 | 250 | %%%%%%% Deficit updated according to weight of the links 251 | s= zeros(L,T); 252 | ED= s; 253 | prior= s; 254 | D1= s; 255 | D= s; 256 | z= zeros(sum(tao),T); 257 | A= s; 258 | A(:,1)= a(:,1); 259 | R= s; 260 | 261 | 262 | w= w*1/max(w); 263 | 264 | for t= 1:T 265 | 266 | for j= 1:L 267 | 268 | start= sum(tao(1:j-1))+1; 269 | last= sum(tao(1:j-1))+tao(j); 270 | jth_link_index= start:last; 271 | 272 | temp= max(0,z(jth_link_index,max(1,t-1))-1); 273 | z(jth_link_index,t)= [0;temp(1:end-1)]; 274 | 275 | if a(j,t)==1 276 | z(start,t)= tao(j); 277 | end 278 | 279 | if sum(z(jth_link_index,t)) 280 | ED(j,t)= z(find(z(jth_link_index,t),1,'last')+start-1,t); 281 | end 282 | 283 | end 284 | 285 | 286 | deadlines= ED(:,t); 287 | min_deadline= min(deadlines(deadlines>0)); 288 | if (min_deadline) 289 | prior(find(ED( :,t)== min_deadline),t)= 1; 290 | end 291 | 292 | prior_ind= 0; 293 | if sum(prior( :,t)) 294 | prior_ind= find(prior( :,t)); 295 | end 296 | 297 | 298 | %%%% Deficit queue update method 299 | 300 | D(:,t)= D(:,max(1,t-1))+ a(:,t)*double(rand>1-w(j))'; 301 | 302 | 303 | if sum(prior_ind) 304 | 305 | links_pack= zeros(length(prior_ind),2); 306 | 307 | for k= 1:length(prior_ind) 308 | links_pack(k,: )= [D(prior_ind(k),t), prior_ind(k)]; % Table of [Deficit, Link Index] 309 | end 310 | 311 | maximum_deficit= max(links_pack( :,1)); 312 | max_def_ind= find(links_pack( :,1)==maximum_deficit); % Find the links that have the largest deficit with packets in the input 313 | chosen= links_pack(datasample(max_def_ind,1),2); % links_pack(datasample(def_ind,1),2) is the link chosen to schedule from the above table 314 | s(chosen,t)= 1; 315 | 316 | D(chosen,t)= max(0,D(chosen,t)-1); 317 | 318 | start= sum(tao(1:chosen-1))+1; 319 | last= sum(tao(1:chosen-1))+tao(chosen); 320 | jth_link_index= start:last; 321 | z(find(z(jth_link_index,t),1,'last')+start-1,t)= 0; 322 | 323 | end 324 | 325 | 326 | D1( :,t)= D1( :,max(1,t-1)); 327 | 328 | for j= 1:L 329 | 330 | start= sum(tao(1:j-1))+1; 331 | last= sum(tao(1:j-1))+tao(j); 332 | jth_link_index= start:last; 333 | 334 | if z(last,t)==1 && s(j,t)==0 335 | D1(j,t)= D1(j,t)+1; 336 | end 337 | 338 | end 339 | 340 | 341 | 342 | 343 | if t>1 344 | for j= 1:L 345 | A(j,t)= A(j,t-1)+a(j,t); % Cumulative arrival 346 | R(j,t)= R(j,t-1)+s(j,t); % Cumulative schedule 347 | end 348 | end 349 | 350 | 351 | 352 | end 353 | 354 | cost_w_deficit= [cost_w_deficit w*D1(:,T)/T]; 355 | 356 | 357 | disp(['Total packet arrived ', num2str(sum(sum(a)))]); 358 | disp(['Total packet scheduled ', num2str(sum(sum(s)))]); 359 | 360 | disp('Delivery ratio- aggregate'); 361 | sum(sum(s))/sum(sum(a)) 362 | 363 | disp('Delivery ratio- linkwise'); 364 | WDF= sum(s,2)./sum(a,2) 365 | 366 | disp('Difference of arrival and schedule'); 367 | sum(a,2)-sum(s,2) 368 | disp('Total drop '); 369 | sum(sum(a,2)-sum(s,2)) 370 | 371 | disp('Cumulative drop at the end'); 372 | D1(:,T) 373 | sum(D1(:,T)) 374 | 375 | 376 | 377 | end 378 | 379 | % mu= sum(s,2)/T; 380 | % [mu lambda'] 381 | % mu-lambda' 382 | 383 | 384 | 385 | %% 386 | 387 | figure(1) 388 | plot(e, cost_sp,'-ro', e,cost_edf, '-go', e, cost_w_deficit, '-ko', 'LineWidth',2); hold on; 389 | % ylim([1 500]); 390 | xlabel('10^e'); 391 | ylabel('Cost due to packet drop'); 392 | legend('SP', 'EDF','WDF'); 393 | title('Weighted packet loss Comparison for EDF and SP and WDF'); 394 | grid on; 395 | 396 | figure(2); 397 | plot(1:L, SP, '-ro', 1:L, EDF, '-go', 1:L, WDF, '-ko', 'LineWidth',2); hold on; 398 | legend('SP', 'EDF', 'WDF'); 399 | xlabel('Links'); 400 | ylabel('Achieved delivery ratio'); 401 | title('QOS Comparison of EDF and SP and WDF policy'); 402 | grid on; 403 | 404 | toc; 405 | 406 | % h = findobj('type','figure'); 407 | % n = length(h); 408 | % 409 | % mkdir(File_Name); 410 | % cd(File_Name); 411 | % for i=1:n 412 | % savefig(i,num2str(i)); 413 | % end 414 | % cd .. 415 | 416 | -------------------------------------------------------------------------------- /Traffic Pattern RLPF.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tashrifbillah/Wireless-Network-Packet-Scheduling-Algorithms/f499ad9976e4a3f25b18481c418a73b2b392aa88/Traffic Pattern RLPF.xlsx -------------------------------------------------------------------------------- /num_packet_arrival.m: -------------------------------------------------------------------------------- 1 | % Given some arrival rates, it calculates the probability with which <=L packets arrive to the links 2 | 3 | clc; 4 | clear all; 5 | close all; 6 | 7 | T= 10^5; 8 | % lambda= [0.4 0.3 0.29]; 9 | % lambda= [0.5 0.4 0.1]; 10 | % lambda= [0.4 0.3 0.15 0.09]; 11 | lambda= 0.1*ones(1,10); 12 | % lambda= [ 0.23 0.20 0.16 0.14 0.09 0.07 0.04 0.02 0.02 .01 ]; 13 | % lambda= [ 0.5 ones(1,9)*0.5/9 ]; 14 | % lambda= [0.5 0.5]; 15 | % lambda= [ 0.20 0.16 0.23 .01 ]; 16 | % lambda= [0.1*ones(1,2) 0.09 0.07*ones(1,5) 0.04*ones(1,5) 0.02*ones(1,7)]; 17 | 18 | 19 | % L= 3; % length(lambda); 20 | % lambda= rand(1,L); 21 | % lambda= lambda/sum(lambda); 22 | % lambda= round(lambda,3); 23 | 24 | 25 | L= length(lambda); 26 | a= [ ]; 27 | for j=1:L 28 | a(j,: )= rand(1,T)>(1-lambda(j)); 29 | end 30 | 31 | tao_max= 3; 32 | 33 | soj= 0; 34 | i=1; 35 | while i<= T-tao_max 36 | % for i=1:T-tao_max 37 | 38 | if sum(a(:,i)) 39 | % i 40 | % disp('sum of a(:,i)'); 41 | % sum(a(:,i)) 42 | backup= i+tao_max-1; 43 | 44 | j= i; 45 | while j<=min(backup, T) 46 | % for j= i+1:backup % i+tao_max-1 47 | if sum(a(:,j)) 48 | % j 49 | % disp('sum of a(:,j)'); 50 | % sum(a(:,j)) 51 | backup= j+tao_max-1; 52 | end 53 | 54 | j= j+1; 55 | 56 | end 57 | 58 | soj= soj+(backup-i+1); 59 | % backup 60 | i= backup; 61 | 62 | end 63 | 64 | i= i+1; 65 | 66 | end 67 | 68 | soj 69 | soj/T 70 | 71 | 72 | 73 | num_arrivals= zeros(1,L); 74 | z= [ ]; 75 | for j= 1:L 76 | 77 | z= nchoosek(1:L,j); 78 | 79 | for i= 1:nchoosek(L,j) 80 | arrive= z(i,: ); 81 | noarrive= setdiff(1:L,arrive); 82 | num_arrivals(j)= num_arrivals(j)+ prod(lambda(arrive))*prod(1-lambda(noarrive)); 83 | 84 | end 85 | 86 | 87 | end 88 | 89 | num_arrivals= [prod(1-lambda) num_arrivals]; 90 | 91 | atot= sum(a); 92 | z= zeros(1,L); 93 | for i= 0:L 94 | z(i+1)= length(find(atot==i)); 95 | end 96 | 97 | 98 | 99 | disp(' #of Packets Prob Actual'); 100 | [(0:L)' num_arrivals' z'/T] 101 | 102 | 103 | --------------------------------------------------------------------------------