GaussMarkovMovement.java 6.97 KB
Newer Older
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
/*
 * 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.topology.movement;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import de.tud.kom.p2psim.api.topology.movement.MovementInformation;
import de.tud.kom.p2psim.api.topology.movement.MovementSupported;
29
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
30
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
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
import de.tud.kom.p2psim.impl.util.stat.distributions.NormalDistribution;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.util.Distribution;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * A movement Model that produces more realistic paths for mobile users, without
 * sharp edges or sudden motion changes. It is described in <i>A Survey of
 * Mobility Models for Ad Hoc Network Research</i> by Tracy Camp et al.
 * 
 * The notation used in this class is the same as in the paper
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, 05/31/2011
 */
public class GaussMarkovMovement extends AbstractMovementModel {

	private double alpha;

	private double edgeThreshold = 20;

	private Distribution distribution_s = null;

	private Distribution distribution_d = null;

57
	private Map<SimLocationActuator, GaussMarkovMovementInfo> stateInfos;
58
59
60
61
62
63
64
65
66
67
68
69
70

	/**
	 * 
	 * @param alpha
	 */
	@XMLConfigurableConstructor({ "alpha", "edgeThreshold" })
	public GaussMarkovMovement(double alpha, double edgeThreshold) {
		this.alpha = alpha;
		if (alpha > 1 || alpha < 0) {
			throw new AssertionError(
					"For GaussMarkovMovement alpha has to be set between zero and one.");
		}
		this.edgeThreshold = edgeThreshold;
71
		this.stateInfos = new HashMap<SimLocationActuator, GaussMarkovMovement.GaussMarkovMovementInfo>();
72
73
74
75
76
77
78
79
80
81
	}

	@Override
	public void move() {
		if (distribution_s == null) {
			distribution_s = new NormalDistribution(0, 1);
		}
		if (distribution_d == null) {
			distribution_d = new NormalDistribution(0, 1);
		}
82
83
84
		Set<SimLocationActuator> comps = getComponents();
		for (SimLocationActuator comp : comps) {
			
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
			// get old x and y
			PositionVector pos = comp.getRealPosition();
			GaussMarkovMovementInfo inf = stateInfos.get(comp);
			double x_old = pos.getEntry(0);
			double y_old = pos.getEntry(1);
			// position within threshold?
			if (x_old < edgeThreshold
					|| x_old > getWorldDimension(0) - edgeThreshold
					|| y_old < edgeThreshold
					|| y_old > getWorldDimension(1) - edgeThreshold) {
				if (x_old < edgeThreshold
						&& y_old > getWorldDimension(1) - edgeThreshold) {
					// System.err.println(inf.d_bar + " bl to 45");
					// inf.d_bar = 45;
					inf.d_bar = Math.PI * 0.25;
				} else if (x_old < edgeThreshold && y_old < edgeThreshold) {
					// System.err.println(inf.d_bar + " tl to 315");
					// inf.d_bar = 315;
					inf.d_bar = Math.PI * 1.75;
				} else if (x_old < edgeThreshold) {
					// System.err.println(inf.d_bar + " l to 0");
					// inf.d_bar = 0;
					inf.d_bar = 0;
				} else if (x_old > getWorldDimension(0) - edgeThreshold
						&& y_old > getWorldDimension(1) - edgeThreshold) {
					// System.err.println(inf.d_bar + " br to 135");
					// inf.d_bar = 135;
					inf.d_bar = Math.PI * 0.75;
				} else if (x_old > getWorldDimension(0) - edgeThreshold
						&& y_old < edgeThreshold) {
					// System.err.println(inf.d_bar + " tr to 225");
					// inf.d_bar = 225;
					inf.d_bar = Math.PI * 1.25;
				} else if (x_old > getWorldDimension(0) - edgeThreshold) {
					// System.err.println(inf.d_bar + " r to 180");
					// inf.d_bar = 180;
					inf.d_bar = Math.PI;
				} else if (y_old < edgeThreshold) {
					// System.err.println(inf.d_bar + " t to 270");
					// inf.d_bar = 270;
					inf.d_bar = Math.PI * 1.5;
				} else if (y_old > getWorldDimension(1) - edgeThreshold) {
					// System.err.println(inf.d_bar + " b to 90");
					// inf.d_bar = 90;
					inf.d_bar = Math.PI * 0.5;
				}
				inf.d_old = inf.d_bar;
			}

			double s_n = alpha * inf.s_old + (1 - alpha) * inf.s_bar
					+ Math.sqrt(1 - (alpha * alpha))
					* distribution_s.returnValue();
			if (s_n > comp.getMaxMovementSpeed()
					* getTimeBetweenMoveOperations()
					/ Time.SECOND) {
				// Maximum speed
				s_n = comp.getMaxMovementSpeed()
						* getTimeBetweenMoveOperations()
						/ Time.SECOND;
			}
			if (s_n < 0) {
				s_n = 0;
			}
			//System.out.println(inf.d_old + " " + inf.d_bar);
			double d_n = (alpha * inf.d_old + (1 - alpha) * inf.d_bar + Math
					.sqrt(1 - (alpha * alpha)) * distribution_d.returnValue());
			// if (d_n < 0) {
			// d_n += 2 * Math.PI;
			// }
			// if (d_n >= 2 * Math.PI) {
			// d_n -= 2 * Math.PI;
			// }
			double x_n = x_old + inf.s_old * Math.cos(inf.d_old);
			double y_n = y_old - inf.s_old * Math.sin(inf.d_old);
			/* minus, because origin is in the upper left */
			inf.d_old = d_n;
			inf.s_old = s_n;
			PositionVector newPos = new PositionVector(x_n, y_n);
			if (isValidPosition(newPos)) {
164
				updatePosition(comp, newPos);
165
166
167
168
169
			}
		}
	}

	@Override
170
	public void addComponent(SimLocationActuator component) {
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
		super.addComponent(component);
		GaussMarkovMovementInfo info = new GaussMarkovMovementInfo();
		double rand = Randoms.getRandom(GaussMarkovMovement.class).nextDouble();
		info.d_bar = rand * 2 * Math.PI;
		info.d_old = info.d_bar;
		info.s_bar = component.getMaxMovementSpeed() * rand
				* getTimeBetweenMoveOperations()
				/ Time.SECOND;
		info.s_old = rand * component.getMaxMovementSpeed()
				* getTimeBetweenMoveOperations()
				/ Time.SECOND;
		stateInfos.put(component, info);
	}

	/**
	 * This allows to overwrite the distribution for directions
	 * 
	 * @param distribution
	 */
	public void setDirectionDistribution(Distribution distribution) {
		this.distribution_d = distribution;
	}

	/**
	 * This allows to overwrite the distribution for speed
	 * 
	 * @param distribution
	 */
	public void setSpeedDistribution(Distribution distribution) {
		this.distribution_s = distribution;
	}

	/**
	 * Movement State Information for this movement model
	 * 
	 * @author Bjoern Richerzhagen
	 * @version 1.0, 05/31/2011
	 */
	private class GaussMarkovMovementInfo implements MovementInformation {

		public GaussMarkovMovementInfo() {
			// ntd
		}

		public double s_old = 0;

		public double d_old = 0;

		public double d_bar;

		public double s_bar;

	}

}