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

📄 statistics.cc

📁 用OMNET++仿真蚁群的源码,是无线传感器网络仿真的一个重要工具
💻 CC
字号:
// -*- C++ -*-
// Copyright (C) 2003 Leherstuh f黵 Betrieb System/ Verteilte System,
// Universitaet Dortmund
//
// 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.

// Author: Muddassar Farooq
// Informatik III, Universitaet Dortmund
// Germany

//-------------------------------------------------------------
// file: statisitics.h -- A Singleton Class for Global Statisitics
//        (part of AntNet Routing Simulation)
//-------------------------------------------------------------

#include "statistics.h"


statistics* statistics::_instance = 0;


statistics* statistics::Instance(const char* fName)
{
	if(_instance == 0)
	{
		_instance = new statistics();
		_instance->setFileName(fName);

	}
	return _instance;
}

statistics::statistics()
{
	totalBitsGenerated = 0;
	totalBitsDelivered = 0;
	totalBitsLost = 0;

	totalAntsGenerated = 0;
	totalAntsDelivered = 0;
	totalAntsDeleted = 0;
	queueDelay = 0;
	qEntries = 0;

	packetDelay = 0;
	pEntries = 0;
	i = 0;
	packetsInQueue = 0;
	hops = 0;
	hEntries = 0;
	antHops = 0;
	aEntries = 0;

}


void statistics::setFileName(const char* fName)
{
	strcpy(sFileName,fName);
}


void statistics::incrNumHops(int nHops)
{
	hops += nHops;
	hEntries++;
}

void statistics::incrAntHops(int aHops)
{
	antHops += aHops;
	aEntries++;
}


void statistics:: incrTotalBitsGenerated()
{
	totalBitsGenerated++;
}

void statistics:: incrTotalBitsDelivered()
{
	totalBitsDelivered++;
}

void statistics:: incrTotalBitsLost()
{
	totalBitsLost++;
}

void statistics:: incrTotalAntsGenerated()
{
	totalAntsGenerated++;
}

void statistics:: incrTotalAntsReceived()
{
	totalAntsDelivered++;
}

void statistics:: incrTotalAntsDeleted()
{
	totalAntsDeleted++;
}

void statistics:: insertQueueDelay(double qDelay)
{
	queueDelay+= qDelay,
	qEntries++;
}

void statistics::insertPacketDelay(double delay)
{
	pDelay.collect(delay);

	packetDelay += delay;
	pEntries++;
}

void statistics::insertAntLife(double lValue)
{
	antLife.collect(lValue);
}

void statistics::incrPacketInQueue(double lPacket)
{
	packetsInQueue += lPacket;
}


statistics::~statistics()
{
	if( _instance != NULL)
	{
//		delete _instance;
		_instance = NULL;
	}
}

void statistics::insertThroughPut(double tPut, int time)
{

	throughputTimePair *temp = new throughputTimePair;
	temp->first = tPut;
	temp->second = time;

	simTime = time;
	tPutVector.push_back( temp ) ;
}

double statistics::calculateThroughPut(int time)
{
	vector<throughputTimePair*>::iterator iter;
	double *timeEntry = new double[time];
	double totalThroughPut = 0.0;
	int i = 0;

	for(i = 0; i < time; i++)
	{
		timeEntry[i] = 0.0;
	}

	for(iter = tPutVector.begin(); iter != tPutVector.end(); ++iter)
	{
		throughputTimePair *temp = *iter;
		timeEntry[temp->second] += temp->first;
		delete temp;
	}

	for(i = 0; i < time; i++)
	{
		totalThroughPut += timeEntry[i];
	}

	delete[] timeEntry;
	return totalThroughPut/time;
}

double statistics::ninteythPercentile(cStdDev tVal)
{

	double mean = tVal.mean();
	double stdDev = tVal.stddev();
	double z = 1.29;

	double value = mean + z * stdDev;
	return value;
}

void statistics::showStatistics()
{
	if( i == 0)
	{

		sFile = fopen(sFileName,"w+");

		fprintf(sFile, "************************\n");
		fprintf(sFile, "\t Final Values    \n");
		fprintf(sFile, "************************\n");

		fprintf(sFile, "Total Packets Generated:  %f\n", totalBitsGenerated);
		fprintf(sFile, "Total Packets Delivered:  %f\n",totalBitsDelivered);
		fprintf(sFile, "Total Packets Dropped:  %f\n",totalBitsLost);
		fprintf(sFile, "Total Packets in Queue:  %f\n\n",packetsInQueue);


		fprintf(sFile, "Total Ants Generated:  %f\n", totalAntsGenerated);
		fprintf(sFile, "Total Ants Received:  %f\n",totalAntsDelivered);
		fprintf(sFile, "Total Ants Dropped:  %f\n",totalAntsDeleted);
		fprintf(sFile, "Average Number Of Hops for Ants:  %f\n\n", (float) antHops/aEntries);
		fprintf(sFile, "Average Life for Ants:  %f\n", (float) antLife.mean());
		double aLife = ninteythPercentile(antLife);
		fprintf(sFile, "90 Percentile Life for Ants:  %f\n\n", aLife);

		fprintf(sFile, "Average Queue Delay:  %f\n",queueDelay/qEntries);
		fprintf(sFile, "Average Packet Delay:  %f\n",packetDelay/pEntries);

		double ninteyDelay = ninteythPercentile(pDelay);
		fprintf(sFile, "90% Packet Delay:  %f\n\n",ninteyDelay);

		double throughPut = calculateThroughPut(simTime);
		fprintf(sFile, "SimTime:  %d\n", simTime);
		fprintf(sFile, "ThroughPut Value:  %f\n", throughPut);
		float fHops = (float) hops /hEntries;
		fprintf(sFile, "Average Number Of Hops:  %f\n", fHops);


		fclose(sFile);
	}
	i++;
}


void statistics::registerModule(cModule *mPtr)
{
	lModules.push_back(mPtr);
}

void statistics::deregisterModule(cModule *mPtr)
{
	vector<cModule*>::iterator iter;

	for( iter = lModules.begin(); iter != lModules.end(); ++iter)
	{
		cModule *temp = *iter;
		if( temp == mPtr)
		{
			break;
		}
	}

	if( iter != lModules.end())
	{
		lModules.erase(iter);
	}
}

⌨️ 快捷键说明

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