Alex Bikfalvi
SimStream Documentation
ModelSelectProbRange.cpp
00001 #include "Headers.h" 00002 #include "ModelSelectProbRange.h" 00003 #include "Rand.h" 00004 00005 CModelSelectProbRange::CModelSelectProbRange( 00006 unsigned int numChannels, 00007 unsigned int numLayers, 00008 unsigned int numPeers, 00009 CProb* prob, 00010 double delta 00011 ) : CModelSelect(numChannels, numLayers, numPeers) 00012 { 00013 this->prob = prob; 00014 this->delta = 1 - delta; 00015 00016 // Allocate candidate peers 00017 this->candidates = new SCandidate[this->numPeers]; 00018 } 00019 00020 CModelSelectProbRange::~CModelSelectProbRange() 00021 { 00022 delete[] this->candidates; 00023 } 00024 00025 CPeer** CModelSelectProbRange::Select(__time time, __uint32 channel, __uint32 child) 00026 { 00027 assert(channel < this->numChannels); 00028 assert(child < this->numPeers); 00029 00030 CPeer* peerChild = &this->peers[child]; 00031 00032 for(__uint32 layer = 0; layer < this->numLayers; layer++) 00033 { 00034 double maxProb = 0; 00035 double minProb; 00036 unsigned int countCandidates = 0; 00037 unsigned int countRange = 0; 00038 00039 for(set<CPeer*>::iterator iter = this->index[channel].begin(); iter != this->index[channel].end(); iter++) 00040 { 00041 if(!this->IsResult(*iter, layer)) 00042 { 00043 if(peerChild->VerifyParent(channel, layer, *iter)) 00044 { 00045 // Calculating the maximum probability 00046 double prob = this->prob->Prob((*iter)->Elapsed(time, channel), peerChild->Elapsed(time, channel)); 00047 if(maxProb < prob) maxProb = prob; 00048 00049 // Build the list of candidate peers meeting bandwidth and parent constraints 00050 this->candidates[countCandidates].peer = *iter; 00051 this->candidates[countCandidates++].prob = prob; 00052 } 00053 } 00054 } 00055 00056 // Calculate minimum probability 00057 minProb = maxProb * this->delta; 00058 00059 // Select the candidates with a probability in the range: maxProb * delta ... maxProb 00060 for(unsigned int index = 0; index < countCandidates; index++) 00061 { 00062 if(this->candidates[index].prob >= minProb) 00063 { 00064 this->candidates[countRange++] = this->candidates[index]; 00065 } 00066 } 00067 00068 // Select a random peer 00069 this->result[layer] = countRange ? this->candidates[CRand::Generate(countRange)].peer : NULL; 00070 } 00071 00072 return this->result; 00073 } 00074 00075 CPeer* CModelSelectProbRange::Select(__time time, __uint32 channel, __uint32 layer, __uint32 child) 00076 { 00077 assert(channel < this->numChannels); 00078 assert(child < this->numPeers); 00079 00080 CPeer* peerChild = &this->peers[child]; 00081 00082 double maxProb = 0; 00083 double minProb; 00084 unsigned int countCandidates = 0; 00085 unsigned int countRange = 0; 00086 00087 for(set<CPeer*>::iterator iter = this->index[channel].begin(); iter != this->index[channel].end(); iter++) 00088 { 00089 if(!this->IsResult(*iter, layer)) 00090 { 00091 if(peerChild->VerifyParent(channel, layer, *iter)) 00092 { 00093 // Calculating the maximum probability 00094 double prob = this->prob->Prob((*iter)->Elapsed(time, channel), peerChild->Elapsed(time, channel)); 00095 if(maxProb < prob) maxProb = prob; 00096 00097 // Build the list of candidate peers meeting bandwidth and parent constraints 00098 this->candidates[countCandidates].peer = *iter; 00099 this->candidates[countCandidates++].prob = prob; 00100 } 00101 } 00102 } 00103 00104 // Calculate minimum probability 00105 minProb = maxProb * this->delta; 00106 00107 // Select the candidates with a probability in the range: maxProb * delta ... maxProb 00108 for(unsigned int index = 0; index < countCandidates; index++) 00109 { 00110 if(this->candidates[index].prob >= minProb) 00111 { 00112 this->candidates[countRange++] = this->candidates[index]; 00113 } 00114 } 00115 00116 // Select a random peer 00117 return countRange ? this->candidates[CRand::Generate(countRange)].peer : NULL; 00118 }
Last updated: February 8, 2011