Posted By

eiger_824 on 10/27/14


Tagged

system matlab SLOTTED ALOHA


Versions (?)

SLOTTED ALOHA SYSTEM


 / Published in: MatLab
 

SLOTTED ALOHA SYSTEM IN MATLAB, INPUTS ARE qr and lambda.

  1. %Student: Santiago Pagola
  2. %LiU-ID & Personal Number: 930302-T092, sanpa993
  3.  
  4. %IMPORTANT NOTE: AN EXTERNAL FUNCTION WAS CREATED IN ORDER TO WRAP THE SUMS
  5. %OF THE VECTORS: a = sumk(vector,k). It will be described in the report,
  6. %but basically sums the first k elements of an array called 'vector'.
  7.  
  8.  
  9. %clear all variables from workspace, close all figures and clear everything
  10. %in command line
  11.  
  12. %--------------------------CONSTANT DEFINITIONS-----------------------------
  13. m = 100; %100 nodes on no-buffering assumption
  14. t = 1000; %Time: 1000 slots of time
  15. slots = 1:t; %Slots array
  16. backlog_array = zeros(size(1:t)); %Array of the values of the system backlog
  17. n = 0; %Initially, all nodes are unbacklogged
  18. packets_leaving = 1:t; %Array which will count #packets leaving the system
  19. packets_arriving = 1:t; %Array which will count #packets entering the system
  20. success = 0; %number of succesful transmissions
  21. ps_simulation = 0; %Simulated probability of success
  22. ps_theory = 0; %Theoretical probability of success
  23. state_probs = zeros(size(1:m)); %Array with the values of the steady state probabilities
  24. states = 1:m; %States array
  25. Att_Rate = 1:m; %Attemp-rate array
  26. G_n = zeros(size(1:m)); %Attempt rate array
  27. Ps = zeros(size(1:m));
  28. %---------------------------------------------------------------------------
  29.  
  30. qr = input('Type retransmission probability, qr: '); %Ask user for qr
  31. lambda = input('Type overal arrival rate: '); %Ask user for overall arrival rate
  32. qa = 1-exp(1)^(-lambda/m); %qa is the probability that each unbacklogged node has to transmit in the next slot
  33.  
  34.  
  35. for i = 1:t
  36. %Let's now calculate the probabilities that up to 10 unbacklogged nodes
  37. %transmit (up to 10 new arrivals)(Same with the probabilities that up to 10 backlogged nodes
  38. %retransmit (up to 10 new arrivals))
  39. Qa = zeros(size(1,11));
  40. Qr = zeros(size(1,11));
  41. for j = 0:10
  42. Qa (j+1) = binopdf(j,m-n,qa);
  43. end
  44. for j = 0:10
  45. Qr (j+1) = binopdf(j,n,qr);
  46. end
  47.  
  48. %a and b will act as two random numbers between 0 and 1, which will be
  49. %two different realizations of Qa_i and Qr_i
  50. a = rand(1);
  51. b = rand(1);
  52.  
  53. %Now, two conditions must be made: one for the case when there are
  54. %backlogged nodes, and other one for the case when there are no
  55. %backlogged nodes:
  56.  
  57. %CASE 1: NO BACKLOGGED NODES
  58. if n == 0
  59. if 0 <= a && a <= sumk(Qa,1) %CASE: NO BACKLOGGED NODES NOR UNBACKLOGGED PACKETS, NO TRANSMISSION->IDLE SLOT
  60. packets_arriving(i)=0;
  61. packets_leaving(i)=0;
  62. elseif sumk(Qa,1) < a && a <= sumk(Qa,2) %CASE: NO BACKLOGGED NODES % 1 UNBACKLOGGED PACKET TRANSMITS SUCCESFULLY
  63. packets_arriving(i)=1;
  64. packets_leaving(i)=1;
  65. success=success+1;
  66. elseif sumk(Qa,2) < a && a <= sumk(Qa,3) %CASE: NO BACKLOGGED NODES & 2 UNBACKLOGGED PACKETS TRANSMIT->COLLISION
  67. n=n+2;
  68. packets_arriving(i)=2;
  69. packets_leaving(i)=0;
  70. elseif sumk(Qa,3) < a && a <= sumk(Qa,4) %COLLISION
  71. n=n+3;
  72. packets_arriving(i)=3;
  73. packets_leaving(i)=0;
  74. elseif sumk(Qa,4) < a && a <= sumk(Qa,5) %COLLISION
  75. n=n+4;
  76. packets_arriving(i)=4;
  77. packets_leaving(i)=0;
  78. elseif sumk(Qa,5) < a && a <= sumk(Qa,6) %AND SO ON...
  79. n=n+5;
  80. packets_arriving(i)=5;
  81. packets_leaving(i)=0;
  82. elseif sumk(Qa,6) < a && a <= sumk(Qa,7)
  83. n=n+6;
  84. packets_arriving(i)=6;
  85. packets_leaving(i)=0;
  86. elseif sumk(Qa,7) < a && a <= sumk(Qa,8)
  87. n=n+7;
  88. packets_arriving(i)=7;
  89. packets_leaving(i)=0;
  90. elseif sumk(Qa,8) < a && a <= sumk(Qa,9)
  91. n=n+8;
  92. packets_arriving(i)=8;
  93. packets_leaving(i)=0;
  94. elseif sumk(Qa,9) < a && a <= sumk(Qa,10)
  95. n=n+9;
  96. packets_arriving(i)=9;
  97. packets_leaving(i)=0;
  98. elseif sumk(Qa,10) < a && a <= 1
  99. n=n+10;
  100. packets_arriving(i)=10;
  101. packets_leaving(i)=0;
  102. end
  103.  
  104. else %CASE 2: BACKLOGGED NODES. INSIDE THIS CASE, THREE OPTIONS:
  105. %2.1.NO RETRANSMISSION AT ALL
  106. %2.2.RETRANSMISSION OF 1 PACKET
  107. %2.3.RETRANSMISSION OF 2 OR MORE PACKETS
  108.  
  109. if 0 <= a && a <= sumk(Qr,1) % CASE 2.1: NO RETRANSMISSION OF ANY BACKLOGGED PACKETS
  110.  
  111. if 0 <= b && b <= sumk(Qa,1) %IDLE SLOT, NO NEW ARRIVALS
  112. packets_arriving(i)=0;
  113. packets_leaving(i)=0;
  114. elseif sumk(Qa,1) < b && b <= sumk(Qa,2) %1 NEW PACKET SUCCESFULLY ARRIVED
  115. packets_arriving(i)=1;
  116. packets_leaving(i)=1;
  117. success=success+1;
  118. elseif sumk(Qa,2) < b && b <= sumk(Qa,3) %2 NEW PACKETS ARRIVED - COLLISION
  119. n=n+2;
  120. packets_arriving(i)=2;
  121. packets_leaving(i)=0;
  122. elseif sumk(Qa,3) < b && b <= sumk(Qa,4) %3 NEW PACKETS ARRIVED - COLLISION
  123. n=n+3;
  124. packets_arriving(i)=3;
  125. packets_leaving(i)=0;
  126. elseif sumk(Qa,4) < b && b <= sumk(Qa,5) %AND SO ON...
  127. n=n+4;
  128. packets_arriving(i)=4;
  129. packets_leaving(i)=0;
  130. elseif sumk(Qa,5) < b && b <= sumk(Qa,6)
  131. n=n+5;
  132. packets_arriving(i)=5;
  133. packets_leaving(i)=0;
  134. elseif sumk(Qa,6) < b && b <= sumk(Qa,7)
  135. n=n+6;
  136. packets_arriving(i)=6;
  137. packets_leaving(i)=0;
  138. elseif sumk(Qa,7) < b && b <= sumk(Qa,8)
  139. n=n+7;
  140. packets_arriving(i)=7;
  141. packets_leaving(i)=0;
  142. elseif sumk(Qa,8) < b && b <= sumk(Qa,9)
  143. n=n+8;
  144. packets_arriving(i)=8;
  145. packets_leaving(i)=0;
  146. elseif sumk(Qa,9) < b && b <= sumk(Qa,10)
  147. n=n+9;
  148. packets_arriving(i)=9;
  149. packets_leaving(i)=0;
  150. elseif sumk(Qa,10) < b && b <= 1
  151. n=n+10;
  152. packets_arriving(i)=10;
  153. packets_leaving(i)=0;
  154. end
  155.  
  156.  
  157. elseif sumk(Qr,1) < a && a <= sumk(Qr,2) % CASE 2.2: RETRANSMISSION OF A SINGLE PACKET
  158.  
  159. if 0 <= b && b <= sumk(Qa,1) %CASE: SUCCESFUL BACKLOG
  160. n=n-1; %n DECREASES 1 UNIT
  161. packets_arriving(i)=1;
  162. packets_leaving(i)=1;
  163. success=success+1;
  164. elseif sumk(Qa,1) < b && b <= sumk(Qa,2) %CASE: COLLISION->1 NEW ARRIVAL, 1 BACKLOG
  165. n=n+1;
  166. packets_arriving(i)=2;
  167. packets_leaving(i)=0;
  168. elseif sumk(Qa,2) < b && b <= sumk(Qa,3) %CASE: COLLISION->2 NEW ARRIVALS, 1 BACKLOG
  169. n=n+2;
  170. packets_arriving(i)=3;
  171. packets_leaving(i)=0;
  172. elseif sumk(Qa,3) < b && b <= sumk(Qa,4) %AND SO ON...
  173. n=n+3;
  174. packets_arriving(i)=4;
  175. packets_leaving(i)=0;
  176. elseif sumk(Qa,4) < b && b <= sumk(Qa,5)
  177. n=n+4;
  178. packets_arriving(i)=5;
  179. packets_leaving(i)=0;
  180. elseif sumk(Qa,5) < b && b <= sumk(Qa,6)
  181. n=n+5;
  182. packets_arriving(i)=6;
  183. packets_leaving(i)=0;
  184. elseif sumk(Qa,6) < b && b <= sumk(Qa,7)
  185. n=n+6;
  186. packets_arriving(i)=7;
  187. packets_leaving(i)=0;
  188. elseif sumk(Qa,7) < b && b <= sumk(Qa,8)
  189. n=n+7;
  190. packets_arriving(i)=8;
  191. packets_leaving(i)=0;
  192. elseif sumk(Qa,8) < b && b <= sumk(Qa,9)
  193. n=n+8;
  194. packets_arriving(i)=9;
  195. packets_leaving(i)=0;
  196. elseif sumk(Qa,9) < b && b <= sumk(Qa,10)
  197. n=n+9;
  198. packets_arriving(i)=10;
  199. packets_leaving(i)=0;
  200. elseif sumk(Qa,10) < b && b <= sumk(Qa,11)
  201. n=n+10;
  202. packets_arriving(i)=11;
  203. packets_leaving(i)=0;
  204. end
  205.  
  206.  
  207. elseif sumk(Qr,2) < a && a <= 1 %CASE 2.3: RETRANSMISSION OF 2 OR MORE PACKETS
  208.  
  209. if sumk(Qr,2) < a && a <= sumk(Qr,3) %Let aux be an auxiliary variable indicating the number of backlogged nodes.
  210. aux=2;
  211. elseif sumk(Qr,3) < a && a <= sumk(Qr,4)
  212. aux=3;
  213. elseif sumk(Qr,4) < a && a <= sumk(Qr,5)
  214. aux=4;
  215. elseif sumk(Qr,5) < a && a <= sumk(Qr,6)
  216. aux=5;
  217. elseif sumk(Qr,6) < a && a <= sumk(Qr,7)
  218. aux=6;
  219. elseif sumk(Qr,7) < a && a <= sumk(Qr,8)
  220. aux=7;
  221. elseif sumk(Qr,8) < a && a <= sumk(Qr,9)
  222. aux=8;
  223. elseif sumk(Qr,9) < a && a <= sumk(Qr,10)
  224. aux=9;
  225. end
  226.  
  227. if 0 <= b && b <= sumk(Qa,1) % 0 ARRIVALS->BACKLOG REMAINS THE SAME BEACUSE NO UNBACKLOGGED NODES TRANSMIT
  228. packets_arriving(i)=aux;
  229. packets_leaving(i)=0;
  230. elseif sumk(Qa,1) < b && b <= sumk(Qa,2) %BACKLOG WILL INCREASE 1 BECAUSE THERE IS COLLISION
  231. n=n+1;
  232. packets_arriving(i)=aux+1;
  233. packets_leaving(i)=0;
  234. elseif sumk(Qa,2) < b && b <= sumk(Qa,3) %2 ARRIVALS, BACKLOG INCREASES TOO BECAUSE OF COLLISION
  235. n=n+2;
  236. packets_arriving(i)=aux+2;
  237. packets_leaving(i)=0;
  238. elseif sumk(Qa,3) < b && b <= sumk(Qa,4) %AND SO ON...
  239. n=n+3;
  240. packets_arriving(i)=aux+3;
  241. packets_leaving(i)=0;
  242. elseif sumk(Qa,4) < b && b <= sumk(Qa,5)
  243. n=n+4;
  244. packets_arriving(i)=aux+4;
  245. packets_leaving(i)=0;
  246. elseif sumk(Qa,5) < b && b <= sumk(Qa,6)
  247. n=n+5;
  248. packets_arriving(i)=aux+5;
  249. packets_leaving(i)=0;
  250. elseif sumk(Qa,6) < b && b <= sumk(Qa,7)
  251. n=n+6;
  252. packets_arriving(i)=aux+6;
  253. packets_leaving(i)=0;
  254. elseif sumk(Qa,7) < b && b <= sumk(Qa,8)
  255. n=n+7;
  256. packets_arriving(i)=aux+7;
  257. packets_leaving(i)=0;
  258. elseif sumk(Qa,8) < b && b <= sumk(Qa,9)
  259. n=n+8;
  260. packets_arriving(i)=aux+8;
  261. packets_leaving(i)=0;
  262. elseif sumk(Qa,9) < b && b <= sumk(Qa,10)
  263. n=n+9;
  264. packets_arriving(i)=aux+9;
  265. packets_leaving(i)=0;
  266. elseif sumk(Qa,10) < b && b <= 1
  267. n=n+10;
  268. packets_arriving(i)=aux+10;
  269. packets_leaving(i)=0;
  270. end
  271. end
  272. end
  273. backlog_array(i) = n; %Fill the array of the system's backlog
  274. end
  275.  
  276. figure(1) %Setting up the plotting environment for the backlog of the system
  277. plot(slots,backlog_array)
  278. xlabel('Slot number')
  279. ylabel('Backlog of the system')
  280. title('Backlog of the system vs. Slot number')
  281.  
  282. figure(2) %Setting up the plotting environment for the packets of the system
  283. plot(slots,packets_arriving)
  284. hold on
  285. plot(slots,packets_leaving,'k')
  286. grid on
  287. xlabel('Slot number')
  288. ylabel('Number of packets')
  289. title('Number of packets entering (blue) or leaving (black) the system vs. Slot number')
  290.  
  291. figure(3) %Setting up the plotting environment for the histogram of the states
  292. max = max(backlog_array);
  293. hist(backlog_array,max) %array with the counts of the times in each state
  294. nelements = hist(backlog_array,100); %In each bin, it counts how many times element i was seen ("numerical plot of the hist function")
  295. for i = 1:m
  296. state_probs(i) = nelements(i)/1000;
  297. end
  298. xlabel('Number of state (n)')
  299. ylabel('Frequency of each state')
  300. title('Frequency of each state n')
  301.  
  302. %Calculation of the simulated probability of success:
  303. ps_simulation = success/t;
  304. %Calculation of the theoretical probability of success:
  305. for i = 1:m
  306. aux = (binopdf(1,m-(i-1),qa)*binopdf(0,(i-1),qr))+(binopdf(0,m-(i-1),qa)*binopdf(1,i-1,qr));
  307. aux2 = aux * state_probs(i);
  308. ps_theory = ps_theory + aux2;
  309. end
  310.  
  311. %Attempt rate plot (theoretical value):
  312. for n = 1:m
  313. Att_Rate(n) = (m-(n-1))*qa + (n-1)*qr;
  314. end
  315. plot(states,Att_Rate,'r')
  316. axis([0 100 0 1])
  317. xlabel('State, n')
  318. ylabel('Attemp rate, G(n)')
  319. title('Attempt rate as a function of the current state n')
  320.  
  321. disp(['Simulated Probability of Success: ' num2str(ps_simulation) '.'])
  322. disp(['Theoretical Probability of Success: ' num2str(ps_theory) '.'])

Report this snippet  

You need to login to post a comment.