⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomwaypoint.java

📁 能在 ns-2 下模拟出一个使用 swarm 算法通讯的网络
💻 JAVA
字号:
/******************************************************************************* ** BonnMotion - a mobility scenario generation and analysis tool             ** ** Copyright (C) 2002, 2003 University of Bonn                               ** **                                                                           ** ** This program 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 2 of the License, or         ** ** (at your option) any later version.                                       ** **                                                                           ** ** This program 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 this program; if not, write to the Free Software               ** ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA ** *******************************************************************************/package edu.bonn.cs.iv.bonnmotion.models;import java.io.*;import java.util.*;import edu.bonn.cs.iv.bonnmotion.*;/** Application to construct Randomwaypoint mobility scenarios. */public class RandomWaypoint extends RandomSpeedBase {	private static final String MODEL_NAME = "RandomWaypoint";	/** Restrict the mobiles' movements: 1 . */	protected int dim = 3;	/** Choose positions from a circle fitting into the simulation area rather than from the simulation area itself. */	protected boolean circular = false;	/** Parameters for the attractor field used to choose destinations. */	protected double[] aFieldParams = null;	public RandomWaypoint(int nodes, double x, double y, double duration, double ignore, long randomSeed, double minspeed, double maxspeed, double maxpause, int dim, boolean circular) {		super(nodes, x, y, duration, ignore, randomSeed, minspeed, maxspeed, maxpause);		this.dim = dim;		this.circular = circular;		generate();	}		public RandomWaypoint( String[] args ) {		go( args );	}	public void go( String[] args ) {		super.go(args);		generate();	}	public void generate() {		preGeneration();				AttractorField aField = null;		if (aFieldParams != null) {			aField = new AttractorField(x, y);			aField.add(aFieldParams);		}		double x2 = x/2.0;		double y2 = y/2.0;		double r = (x2 < y2) ? x2 : y2;				for (int i = 0; i < node.length; i++) {			node[i] = new MobileNode();			double t = 0.0;			Position src;			do {				src = new Position(x * randomNextDouble(), y * randomNextDouble());			} while (circular && (Math.sqrt((src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2)) > r));			while (t < duration) {				Position dst;				if (! node[i].add(t, src))					throw new RuntimeException("RandomWaypoint.go: error while adding waypoint (1)");				do {					switch (dim) {					case 1:						dst = new Position(x * randomNextDouble(), src.y);						break;					case 2:						switch ((int)(randomNextDouble() * 2.0)) {						case 0:							dst = new Position(x * randomNextDouble(), src.y);							break;						case 1:							dst = new Position(src.x, y * randomNextDouble());							break;						default:							throw new RuntimeException("RandomWaypoint.go: This is impossible - how can (int)(randomNextDouble() * 2.0) be something other than 0 or 1?!");						}						break;					case 3:						if (aField == null)							dst = new Position(x * randomNextDouble(), y * randomNextDouble());						else							do {								dst = aField.getPos(randomNextDouble(), randomNextDouble(), randomNextGaussian());							} while (dst == null);						break;					default:						throw new RuntimeException("RandomWaypoint.go: dimension may only be of value 1, 2 or 3.");					}				} while (circular && (Math.sqrt((dst.x - x2) * (dst.x - x2) + (dst.y - y2) * (dst.y - y2)) > r));				double speed = (maxspeed - minspeed) * randomNextDouble() + minspeed;				t += src.distance(dst) / speed;				if (! node[i].add(t, dst))					throw new RuntimeException("RandomWaypoint.go: error while adding waypoint (2)");				if ((t < duration) && (maxpause > 0.0)) {					double pause = maxpause * randomNextDouble();					t += pause;				}				src = dst;			}		}		postGeneration();	}	protected boolean parseArg(String key, String value) {		if (key.equals("model") ) {			if (!value.equals(MODEL_NAME)) {				System.out.println("wrong model " + value);				System.exit(-1);			}			return true;		} else if (	key.equals("dim") ) {			dim = Integer.parseInt(value);			return true;		} else return super.parseArg(key, value);	}	public void write( String _name ) throws FileNotFoundException, IOException {		String[] p = new String[2];		p[0] = "model="+MODEL_NAME;		p[1] = "dim=" + dim;		super.write(_name, p);	}	protected boolean parseArg(char key, String val) {		switch (key) {			case 'a':				aFieldParams = parseDoubleArray(val);				if (dim != 3)					System.out.println("warning: attractor field not used if dim != 3");				return true;			case 'c': // "circular"				circular = true;				return true;			case 'o': // "dimensiOn"				dim = Integer.parseInt(val);				if ((dim < 1) || (dim > 3)) {					System.out.println("dimension must be between 1 and 3");					System.exit(0);				}				if ((aFieldParams != null) && (dim != 3))					System.out.println("warning: attractor field not used if dim != 3");				return true;			default:				return super.parseArg(key, val);		}	}		public static void printHelp() {		RandomSpeedBase.printHelp();		System.out.println( MODEL_NAME + ":");		System.out.println("\t-a <attractor parameters>");		System.out.println("\t-c [circular]");		System.out.println("\t-o <dimension: 1: x only, 2: x or y, 3: x and y>");	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -