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 
#include 
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 
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

test_main.cc

#include 
#include 
#include 
#include "bitmap.h"
using namespace std;

int main()
{
    std::default_random_engine generator1(time(0));
    std::default_random_engine generator2;
    std::uniform_real_distribution distribution1(0.0,1.0);
    std::uniform_real_distribution 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;ip;
            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 <

 输出结果:

0.099  0.095

 尝试推导一下:
Gilbert Elliot丢包模型_第1张图片
 图来自于[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 & p \\ r& 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

你可能感兴趣的:(C加加)