minor changes and documentation regarding using random generators in Cooja

This commit is contained in:
fros4943 2009-02-18 12:07:19 +00:00
parent cc56325300
commit 00273dce75
7 changed files with 373 additions and 334 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: AreaViewer.java,v 1.4 2008/02/15 13:20:23 fros4943 Exp $
* $Id: AreaViewer.java,v 1.5 2009/02/18 12:08:10 fros4943 Exp $
*/
package se.sics.mrm;
@ -2128,7 +2128,7 @@ public class AreaViewer extends VisPlugin {
g2d.setTransform(realWorldTransformScaled);
g2d.setStroke(new BasicStroke((float) 0.0));
Random random = new Random();
Random random = new Random(); /* Do not use main random generator */
for (int i=0; i < trackedComponents.size(); i++) {
g2d.setColor(new Color(255, random.nextInt(255), random.nextInt(255), 255));
Line2D originalLine = trackedComponents.get(i);

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ChannelModel.java,v 1.3 2007/03/23 21:13:43 fros4943 Exp $
* $Id: ChannelModel.java,v 1.4 2009/02/18 12:08:10 fros4943 Exp $
*/
package se.sics.mrm;
@ -85,9 +85,6 @@ public class ChannelModel {
}
private SettingsObservable settingsObservable = new SettingsObservable();
// A random number generator
private Random random = new Random();
public ChannelModel() {
// - Set initial parameter values -
@ -243,9 +240,10 @@ public class ChannelModel {
public void addRectObstacle(double startX, double startY, double width, double height, boolean notify) {
myObstacleWorld.addObstacle(startX, startY, width, height);
if (notify)
if (notify) {
settingsObservable.notifySettingsChanged();
}
}
/**
* @return Number of registered obstacles
@ -405,17 +403,21 @@ public class ChannelModel {
// Check which sides of the rectangle the test line passes through
Vector<Line2D> intersectedSides = new Vector<Line2D>();
if (rectangleLower.intersectsLine(testLine))
if (rectangleLower.intersectsLine(testLine)) {
intersectedSides.add(rectangleLower);
}
if (rectangleUpper.intersectsLine(testLine))
if (rectangleUpper.intersectsLine(testLine)) {
intersectedSides.add(rectangleUpper);
}
if (rectangleLeft.intersectsLine(testLine))
if (rectangleLeft.intersectsLine(testLine)) {
intersectedSides.add(rectangleLeft);
}
if (rectangleRight.intersectsLine(testLine))
if (rectangleRight.intersectsLine(testLine)) {
intersectedSides.add(rectangleRight);
}
// If no sides are intersected, return null (no intersection)
if (intersectedSides.isEmpty()) {
@ -449,8 +451,9 @@ public class ChannelModel {
return null;
}
if (intersectingLinePoints.get(0).distance(intersectingLinePoints.get(1)) < 0.001)
if (intersectingLinePoints.get(0).distance(intersectingLinePoints.get(1)) < 0.001) {
return null;
}
return new Line2D.Double(
intersectingLinePoints.get(0),
@ -472,9 +475,10 @@ public class ChannelModel {
double dy2 = secondLine.getY2() - secondLine.getY1();
double det = (dx2*dy1-dy2*dx1);
if (det == 0.0)
if (det == 0.0) {
// Lines parallell, not intersecting
return null;
}
double mu = ((firstLine.getX1() - secondLine.getX1())*dy1 - (firstLine.getY1() - secondLine.getY1())*dx1)/det;
if (mu >= 0.0 && mu <= 1.0) {
@ -502,9 +506,10 @@ public class ChannelModel {
double dy2 = secondLine.getY2() - secondLine.getY1();
double det = (dx2*dy1-dy2*dx1);
if (det == 0.0)
if (det == 0.0) {
// Lines parallell, not intersecting
return null;
}
double mu = ((firstLine.getX1() - secondLine.getX1())*dy1 - (firstLine.getY1() - secondLine.getY1())*dx1)/det;
Point2D.Double intersectionPoint = new Point2D.Double((secondLine.getX1() + mu*dx2),
@ -577,10 +582,11 @@ public class ChannelModel {
Rectangle2D bounds = reflectingSide.getBounds2D();
double newPsuedoSourceX = source.getX();
double newPsuedoSourceY = source.getY();
if (bounds.getHeight() > bounds.getWidth())
if (bounds.getHeight() > bounds.getWidth()) {
newPsuedoSourceX = 2*reflectingSide.getX1() - newPsuedoSourceX;
else
} else {
newPsuedoSourceY = 2*reflectingSide.getY1() - newPsuedoSourceY;
}
// Recursively build and add subtrees
RayData newRayData = new RayData(
@ -663,10 +669,11 @@ public class ChannelModel {
// Check if direct path exists
justBeforeDestination = sourcePoint;
if (!getParameterBooleanValue("rt_disallow_direct_path"))
if (!getParameterBooleanValue("rt_disallow_direct_path")) {
directPathExists = isDirectPath(justBeforeDestination, dest);
else
} else {
directPathExists = false;
}
} else if (type == RayData.RayType.REFRACTION && pseudoSourceToDest.intersectsLine(line)) {
@ -742,8 +749,9 @@ public class ChannelModel {
currentPath.addPoint(newestPoint, currentlyTracedNodeType);
// Check that this ray subpath is long enough to be considered
if (newestPoint.distance(lastPoint) < 0.01)
if (newestPoint.distance(lastPoint) < 0.01) {
pathBroken = true;
}
} else {
// Trace further up in the tree
@ -764,18 +772,20 @@ public class ChannelModel {
currentPath.addPoint(newestPoint, currentlyTracedNodeType);
// Check that this ray subpath is long enough to be considered
if (newestPoint == null || lastPoint == null || newestPoint.distance(lastPoint) < 0.01)
if (newestPoint == null || lastPoint == null || newestPoint.distance(lastPoint) < 0.01) {
pathBroken = true;
}
}
// Subpath must be double-direct if from diffraction
if (currentlyTracedNodeType == RayData.RayType.DIFFRACTION && !isDirectPath(lastPoint, newestPoint)) {
pathBroken = true;
}
if (pathBroken)
if (pathBroken) {
break;
}
}
// Save ray path
if (!pathBroken) {
@ -827,11 +837,12 @@ public class ChannelModel {
if (visibleSides.get(i).intersectsLine(sourceToDest)) {
// Check that intersection point is not destination
Point2D intersectionPoint = getIntersectionPointInfinite(visibleSides.get(i), sourceToDest);
if (dest.distance(intersectionPoint) > 0.01)
if (dest.distance(intersectionPoint) > 0.01) {
return false;
}
}
}
}
return true;
}
@ -942,16 +953,18 @@ public class ChannelModel {
Vector<AngleInterval> unhandledAngles = new Vector<AngleInterval>();
if (lookThrough != null) {
if (angleInterval == null)
if (angleInterval == null) {
unhandledAngles.add(AngleInterval.getAngleIntervalOfLine(source, lookThrough));
else
unhandledAngles.add(AngleInterval.getAngleIntervalOfLine(source, lookThrough).intersectWith(angleInterval));
} else {
if (angleInterval == null)
unhandledAngles.add(AngleInterval.getAngleIntervalOfLine(source, lookThrough).intersectWith(angleInterval));
}
} else {
if (angleInterval == null) {
unhandledAngles.add(new AngleInterval(0, 2*Math.PI));
else
} else {
unhandledAngles.add(angleInterval);
}
}
// Do forever (will break when no more unhandled angles exist)
while (!unhandledAngles.isEmpty()) {
@ -985,22 +998,26 @@ public class ChannelModel {
Rectangle2D obstacle = visibleObstacleCandidates.get(i);
int outcode = obstacle.outcode(source);
if ((outcode & Rectangle2D.OUT_BOTTOM) != 0)
if ((outcode & Rectangle2D.OUT_BOTTOM) != 0) {
visibleLineCandidates.add(
new Line2D.Double(obstacle.getMinX(), obstacle.getMaxY(), obstacle.getMaxX(), obstacle.getMaxY()));
}
if ((outcode & Rectangle2D.OUT_TOP) != 0)
if ((outcode & Rectangle2D.OUT_TOP) != 0) {
visibleLineCandidates.add(
new Line2D.Double(obstacle.getMinX(), obstacle.getMinY(), obstacle.getMaxX(), obstacle.getMinY()));
}
if ((outcode & Rectangle2D.OUT_LEFT) != 0)
if ((outcode & Rectangle2D.OUT_LEFT) != 0) {
visibleLineCandidates.add(
new Line2D.Double(obstacle.getMinX(), obstacle.getMinY(), obstacle.getMinX(), obstacle.getMaxY()));
}
if ((outcode & Rectangle2D.OUT_RIGHT) != 0)
if ((outcode & Rectangle2D.OUT_RIGHT) != 0) {
visibleLineCandidates.add(
new Line2D.Double(obstacle.getMaxX(), obstacle.getMinY(), obstacle.getMaxX(), obstacle.getMaxY()));
}
}
//logger.info("Line candidates count = " + visibleLineCandidates.size());
if (visibleLineCandidates.isEmpty()) {
//logger.info("Visible line candidates empty");
@ -1039,7 +1056,9 @@ public class ChannelModel {
).intersectsLine(lookThrough)) {
croppedVisibleLineCandidates.add(lineCandidate);
} // else Skip line
} else croppedVisibleLineCandidates.add(lineCandidate);
} else {
croppedVisibleLineCandidates.add(lineCandidate);
}
}
@ -1094,7 +1113,9 @@ public class ChannelModel {
).intersectsLine(lookThrough)) {
croppedVisibleLineCandidates.add(newCropped);
} // else Skip line
} else croppedVisibleLineCandidates.add(newCropped);
} else {
croppedVisibleLineCandidates.add(newCropped);
}
}
}
@ -1197,20 +1218,25 @@ public class ChannelModel {
Vector<AngleInterval> tempVector1 =
AngleInterval.intersect(unhandledAngles, intersectedInterval);
if (tempVector1 != null)
for (int k=0; k < tempVector1.size(); k++)
if (tempVector1 != null) {
for (int k=0; k < tempVector1.size(); k++) {
if (tempVector1.get(k) != null && !tempVector1.get(k).isEmpty()) {
newIntervalsToAdd.add(tempVector1.get(k));
}
}
}
}
// Add angle interval of visible candidate without shadow candidate
Vector<AngleInterval> tempVector2 =
visibleLineCandidateAngleInterval.subtract(shadowLineCandidateAngleInterval);
if (tempVector2 != null)
for (int k=0; k < tempVector2.size(); k++)
if (tempVector2.get(k) != null && !tempVector2.get(k).isEmpty())
if (tempVector2 != null) {
for (int k=0; k < tempVector2.size(); k++) {
if (tempVector2.get(k) != null && !tempVector2.get(k).isEmpty()) {
newIntervalsToAdd.addAll(AngleInterval.intersect(unhandledAngles, tempVector2.get(k)));
}
}
}
// Subtract angle interval of visible candidate
unhandledAngles = AngleInterval.subtract(unhandledAngles, visibleLineCandidateAngleInterval);
@ -1233,9 +1259,10 @@ public class ChannelModel {
}
}
if (!unshadowed)
if (!unshadowed) {
break;
}
}
if (unhandledAnglesChanged) {
//logger.info("Unhandled angles changed, restarting..");
@ -1438,8 +1465,9 @@ public class ChannelModel {
double pathLengthDiff = Math.abs(pathLengths[i] - pathLengths[bestSignalNr]);
// Update delay spread TODO Now considering best signal, should be first or mean?
if (pathLengthDiff > delaySpread)
if (pathLengthDiff > delaySpread) {
delaySpread = pathLengthDiff;
}
// Update root-mean-square delay spread TODO Now considering best signal time, should be mean delay?
@ -1481,10 +1509,10 @@ public class ChannelModel {
// Using formula (dB)
// Received power = Output power + System gain + Transmitter gain + Path Loss + Receiver gain
// TODO Update formulas
Random random = new Random();
double outputPower = getParameterDoubleValue("tx_power");
double systemGain = getParameterDoubleValue("system_gain_mean");
if (getParameterBooleanValue("apply_random")) {
Random random = new Random(); /* TODO Use main random generator? */
systemGain += Math.sqrt(getParameterDoubleValue("system_gain_var")) * random.nextGaussian();
} else {
accumulatedVariance += getParameterDoubleValue("system_gain_var");
@ -1496,8 +1524,9 @@ public class ChannelModel {
logger.info("Resulting received signal strength:\t" + receivedPower + " (" + accumulatedVariance + ")");
}
if (dataType == TransmissionData.DELAY_SPREAD || dataType == TransmissionData.DELAY_SPREAD_RMS)
if (dataType == TransmissionData.DELAY_SPREAD || dataType == TransmissionData.DELAY_SPREAD_RMS) {
return new double[] {delaySpread, delaySpreadRMS};
}
return new double[] {receivedPower, accumulatedVariance};
}
@ -1558,10 +1587,12 @@ public class ChannelModel {
double noiseVariance = getParameterDoubleValue("bg_noise_var");
double noiseMean = getParameterDoubleValue("bg_noise_mean");
if (interference > noiseMean)
if (interference > noiseMean) {
noiseMean = interference;
}
if (getParameterBooleanValue("apply_random")) {
Random random = new Random(); /* TODO Use main random generator? */
noiseMean += Math.sqrt(noiseVariance) * random.nextGaussian();
noiseVariance = 0;
}
@ -1613,10 +1644,11 @@ public class ChannelModel {
}
// If not random varianble, probability is either 1 or 0
if (snrVariance == 0)
if (snrVariance == 0) {
return new double[] {
threshold - snrMean > 0 ? 0:1, signalStrength
};
}
double snrStdDev = Math.sqrt(snrVariance);

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: MRM.java,v 1.7 2008/03/18 16:37:35 fros4943 Exp $
* $Id: MRM.java,v 1.8 2009/02/18 12:08:10 fros4943 Exp $
*/
package se.sics.mrm;
@ -63,7 +63,7 @@ public class MRM extends AbstractRadioMedium {
private ChannelModel currentChannelModel = null;
private Simulation mySimulation = null;
private Random random = new Random();
private Random random = null;
/**
* Notifies observers when this radio medium has changed settings.
@ -75,6 +75,7 @@ public class MRM extends AbstractRadioMedium {
*/
public MRM(Simulation simulation) {
super(simulation);
random = simulation.getRandomGenerator();
// Create the channel model
currentChannelModel = new ChannelModel();

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: SkyMote.java,v 1.10 2009/02/07 16:38:51 joxe Exp $
* $Id: SkyMote.java,v 1.11 2009/02/18 12:08:37 fros4943 Exp $
*/
package se.sics.cooja.mspmote;
@ -74,7 +74,7 @@ public class SkyMote extends MspMote {
// Add position interface
Position motePosition = new Position(this);
Random random = new Random();
Random random = new Random(); /* Do not use main random generator for positioning */
motePosition.setCoordinates(random.nextDouble()*100, random.nextDouble()*100, random.nextDouble()*100);
moteInterfaceHandler.addInterface(motePosition);

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: DummyVisualizer.java,v 1.1 2008/10/28 16:56:59 fros4943 Exp $
* $Id: DummyVisualizer.java,v 1.2 2009/02/18 12:07:42 fros4943 Exp $
*/
import java.awt.*;
@ -50,8 +50,6 @@ public class DummyVisualizer extends Visualizer2D {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(DummyVisualizer.class);
private Random random = new Random();
public DummyVisualizer(Simulation simulationToVisualize, GUI gui) {
super(simulationToVisualize, gui);
setTitle("Dummy Visualizer");
@ -60,6 +58,7 @@ public class DummyVisualizer extends Visualizer2D {
}
public Color[] getColorOf(Mote m) {
Random random = new Random(); /* Do not use main random generator */
Color moteColors[] = new Color[2];
/* Outer color */
@ -76,6 +75,7 @@ public class DummyVisualizer extends Visualizer2D {
/* TODO Analyze data - determine color */
Random random = new Random(); /* Do not use main random generator */
return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}

View File

@ -26,10 +26,11 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: RandomIPDistributor.java,v 1.1 2006/08/21 12:13:06 fros4943 Exp $
* $Id: RandomIPDistributor.java,v 1.2 2009/02/18 12:07:19 fros4943 Exp $
*/
package se.sics.cooja.ipdistributors;
import java.util.Random;
import java.util.Vector;
import se.sics.cooja.*;
@ -42,16 +43,17 @@ import se.sics.cooja.*;
@ClassDescription("Random (10.10.?.?)")
public class RandomIPDistributor extends IPDistributor {
private Random random = new Random(); /* Do not use main random generator for setup */
/**
* Creates a random IP distributor.
* @param newMotes All motes which later will be assigned IP numbers.
*/
public RandomIPDistributor(Vector<Mote> newMotes) {
// NOP
}
public String getNextIPAddress() {
return "" + 10 + "." + 10 + "." + (Math.round(Math.random()*20) + 1) + "." + (Math.round(Math.random()*20) + 1);
return "" + 10 + "." + 10 + "." + (1+random.nextInt(20)) + "." + (1+random.nextInt(20));
}
}

View File

@ -26,10 +26,12 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: RandomPositioner.java,v 1.1 2006/08/21 12:13:11 fros4943 Exp $
* $Id: RandomPositioner.java,v 1.2 2009/02/18 12:07:19 fros4943 Exp $
*/
package se.sics.cooja.positioners;
import java.util.Random;
import se.sics.cooja.*;
/**
@ -41,6 +43,8 @@ import se.sics.cooja.*;
public class RandomPositioner extends Positioner {
double startX, endX, startY, endY, startZ, endZ;
private Random random = new Random(); /* Do not use main random generator for setup */
/**
* Creates a random positioner.
* @param totalNumberOfMotes Total number of motes
@ -65,9 +69,9 @@ public class RandomPositioner extends Positioner {
public double[] getNextPosition() {
return new double[] {
startX + Math.random()*(endX - startX),
startY + Math.random()*(endY - startY),
startZ + Math.random()*(endZ - startZ)
startX + random.nextDouble()*(endX - startX),
startY + random.nextDouble()*(endY - startY),
startZ + random.nextDouble()*(endZ - startZ)
};
}