Alex Bikfalvi
SimStream Documentation
ModelChannelSurf2Cat.cpp
00001 #include "Headers.h" 00002 #include "ModelChannelSurf2Cat.h" 00003 #include "Rand.h" 00004 00005 CModelChannelSurf2Cat::CModelChannelSurf2Cat( 00006 __uint32 numChannels, 00007 __uint32 numPopular, 00008 double probSurfing, 00009 double probPopular, 00010 double probSurfForward, 00011 double probSurfCycle 00012 ) : CModelChannelData(numChannels) 00013 { 00014 this->numPopular = numPopular; 00015 this->probSurfing = (__uint32)(0xFFFFFFFF * probSurfing); 00016 this->probPopular = (__uint32)(0xFFFFFFFF * probPopular); 00017 this->probSurfForward = (__uint32)(0xFFFFFFFF * probSurfForward); 00018 this->probSurfCycle = (__uint32)(0xFFFFFFFF * probSurfCycle); 00019 00020 // Calculate the surfing threshold - the smallest duration for which the probability is greater or equal to the surfing probability 00021 if(0 == this->probSurfing) 00022 this->surfThreshold = 0; 00023 else 00024 { 00025 for(__uint32 index = 0; index < CDF_POINTS; index++) 00026 if(channelCdf[index][1] >= this->probSurfing) 00027 { 00028 this->surfThreshold = channelCdf[index][0]; 00029 index = CDF_POINTS; 00030 } 00031 } 00032 } 00033 00034 void CModelChannelSurf2Cat::GenerateUptime(__uint32 &newCh, __time &duration) 00035 { 00036 __uint32 pr = CRand::GenerateUInt32(); 00037 __uint32 ch = CRand::GenerateUInt32(); 00038 __uint32 dindex; 00039 00040 //#ifndef DETRAND 00041 // rand_s(&pr); 00042 // rand_s(&ch); 00043 //#else 00044 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00045 // ch = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00046 //#endif 00047 00048 dindex = channelCdfIndex[pr >> 15]; 00049 duration = channelCdf[dindex][0]; 00050 00051 pr = CRand::GenerateUInt32(); 00052 //#ifndef DETRAND 00053 // rand_s(&pr); 00054 //#else 00055 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00056 //#endif 00057 00058 // Generate new channel (a random channel) 00059 if(pr < this->probPopular) 00060 newCh = (__uint32)(((__uint64)ch)*this->numPopular/0x100000000LL); 00061 else 00062 newCh = this->numPopular + (__uint32)(((__uint64)ch)*(this->numChannels-this->numPopular-1)/0x100000000LL); 00063 } 00064 00065 void CModelChannelSurf2Cat::GenerateUptime(__uint32 oldCh, __time oldDuration, __uint32 &newCh, __time &duration) 00066 { 00067 __uint32 pr = CRand::GenerateUInt32(); 00068 __uint32 prb = CRand::GenerateUInt32(); 00069 __uint32 ch = CRand::GenerateUInt32(); 00070 __uint32 dindex; 00071 00072 //#ifndef DETRAND 00073 // rand_s(&pr); 00074 //#else 00075 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00076 //#endif 00077 00078 dindex = channelCdfIndex[pr >> 15]; 00079 duration = channelCdf[dindex][0]; 00080 00081 pr = CRand::GenerateUInt32(); 00082 //#ifndef DETRAND 00083 // rand_s(&ch); 00084 // rand_s(&pr); 00085 // rand_s(&prb); 00086 //#else 00087 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00088 // ch = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00089 // prb = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00090 //#endif 00091 00092 if(oldDuration < this->surfThreshold) 00093 { 00094 // Surfing 00095 00096 // Check if surfing forward or backward 00097 if(pr < this->probSurfForward) 00098 { 00099 // Surfing forward 00100 // Check if old channel is M-1 or N-1 00101 if(oldCh == this->numPopular-1) 00102 { 00103 if(prb < this->probSurfCycle) newCh = 0; // Cycle surfing 00104 else newCh = this->numPopular; // Normal surfing 00105 } 00106 else if(oldCh == this->numChannels-1) 00107 { 00108 if(prb < this->probSurfCycle) newCh = this->numPopular; // Cycle surfing 00109 else newCh = 0; // Normal surfing 00110 } 00111 else 00112 newCh = oldCh + 1; 00113 } 00114 else 00115 { 00116 // Surfing backward 00117 // Check is old channel is 0 or M 00118 if(oldCh == 0) 00119 { 00120 if(prb < this->probSurfCycle) newCh = this->numPopular-1; // Cycle surfing 00121 else newCh = this->numChannels-1; // Normal surfing 00122 } 00123 else if(oldCh == this->numPopular) 00124 { 00125 if(prb < this->probSurfCycle) newCh = this->numChannels-1; // Cycle surfing 00126 else newCh = this->numPopular-1; // Normal surfing 00127 } 00128 else 00129 newCh = oldCh - 1; 00130 } 00131 } 00132 else 00133 { 00134 // Switch 00135 if(pr < this->probPopular) 00136 { 00137 // Switch to popular channel 00138 if(oldCh < this->numPopular) 00139 { 00140 newCh = (__uint32)(((__uint64)ch)*(this->numPopular-1)/0x100000000LL); 00141 if(newCh >= oldCh) newCh++; 00142 } 00143 else 00144 newCh = (__uint32)(((__uint64)ch)*this->numPopular/0x100000000LL); 00145 } 00146 else 00147 { 00148 // Switch to unpopular channel 00149 if(oldCh < this->numPopular) 00150 newCh = this->numPopular + (__uint32)(((__uint64)ch)*(this->numChannels-this->numPopular)/0x100000000LL); 00151 else 00152 { 00153 newCh = this->numPopular + (__uint32)(((__uint64)ch)*(this->numChannels-this->numPopular-1)/0x100000000LL); 00154 if(newCh >= oldCh) newCh++; 00155 } 00156 } 00157 } 00158 assert(newCh != oldCh); 00159 assert(newCh < this->numChannels); 00160 } 00161 00162 __uint32 CModelChannelSurf2Cat::Cdf(__uint32 dindex) 00163 { 00164 return channelCdf[dindex][0]; 00165 }
Last updated: February 8, 2011