TTLbasedCacheDecisionStrategy.java 10.7 KB
Newer Older
Tobias Meuser's avatar
Tobias Meuser committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 *
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.vehicular.caching.decision;

Björn Richerzhagen's avatar
Björn Richerzhagen committed
23
import java.util.Arrays;
24
25
import java.util.Collections;
import java.util.Comparator;
Tobias Meuser's avatar
Tobias Meuser committed
26
27
28
29
30
31
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import de.tudarmstadt.maki.simonstrator.api.Time;
32
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.RoadProperty;
33
34
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.jam.VectoralJamProperty;
Tobias Meuser's avatar
Tobias Meuser committed
35
36
37
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
38
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
Tobias Meuser's avatar
Tobias Meuser committed
39
40
41
42

public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
	private static final long SCALING = Time.SECOND;

43
44
	private static final double ACCURACY_FACTOR = 100000;

Tobias Meuser's avatar
Tobias Meuser committed
45
46
47
48
49
50
	private long ttl = 300 * Time.SECOND / SCALING;
	private double accuracy = 1;

	private double costWrongKeep = 1;
	private double costWrongChange = 1;

51
52
	private Object _lastDecision = false;

Tobias Meuser's avatar
Tobias Meuser committed
53
54
55
56
57
58
	public TTLbasedCacheDecisionStrategy(Map<String, String> pParams) {
		for (Entry<String, String> param : pParams.entrySet()) {
			switch (param.getKey()) {
			case "ACCURACY":
				accuracy = Double.valueOf(param.getValue());
				break;
59
60
61
62
63
			case "COST_RATIO":
				double ratio = Double.valueOf(param.getValue());
				costWrongChange = 2 / (ratio + 1);
				costWrongKeep = 2 - costWrongChange;
				break;
Tobias Meuser's avatar
Tobias Meuser committed
64
65
66
67
68
69
			default:
				break;
			}
		}
	}

70
71
72
73
74
75
76
77
	public double getCostWrongChange() {
		return costWrongChange;
	}

	public double getCostWrongKeep() {
		return costWrongKeep;
	}

Tobias Meuser's avatar
Tobias Meuser committed
78
79
80
81
	@Override
	public <T extends PointInformation> T decideOnCorrectInformation(
			List<T> pSimilarPointInformation) {
		if (pSimilarPointInformation.size() == 1) {
82
83
84
			T decision = pSimilarPointInformation.get(0);
			_lastDecision = decision.getValue();
			return decision;
Tobias Meuser's avatar
Tobias Meuser committed
85
86
87
		} else if (pSimilarPointInformation.size() == 0) {
			return null;
		}
88
89
90
91
92
93
94
95
96
		
		Collections.sort(pSimilarPointInformation, new Comparator<T>() {

			@Override
			public int compare(T pArg0, T pArg1) {
				return Long.compare(pArg0.getDetectionDate(), pArg1.getDetectionDate());
			}
			
		});
Tobias Meuser's avatar
Tobias Meuser committed
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

		long minTimestamp = Long.MAX_VALUE;
		long maxTimestamp = 0;
		Object value = pSimilarPointInformation.get(0).getValue();
		boolean differentValue = false;
		for (T t : pSimilarPointInformation) {
			if (!t.hasAttribute(AvailableInformationAttributes.TTL)) {
				throw new AssertionError("Unable to perform TTL-based majority voting witout TTL");
			}
			long timestamp = t.getDetectionDate();

			if (timestamp < minTimestamp) {
				minTimestamp = timestamp;
			}
			if (timestamp > maxTimestamp) {
				maxTimestamp = timestamp;
			}

			if (!value.equals(t.getValue())) {
				differentValue = true;
			}
		}

		if (differentValue) {
			long difference = maxTimestamp - minTimestamp;

			if (difference == 0) {
				return pSimilarPointInformation.get(pSimilarPointInformation.size() - 1);
			}

			double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);

129
130
			long ttl = (long)pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.TTL) / SCALING;
			rate = Math.min(rate, ttl / 10.0);
Tobias Meuser's avatar
Tobias Meuser committed
131

132
133
134
135
136
137
			double b;
			if (Boolean.FALSE.equals(_lastDecision)) {
				b = determineB(rate, 1 - accuracy, ttl, costWrongKeep, costWrongChange);
			} else {
				b = determineB(rate, 1 - accuracy, ttl, costWrongChange, costWrongKeep);
			}
Tobias Meuser's avatar
Tobias Meuser committed
138
139

			Map<Object, Double> weight = new HashMap<>();
140
			
Tobias Meuser's avatar
Tobias Meuser committed
141
142
143
144
145
			for (T t : pSimilarPointInformation) {
				double impact = calculateImpact(1 - accuracy, ttl, t.getDetectionDate() / SCALING, b, maxTimestamp / SCALING);

				double sumImpact = 0;

146
147
148
149
150
151
152
153
				Object currentValue = t.getValue();
				
				if (currentValue instanceof VectoralJamProperty) {
					currentValue = ((VectoralJamProperty) currentValue).getMostProbableValue();
				}

				if (weight.containsKey(currentValue)) {
					sumImpact = weight.get(currentValue);
Tobias Meuser's avatar
Tobias Meuser committed
154
155
156
				}
				sumImpact += impact;

157
				weight.put(currentValue, sumImpact);
Tobias Meuser's avatar
Tobias Meuser committed
158
159
			}

160
			double maxWeight = -1;
Tobias Meuser's avatar
Tobias Meuser committed
161
162
163
164
165
166
167
168
169
			Object maxValue = null;

			for (Object key : weight.keySet()) {
				if (weight.get(key) > maxWeight) {
					maxWeight = weight.get(key);
					maxValue = key;
				}
			}

170
			maxTimestamp = -1;
Tobias Meuser's avatar
Tobias Meuser committed
171
172
173
174
			T maxFitting = null;
			for (T t : pSimilarPointInformation) {
				long timestamp = t.getDetectionDate();

175
				Object currentValue = t.getValue();
176
177
				if (currentValue instanceof VectoralProperty) {
					currentValue = ((VectoralProperty)currentValue).getMostProbableValue();
178
179
180
				}
				
				if (currentValue.equals(maxValue) && timestamp > maxTimestamp) {
Tobias Meuser's avatar
Tobias Meuser committed
181
182
183
184
					maxTimestamp = timestamp;
					maxFitting = t;
				}
			}
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
			
			if (maxFitting.getValue() instanceof VectoralProperty) {
				VectoralProperty vectoralProperty = ((VectoralProperty)maxFitting.getValue()).clone();
				double[] valueProbabilities = vectoralProperty.getValueProbabilities();
				Arrays.fill(valueProbabilities, 0);
				
				double sum = 0;
				for (Object key : weight.keySet()) {
					valueProbabilities[vectoralProperty.getIndexForValue(key)] = weight.get(key);
					sum += weight.get(key);
				}
				
				for (int i = 0; i < valueProbabilities.length; i++) {
					valueProbabilities[i] /= sum;
				}
				
				RoadInformation roadInformation = new RoadInformation(vectoralProperty);
				roadInformation.copyAttributes((RoadInformation)maxFitting);
				maxFitting = (T) roadInformation;
			}
Tobias Meuser's avatar
Tobias Meuser committed
205

206
207
			_lastDecision = maxFitting.getValue();

Tobias Meuser's avatar
Tobias Meuser committed
208
209
			return maxFitting;
		} else {
210
			maxTimestamp = -1;
Tobias Meuser's avatar
Tobias Meuser committed
211
212
213
214
215
216
217
218
219
220
			T maxFitting = null;
			for (T t : pSimilarPointInformation) {
				long timestamp = (long) t.getAttribute(AvailableInformationAttributes.TTL);

				if (timestamp > maxTimestamp) {
					maxTimestamp = timestamp;
					maxFitting = t;
				}
			}

221
222
			_lastDecision = maxFitting.getValue();

Tobias Meuser's avatar
Tobias Meuser committed
223
224
225
			return maxFitting;
		}
	}
226
	
Tobias Meuser's avatar
Tobias Meuser committed
227
	public double calculateImpact(double errorProbability, long ttl, long time, double b, long maxTimestamp) {
228
		long age = maxTimestamp - time;
Tobias Meuser's avatar
Tobias Meuser committed
229
230
231
232
233
234
235
236
237
238
		if (errorProbability == 0) {
			if (time == maxTimestamp) {
				return 1;
			} else {
				return 0;
			}
		} else if (errorProbability == 1) {
			return 1;
		} else if (errorProbability == 0.5) {
			return (errorProbability - 1) / ttl * age + errorProbability;
239
240
241
242
243
244
		} else if (b == Double.NEGATIVE_INFINITY) {
			if (time == maxTimestamp) {
				return 1;
			} else {
				return 0;
			}
Tobias Meuser's avatar
Tobias Meuser committed
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
		}

		return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * ttl)) / (1 - Math.exp(b * ttl));
	}

	public double getChangeProbability(long ttl) {
		return 1 - Math.pow(0.5, 1 / (double) ttl);
	}

	public int getOptimalMessageAmountForSwitch(double changeProbability, double errorProbability, double costSlow, double costFast) {
		return (int) Math.round(Math.log(-changeProbability / Math.log(errorProbability) * costSlow / costFast) / Math.log(errorProbability));
	}

	public double determineB(double rate, double errorProbability, long ttl, double costSlow, double costFast) {
		return determineB(rate, errorProbability, ttl, costSlow, costFast, 1);
	}

	public double determineB(double rate, double errorProbability, long ttl, double costSlow, double costFast, int reversed) {
		if (errorProbability == 0 || errorProbability == 1 || errorProbability == 0.5) {
			return Double.NaN;
		}

		double b;
		double p_c = getChangeProbability((long) (ttl / rate));

		int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);

272
273
274
275
		if (optimalAmount == 1) {
			return Double.NEGATIVE_INFINITY;
		}
		
Tobias Meuser's avatar
Tobias Meuser committed
276
277
278
279
280
		boolean first = true;

		double leftSide;
		double rightSide;

281
		double step = 5;
Tobias Meuser's avatar
Tobias Meuser committed
282
283

		if (errorProbability < 0.5) {
284
			b = -2 * step * reversed;
Tobias Meuser's avatar
Tobias Meuser committed
285
		} else {
286
			b = 2 * step * reversed;
Tobias Meuser's avatar
Tobias Meuser committed
287
288
		}

289
290
		int similar = 0;
		double lastDifference = -1;
Tobias Meuser's avatar
Tobias Meuser committed
291
292
293
		do {
			leftSide = calculateWeightingForOldState(optimalAmount, rate, errorProbability, ttl, b);
			rightSide = calculateWeightingForNewState(optimalAmount, rate, errorProbability, ttl, b);
294
295
296
297
298
299
			if (Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR)) == lastDifference) {
				similar++;
			} else {
				lastDifference = Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR));
				similar = 0;
			}
Tobias Meuser's avatar
Tobias Meuser committed
300

301
			if (Double.isNaN(leftSide) || Double.isNaN(rightSide) || similar > 100) {
Tobias Meuser's avatar
Tobias Meuser committed
302
				if (reversed != -1) {
303
304
305
306
307
308
					double determineB = determineB(rate, errorProbability, ttl, costSlow, costFast, -1);
					if (!Double.isNaN(determineB)) {
						return determineB;
					} else {
						return b;
					}
Tobias Meuser's avatar
Tobias Meuser committed
309
310
311
312
313
				} else {
					return Double.NaN;
				}
			}

314
315
			leftSide = Math.round(leftSide * ACCURACY_FACTOR);
			rightSide = Math.round(rightSide * ACCURACY_FACTOR);
Tobias Meuser's avatar
Tobias Meuser committed
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

			if (leftSide > rightSide) {
				if (b < 0) {
					b -= step;
					if (!first) {
						step *= 0.5;
					}
				} else {
					b -= step;
					step *= 0.5;
					first = false;
				}
			} else if (leftSide < rightSide) {
				if (b > 0) {
					b += step;
					if (!first) {
						step *= 0.5;
					}
				} else {
					b += step;
					step *= 0.5;
					first = false;
				}
			} else {
				break;
			}
		} while (true);

		return b;
	}

	public double calculateWeightingForOldState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
		double impact = 0;
349
350
		for (int a = optimalMessageAmount; a < Math.max(Math.floor(ttl / rate), optimalMessageAmount + 2); a++) {
			impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
Tobias Meuser's avatar
Tobias Meuser committed
351
352
353
354
355
356
		}
		return impact;
	}

	public double calculateWeightingForNewState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
		double impact = 0;
357
358
		for (int a = 0; a < optimalMessageAmount; a++) {
			impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
Tobias Meuser's avatar
Tobias Meuser committed
359
360
361
362
		}
		return impact;
	}
}