基于ALOHA MAC方法的蒙特卡罗模拟(Matlab代码实现)

 👨‍🎓个人主页:研学社的博客 

💥💥💞💞欢迎来到本博客❤️❤️💥💥

🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。

⛳️座右铭:行百里者,半于九十。

📋📋📋本文目录如下:🎁🎁🎁

目录

💥1 概述

📚2 运行结果

🎉3 参考文献

🌈4 Matlab代码实现


💥1 概述

本文基于ALOHA MAC方法的蒙特卡罗模拟。

📚2 运行结果

部分代码:

N=60;
Ts=0.0002; %Ts= 1200/6000000= 0.2msec
lamda= 250:200:2050; %[packet/sec] 
lamda2= lamda.*Ts; % lamda2=arrival rate=0.05:0.03:0.32 [packet/slot]
s = zeros(1, length(lamda));
g = zeros(1, length(lamda));
d = zeros(1, length(lamda));

for l=1:1:length(lamda)
    %TIMING PARAMETERS (PART 1)
    num_arrival_per_station = 1000000;
    interarrival_time= exprnd(1/(lamda2(l)), N, num_arrival_per_station); %[slot]
    arrival_schedule= cumsum(interarrival_time,2); %cumsum:Cumulative sumcollapse. B = cumsum(A,dim)
    %this double loop method works but very inefficient
    % arrival_schedule= zeros(N,num_arrival_per_station);
    % for n =1:N
    %     for i = 1:num_arrival_per_station
    %         arrival_schedule(n, i) = sum(interarrival_time(n, 1:i));
    %     end
    % end
    retransmission_schedule= zeros(1,N);
    simulation_time=1000000;
    current_time_slot=1;
    
    %DIFFERENT TRANSMIT INDICATORS (PART 2)
    transmit_status_indicator = zeros(1,N) ; % 0 for not transmitting, 1 for transmitting
    retransmit_status = zeros(1,N) ; % 0 for no retransmission, 1 for pending retransmission
    retransmission_attempt = zeros(1,N) ; %if pkt tx fail once becomes 1 , fail twice becomes 2, and so on ...useful for updating window for retransmission scheduling
    max_retransmission_attempt = 11;
    
    %%STATS PARAMETERS (PART 3)
    total_transmissions = 0;
    total_successful_transmissions = 0 ; %use for throughput computation
    total_delay_time = 0 ; %use for average delay computation
    packet_index=ones(1, N);
    
    while (current_time_slot < simulation_time)
        %%FUNCTION: check_whether_each_station_transmits_in_this_slot (PART 4)
        for j= 1:1:N
            %retransmission_schedule(1,j)
            if (retransmit_status(1,j)==1) %check if status is retransmit
                if (retransmission_schedule(1,j)==current_time_slot) %check if retransmission count down timer arrived
                    transmit_status_indicator(1,j)=1;
                    j_tx=j;
                end
            elseif (all(retransmit_status==0)==1) %no pendiong retranmission
                if (current_time_slot >= arrival_schedule(j,packet_index(1,j))) % there's a new packet
                    transmit_status_indicator(1,j)=1;
                    j_tx=j;
                end
            end
        end
        % Number of stations which transmit in this slot
        num_transmitting_stations = sum(transmit_status_indicator);
        total_transmissions = total_transmissions + num_transmitting_stations;
        %%FUNCTION: CHECK_SUCCESSFUL_OR_FAILED_TRANSMISSION (PART 5)
        if (num_transmitting_stations==1) %if transmission succeed
            total_successful_transmissions = total_successful_transmissions+1;
            % get packet arrival time. Based on difference between transmitted time and original arrival time, we can get this_unique_packet_delay_time
            this_unique_packet_delay_time_after_successful_tx= current_time_slot - arrival_schedule(j_tx, packet_index(1,j_tx));
            total_delay_time = total_delay_time + this_unique_packet_delay_time_after_successful_tx;
            transmit_status_indicator(1,j_tx) = 0; %update tx status
            retransmit_status(1,j_tx) = 0; %update re-tx status
            %update which next packet to be served, do something with arrival_schedule?
            packet_index(1,j_tx)= packet_index(1,j_tx) + 1; %increase packet index at the station has packet successfully transmitted
            retransmission_attempt(1,j_tx)=0;
            retransmission_schedule(1,j_tx)=0;
        else %if transmission fails
            for j=1:1:N
                retransmit_status(1,j) = transmit_status_indicator(1,j);
                if ((retransmit_status(1,j)==1)&&(retransmission_schedule(1,j)<=current_time_slot))
                    %if (retransmit_status(1,j)==1)
                    retransmission_schedule(1,j) = current_time_slot + randi([1, 2^(retransmission_attempt(1,j))+1],1);
                    retransmission_attempt(1,j) = retransmission_attempt(1,j)+1;
                    if (retransmission_attempt(1,j) > max_retransmission_attempt)
                        %discard the packet and serving next packet
                        packet_index(1,j)= packet_index(1,j) + 1;
                        retransmission_attempt(1,j)=0;
                        retransmission_schedule(1,j)=0;
                        retransmit_status(1,j)=0;
                    end
                    transmit_status_indicator(1,j)=0;
                end
            end
        end
        current_time_slot = current_time_slot + 1 ;
    end %belongs to while loop of incrementing simulation time

🎉3 参考文献

部分理论来源于网络,如有侵权请联系删除。

🌈4 Matlab代码实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝科研社

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值