GnpPosition.java 8.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (c) 2005-2011 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/>.
 *
 */


22
23
package de.tud.kom.p2psim.impl.network.gnp.topology;

24
25
26
import java.util.ArrayList;

import de.tudarmstadt.maki.simonstrator.api.Randoms;
Björn Richerzhagen's avatar
Björn Richerzhagen committed
27
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
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
/**
 * This class implements a NetPosition for a GNP-Based calculation of round trip
 * times. Therefore it includes methods for error estimation and methods for
 * positioning by a downhill simplex algorithm in the GnpSpace class
 * 
 * @author Gerald Klunker
 * @version 0.1, 09.01.2008
 * 
 */

public class GnpPosition implements Location, Comparable<GnpPosition> {

	private static final long serialVersionUID = -1103996725403557900L;

	private double[] gnpCoordinates;

	private GnpSpace gnpRef;

	private Host hostRef;

	private double error = -1.0;

	/**
	 * 
	 * @param gnpCoordinates
	 *            coordinate array for new position
	 */
	public GnpPosition(double[] gnpCoordinates) {
		super();
		this.gnpCoordinates = gnpCoordinates;
	}

	/**
	 * Object will be initialized with a random position. Position must be
	 * random according to the downhill simplex
	 * 
	 * @param noOfDimensions
	 *            number of dimensions
	 * @param hostRef
	 *            related Host object
	 * @param gnpRef
	 *            related GnpSpace object
	 */
	public GnpPosition(int noOfDimensions, Host hostRef, GnpSpace gnpRef) {
		super();
		gnpCoordinates = new double[noOfDimensions];
		this.hostRef = hostRef;
		this.gnpRef = gnpRef;
		for (int c = 0; c < gnpCoordinates.length; c++)
78
			gnpCoordinates[c] = Randoms.getRandom(GnpPosition.class)
79
					.nextDouble();
80
81
	}

82
83
84
85
86
87
88
89
90
91
92
	/**
	 * 
	 * @param dimension
	 * @param maxDiversity
	 */
	public void diversify(double[][] dimension, double maxDiversity) {
		for (int c = 0; c < this.gnpCoordinates.length; c++) {
			double rand = (2 * maxDiversity * Math.random()) - maxDiversity;
			gnpCoordinates[c] = gnpCoordinates[c] + (rand * dimension[c][2]);
		}
		error = -1.0;
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
	/**
	 * reposition
	 * 
	 * @param pos
	 *            position in the coordinate array
	 * @param value
	 *            new value at position pos
	 */
	public void setGnpCoordinates(int pos, double value) {
		gnpCoordinates[pos] = value;
		error = -1.0;
	}

	/**
	 * 
	 * @return the related GnpSpace object
	 */
	private GnpSpace getGnpRef() {
		return gnpRef;
	}

	/**
	 * 
	 * @return the related Host object
	 */
	public Host getHostRef() {
		return hostRef;
	}

	/**
	 * 
	 * @return number of dimensions
	 */
	public int getNoOfDimensions() {
		return gnpCoordinates.length;
	}

	/**
	 * 
	 * @param pos
	 *            position in the coordinate array
	 * @return value at position pos
	 */
	public double getGnpCoordinates(int pos) {
		return gnpCoordinates[pos];
	}

	/**
	 * Calculates the sum of all errors according to the downhill simplex
	 * operator.
	 * 
	 * @return error
	 */
	public double getDownhillSimplexError() {
		if (error < 0.0) {
			error = 0.0;
			for (int c = 0; c < getGnpRef().getNumberOfMonitors(); c++) {
				error += getDownhillSimplexError(getGnpRef()
						.getMonitorPosition(c));
			}
		}
		return error;
	}

	/**
	 * Calculates the error to a monitor according to the downhill simplex
	 * operator
	 * 
	 * @param monitor
	 * @return error
	 */
	public double getDownhillSimplexError(GnpPosition monitor) {
		double calculatedDistance = this.distanceTo(monitor);
		double measuredDistance = this.getMeasuredRtt(monitor);
		if (Double.compare(measuredDistance, Double.NaN) == 0)
			return 0.0;
		double error = Math.pow((calculatedDistance - measuredDistance)
				/ calculatedDistance, 2);
		return error;
	}

	/**
	 * Calculates an error, that indicates the deviation of the measured vs. the
	 * calculated rtt.
	 * 
	 * @param monitor
	 * @return error value
	 */
	public double getDirectionalRelativError(GnpPosition monitor) {
		double calculatedDistance = this.distanceTo(monitor);
		double measuredDistance = this.getMeasuredRtt(monitor);
		if (Double.compare(measuredDistance, Double.NaN) == 0)
			return Double.NaN;
		double error = (calculatedDistance - measuredDistance)
				/ Math.min(calculatedDistance, measuredDistance);
		return error;
	}

	/**
	 * Method must be overwrite to sort different GnpPositions in order of their
	 * quality.
	 * 
	 * Is needed for the positioning with the downhill simplex
	 * 
	 */
	public int compareTo(GnpPosition arg0) {
		double val1 = this.getDownhillSimplexError();
		double val2 = arg0.getDownhillSimplexError();
		if (val1 < val2)
			return -1;
		if (val1 > val2)
			return 1;
		else
			return 0;
	}

	/**
	 * 
	 * @return Comma-separated list of coordinates
	 */
	public String getCoordinateString() {
		if (gnpCoordinates.length == 0) {
			return "";
		} else {
			String result = String.valueOf(gnpCoordinates[0]);
			for (int c = 1; c < gnpCoordinates.length; c++)
				result = result + "," + gnpCoordinates[c];
			return result;
		}
	}

	/**
	 * 
	 * @param monitor
	 * @return measured rtt to monitor, nan if no rtt was measured
	 */
	public double getMeasuredRtt(GnpPosition monitor) {
		return this.getHostRef().getRtt(monitor.getHostRef());
	}

	/**
	 * @return euclidean distance
	 */
	public double getDistance(Location point) {
		GnpPosition coord = (GnpPosition) point;
		double distance = 0.0;
		for (int c = 0; c < gnpCoordinates.length; c++)
			distance += Math.pow(
					gnpCoordinates[c] - coord.getGnpCoordinates(c), 2);
		return Math.sqrt(distance);
	}
246
247
248
249
250

	@Override
	public int getTransmissionSize() {
		return 16; // 2 * double
	}
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

	/**
	 * Static method generates a new GnpPosition according to the downhill
	 * simplex operator
	 * 
	 * @param solution
	 * @param moveToSolution
	 * @param moveFactor
	 * @return new position
	 */
	public static GnpPosition getMovedSolution(GnpPosition solution,
			GnpPosition moveToSolution, double moveFactor) {
		GnpPosition returnValue = new GnpPosition(solution.getNoOfDimensions(),
				solution.getHostRef(), solution.getGnpRef());
		for (int c = 0; c < solution.getNoOfDimensions(); c++) {
			double newCoord = (moveToSolution.getGnpCoordinates(c) - solution
					.getGnpCoordinates(c))
					* moveFactor + solution.getGnpCoordinates(c);
			returnValue.setGnpCoordinates(c, newCoord);
		}
		return returnValue;
	}

	/**
	 * Static method generates a new GnpPosition according to the downhill
	 * simplex operator
	 * 
	 * @param solution
	 * @param moveToSolution
	 * @param moveFactor
	 * @return new position
	 */
	public static GnpPosition getCenterSolution(ArrayList<GnpPosition> solutions) {
		GnpPosition returnValue = new GnpPosition(solutions.get(0)
				.getNoOfDimensions(), solutions.get(0).getHostRef(), solutions
				.get(0).getGnpRef());
		for (int d = 0; d < solutions.size(); d++) {
			for (int c = 0; c < solutions.get(0).getNoOfDimensions(); c++) {
				returnValue.setGnpCoordinates(c, returnValue
						.getGnpCoordinates(c)
						+ solutions.get(d).getGnpCoordinates(c));
			}
		}
		for (int c = 0; c < returnValue.getNoOfDimensions(); c++) {
			returnValue.setGnpCoordinates(c, returnValue.getGnpCoordinates(c)
					/ solutions.size());
		}
		return returnValue;
299
300
301
302
	}

	public GnpPosition clone() {
		return new GnpPosition(gnpCoordinates);
Björn Richerzhagen's avatar
Björn Richerzhagen committed
303
304
305
306
307
308
309
310
311
312
	}
	
	@Override
	public float bearingTo(Location dest) {
		throw new AssertionError(
				"bearingTo is not defined for this Position-Type");
	}
	
	@Override
	public double distanceTo(Location dest) {
313
		return getDistance(dest);
Björn Richerzhagen's avatar
Björn Richerzhagen committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	}
	
	@Override
	public long getAgeOfLocation() {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public double getLatitude() {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public double getLongitude() {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public void set(Location l) {
		throw new UnsupportedOperationException();
334
335
	}
}