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

📄 rpgm.c

📁 无线通信的各种运动模型。适用于移动通信、无线传感器网络等领域。 包括:Random walk、random waypoint、random direction、boundless simulation
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************   File Name: rpgm.c*   Purpose: Reference Point Group Mobility Model (RPGM)*   Author: Jeff Boleng*   Date Created: some time in the spring of 2002**   Copyright (C) 2004  Toilers Research Group -- Colorado School of Mines**   Please see COPYRIGHT.TXT and LICENSE.TXT for copyright and license*   details.* *   Bug fixes:* *   Sept. 16th, 2004 *   - reported by Ricky Martin*   - Code fixed by Ricky Martin*   - Verified by Feng Sun*   Fixed the maxX, maxY problem in ns2 when X destination value equals to *   the maximum X value of the simulation range.**   Edited 12 July 2006 by Jon Petty:  *   Changed seed code to rand((int)time(NULL) + (int)getpid())*******************************************************************************/ #include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#include <unistd.h>#define rand48#define true  1#define false 0/* double startTime = 1000.0; */double startTime = 0.0;#define minX 0.00001#define minY 0.00001/*See bug fix Sept. 16th, 2004 */#define ZERO 0.000000000001double getRand(){  #ifdef rand48	return drand48();  #else	return (double)rand()/(double)RAND_MAX;  #endif}int main(int argc, char *argv[]){	int g, n, i;	int numGroups=0;	int nextNode=0;	int nodesPerGroup=0;	double refPtSep=0.0, nodeSep=0.0;	double maxX=0.0, maxY=0.0;	double endTime=0.0, lowest;	double speedMean=0.0, speedDelta=0.0;	double pauseMean=0.0, pauseDelta=0.0;	int *movingNode;	double *nextEventNode;	double *nextEventRefPt;	double *startEventRefPt;	double *xLocNode, *yLocNode;	int *movingRefPt;	double *speedRefPt;	double *xLocRefPt, *yLocRefPt;	double *xDestRefPt, *yDestRefPt;	double newX, newY, dist;	double speedLow, pauseLow;	double speedRange, pauseRange;	char output;	double minXlocal, maxXlocal, minYlocal, maxYlocal;	double minXdest, maxXdest, minYdest, maxYdest;	double speed;	int nodeIDstart = 0;	if ((argc == 13) || (argc == 14))	{		if (argc == 13)		{			nodeIDstart = 0;		} else {			nodeIDstart = atoi(argv[13]);		}		numGroups      = atoi(argv[1]);		nodesPerGroup  = atoi(argv[2]);		refPtSep       = atof(argv[3]);		nodeSep        = atof(argv[4]);		maxX           = atof(argv[5]);		maxY           = atof(argv[6]);		endTime        = atof(argv[7]);		speedMean      = atof(argv[8]);		speedDelta     = atof(argv[9]);		pauseMean      = atof(argv[10]);		pauseDelta     = atof(argv[11]);		output         = argv[12][0];		fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");		fprintf(stdout, "#\tnumGroups      = %6d\n", numGroups);		fprintf(stdout, "#\tnodesPerGroup  = %6d\n", nodesPerGroup);		fprintf(stdout, "#\trefPtSep       = %9.2f\n", refPtSep);		fprintf(stdout, "#\tnodeSep        = %9.2f\n", nodeSep);		fprintf(stdout, "#\tmaxX           = %9.2f\n", maxX);		fprintf(stdout, "#\tmaxY           = %9.2f\n", maxY);		fprintf(stdout, "#\tendTime        = %9.2f\n", endTime);		fprintf(stdout, "#\tspeedMean      = %9.2f\n", speedMean);		fprintf(stdout, "#\tspeedDelta     = %9.2f\n", speedDelta);		fprintf(stdout, "#\tpauseMean      = %9.2f\n", pauseMean);		fprintf(stdout, "#\tpauseDelta     = %9.2f\n", pauseDelta);		fprintf(stdout, "#\toutput         = %6c\n", output);		fprintf(stdout, "#\tnodeIDstart    = %6d\n", nodeIDstart);		fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");	} else {		fprintf(stdout, "Usage:  rpgm <number of groups>\n");		fprintf(stdout, "             <nodes per group>\n");		fprintf(stdout, "             <ref. pt. separation>\n");		fprintf(stdout, "             <node separation from ref. pt.>\n");		fprintf(stdout, "             <max-x> <max-y> <end time>\n");		fprintf(stdout, "             <speed mean> <speed delta>\n");		fprintf(stdout, "             <pause time> <pause time delta>\n");		fprintf(stdout, "             <'N' or 'G'>\n");		fprintf(stdout, "             'N' implies NS2 mobility file\n");		fprintf(stdout, "             'G' implies gnuplot path file\n");		fprintf(stdout, "             [<node ID start> default 0]\n");		return -1;	}	endTime += startTime;	if (output == 'N')	{		fprintf(stdout, "# output format is NS2\n");	} else if (output == 'G') {		fprintf(stdout, "# output format is gnuplot\n");		fprintf(stdout, "set xrange[0:%.12f]\n", maxX);		fprintf(stdout, "set yrange[0:%.12f]\n", maxY);		//    if (numGroups != 1)		//    {		//      fprintf(stderr, "Gnuplot output is only possible with one mobile group.\n");		//      return -1;		//    } else {		fprintf(stdout, "set multiplot\n");		fprintf(stdout, "plot \'-\' using 3:4 notitle with linespoints\n");		//    }	} else {		fprintf(stderr, "Unknown output type requested\n");		return -1;	}  #ifdef rand48	srand48((int)time(NULL) + (int)getpid());  #else	srand((int)time(NULL) + (int)getpid());  #endif	speedLow = speedMean-speedDelta;	pauseLow = pauseMean-pauseDelta;	speedRange = 2*speedDelta;	pauseRange = 2*pauseDelta;	movingNode = (int*)malloc(sizeof(int)*numGroups*nodesPerGroup);	xLocNode = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	yLocNode = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	nextEventNode = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	movingRefPt = (int*)malloc(sizeof(int)*numGroups*nodesPerGroup);	speedRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	xLocRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	yLocRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	xDestRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	yDestRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	nextEventRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	startEventRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);	fprintf(stdout, "#\tInitial positions:\n");	fprintf(stdout, "#\t  (node initial positions are also initial reference point positions)\n");	for (g=0; g<numGroups; g++)	{		/* node 0 in each group is also the group reference point */		/* it can be placed in a totally random fashion */		/* each other point in the group must be placed in reference to it */		movingNode[g*nodesPerGroup] = movingRefPt[g*nodesPerGroup] = (getRand()>=0.5)?true:false;		xLocNode[g*nodesPerGroup] = xLocRefPt[g*nodesPerGroup] = getRand()*maxX;		yLocNode[g*nodesPerGroup] = yLocRefPt[g*nodesPerGroup] = getRand()*maxY;		xDestRefPt[g*nodesPerGroup] = getRand()*maxX;		yDestRefPt[g*nodesPerGroup] = getRand()*maxY;		nextEventNode[g*nodesPerGroup] = nextEventRefPt[g*nodesPerGroup] = startEventRefPt[g*nodesPerGroup] = startTime;		speedRefPt[g*nodesPerGroup] = startTime;		minXlocal = xLocRefPt[g*nodesPerGroup]-refPtSep;		if (minXlocal < minX) minXlocal = minX;		maxXlocal = xLocRefPt[g*nodesPerGroup]+refPtSep;		if (maxXlocal >= maxX) maxXlocal = maxX-ZERO;		minYlocal = yLocRefPt[g*nodesPerGroup]-refPtSep;		if (minYlocal < minY) minYlocal = minY;		maxYlocal = yLocRefPt[g*nodesPerGroup]+refPtSep;		if (maxYlocal >= maxY) maxYlocal = maxY-ZERO;		minXdest = xDestRefPt[g*nodesPerGroup]-refPtSep;		if (minXdest < minX) minXdest = minX;		maxXdest = xDestRefPt[g*nodesPerGroup]+refPtSep;		if (maxXdest >= maxX) maxXdest = maxX-ZERO;		minYdest = yDestRefPt[g*nodesPerGroup]-refPtSep;		if (minYdest < minY) minYdest = minY;		maxYdest = yDestRefPt[g*nodesPerGroup]+refPtSep;		if (maxYdest >= maxY) maxYdest = maxY-ZERO;		for (n=1; n<nodesPerGroup; n++)		{			/* movingNode[g*nodesPerGroup+n] = movingRefPt[g*nodesPerGroup+n] = (getRand()>=0.5)?true:false; */			/* all group member nodes have the same moving status as the group reference point (node 0) */			movingNode[g*nodesPerGroup+n] = movingRefPt[g*nodesPerGroup+n] = movingRefPt[g*nodesPerGroup];			xLocNode[g*nodesPerGroup+n] = xLocRefPt[g*nodesPerGroup+n] = minXlocal + getRand()*(maxXlocal - minXlocal);			yLocNode[g*nodesPerGroup+n] = yLocRefPt[g*nodesPerGroup+n] = minYlocal + getRand()*(maxYlocal - minYlocal);			xDestRefPt[g*nodesPerGroup+n] = minXdest + getRand()*(maxXdest - minXdest);			yDestRefPt[g*nodesPerGroup+n] = minYdest + getRand()*(maxYdest - minYdest);			nextEventNode[g*nodesPerGroup+n] = nextEventRefPt[g*nodesPerGroup+n] = startEventRefPt[g*nodesPerGroup+n] = startTime;			speedRefPt[g*nodesPerGroup+n] = startTime;		}	}	for (g=0; g<numGroups; g++)	{		fprintf(stdout, "#\t### Group %d ###\n", g);		fprintf(stdout, "#\tGroup %d:  %s moving\n", g, ((movingNode[g*nodesPerGroup+n])?"is not":"is"));		for (n=0; n<nodesPerGroup; n++)		{			/* fprintf(stdout, "#\t(group,node) (%d,%d):  %s moving\n", g, n, ((movingNode[g*nodesPerGroup+n])?"is not":"is")); */			if (output == 'N')			{				fprintf(stdout, "$node_(%d) set X_ %.12f\n", g*nodesPerGroup+n+nodeIDstart, xLocNode[g*nodesPerGroup+n]);				fprintf(stdout, "$node_(%d) set Y_ %.12f\n", g*nodesPerGroup+n+nodeIDstart, yLocNode[g*nodesPerGroup+n]);				fprintf(stdout, "$node_(%d) set Z_ %.12f\n", g*nodesPerGroup+n+nodeIDstart, startTime);			} else if (output == 'G') {				/* xLocNode[g*nodesPerGroup+n] = maxX/2.0; */				/* yLocNode[g*nodesPerGroup+n] = maxY/2.0; */				fprintf(stdout, "%d %.10f %.12f %.12f\n", g*nodesPerGroup+n+nodeIDstart, startTime,				        xLocNode[g*nodesPerGroup+n], yLocNode[g*nodesPerGroup+n]);			}		}		if (output == 'N')		{			fprintf(stdout, "\n");

⌨️ 快捷键说明

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