summaryrefslogtreecommitdiffstats
path: root/DataStructure/Interpolator.java
blob: 3869255da69f64a6308fa822aad5afa3f56931e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package DataStructure;

import helper.ListBTS;

import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Date;

import voronoi.voronoi;
import Parse.sqlreader;

public class Interpolator extends GSMMap {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1388761837460629751L;
	public ArrayList<SingleBTS>[][] buffer;

	public Interpolator(sqlreader SQL, double accuracy) {
		super(SQL, accuracy);
		// initialize buffer
		buffer = new ArrayList[this.Xcoords.length][this.Ycoords.length];
		for (int x = 0; x < Xcoords.length; x++) {
			for (int y = 0; y < Ycoords.length; y++) {
				buffer[x][y] = new ArrayList<SingleBTS>();
			}
		}

	}

	/**
	 * Interpolates every Point on the map,every arfcn
	 */
	public void interpolateVR() {
		// get starting time
		long start = new Date().getTime();

		// average first!
		// average();
		// removeOutlier();
		SingleBTS[] content = content();
		int success = 0;
		int failure = 0;
		int number = Xcoords.length * Ycoords.length * content.length;
		// traverse each bts
		for (int i = 0; i < content.length; i++) {
			// for (int i = 2; i < 3; i++) {
			// initialize voronoi with current arfcn
			voronoi primary = new voronoi();
			primary.sortNode(this, content[i].ARFCN, 1);
			primary.generateVoronoi(0, Xcoords.length * 3.5, 0,
					Ycoords.length * 3.5);
			// now, traverse each point. Check if interpolation is needed!
			// loop is a little tricky because of threats!
			for (int x = 0; x < Xcoords.length; x++) {
				for (int y = 0; y < Ycoords.length; y++) {
					// this is a crucial point for parallelism. Change this if
					// statement
					// if (ListBTS.contains(map[x][y], content[i])) {
					// this coordinates already contains this bts
					// continue;
					// }
					// System.out.print("Interpoliere bei:" + x + "," + y
					// + ", arfcn " + content[i].ARFCN);

					// make it parallel?
					interpolateThread it1 = new interpolateThread(x, y,
							content[i], primary);
					Thread it1T = new Thread(it1);
					try {
						if (!ListBTS.contains(map[x][y], content[i])) {
							it1T.start();
							// success++;
						}
					} catch (Exception e) {
						failure++;
					}

					y++;

					interpolateThread it2 = new interpolateThread(x, y,
							content[i], primary);
					Thread it2T = new Thread(it2);
					try {
						if (checkBounds(x, y)
								&& !ListBTS.contains(map[x][y], content[i])) {
							it2T.start();
							// success++;
						}
					} catch (Exception e) {
						failure++;
					}

					y++;

					interpolateThread it3 = new interpolateThread(x, y,
							content[i], primary);
					Thread it3T = new Thread(it3);
					try {
						if (checkBounds(x, y)
								&& !ListBTS.contains(map[x][y], content[i])) {
							it3T.start();
							// success++;
						}
					} catch (Exception e) {
						failure++;
					}

					y++;
					interpolateThread it4 = new interpolateThread(x, y,
							content[i], primary);

					Thread it4T = new Thread(it4);
					try {
						if (checkBounds(x, y)
								&& !ListBTS.contains(map[x][y], content[i])) {
							it4T.start();
							// success++;
						}
					} catch (Exception e) {
						failure++;
					}

					// y++;
					// interpolateThread it5 = new interpolateThread(x, y,
					// content[i], primary);

					// Thread it5T = new Thread(it5);
					// try {
					// if (checkBounds(x, y)
					// && !ListBTS.contains(map[x][y], content[i])) {
					// it5T.start();
					// success++;
					// }
					// } catch (Exception e) {
					// failure++;
					// }

					// success += 5;
					success += 4;
					if (success % 200 == 0) {
						// calc ETA
						long difference = new Date().getTime() - start;
						int resttime = (int) (difference / success * (number - success));
						resttime = (resttime / 1000) / 60;
						System.out.println("Total: " + number + " current: "
								+ success + " rest: " + resttime
								+ " min; current ARFCN: " + content[i].ARFCN
								+ ", Index:" + i + "/" + content.length);

					}

					try {
						it1T.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						it2T.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						it3T.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						it4T.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// try {
					// it5T.join();
					// } catch (InterruptedException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
					// }

					// interpolateAT(x, y, content[i], primary);
					// //singleThread

					// } catch (Exception e) {
					// System.out.println("Fehler bei:" + x + "," + y);
					// failure++;
					// }

				}
			}
		}
		mergebuffer();
		System.out.println("Fertig!");
	}

	private boolean checkBounds(int x, int y) {
		return (x < Xcoords.length && y < Ycoords.length);
	}

	/**
	 * Takes map[x][y]. Inserts an interpolated BTS on this point if neccessary
	 * 
	 * @param x
	 * @param y
	 * @param arfcn
	 * @param primary
	 *            original Voronoi without the current Interpolationpoint
	 */
	private void interpolateAT(int x, int y, SingleBTS interpolateThis,
			voronoi primary) {
		if (!ListBTS.contains(map[x][y], interpolateThis)) {
			// do it!

			voronoi secondary = new voronoi();
			secondary.sortNode((GSMMap) this, interpolateThis.ARFCN, 1, x, y);
			secondary.generateVoronoi(0, 4000, 0, 4000);

			// get area of polygon for current x,y
			// double area = secondary.areaOf(x, y);
			Path2D region = secondary.getPoly(x, y);
			double area = secondary.area(region);
			if (region == null) {
				return;
			}
			// get Neighbors of x,y
			ArrayList<Point2D> neighbors = secondary.getDirectNeighbors(x, y);

			// calculate intersection of voronoi region for each neighbor
			ArrayList<Weight> weights = new ArrayList<Weight>(neighbors.size());
			for (int i = 0; i < neighbors.size(); i++) {
				int neighX = (int) neighbors.get(i).getX();
				int neighY = (int) neighbors.get(i).getY();
				// double area2 = primary.areaOf(currentx, currenty);

				Path2D underling = primary.getPoly(neighX, neighY);
				if (underling == null) {
					// one of the neighbors doesn't have a vornoi region.
					// this happens at the bounds of the gsm map:a infinit big
					// polygon
					continue;
					// TODO: das continue könnte zu problemen führen, weil dann
					// auch kein weights element an dieser Stelle eingefügt wird
					// und das Array somit an dieser Stelle auch null ist
					// TODO: warum ist temporary manchmal null?!!!!!!!
				}
				// intersection between both voronoi regions
				double intersectA = primary.intersectArea(region, underling);
				Weight new_element = new Weight();
				new_element.weight = intersectA / area;
				new_element.bts = ListBTS.getARFCN(map[neighX][neighY],
						interpolateThis);
				weights.add(new_element);
			}

			// now, traverse weights, get sum of weights (to check if it equals
			// one)
			double newDL = 0;
			double mode = 0;
			double var = 0;
			double DlQualsub = 0;
			// double DlQualFull = 0;
			double UlQualsub = 0;
			// double UlQualFull = 0;
			double sum_of_weights = 0;
			for (int i = 0; i < weights.size(); i++) {
				if (weights.get(i) == null)
					continue;
				sum_of_weights += weights.get(i).weight;
			}

			// calculate with weights and normalize
			for (int i = 0; i < weights.size(); i++) {
				if (weights.get(i) == null)
					continue;
				// HACK: normalize weights
				weights.get(i).weight = weights.get(i).weight / sum_of_weights;
				// calulate
				newDL = newDL + weights.get(i).weight
						* weights.get(i).bts.getDLmW();
				mode = mode + weights.get(i).weight
						* weights.get(i).bts.getStrictDLdBAverage();
				var = var + weights.get(i).weight
						* weights.get(i).bts.getTrueVarianceDLdB();
				DlQualsub = DlQualsub + weights.get(i).weight
						* weights.get(i).bts.getDLQsub();
				// DlQualFull = DlQualFull + weights.get(i).weight
				// * weights.get(i).bts.getDLQfull();
				UlQualsub = UlQualsub + weights.get(i).weight
						* weights.get(i).bts.getULQsub();
				// UlQualFull = UlQualFull + weights.get(i).weight
				// * weights.get(i).bts.getULQfull();

				// weights.get(i).bts.
			}

			// map[x][y] doesn't contain the just interpolated bts
			SingleBTS temp = new SingleBTS(interpolateThis.ARFCN,
					interpolateThis.name + "I");
			temp.addDl(newDL);
			temp.dlQsub.add(DlQualsub);
			// temp.dlQfull.add(DlQualFull);
			temp.ulQsub.add(UlQualsub);
			// temp.ulQfull.add(UlQualFull);
			temp.interpolated.add(true);
			temp.setVarDLdB(var);
			temp.mode = mode;
			buffer[x][y].add(temp);

		}

	}

	private void mergebuffer() {
		for (int x = 0; x < Xcoords.length; x++) {
			for (int y = 0; y < Ycoords.length; y++) {
				if (map[x][y].isEmpty()) {
					map[x][y] = buffer[x][y];
				} else {
					mergeSingleBTS(x, y);
				}
			}
		}
	}

	/**
	 * If map[x][y] contains measurements, check if some BTS are missing and
	 * merge with buffer
	 * 
	 * @param x
	 * @param y
	 */
	private void mergeSingleBTS(int x, int y) {
		for (SingleBTS current : buffer[x][y]) {
			if (!ListBTS.contains(map[x][y], current)) {
				map[x][y].add(current);
			}
		}

	}

	class interpolateThread implements Runnable {
		public int x;
		public int y;
		public SingleBTS what;
		public voronoi primary;

		@Override
		public void run() {
			if (x < Xcoords.length && y < Ycoords.length)
				interpolateAT(x, y, what, primary);
		}

		public interpolateThread(int x, int y, SingleBTS what, voronoi primary) {
			this.x = x;
			this.y = y;
			this.what = what;
			this.primary = primary;
		}

	}

}