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

📄 probrandwalk.c

📁 无线通信的各种运动模型。适用于移动通信、无线传感器网络等领域。 包括:Random walk、random waypoint、random direction、boundless simulation
💻 C
字号:
/*******************************************************************************   File Name: probrandwalk.c*   Purpose: probabilistic random walk mobility model*   Author: Jeff Boleng*   Date Created:**   Edited 12 July 2006 by Jon Petty:  *   Changed seed code to rand((int)time(NULL) + (int)getpid())**   Copyright (C) 2004  Toilers Research Group -- Colorado School of Mines**   Please see COPYRIGHT.TXT and LICENSE.TXT for copyright and license*   details.******************************************************************************/ #include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#include <unistd.h>#undef rand48#define true  1#define false 0#define ZERO 0.00001double minX = 0.0;double minY = 0.0;double maxX = 0.0;double maxY = 0.0;double speed = 100.0;int main(int argc, char *argv[]){	int i;	int xstate=0, ystate=0;	int numNodes=0;	double endTime=0.0;	double *xLoc, *yLoc;	double r1, r2;	double curtime=0.0;	double interval=1.0;	char output;	if (argc == 7)	{		numNodes      = atoi(argv[1]);		maxX          = atof(argv[2]);		maxY          = atof(argv[3]);		endTime       = atof(argv[4]);		interval      = atof(argv[5]);		output        = (char)argv[6][0];		fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");		fprintf(stdout, "#\tnumNodes      = %6d\n", numNodes);		fprintf(stdout, "#\tmaxX          = %9.2f\n", maxX);		fprintf(stdout, "#\tmaxY          = %9.2f\n", maxY);		fprintf(stdout, "#\tendTime       = %9.2f\n", endTime);		fprintf(stdout, "#\tinterval      = %9.2f\n", interval);		fprintf(stdout, "#\toutput        = %6c\n", output);		fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");	} else {		fprintf(stdout, "Usage:  probrandwalk <number of nodes>\n");		fprintf(stdout, "                     <max-x> <max-y> <end time>\n");		fprintf(stdout, "                     <interval> <'N' or 'G'>\n");		fprintf(stdout, "                     'N' implies NS2 mobility file\n");		fprintf(stdout, "                     'G' implies gnuplot path file\n");		return -1;	}	if (output == 'N')	{		fprintf(stdout, "# output format is NS2\n");	} else if (output == 'G') {		fprintf(stdout, "# output format is gnuplot\n");		if (numNodes != 1)		{			fprintf(stderr, "Gnuplot output is only possible with one mobile node.\n");			return -1;		} else {			fprintf(stdout, "set xrange[%.5f:%.5f]\n", minX, maxX);			fprintf(stdout, "set yrange[%.5f:%.5f]\n", minY, maxY);			fprintf(stdout, "plot \'-\' notitle with lines\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	xLoc = (double*)malloc(sizeof(double)*numNodes);	yLoc = (double*)malloc(sizeof(double)*numNodes);	if (output == 'N')	{		fprintf(stdout, "#\tInitial positions:\n");	}	for (i=0; i<numNodes; i++)	{    #ifdef rand48		xLoc[i] = drand48()*maxX;		yLoc[i] = drand48()*maxY;    #else		xLoc[i] = ((double)rand()/(double)RAND_MAX)*maxX;		yLoc[i] = ((double)rand()/(double)RAND_MAX)*maxY;    #endif		if (xLoc[i] >= maxX)		{			xLoc[i] = maxX - ZERO;		} else if (xLoc[i] <= minX) {			xLoc[i] = minX + ZERO;		}		if (yLoc[i] >= maxY)		{			yLoc[i] = maxY - ZERO;		} else if (yLoc[i] <= minY) {			yLoc[i] = minY + ZERO;		}		if (output == 'N')		{			fprintf(stdout, "$node_(%d) set X_ %.12f\n", i, xLoc[i]);			fprintf(stdout, "$node_(%d) set Y_ %.12f\n", i, yLoc[i]);			fprintf(stdout, "$node_(%d) set Z_ %.12f\n", i, 0.0);		} else if (output == 'G') {			xLoc[i] = maxX/2.0;			yLoc[i] = maxY/2.0;			fprintf(stdout, "%.12f %.12f\n", xLoc[i], yLoc[i]);		}	}	if (output == 'N')	{		fprintf(stdout, "\n\n#\tMovements:\n");	}	while (curtime <= endTime)	{		for (i=0; i<numNodes; i++)		{      #ifdef rand48			r1 = drand48();			r2 = drand48();      #else			r1 = ((double)rand()/(double)RAND_MAX);			r2 = ((double)rand()/(double)RAND_MAX);      #endif			if (xstate == 0)			{				if (r1<0.5)				{					xstate=2;					xLoc[i] += interval;				} else {					xstate=1;					xLoc[i] -= interval;				}			} else if (xstate == 1) {				if (r1<0.7)				{					xLoc[i] -= interval;				} else {					xstate=0;				}			} else if (xstate == 2) {				if (r1<0.7)				{					xLoc[i] += interval;				} else {					xstate=0;				}			}			if (ystate == 0)			{				if (r2<0.5)				{					ystate=2;					yLoc[i] += interval;				} else {					ystate=1;					yLoc[i] -= interval;				}			} else if (ystate == 1) {				if (r2<0.7)				{					yLoc[i] -= interval;				} else {					ystate=0;				}			} else if (ystate == 2) {				if (r2<0.7)				{					yLoc[i] += interval;				} else {					ystate=0;				}			}			if (xLoc[i] >= maxX)			{				xLoc[i] = maxX - ZERO;			} else if (xLoc[i] <= minX) {				xLoc[i] = minX + ZERO;			}			if (yLoc[i] >= maxY)			{				yLoc[i] = maxY - ZERO;			} else if (yLoc[i] <= minY) {				yLoc[i] = minY + ZERO;			}			if (output == 'N')			{				fprintf(stdout, "$ns_ at %.10f \"$node_(%d) setdest %.10f %.10f %.10f\"\n",				        curtime, i, xLoc[i], yLoc[i], speed);			} else if (output == 'G') {				fprintf(stdout, "%.12f %.12f # node %d at %.10f speed=%.10f\n", xLoc[i], yLoc[i], i, curtime, speed);			}			curtime += interval;		} /* for (i=0; i<numNodes; i++) */	} /* while (curtime <= endTime) */	if (output == 'N')	{		fprintf(stdout, "\n\n\n");	} else if (output == 'G') {		fprintf(stdout, "e\n\n");	}	free(xLoc);	free(yLoc);	return 0;}

⌨️ 快捷键说明

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