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

📄 gaussmarkov.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 Gauss-Markov mobility scenarios. */public class GaussMarkov extends Scenario {	private static final String MODEL_NAME = "GaussMarkov";	/** Update frequency [s]. */	protected double updateFrequency = 2.5;	/** Maximum speed [m/s]. */	protected double maxspeed = 1.5;	/** Border width [m]. */	protected double angleStdDev = 0.125 * Math.PI;	/** Speed standard deviation [m/s]. */	protected double speedStdDev = 0.5;	protected double inputX=0;	protected double inputY=0;	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("updateFrequency")) {			updateFrequency = Double.parseDouble(value);			return true;		} else if (key.equals("maxspeed")) {			maxspeed = Double.parseDouble(value);			return true;		} else if (key.equals("angleStdDev")) {			angleStdDev = Double.parseDouble(value);			return true;		} else if (key.equals("speedStdDev")) {			speedStdDev = Double.parseDouble(value);			return true;		} else if (key.equals("randomSeed")) {			randomSeed = Long.parseLong(value);			return true;		} else if (key.equals("x")) {			return true;		} else if (key.equals("y")) {			return true;		} else if (key.equals("inputX")) {			x = Double.parseDouble(value);			return true;		} else if (key.equals("inputY")) {			y = Double.parseDouble(value);			return true;		} else			return super.parseArg(key, value);	}	protected boolean parseArg(char key, String val) {		switch (key) {			case 'a' :				angleStdDev = Double.parseDouble(val);				return true;			case 'h' :				maxspeed = Double.parseDouble(val);				return true;			case 'q' :				updateFrequency = Double.parseDouble(val);				return true;			case 's' :				speedStdDev = Double.parseDouble(val);				return true;			default :				return super.parseArg(key, val);		}	}	public GaussMarkov(String[] args) {		go(args);	}		public GaussMarkov(		int nodes,		double x,		double y,		double duration,		double ignore,		long randomSeed,		double updateFrequency,		double maxspeed,		double angleStdDev,		double speedStdDev) {		super(nodes, x, y, duration, ignore, randomSeed);		this.updateFrequency = updateFrequency;		this.maxspeed = maxspeed;		this.angleStdDev = angleStdDev;		this.speedStdDev = speedStdDev;		generate();	}	public void go(String[] args) {		super.go(args);		generate();	}	public void generate() {		preGeneration();		double maxX = x;		double maxY = y;		double minX = 0;		double minY = 0;		for (int i = 0; i < node.length; i++) {			node[i] = new MobileNode();			double t = 0.0;			Position src;			src = new Position(x * randomNextDouble(), y * randomNextDouble());			if (!node[i].add(0.0, src)) {				System.out.println("GaussMarkov.<init>: error while adding node movement (1)");				System.exit(0);			}			double dir = randomNextDouble() * 2 * Math.PI;			double speed = randomNextDouble() * maxspeed;			while (t < duration) {				double t1 = t + updateFrequency;				dir = getNewDir(dir, src);				speed = getNewSpeed(speed);				if (speed > 0.0) {					Position dst =						new Position(							src.x + Math.cos(dir) * updateFrequency * speed,							src.y + Math.sin(dir) * updateFrequency * speed);					if (dst.x < minX)						minX = dst.x;					else if (dst.x > maxX)						maxX = dst.x;					if (dst.y < minY)						minY = dst.y;					else if (dst.y > maxY)						maxY = dst.y;					if (!node[i].add(t1, dst)) {						System.out.println(							"GaussMarkov.<init>: error while adding node movement (2)");						System.exit(0);					}					src = dst;				}				t = t1;			}		}			inputX = x;		inputY = y;		// setting new borders and shifting the waypoints ...		double shiftX = Math.abs(minX);		double shiftY = Math.abs(minY);		x = Math.ceil(maxX + shiftX);		y = Math.ceil(maxY + shiftY);		for (int i = 0; i < node.length; i++)			node[i].shiftPos( shiftX, shiftY );		postGeneration();	}	public double getNewDir(double oldDir, Position pos) {		// move away from the border in case we are getting too close		if (pos.x < 0)			if (pos.y < 0)				oldDir = 0.25 * Math.PI;			else if (pos.y > y)				oldDir = 1.75 * Math.PI;			else				oldDir = 0.0;		else if (pos.x > x)			if (pos.y < 0)				oldDir = 0.75 * Math.PI;			else if (pos.y > y)				oldDir = 1.25 * Math.PI;			else				oldDir = Math.PI;		else if (pos.y < 0)			oldDir = 0.5 * Math.PI;		else if (pos.y > y)			oldDir = 1.5 * Math.PI;		return randomNextGaussian() * angleStdDev + oldDir;	}	public double getNewSpeed(double oldSpeed) {		double speed = oldSpeed + randomNextGaussian() * speedStdDev;		if (speed < 0.0)			speed = 0.0;		else if (speed > maxspeed)			speed = maxspeed;		return speed;	}	public void write(String _name) throws FileNotFoundException, IOException {		String[] p = new String[7];		p[0] = "model=" + MODEL_NAME;		p[1] = "updateFrequency=" + updateFrequency;		p[2] = "maxspeed=" + maxspeed;		p[3] = "angleStdDev=" + angleStdDev;		p[4] = "speedStdDev=" + speedStdDev;		p[5] = "inputX=" + inputX;		p[6] = "inputY=" + inputY;				super.write(_name, p);	}	public static void printHelp() {		Scenario.printHelp();		System.out.println( MODEL_NAME + ":" );		System.out.println("\t-a <angle standard deviation>");		System.out.println("\t-h <max. speed>");		System.out.println("\t-q <speed, angle update frequency>");		System.out.println("\t-s <speed standard deviation>");	}}

⌨️ 快捷键说明

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