TTLbasedCacheDecisionStrategy.java 5.91 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;
39
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.relevance.impl.SimpleQoIBasedImpactFunction;
Tobias Meuser's avatar
Tobias Meuser committed
40
41
42
43

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

44
45
	private static final double ACCURACY_FACTOR = 100000;

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

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

52
53
	private Object _lastDecision = false;

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

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

	public double getCostWrongKeep() {
		return costWrongKeep;
	}

Tobias Meuser's avatar
Tobias Meuser committed
79
80
81
82
	@Override
	public <T extends PointInformation> T decideOnCorrectInformation(
			List<T> pSimilarPointInformation) {
		if (pSimilarPointInformation.size() == 1) {
83
84
85
			T decision = pSimilarPointInformation.get(0);
			_lastDecision = decision.getValue();
			return decision;
Tobias Meuser's avatar
Tobias Meuser committed
86
87
88
		} else if (pSimilarPointInformation.size() == 0) {
			return null;
		}
89

90
91
92
93
94
95
		Collections.sort(pSimilarPointInformation, new Comparator<T>() {

			@Override
			public int compare(T pArg0, T pArg1) {
				return Long.compare(pArg0.getDetectionDate(), pArg1.getDetectionDate());
			}
96

97
		});
Tobias Meuser's avatar
Tobias Meuser committed
98
99
100
101
102
103
104
105
106
107

		Object value = pSimilarPointInformation.get(0).getValue();
		boolean differentValue = false;
		for (T t : pSimilarPointInformation) {
			if (!value.equals(t.getValue())) {
				differentValue = true;
			}
		}

		if (differentValue) {
108
			SimpleQoIBasedImpactFunction<T> impactFunction = new SimpleQoIBasedImpactFunction<>(pSimilarPointInformation, accuracy);
Tobias Meuser's avatar
Tobias Meuser committed
109
110

			Map<Object, Double> weight = new HashMap<>();
111

Tobias Meuser's avatar
Tobias Meuser committed
112
			for (T t : pSimilarPointInformation) {
113
				double impact = impactFunction.calculateImpact(t);
Tobias Meuser's avatar
Tobias Meuser committed
114
115
116

				double sumImpact = 0;

117
				Object currentValue = t.getValue();
118

119
120
121
122
123
124
				if (currentValue instanceof VectoralJamProperty) {
					currentValue = ((VectoralJamProperty) currentValue).getMostProbableValue();
				}

				if (weight.containsKey(currentValue)) {
					sumImpact = weight.get(currentValue);
Tobias Meuser's avatar
Tobias Meuser committed
125
126
127
				}
				sumImpact += impact;

128
				weight.put(currentValue, sumImpact);
Tobias Meuser's avatar
Tobias Meuser committed
129
130
			}

131
			double maxWeight = -1;
Tobias Meuser's avatar
Tobias Meuser committed
132
133
134
135
136
137
138
139
140
			Object maxValue = null;

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

141
			long maxTimestamp = -1;
Tobias Meuser's avatar
Tobias Meuser committed
142
143
144
145
			T maxFitting = null;
			for (T t : pSimilarPointInformation) {
				long timestamp = t.getDetectionDate();

146
				Object currentValue = t.getValue();
147
148
				if (currentValue instanceof VectoralProperty) {
					currentValue = ((VectoralProperty)currentValue).getMostProbableValue();
149
				}
150

151
				if (currentValue.equals(maxValue) && timestamp > maxTimestamp) {
Tobias Meuser's avatar
Tobias Meuser committed
152
153
154
155
					maxTimestamp = timestamp;
					maxFitting = t;
				}
			}
156

157
158
159
160
			if (maxFitting.getValue() instanceof VectoralProperty) {
				VectoralProperty vectoralProperty = ((VectoralProperty)maxFitting.getValue()).clone();
				double[] valueProbabilities = vectoralProperty.getValueProbabilities();
				Arrays.fill(valueProbabilities, 0);
161

162
163
164
165
166
				double sum = 0;
				for (Object key : weight.keySet()) {
					valueProbabilities[vectoralProperty.getIndexForValue(key)] = weight.get(key);
					sum += weight.get(key);
				}
167

168
169
170
				for (int i = 0; i < valueProbabilities.length; i++) {
					valueProbabilities[i] /= sum;
				}
171

172
173
174
175
				RoadInformation roadInformation = new RoadInformation(vectoralProperty);
				roadInformation.copyAttributes((RoadInformation)maxFitting);
				maxFitting = (T) roadInformation;
			}
Tobias Meuser's avatar
Tobias Meuser committed
176

177
178
			_lastDecision = maxFitting.getValue();

Tobias Meuser's avatar
Tobias Meuser committed
179
180
			return maxFitting;
		} else {
181
			long maxTimestamp = -1;
Tobias Meuser's avatar
Tobias Meuser committed
182
183
184
185
186
187
188
189
190
191
			T maxFitting = null;
			for (T t : pSimilarPointInformation) {
				long timestamp = (long) t.getAttribute(AvailableInformationAttributes.TTL);

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

192
193
			_lastDecision = maxFitting.getValue();

Tobias Meuser's avatar
Tobias Meuser committed
194
195
196
197
			return maxFitting;
		}
	}
}