MetricsDetector Class Reference

A derived class to represent a detector that tracks metrics information. More...

#include <MetricsDetector.h>

Inheritance diagram for MetricsDetector:

Inheritance graph
[legend]
Collaboration diagram for MetricsDetector:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 MetricsDetector (std::string dir, WORD DetType, bool DirPos, int nLanes, WORD VehType, int timeInterval, int loc, int RoadLength)
 Constructor.
virtual ~MetricsDetector ()
 Destructor.
virtual void EndOutput ()
 Close the output file.
virtual void addVehicle (Vehicle *pVeh, double curTime)
 Check if we are to add the current vehicle to the detector.

Private Member Functions

virtual void InitOutputFiles ()
 Initialise the output files.
virtual void doIntervalOutput ()
 Do the output at the end of each time interval.
virtual void ReInit ()
 At the end of each time interval reset vectors for next time period.
void InitCompositionFile ()
 Initialise the Composition output file.
void InitHeadwayFile ()
 Initialise the Headway output file.
void InitFlowDensityFile ()
 Initialise the Flow-Density output file.
void doCompositionOutput ()
 Do the Composition output.
void doHeadwayOutput ()
 Do the Headway output.
void doFlowDensityOutput ()
 Do the Flow-Density output.
void AddCurrentVehicle (Vehicle *pVeh, const double curTime)
 Add the current vehicle to the detector.
void initVectors ()
 Initialise storage vectors.
std::string intToString (int d)
 Change an int to a string object.

Private Attributes

double m_AvgVel
std::ofstream m_OutFile
std::vector< WORD > m_vVehicleTypes
M2Ddbl m_vVelocities
M2Ddbl m_vHeadways
std::vector< double > m_vTimeOfLastArrival
std::vector< int > m_vNoVehicles


Detailed Description

A derived class to represent a detector that tracks metrics information.

Definition at line 19 of file MetricsDetector.h.


Constructor & Destructor Documentation

MetricsDetector::MetricsDetector ( std::string  dir,
WORD  DetType,
bool  DirPos,
int  nLanes,
WORD  VehType,
int  timeInterval,
int  loc,
int  RoadLength 
)

Constructor.

Parameters:
dir The detector output directory
DetType The particular type of detector
DirPos Whether the detector is in the positive direction
nLanes The number of lanes the detector covers
VehType The type of vehicles to track
timeInterval The time interval to use
loc The location of the detector
RoadLength The length of the road

Definition at line 30 of file MetricsDetector.cpp.

References InitOutputFiles(), initVectors(), Detector::m_CurrentTime, Detector::m_DetectorType, Detector::m_DirPos, Detector::m_IntervalNo, Detector::m_Location, Detector::m_MetricsDir, Detector::m_NoLanes, Detector::m_RoadLength, Detector::m_TimeInterval, Detector::m_VehicleType, and m_vTimeOfLastArrival.

00032 {
00033         m_MetricsDir = dir;
00034         m_DetectorType = DetType;
00035         m_DirPos = DirPos;
00036         m_NoLanes = nLanes;
00037         m_VehicleType = VehType;
00038         m_TimeInterval = timeInterval;
00039         m_Location = loc;
00040         m_RoadLength = RoadLength;
00041 
00042         m_CurrentTime = 0;
00043         m_IntervalNo = 0;
00044         
00045         m_vTimeOfLastArrival.assign(m_NoLanes,0.0);     // vector of last arrival times by lane
00046         initVectors();
00047         InitOutputFiles();
00048 }

Here is the call graph for this function:

MetricsDetector::~MetricsDetector (  )  [virtual]

Destructor.

Definition at line 51 of file MetricsDetector.cpp.

00052 {
00053 
00054 }


Member Function Documentation

void MetricsDetector::EndOutput (  )  [virtual]

Close the output file.

Reimplemented from Detector.

Definition at line 367 of file MetricsDetector.cpp.

References m_OutFile.

00368 {
00369         m_OutFile.close();
00370 }

void MetricsDetector::addVehicle ( Vehicle pVeh,
double  curTime 
) [virtual]

Check if we are to add the current vehicle to the detector.

Reimplemented from Detector.

Definition at line 77 of file MetricsDetector.cpp.

References AddCurrentVehicle(), Vehicle::getID(), Detector::m_VehicleType, METRICS_VEH_ALL, METRICS_VEH_CAR, METRICS_VEH_CRANE, METRICS_VEH_LARGETRUCK, METRICS_VEH_LOWLOADER, METRICS_VEH_SMALLTRUCK, VEH_ID_CAR, VEH_ID_CRANE, VEH_ID_LARGETRUCK, VEH_ID_LOWLOADER, and VEH_ID_SMALLTRUCK.

00078 {
00079         WORD vehID = pVeh->getID();
00080 
00081         switch(m_VehicleType)
00082         {
00083                 case METRICS_VEH_ALL:                   
00084                         AddCurrentVehicle(pVeh, curTime);
00085                         break;
00086                 case METRICS_VEH_CAR:
00087                         if(vehID == VEH_ID_CAR)
00088                                 AddCurrentVehicle(pVeh, curTime);
00089                         break;
00090                 case METRICS_VEH_SMALLTRUCK:
00091                         if(vehID == VEH_ID_SMALLTRUCK)
00092                                 AddCurrentVehicle(pVeh, curTime);
00093                         break;
00094                 case METRICS_VEH_LARGETRUCK:
00095                         if(vehID == VEH_ID_LARGETRUCK)
00096                                 AddCurrentVehicle(pVeh, curTime);
00097                         break;
00098                 case METRICS_VEH_CRANE:
00099                         if(vehID == VEH_ID_CRANE)
00100                                 AddCurrentVehicle(pVeh, curTime);
00101                         break;
00102                 case METRICS_VEH_LOWLOADER:
00103                         if(vehID == VEH_ID_LOWLOADER)
00104                                 AddCurrentVehicle(pVeh, curTime);
00105                         break;
00106                 default:
00107                         AddCurrentVehicle(pVeh, curTime);
00108         }
00109 
00110 }

Here is the call graph for this function:

void MetricsDetector::InitOutputFiles (  )  [private, virtual]

Initialise the output files.

Reimplemented from Detector.

Definition at line 172 of file MetricsDetector.cpp.

References InitCompositionFile(), InitFlowDensityFile(), InitHeadwayFile(), Detector::m_DetectorType, Detector::m_VehicleType, METRICS_TYPE_COMPOSITION, METRICS_TYPE_FLOWDENSITY, METRICS_TYPE_HEADWAY, and METRICS_VEH_ALL.

Referenced by MetricsDetector().

00173 {
00174         switch(m_DetectorType)
00175         {
00176                 case METRICS_TYPE_FLOWDENSITY:
00177                         InitFlowDensityFile();
00178                         break;
00179                 case METRICS_TYPE_HEADWAY:
00180                         InitHeadwayFile();
00181                         break;
00182                 case METRICS_TYPE_COMPOSITION:
00183                         if(m_VehicleType == METRICS_VEH_ALL)    // since composition only makes sense with all vehicles
00184                                 InitCompositionFile();
00185                         break;
00186                 default:
00187                         InitFlowDensityFile();
00188         }
00189 }

Here is the call graph for this function:

void MetricsDetector::doIntervalOutput (  )  [private, virtual]

Do the output at the end of each time interval.

Reimplemented from Detector.

Definition at line 56 of file MetricsDetector.cpp.

References doCompositionOutput(), doFlowDensityOutput(), doHeadwayOutput(), Detector::m_DetectorType, Detector::m_VehicleType, METRICS_TYPE_COMPOSITION, METRICS_TYPE_FLOWDENSITY, METRICS_TYPE_HEADWAY, METRICS_VEH_ALL, and ReInit().

00057 {
00058         switch(m_DetectorType)
00059         {
00060                 case METRICS_TYPE_FLOWDENSITY:
00061                         doFlowDensityOutput();
00062                         break;
00063                 case METRICS_TYPE_HEADWAY:
00064                         doHeadwayOutput();
00065                         break;
00066                 case METRICS_TYPE_COMPOSITION:
00067                         if(m_VehicleType == METRICS_VEH_ALL)
00068                                 doCompositionOutput();  // since composition only makes sense with all vehicles
00069                         break;
00070                 default:
00071                         doFlowDensityOutput();
00072         }
00073 
00074         ReInit();
00075 }

Here is the call graph for this function:

void MetricsDetector::ReInit (  )  [private, virtual]

At the end of each time interval reset vectors for next time period.

Reimplemented from Detector.

Definition at line 147 of file MetricsDetector.cpp.

References initVectors(), Detector::m_NoLanes, m_vHeadways, m_vNoVehicles, m_vVehicleTypes, and m_vVelocities.

Referenced by doIntervalOutput().

00148 {
00149         m_vVehicleTypes.clear();
00150         m_vNoVehicles.clear();
00151         for(int i = 0; i < m_NoLanes; i++)
00152         {
00153                 m_vVelocities[i].clear();
00154                 m_vHeadways[i].clear();
00155         }
00156         m_vVelocities.clear();
00157         m_vHeadways.clear();
00158                 
00159         initVectors();
00160 }

Here is the call graph for this function:

void MetricsDetector::InitCompositionFile (  )  [private]

Initialise the Composition output file.

Definition at line 239 of file MetricsDetector.cpp.

References intToString(), Detector::m_DirPos, Detector::m_Location, Detector::m_MetricsDir, m_OutFile, Detector::m_RoadLength, Detector::m_VehicleType, Detector::MapDetVehTypeToString(), and Detector::MapDirToString().

Referenced by InitOutputFiles().

00240 {       
00241         // Create file name and open it
00242         int loc = m_DirPos == true ? m_Location : m_RoadLength - m_Location;
00243         std::string file;
00244         file = m_MetricsDir;
00245         file += "Comp_" + MapDetVehTypeToString(m_VehicleType) + "_" 
00246                                    + MapDirToString(m_DirPos) +"_" 
00247                                    + intToString(loc) + ".csv";
00248         m_OutFile.open(file.c_str(), std::ios::out);
00249         
00250         // let's set some headers
00251         m_OutFile << "No. Cars"                 << ",";
00252         m_OutFile << "No. Small Trucks" << ","; 
00253         m_OutFile << "No. Large Trucks" << ","; 
00254         m_OutFile << "No. Cranes"               << ","; 
00255         m_OutFile << "No. Low-Loaders"  << '\n'; 
00256 }

Here is the call graph for this function:

void MetricsDetector::InitHeadwayFile (  )  [private]

Initialise the Headway output file.

Definition at line 220 of file MetricsDetector.cpp.

References intToString(), Detector::m_DirPos, Detector::m_Location, Detector::m_MetricsDir, Detector::m_NoLanes, m_OutFile, Detector::m_RoadLength, Detector::m_VehicleType, Detector::MapDetVehTypeToString(), and Detector::MapDirToString().

Referenced by InitOutputFiles().

00221 {       
00222         // Create file name and open it
00223         int loc = m_DirPos == true ? m_Location : m_RoadLength - m_Location;
00224         std::string file;
00225         file = m_MetricsDir;
00226         file += "Head_" + MapDetVehTypeToString(m_VehicleType) + "_" 
00227                                    + MapDirToString(m_DirPos) +"_" 
00228                                    + intToString(loc) + ".csv";
00229         m_OutFile.open(file.c_str(), std::ios::out);
00230         
00231         // let's set some headers
00232         m_OutFile << "Headways (s)"     << '\n';
00233         for(int i = 1; i < m_NoLanes; i++)
00234                 m_OutFile << "Lane " <<  intToString(i) << ',';
00235         m_OutFile << "Lane " <<  intToString(m_NoLanes) << '\n';
00236 }

Here is the call graph for this function:

void MetricsDetector::InitFlowDensityFile (  )  [private]

Initialise the Flow-Density output file.

Definition at line 192 of file MetricsDetector.cpp.

References intToString(), Detector::m_DirPos, Detector::m_Location, Detector::m_MetricsDir, Detector::m_NoLanes, m_OutFile, Detector::m_RoadLength, Detector::m_VehicleType, Detector::MapDetVehTypeToString(), and Detector::MapDirToString().

Referenced by InitOutputFiles().

00193 {       
00194         // Create file name and open it
00195         int loc = m_DirPos == true ? m_Location : m_RoadLength - m_Location;
00196         std::string file;
00197         file = m_MetricsDir;
00198         file += "FD_" + MapDetVehTypeToString(m_VehicleType) + "_" 
00199                                  + MapDirToString(m_DirPos) +"_" 
00200                                  + intToString(loc) + ".csv";
00201         m_OutFile.open(file.c_str(), std::ios::out);
00202         
00203         // let's set some headers
00204         m_OutFile << " " << ',';        //Interval no column
00205         for(int i = 1; i <= m_NoLanes; i++)
00206                 m_OutFile << "Lane " <<  intToString(i) << ", , ,";
00207         m_OutFile << "Totals" << '\n';
00208 
00209         m_OutFile << "Interval No."             << ',';
00210         for(i = 0; i < m_NoLanes+1; i++)        // + 1 is for the totals columns
00211         {
00212                 char end = i == m_NoLanes ? '\n' : ',';
00213                 m_OutFile << "Density (veh/km)" << ',';
00214                 m_OutFile << "Flow (veh/hr)"    << ',';
00215                 m_OutFile << "Avg. Vel. (km/h)" << end;
00216         }
00217 }

Here is the call graph for this function:

void MetricsDetector::doCompositionOutput (  )  [private]

Do the Composition output.

Definition at line 326 of file MetricsDetector.cpp.

References m_OutFile, m_vVehicleTypes, VEH_ID_CAR, VEH_ID_CRANE, VEH_ID_LARGETRUCK, VEH_ID_LOWLOADER, and VEH_ID_SMALLTRUCK.

Referenced by doIntervalOutput().

00327 {
00328         int nCar = 0;
00329         int nSmallTruck = 0;
00330         int nLargeTruck = 0;
00331         int nCrane = 0;
00332         int nLowLoader = 0;
00333 
00334         for(int i = 0; i < m_vVehicleTypes.size(); i++)
00335         {
00336                 WORD VehType = m_vVehicleTypes.at(i);
00337 
00338                 switch(VehType)
00339                 {
00340                         case VEH_ID_CAR:                nCar++;                 break;
00341                         case VEH_ID_SMALLTRUCK: nSmallTruck++;  break;
00342                         case VEH_ID_LARGETRUCK: nLargeTruck++;  break;
00343                         case VEH_ID_CRANE:              nCrane++;               break;
00344                         case VEH_ID_LOWLOADER:  nLowLoader++;   break;
00345                         default: nCar++; 
00346                 }
00347         }       
00348 
00349         m_OutFile << nCar                       << ",";
00350         m_OutFile << nSmallTruck        << ","; 
00351         m_OutFile << nLargeTruck        << ","; 
00352         m_OutFile << nCrane                     << ","; 
00353         m_OutFile << nLowLoader         << '\n'; 
00354 }

void MetricsDetector::doHeadwayOutput (  )  [private]

Do the Headway output.

Definition at line 300 of file MetricsDetector.cpp.

References Detector::m_NoLanes, m_OutFile, and m_vHeadways.

Referenced by doIntervalOutput().

00301 {
00302         // get size of largest headway values vector
00303         int jmax = m_vHeadways[0].size();
00304         for(int i = 1; i < m_NoLanes; i++)
00305         {
00306                 if(m_vHeadways[i].size() > jmax)
00307                         jmax = m_vHeadways[i].size();
00308         }
00309 
00310         // write across then down, leaving blanks where no value
00311         for(int j = 0; j < jmax; j++)
00312         {
00313                 for(i = 0; i < m_NoLanes; i++)
00314                 {
00315                         char end = i == m_NoLanes-1 ? '\n' : ',';
00316 
00317                         if(j < m_vHeadways[i].size())
00318                                 m_OutFile << m_vHeadways[i].at(j) << end;
00319                         else
00320                                 m_OutFile << " " << end;
00321                 }
00322         }
00323 }

void MetricsDetector::doFlowDensityOutput (  )  [private]

Do the Flow-Density output.

Definition at line 259 of file MetricsDetector.cpp.

References Detector::m_IntervalNo, Detector::m_NoLanes, m_OutFile, M_PER_S_TO_KM_PER_H, Detector::m_TimeInterval, m_vNoVehicles, m_vVelocities, and SECS_PER_HOUR.

Referenced by doIntervalOutput().

00260 {
00261         // first find the density, flow and ave velocity
00262         std::vector<double> vAvgVel(m_NoLanes,0.0);
00263         std::vector<double> vFlowPerHour(m_NoLanes,0.0);
00264         std::vector<double> vDensity(m_NoLanes,0.0);
00265 
00266         for(int i = 0; i < m_NoLanes; i++)
00267         {
00268                 for(int j = 0; j < m_vNoVehicles[i]; j++)
00269                         vAvgVel[i] += m_vVelocities[i].at(j);
00270                 // since we have the sum of velocites in lane i now 
00271                 // we can get the average now, in km/h
00272                 vAvgVel[i] = M_PER_S_TO_KM_PER_H * vAvgVel[i] / m_vNoVehicles[i];
00273                 vFlowPerHour[i] = m_vNoVehicles[i] * (SECS_PER_HOUR/m_TimeInterval);
00274                 vDensity[i] = vFlowPerHour[i]/vAvgVel[i];       // units of veh/km
00275         }
00276         
00277         // lastly, do the sum of lanes for total direction properties
00278         double totAvgVel = 0.0; double totFlowPerHour = 0.0; double totDensity = 0.0;
00279         for(i = 0; i < m_NoLanes; i++)
00280         {
00281                 totAvgVel               += vAvgVel[i];
00282                 totFlowPerHour  += vFlowPerHour[i];
00283                 totDensity              += vDensity[i];
00284         }
00285                 
00286         // then do the output and reset the variables
00287         m_OutFile << m_IntervalNo       << ',';
00288         for(i = 0; i < m_NoLanes; i++)
00289         {
00290                 m_OutFile << vDensity[i]                << ',';
00291                 m_OutFile << vFlowPerHour[i]    << ',';
00292                 m_OutFile << vAvgVel[i]                 << ',';
00293         }
00294         m_OutFile << totDensity         << ',';
00295         m_OutFile << totFlowPerHour     << ',';
00296         m_OutFile << totAvgVel          << '\n';
00297 }

void MetricsDetector::AddCurrentVehicle ( Vehicle pVeh,
const double  curTime 
) [private]

Add the current vehicle to the detector.

Definition at line 116 of file MetricsDetector.cpp.

References Detector::FindDetectorArrivalTime(), Vehicle::getID(), Vehicle::getLaneNoInDirection(), Vehicle::getVelocity(), Detector::m_DetectorType, Detector::m_VehicleType, m_vHeadways, m_vNoVehicles, m_vTimeOfLastArrival, m_vVehicleTypes, m_vVelocities, METRICS_TYPE_COMPOSITION, METRICS_TYPE_FLOWDENSITY, METRICS_TYPE_HEADWAY, and METRICS_VEH_ALL.

Referenced by addVehicle().

00117 {
00118         int iLane = pVeh->getLaneNoInDirection();       // get the local lane number in its direction
00119         switch(m_DetectorType)
00120         {
00121                 case METRICS_TYPE_FLOWDENSITY:
00122                 {
00123                         m_vNoVehicles[iLane-1]++;                                                                       // increase vehicle count by lane
00124                         m_vVelocities[iLane-1].push_back(pVeh->getVelocity());          // and store velocity
00125                         break;
00126                 }
00127                 case METRICS_TYPE_HEADWAY:
00128                 {
00129                         double ArrivalTime = FindDetectorArrivalTime(pVeh, curTime);
00130                         double headway = ArrivalTime - m_vTimeOfLastArrival[iLane-1];
00131                         m_vHeadways[iLane-1].push_back(headway);
00132                         m_vTimeOfLastArrival[iLane-1] = ArrivalTime;    // update last arrival time for this lane
00133                         break;
00134                 }
00135                 case METRICS_TYPE_COMPOSITION:
00136                 {
00137                         // since composition only makes sense with all vehicles
00138                         if(m_VehicleType == METRICS_VEH_ALL)
00139                                 m_vVehicleTypes.push_back( pVeh->getID() );
00140                         break;
00141                 }
00142         }
00143         
00144 }

Here is the call graph for this function:

void MetricsDetector::initVectors (  )  [private]

Initialise storage vectors.

Definition at line 163 of file MetricsDetector.cpp.

References Detector::m_NoLanes, m_vHeadways, m_vNoVehicles, and m_vVelocities.

Referenced by MetricsDetector(), and ReInit().

00164 {
00165         m_vNoVehicles.assign(m_NoLanes,0);                      // no of vehicles by lane
00166         std::vector<double> temp;
00167         m_vHeadways.assign(m_NoLanes, temp);
00168         m_vVelocities.assign(m_NoLanes, temp);
00169 }

std::string MetricsDetector::intToString ( int  d  )  [private]

Change an int to a string object.

Definition at line 357 of file MetricsDetector.cpp.

Referenced by InitCompositionFile(), InitFlowDensityFile(), and InitHeadwayFile().

00358 {
00359         std::string a;
00360         char buffer; char* pBuffer = &buffer;
00361         itoa( d, pBuffer, 10 );
00362         a.append(&buffer);
00363         return a;
00364 }


Member Data Documentation

double MetricsDetector::m_AvgVel [private]

Definition at line 46 of file MetricsDetector.h.

std::ofstream MetricsDetector::m_OutFile [private]

std::vector<WORD> MetricsDetector::m_vVehicleTypes [private]

Definition at line 48 of file MetricsDetector.h.

Referenced by AddCurrentVehicle(), doCompositionOutput(), and ReInit().

Definition at line 49 of file MetricsDetector.h.

Referenced by AddCurrentVehicle(), doFlowDensityOutput(), initVectors(), and ReInit().

Definition at line 50 of file MetricsDetector.h.

Referenced by AddCurrentVehicle(), doHeadwayOutput(), initVectors(), and ReInit().

std::vector<double> MetricsDetector::m_vTimeOfLastArrival [private]

Definition at line 51 of file MetricsDetector.h.

Referenced by AddCurrentVehicle(), and MetricsDetector().

std::vector<int> MetricsDetector::m_vNoVehicles [private]

Definition at line 52 of file MetricsDetector.h.

Referenced by AddCurrentVehicle(), doFlowDensityOutput(), initVectors(), and ReInit().


The documentation for this class was generated from the following files:

Generated on Wed Aug 20 00:48:48 2008 for EvolveTraffic by  doxygen 1.5.6