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

📄 attractorfield.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;import java.util.Vector;public class AttractorField {	class Attractor {		public final Position pos;		public final double level;		public final double stdDev;				public Attractor(Position pos, double level, double stdDev) {			this.pos = pos;			this.level = level;			this.stdDev = stdDev;		}	}	protected Vector attractors = new Vector();	/** Sum over all level-values. */	protected double lTotal = 0.0;		protected final double x;	protected final double y;		public AttractorField(double x, double y) {		this.x = x;		this.y = y;	}		public void add(Position attractor, double level, double stdDev) {		attractors.addElement(new Attractor(attractor, level, stdDev));		lTotal += level;	}		public void add(double[] param) {		for (int p = 0; p < param.length; p += 4)			if (param.length - p >= 4)				add(new Position(param[p], param[p+1]), param[p+2], param[p+3]);			else				System.out.println("warning: attraction field argument list has wrong number of elements!");	}	public Position getPos(double rndUniform1, double rndUniform2, double rndGaussian) {		double r = rndUniform1 * lTotal;		double s = 0.0;		Attractor a = null;		int i = 0;		while ((i < attractors.size()) && (r >= s)) {			a = (Attractor)attractors.elementAt(i++);			s += a.level;		}		if ((r >= s) || (a == null)) {			System.out.println("AttractorField.getPos: Somethings going wrong here");			System.exit(0);		}		double dir = Math.PI * rndUniform2; // only half circle cause we can have negative distance as well		double dist = rndGaussian * a.stdDev;		Position rVal = new Position(a.pos.x + Math.cos(dir) * dist, a.pos.y + Math.sin(dir) * dist);		if ((rVal.x >= 0.0) && (rVal.y >= 0.0) && (rVal.x <= x) && (rVal.y <= y))			return rVal;		else			return null;	}		public Scenario createScenario(int nodes) {		Scenario s = new Scenario(nodes, x, y, 1.0, 0.0, System.currentTimeMillis());		java.util.Random rnd = new java.util.Random();		for (int i = 0; i < nodes; i++) {			Position pos;			do {				pos = getPos(rnd.nextDouble(), rnd.nextDouble(), rnd.nextGaussian());			} while (pos == null);			s.getNode(i).add(0.0, pos);		}		return s;	}		public static void main(String[] args) {		AttractorField a = new AttractorField(1000.0, 1000.0);		java.util.Random rnd = new java.util.Random();		a.add(new Position(250.0, 250.0), 1.0, 350.0);		a.add(new Position(250.0, 750.0), 1.0, 350.0);		a.add(new Position(750.0, 250.0), 1.0, 350.0);		a.add(new Position(750.0, 750.0), 1.0, 350.0);		for (int i = 0; i < 500; i++) {			Position p = null;			while (p == null) {				p = a.getPos(rnd.nextDouble(), rnd.nextDouble(), rnd.nextGaussian());			}			System.out.println("" + p.x + " " + p.y);		}	}}

⌨️ 快捷键说明

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