Gilbert Elliot丢包模型

 需要一个网络数据包传输的丢包模型,上代码。

p is the probability of transferring from Good State to the bad state 
and r is the probability of transferring from the bad state to the Good

bitmap.h

#include <stdint.h>
#include <cstdlib>
class BitMap{
public:
    BitMap(int bits);
    ~BitMap();
    void Init(int bits);
    void SetBit(char value);
    double LossRate();
private:
    char *buf_{nullptr};
    int cur_pos_{0};
    int bits_{0};
    int loss_n_{0};
    double lossrate_;
};

bitmap.cc

#include "bitmap.h"
#include <string.h>
BitMap::BitMap(int bits){
    Init(bits);
}
BitMap::~BitMap(){
    if(buf_){
        free(buf_);
    }
}
void BitMap::Init(int bits){
bits_=bits;
cur_pos_=0;
loss_n_=0;
int alloc=(bits_+sizeof(char)-1)/(sizeof(char));
buf_=(char*)malloc(alloc*sizeof(char));
memset(buf_,0,alloc*sizeof(char));
}
void BitMap::SetBit(char value){
    if(cur_pos_>=bits_){
        return;
    }
    int unit=sizeof(char);
    int index=cur_pos_/unit;
    int offset=cur_pos_%unit;
    char origin=buf_[index];
    char flag=0;
    if(value==1){
        flag=1<offset;
        origin+=flag;
        buf_[index]=origin;
    }
    if(value==0){
        loss_n_++;
    }
    cur_pos_++;
}
double BitMap::LossRate(){
    double loss_rate=0.0;
    double num=loss_n_;
    double den=cur_pos_;
    if(cur_pos_){
        loss_rate=num/den;
    }
    return loss_rate;
}

test_main.cc

#include <iostream>
#include <random>
#include <time.h>
#include "bitmap.h"
using namespace std;

int main()
{
    std::default_random_engine generator1(time(0));
    std::default_random_engine generator2;
    std::uniform_real_distribution<double> distribution1(0.0,1.0);
    std::uniform_real_distribution<double> distribution2(0.0,1.0);
    int total_packet=1000;
    double p = 0.02777;
    double r = 0.25;
    int i=0;
    char good=1;
    BitMap bitmap(total_packet);
    for(i=0;i<total_packet;i++){
        if(good==1){
            bitmap.SetBit(good);
            double random=distribution1(generator1);
            bool stay=random>p;
            good=stay;
        }else if(good==0){
            bitmap.SetBit(good);
            double random=distribution2(generator2);
            bool change=random>(1-r);
            good=change;
        }else{
        }
    }
    double theory_loss=1-(r)/(p+r);
    double actual_loss=bitmap.LossRate();
    std::cout <<theory_loss<<" "<<actual_loss<<std::endl;
    return 0;
}

 输出结果:

0.099  0.095

 尝试推导一下:
在这里插入图片描述
 图来自于[6]。其中, r r r是由状态B转移到G的概率, p p p是由状态G转移到B的概率, ( 1 − k ) (1-k) (1k)是在状态G的丢包概率, ( 1 − h ) (1-h) (1h)是在状态B的丢包概率。Gilbert-Elliot丢包模型是两个状态的markov链。markov链的转移矩阵
A = { 1 − p p r 1 − r } A= \left\{ \begin{matrix} 1-p &amp; p \\ r&amp; 1-r \end{matrix} \right\} A={1prp1r}
 当 n ( 0 , 1 ) , r ∈ ( 0 , 1 ) n (0,1) ,r\in (0,1) n(0,1)r(0,1)时,markov链能够达到一个与初始状态无关的平稳分布(可以随便找相关的书翻一下),而在稳定状态,有 π A = π \boldsymbol\pi A=\boldsymbol\pi πA=π,其中 π = ( π G , π B ) \boldsymbol\pi =(\pi_G,\pi_B) π=(πG,πB) π G \pi_G πG是处在good状态的概率, π B \pi_B πB是处在bad状态的概率, π G + π B = 1 \pi_G+\pi_B=1 πG+πB=1
π G ( 1 − p ) + π B r = π G π G p + π B ( 1 − r ) = π B \pi_G(1-p)+\pi_B r=\pi_G\\ \pi_G p+\pi_B(1-r)=\pi_B πG(1p)+πBr=πGπGp+πB(1r)=πB
 求出等式的解:
π G = r r + p π B = p p + r \pi_G=\frac{r}{r+p}\\ \pi_B=\frac{p}{p+r} πG=r+prπB=p+rp
 丢包概率 P E = π G ( 1 − k ) + π B ( 1 − h ) P_E=\pi_G(1-k)+\pi_B(1-h) PE=πG(1k)+πB(1h)。假设在good状态k=1,就是丢包概率是0,在bad状态h=0,全部丢包,就是代码中使用的理论丢包计算公式:
P E = 1 − r / ( p + r ) P_E=1-r/(p+r) PE=1r/(p+r)

[1] Generate Packet Loss Patters using Gilbert Elliot Model https://www.mathworks.com/matlabcentral/fileexchange/38554-generate-packet-loss-patters-using-gilbert-elliot-model
[2] KokoChannel https://github.com/JanValiska/KokoChannel
[3] 基于单向卫星网络的丢包模型及UDP数据抗干扰编码研究
[4] Networked state estimation over a Gilbert-Elliot type channel
[5] Definition of a general and intuitive loss model for packet networks and its implementation in the Netem module in the Linux kernel
[6] The Gilbert-Elliott Model for Packet Loss in Real Time Services on the Internet
[7] 阶层固化中的马尔可夫链 https://zhuanlan.zhihu.com/p/25437964

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值