Project

General

Profile

Revision 8b911557

ID8b91155720a5cfcd26b44d6e52b64f4de49d345d
Parent 0d3d896c
Child e702d896

Added by Mirko Stoffers about 7 years ago

Various fixes for heuristic

View differences:

include/pdf.h
27 27
				simtime_t rightBorder;
28 28
				size_t bin;
29 29

  
30
				Bin() {}
31

  
32
				Bin(const Bin& b)
33
				{
34
					set(b);
35
				}
36

  
37 30
				bool operator<(const Bin& b)
38 31
				{
39 32
					return this->rightBorder<b.rightBorder;
......
53 46
				{
54 47
					return this->rightBorder>=b.rightBorder;
55 48
				}
56

  
57
				void set(const Bin& b)
58
				{
59
					memcpy(this,&b,sizeof(*this));
60
				}
61 49
		} bin_t;
62 50

  
63 51
		static const size_t size=9; // MUST BE (2^n)+1 for some integer n (like 3,5,9,17,...)
......
65 53
		simtime_t leftBorders[size+1];
66 54
		simtime_t* rightBorders;
67 55
		size_t count;
56
		size_t baseLine;
68 57
		bool isDet;
69 58
		simtime_t detVal;
70 59

  
71 60
		Pdf& convolveInPlaceDetDet(const Pdf& b)
72 61
		{
73 62
			this->detVal+=b.detVal;
63
			this->count*=b.count;
64
			this->baseLine*=b.baseLine;
74 65
			return *this;
75 66
		}
76 67

  
......
79 70
			memcpy(this->bins,b.bins,sizeof(bins));
80 71
			memcpy(this->leftBorders,b.leftBorders,sizeof(leftBorders));
81 72
			rightBorders=&(leftBorders[1]);
82
			this->count=b.count;
73
			this->baseLine*=b.baseLine;
83 74
			this->isDet=false;
75
			bins[0]*=this->count;
84 76
			for(size_t i=1;i<size;i++)
77
			{
85 78
				leftBorders[i]+=this->detVal;
79
				bins[i]*=this->count;
80
			}
86 81
			return *this;
87 82
		}
88 83

  
89 84
		Pdf& convolveInPlaceBinDet(const Pdf& b)
90 85
		{
86
			this->baseLine*=b.baseLine;
87
			bins[0]*=b.count;
91 88
			for(size_t i=1;i<size;i++)
89
			{
92 90
				leftBorders[i]+=b.detVal;
91
				bins[i]*=b.count;
92
			}
93 93
			return *this;
94 94
		}
95 95

  
......
98 98
			const size_t s1=size-1;
99 99
			size_t from=1;
100 100
			size_t to=0;
101
			bin_t matrix[s1*s1][s1][2];
101
			bin_t matrix[size][s1*s1][2];
102 102
			bin_t b1,b2;
103
			size_t total=0;
104 103

  
105 104
			for(size_t i=0;i<s1;i++)
106 105
			{
107 106
				for(size_t j=0;j<s1;j++)
108 107
				{
109 108
					matrix[i][j][to].rightBorder=this->rightBorders[i]+b.rightBorders[j];
110
					matrix[i][j][to].bin=this->bins[i]+b.bins[j];
111
					total+=matrix[i][j][to].bin;
109
					matrix[i][j][to].bin=this->bins[i]*b.bins[j];
112 110
				}
113 111
			}
114 112

  
......
123 121
					size_t l=0;
124 122
					for(size_t j=0;j<m*2*s1;j++)
125 123
					{
126
						if(k<m*s1) b1.set(matrix[i][k][from]);
127
						else b1.rightBorder=MAXTIME;
128
						if(l<m*s1) b2.set(matrix[i+1][l][from]);
129
						else b2.rightBorder=MAXTIME;
124
						if(k<m*s1) b1=matrix[i][k][from];
125
						else
126
						{
127
							b1.bin=(size_t)-1;
128
							b1.rightBorder=MAXTIME;
129
						}
130
						if(l<m*s1) b2=matrix[i+1][l][from];
131
						else
132
						{
133
							b2.bin=(size_t)-1;
134
							b2.rightBorder=MAXTIME;
135
						}
130 136
						if(b1<=b2)
131 137
						{
132
							matrix[i/2][j][to].set(b1);
138
							matrix[i/2][j][to]=b1;
133 139
							k++;
134 140
						}
135 141
						else
136 142
						{
137
							matrix[i/2][j][to].set(b2);
143
							matrix[i/2][j][to]=b2;
138 144
							l++;
139 145
						}
140 146
					}
......
154 160
				this->rightBorders[i]=matrix[0][s1-1+i*s1][to].rightBorder;
155 161
			}
156 162
			this->bins[s1]*=b.bins[s1];
157
			this->count*=b.count;
163
			this->baseLine*=b.baseLine;
158 164
			return *this;
159 165
		}
160 166

  
......
195 201
			simtime_t b=max/2;
196 202
			simtime_t step=(b-a)/(size-1);
197 203
			leftBorders[0]=MINTIME;
204
			bins[0]=0;
198 205
			simtime_t next=a;
199 206
			for(size_t i=1;i<size;i++)
200 207
			{
208
				bins[i]=0;
201 209
				leftBorders[i]=next;
202 210
				next+=step;
203 211
			}
204 212
			rightBorders[size-1]=MAXTIME;
213
			isDet=false;
205 214
			insert(detVal,count);
206 215
			insert(delay);
216
			assert(isValidPdf());
207 217
		}
208 218

  
209 219
	public:
210
		Pdf(simtime_t delay)
220
		Pdf(simtime_t delay,size_t baseLine)
211 221
		{
212 222
			isDet=true;
213 223
			detVal=delay;
214 224
			count=1;
225
			this->baseLine=baseLine;
226
			assert(isValidPdf());
227
		}
228

  
229
		Pdf(const Pdf& other)
230
		{
231
			memcpy(this,&other,sizeof(*this));
232
			rightBorders=&(leftBorders[1]);
215 233
		}
216 234

  
217 235
		Pdf& operator+=(const Pdf& b)
218 236
		{
219 237
			if( this->isDet &&  b.isDet)
220
				return convolveInPlaceDetDet(b);
221
			if( this->isDet && !b.isDet)
222
				return convolveInPlaceDetBin(b);
223
			if(!this->isDet &&  b.isDet)
224
				return convolveInPlaceBinDet(b);
225
			if(!this->isDet && !b.isDet)
226
				return convolveInPlaceBinBin(b);
227
			assert(false);
238
				convolveInPlaceDetDet(b);
239
			else if( this->isDet && !b.isDet)
240
				convolveInPlaceDetBin(b);
241
			else if(!this->isDet &&  b.isDet)
242
				convolveInPlaceBinDet(b);
243
			else if(!this->isDet && !b.isDet)
244
				convolveInPlaceBinBin(b);
245
			else
246
				assert(false);
247
			//print();
248
			assert(isValidPdf());
228 249
			return *this;
229 250
		}
230 251

  
231
		double getProbability(simtime_t val)
252
		double getProbability(simtime_t val) const
232 253
		{
233 254
			if(isDet && detVal< val) return 1;
234 255
			if(isDet && detVal>=val) return 0;
......
237 258
			for(size_t i=0;i<size;i++)
238 259
			{
239 260
				if(leftBorders[i]<val) sum+=bins[i];
240
				else return sum;
261
				else return (double)sum/baseLine;
241 262
			}
242
			return sum;
263
			return (double)sum/baseLine;
243 264
		}
244 265

  
245 266
		void sample(simtime_t delay)
......
253 274
			{
254 275
				insert(delay);
255 276
			}
277
			assert(isValidPdf());
278
		}
279

  
280
		void print() const
281
		{
282
			std::cout << "printing " << this << std::endl;
283
			std::cout << "base line: " << baseLine << std::endl;
284
			if(isDet)
285
			{
286
				std::cout << "Deterministic, value: " << detVal << std::endl;
287
			}
288
			else
289
			{
290
				std::cout << "=======" << std::endl;
291
				std::cout << leftBorders << "," << &(leftBorders[1]) << "," << rightBorders << std::endl;
292
				assert(rightBorders==&(leftBorders[1]));
293
				for(size_t i=0;i<size;i++)
294
					std::cout << "[" << leftBorders[i] << ";" << rightBorders[i] << "]: " << bins[i] << std::endl;
295
				std::cout << "=======" << std::endl;
296
			}
297
		}
298

  
299
		bool isValidPdf() const
300
		{
301
			if(isDet)
302
			{
303
				return count!=0 && count <= baseLine;
304
			}
305
			else
306
			{
307
				size_t sum=0;
308
				for(size_t i=0;i<size;i++)
309
				{
310
					if(leftBorders[i]>rightBorders[i]) return false;
311
					sum+=bins[i];
312
				}
313
				if(sum>baseLine) return false;
314
				else return true;
315
			}
316
		}
317

  
318
		void setBaseLine(size_t newBaseLine)
319
		{
320
			baseLine=newBaseLine;
256 321
		}
257 322
};
258 323

  

Also available in: Unified diff