summaryrefslogtreecommitdiffstats
path: root/DataStructure/BayesAll.java
blob: 27ab797c9aff35c707a14e70c7f64789d767e387 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package DataStructure;

import helper.ListBTS;

import java.util.ArrayList;
import java.util.Arrays;

public class BayesAll {
	GSMMap map;
	// ArrayList<SingleBTS> MR;
	ArrayList<ratioElem>[][] RatioMap;
	boolean[][] hasElement;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

	public BayesAll(Interpolator map) {
		this.map = map;
		hasElement = map.getAllocation();

		// Compute ALL Ratios for GSMmap
		RatioMap = computeMapRatio();

	}

	public BayesAll(GSMMap map) {
		this.map = map;
		hasElement = map.getAllocation();
		RatioMap = computeMapRatio();
	}

	public double[][] locate(ArrayList<SingleBTS> MR, double confidence) {

		SingleBTS[] content = ListBTS.content(MR);
		// check if MR only has one BTS inside!
		if (content == null || content.length <= 1) {
			// only one or zero BTS. mark all tiles where this BTS is received!
			// get BTS

			return markOneBTS(content);
		}

		// Compute ALL Ratios out of mobile phone's MR
		ArrayList<ratioElem> RSSVector = computeAllRatios(MR);

		// Compute scoremap
		double[][] scoremap = computeScoreMap(RSSVector);

		if (confidence >= 1)
			return scoremap;
		else
			return toConfidence(scoremap, confidence);
	}

	/**
	 * This returns the distribution if only one BTS is measured
	 * 
	 * @param singleBTS
	 * @return
	 */
	private double[][] markOneBTS(SingleBTS[] singleBTS) {
		int count = 0;
		double[][] scoremap = new double[map.Xcoords.length][map.Ycoords.length];

		if (singleBTS == null) {
			long numberOfTiles = map.countTilesWMeasures();
			for (int x = 0; x < map.Xcoords.length; x++) {
				for (int y = 0; y < map.Ycoords.length; y++) {
					if (hasElement[x][y]) {
						scoremap[x][y] = 1d / (double) numberOfTiles;
					}
				}
			}
		}

		// count tiles where this bts is measured
		for (int x = 0; x < map.Xcoords.length; x++) {
			for (int y = 0; y < map.Ycoords.length; y++) {
				if (ListBTS.contains(map.map[x][y], singleBTS[0])) {
					count++;
				}
			}
		}

		// give every tile a equal distributed possibility
		for (int x = 0; x < map.Xcoords.length; x++) {
			for (int y = 0; y < map.Ycoords.length; y++) {
				if (ListBTS.contains(map.map[x][y], singleBTS[0])) {
					scoremap[x][y] = 1d / (double) count;
				}
			}
		}

		return scoremap;
	}

	private double[][] computeScoreMap(ArrayList<ratioElem> RSSVector) {
		double[][] score = new double[map.Xcoords.length][map.Ycoords.length];
		// Compute possibility that this RSSVector was measured in RatioMap as
		// TODO: Thread!!!
		computeTotalProbability totalProbRunnable = new computeTotalProbability(
				RSSVector);
		Thread totalProbThread = new Thread(totalProbRunnable);
		totalProbThread.start();
		// double totalProbability = computeTotalProb(RSSVector);
		// now we have P(S) (=totalProb that RSSVector appears in map)
		double numberOfTiles = map.countTilesWMeasures();
		// compute Bayes with RSSVector
		for (int x = 0; x < RatioMap.length; x++) {
			for (int y = 0; y < RatioMap[0].length; y++) {
				if (hasElement[x][y]) {
					score[x][y] = (ProbabilityForRSSVector(RSSVector,
							RatioMap[x][y]) * (1 / numberOfTiles));
					// / totalProbability;
				}
			}
		}

		// division with total Probability
		try {
			totalProbThread.join();
			double totalProbability = totalProbRunnable.totalProbability;
			for (int x = 0; x < RatioMap.length; x++) {
				for (int y = 0; y < RatioMap[0].length; y++) {
					if (hasElement[x][y]) {
						score[x][y] = score[x][y] / totalProbability;
					}
				}

			}

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return score;
	}

	/**
	 * Computes Probability that this RSSVector is seen in the Map
	 * 
	 * @param RSSVector
	 * @return
	 */
	@SuppressWarnings("unused")
	private double computeTotalProb(ArrayList<ratioElem> RSSVector) {
		double totalProb = 0;
		// go through all valid Elements in RatioMap. Sum up the possibilities
		// for each tile
		for (int x = 0; x < RatioMap.length; x++) {
			for (int y = 0; y < RatioMap[0].length; y++) {
				if (hasElement[x][y])
					totalProb += ProbabilityForRSSVector(RSSVector,
							RatioMap[x][y]);
			}

			/*
			 * int y = 0; while (y < RatioMap[0].length) { possibilityThread
			 * threadable1 = new possibilityThread( RSSVector, x, y); Thread
			 * thread1 = new Thread(threadable1); if (hasElement[x][y])
			 * thread1.start(); y++; possibilityThread threadable2 = new
			 * possibilityThread( RSSVector, x, y); Thread thread2 = new
			 * Thread(threadable2); if (y < RatioMap[0].length &&
			 * hasElement[x][y]) thread2.start(); y++; try { thread1.join();
			 * thread2.join(); } catch (InterruptedException e) {
			 * e.printStackTrace(); } // Threads fertig.
			 * 
			 * }
			 */
		}

		double numberOfTiles = map.countTilesWMeasures();
		totalProb = totalProb / numberOfTiles;
		return totalProb;
	}

	private double ProbabilityForRSSVector(ArrayList<ratioElem> MRphone,
			ArrayList<ratioElem> MRmap) {

		double probability = 1;
		boolean thereWasAHit = false;
		for (int i = 0; i < MRphone.size(); i++) {
			// when there is no measurement from phone, go to next ratio element
			if (MRphone.get(i) != null) {
				if (MRmap.get(i) == null)
					return 0;
				probability *= MRphone.get(i).probability(MRmap.get(i));
				thereWasAHit = true;
			}

		}

		if (thereWasAHit)
			return probability;
		else
			return 0;
	}

	/**
	 * Creates Ratios out of map (all possible ratios!)
	 * 
	 * @return
	 */
	private ArrayList<ratioElem>[][] computeMapRatio() {
		ArrayList<ratioElem>[][] result = new ArrayList[map.Xcoords.length][map.Ycoords.length];

		// int size = sumOfRatio(map.content().length - 1);
		// result initilisieren und füllen
		for (int x = 0; x < map.Xcoords.length; x++) {
			for (int y = 0; y < map.Ycoords.length; y++) {
				if (hasElement[x][y]) {
					// result[x][y] = new ArrayList<ratioElem>(size);

					result[x][y] = (computeAllRatios(map.map[x][y]));
				}
			}
		}

		return result;
	}

	/**
	 * Returns all possible Ratios that can be built with GSM-Map's content out
	 * of MR. Not available Ratios get stored as null
	 * 
	 * @return
	 */
	private ArrayList<ratioElem> computeAllRatios(ArrayList<SingleBTS> MR) {
		SingleBTS[] content = map.content();
		ArrayList<ratioElem> result = new ArrayList<ratioElem>(
				sumOfRatio(content.length - 1));
		// get ratio between every BTS and the others
		for (int i = 0; i < content.length; i++) {
			for (int j = i + 1; j < content.length; j++) {
				SingleBTS top = ListBTS.getARFCN(MR, content[i]);
				SingleBTS bottom = ListBTS.getARFCN(MR, content[j]);
				if (top == null || bottom == null) {
					result.add(null);
				} else {
					result.add(new ratioElem(top, bottom));
				}
			}
		}

		return result;
	}

	/**
	 * Computes all Ratios that can be built out of MR
	 * 
	 * @param MR
	 * @return
	 */
	@SuppressWarnings("unused")
	private ArrayList<ratioElem> computeRatios(ArrayList<SingleBTS> MR) {
		ArrayList<ratioElem> result = new ArrayList<ratioElem>(
				sumOfRatio(MR.size() - 1));
		for (int i = 0; i < MR.size(); i++) {
			for (int j = i + 1; j < MR.size(); j++) {
				result.add(new ratioElem(MR.get(i), MR.get(j)));
			}
		}
		return result;
	}

	private static int sumOfRatio(int n) {
		return n == 0 ? 1 : n + sumOfRatio(n - 1);
	}

	public double[][] toConfidence(double[][] scoremap, double confidence) {
		if (confidence >= 1) {
			return scoremap;
		}

		// scoremap to possibility objects (faster...)
		PossibilityObject[] possibilities = toPossibility(scoremap);
		double sum_confidence = 0;
		double[][] result = new double[scoremap.length][scoremap[0].length];
		int boundary = possibilities.length - 1;
		while (sum_confidence < confidence) {
			// run from highest element to lowest.
			// as long as confidence is not reached: store current element in
			// result
			sum_confidence += possibilities[boundary].possibility;
			int x = possibilities[boundary].x;
			int y = possibilities[boundary].y;
			result[x][y] = possibilities[boundary].possibility;
			boundary--;
		}
		// result = PossObjToScoremap(possibilities, boundary, result);

		/*
		 * double max = 0; int xmax = 0; int ymax = 0; for (int x = 0; x <
		 * scoremap.length; x++) { for (int y = 0; y < scoremap[0].length; y++)
		 * { if (scoremap[x][y] > max) { max = scoremap[x][y]; xmax = x; ymax =
		 * y; }
		 * 
		 * } } // max found.
		 * 
		 * result[xmax][ymax] = scoremap[xmax][ymax]; sum_confidence +=
		 * scoremap[xmax][ymax]; scoremap[xmax][ymax] = 0; }
		 */
		return result;

	}

	/**
	 * Makes a scoremap to a SORTED one dimensional PossibilityObject Array.
	 * Calculates the entropy on the way and prints to Console
	 * 
	 * @param scoremap
	 * @return
	 */
	public PossibilityObject[] toPossibility(double[][] scoremap) {
		int gsmmapsize = (int) map.countTilesWMeasures();
		// PossibilityObject[] possibilities = new
		// PossibilityObject[gsmmapsize];
		ArrayList<PossibilityObject> result = new ArrayList<PossibilityObject>(
				gsmmapsize);
		double entropy = 0;
		for (int x = 0; x < scoremap.length; x++) {
			for (int y = 0; y < scoremap[0].length; y++) {
				if (hasElement[x][y] && scoremap[x][y] > 0) {
					entropy = entropy + scoremap[x][y]
							* ((Math.log(scoremap[x][y])) / Math.log(2));

					PossibilityObject current = new PossibilityObject();
					current.possibility = scoremap[x][y];
					current.x = x;
					current.y = y;
					result.add(current);
				}
			}

		}

		System.out.println("Entropy dieser Karte: " + (entropy * (-1))
				+ " max. Entropy: " + (Math.log(gsmmapsize) / Math.log(2)));
		PossibilityObject[] possibilities = result
				.toArray(new PossibilityObject[1]);
		Arrays.sort(possibilities);
		return possibilities;
	}

	class computeTotalProbability implements Runnable {
		private ArrayList<ratioElem> RSSVector;
		public double totalProbability;

		public computeTotalProbability(ArrayList<ratioElem> RSSVector) {
			this.RSSVector = RSSVector;
		}

		@Override
		public void run() {
			double totalProb = 0;
			// go through all valid Elements in RatioMap. Sum up the
			// possibilities
			// for each tile
			for (int x = 0; x < RatioMap.length; x++) {
				for (int y = 0; y < RatioMap[0].length; y++) {
					if (hasElement[x][y])
						totalProb += ProbabilityForRSSVector(RSSVector,
								RatioMap[x][y]);
				}

			}

			double numberOfTiles = map.countTilesWMeasures();
			totalProb = totalProb / numberOfTiles;
			totalProbability = totalProb;
		}

	}

}