Skip to content

Commit

Permalink
Add DTP Runtime Row and Collection for Statistics (#81)
Browse files Browse the repository at this point in the history
* Add DTP runtime row for statistics
* Add DTP runtime collection for statistics
* [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
  • Loading branch information
franziska-wegner committed Jan 4, 2024
1 parent cb587f8 commit f321e9c
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 0 deletions.
113 changes: 113 additions & 0 deletions include/IO/Statistics/DtpRuntimeCollection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* DtpRuntimeCollection.hpp
*
* Created on: Feb 26, 2019
* Author: Franziska Wegner, Matthias Wolf
*/

#ifndef EGOA__IO__STATISTICS__DTP_RUNTIME_COLLECTION_HPP
#define EGOA__IO__STATISTICS__DTP_RUNTIME_COLLECTION_HPP

#include <fstream>
#include <iostream>
#include <vector>

#include "IO/Statistics/DtpRuntimeRow.hpp"

namespace egoa::IO {

/**
* @brief A collections of DtpRuntimeRow objects for multiple runs
* of the DTP-algorithm.
*/
class DtpRuntimeCollection {
public:
using TRow = DtpRuntimeRow;
public:
/// @name Modifying content
/// @{
/**
* @brief Adds a DtpRuntimeRow to the collection.
*
* @param rhs The row to add.
*
* @return @c *this.
*/
inline DtpRuntimeCollection & operator+=( TRow const & rhs )
{
collection_.emplace_back ( rhs );
return *this;
}

/**
* @brief Clears the content of the collection.
*/
inline void Clear ()
{
collection_.clear();
}
/// @}

/// @name Accessors
/// @{
inline std::vector<TRow> const & Collection () const
{
return collection_;
}
/// @}

/// @name Output
/// @{
friend std::ostream & operator<<( std::ostream & os, DtpRuntimeCollection const & collection )
{
for ( const auto & row : collection.collection_ )
{
row.Content(os);
}
return os;
}

/**
* @brief Writes the data in the collection to a file.
*
* @details If <tt>overwrite == true</tt> or the file is empty,
* a header is written before the content.
* Otherwise, onlye the content is written.
*
* @param[in] filename The filename
* @param[in] overwrite @c true if the content of the file shall be
* overwritten, @p false if the content shall be
* appended to the file.
*/
inline void WriteCollectionToFileWith ( Types::string const filename
, bool overwrite = true )
{
#ifndef NDEBUG
std::cout << "Write DTP runtime information row to: " << filename << std::endl;
#endif
// Open output stream.
std::ofstream fileStream;
fileStream.open( filename, overwrite ? std::ofstream::trunc : std::ofstream::app );
if ( !fileStream.is_open() ) return;

// Check if the file is empty
fileStream.seekp(0, std::ios::end);
if ( fileStream.tellp() == 0 )
{
TRow::Header ( fileStream );
}

for ( const auto & row : collection_ )
{
row.Content(fileStream);
}
}
/// @}

private:
std::vector<TRow> collection_;
};

} // namespace egoa::IO

#endif // EGOA__IO__STATISTICS__DTP_RUNTIME_COLLECTION_HPP
170 changes: 170 additions & 0 deletions include/IO/Statistics/DtpRuntimeRow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* DtpRuntimeRow.hpp
*
* Created on: Feb 03, 2019
* Author: Franziska Wegner
*/

#ifndef EGOA__IO__STATISTICS__DTP_RUNTIME_ROW_HPP
#define EGOA__IO__STATISTICS__DTP_RUNTIME_ROW_HPP

#include <fstream>
#include <iostream>
#include <vector>

#include "Auxiliary/Constants.hpp"
#include "Auxiliary/Types.hpp"

namespace egoa::IO {

/**
* @brief Statistics about one execution of the DTP algorithm.
*
* @see egoa::DominatingThetaPath
*/
class DtpRuntimeRow {
public:
Types::string NameOfProblem; /**< The name of the problem that is solved. */
Types::name Name; /**< The name of the instance. */

Types::vertexId SourceId; /**< The source identifier. */

Types::count NumberOfVertices; /**< The number of vertices. */
Types::count NumberOfGenerators; /**< The number of generators. */
Types::count NumberOfLoads; /**< The number of loads. */
Types::count NumberOfEdges; /**< The number of edges. */

Types::count NumberOfEdgesProducingNoCycle;/**< The number of edges that produce not a cycle. */
Types::count NumberOfRelaxedEdges; /**< The number of relaxed edges. */
Types::count NumberOfScannedEdges; /**< The number of scanned edges. */
Types::count NumberOfLabels; /**< The number of labels. */

Types::real GlobalElapsedMilliseconds; /**< The total runtime. */

DtpRuntimeRow() :
NameOfProblem("DTP")
, Name("")

, SourceId(0)

, NumberOfVertices(0)
, NumberOfGenerators(0)
, NumberOfLoads(0)
, NumberOfEdges(0)

, NumberOfEdgesProducingNoCycle(0)
, NumberOfRelaxedEdges(0)
, NumberOfScannedEdges(0)
, NumberOfLabels(0)

, GlobalElapsedMilliseconds(0.0)
{}

inline void Clear () {
NameOfProblem = "DTP";
Name = "";
SourceId = 0;
NumberOfVertices = 0;
NumberOfGenerators = 0;
NumberOfLoads = 0;
NumberOfEdges = 0;
NumberOfScannedEdges = 0;
NumberOfEdgesProducingNoCycle = 0;
NumberOfRelaxedEdges = 0;
GlobalElapsedMilliseconds = 0;
NumberOfLabels = 0;
}

inline static void Header ( std::ostream & os )
{
os
<< "NameOfProblem" << ",\t"
<< "Name" << ",\t"

<< "SourceId" << ",\t"

<< "NumberOfVertices" << ",\t"
<< "NumberOfGenerators" << ",\t"
<< "NumberOfLoads" << ",\t"
<< "NumberOfEdges" << ",\t"

<< "NumberOfScannedEdges" << ",\t"
<< "NumberOfEdgesProducingNoCycle"<< ",\t"
<< "NumberOfRelaxedEdges" << ",\t"

<< "NumberOfLabels" << ",\t"

<< "GlobalElapsedMilliseconds" << ",\t"

<< "\n";
}

inline void Content ( std::ostream & os ) const
{
os
<< NameOfProblem << ",\t"
<< Name << ",\t"

<< SourceId << ",\t"

<< NumberOfVertices << ",\t"
<< NumberOfGenerators << ",\t"
<< NumberOfLoads << ",\t"
<< NumberOfEdges << ",\t"

<< NumberOfScannedEdges << ",\t"
<< NumberOfEdgesProducingNoCycle<< ",\t"
<< NumberOfRelaxedEdges << ",\t"

<< NumberOfLabels << ",\t"

<< GlobalElapsedMilliseconds

<< "\n";
}

inline DtpRuntimeRow & operator+= ( const DtpRuntimeRow & rhs )
{
NumberOfEdgesProducingNoCycle += rhs.NumberOfEdgesProducingNoCycle;
NumberOfRelaxedEdges += rhs.NumberOfRelaxedEdges;
NumberOfScannedEdges += rhs.NumberOfScannedEdges;
NumberOfLabels += rhs.NumberOfLabels;

GlobalElapsedMilliseconds += rhs.GlobalElapsedMilliseconds;

return *this;
}

friend std::ostream & operator<< ( std::ostream & os
, DtpRuntimeRow & dtpRuntimeRow )
{
dtpRuntimeRow.Content ( os );
return os;
}

inline void WriteRowToFileWith ( Types::string const filename
, bool overwrite = false )
{
#ifndef NDEBUG
std::cout << "Write DTP runtime information row to: " << filename << std::endl;
#endif
// Open output stream.
// ofstream fileStream(filename);
std::ofstream fileStream;
overwrite?fileStream.open(filename, std::ofstream::trunc):fileStream.open(filename, std::ofstream::app);
if (!fileStream.is_open()) return;

// file is empty
fileStream.seekp(0, std::ios::end);
if ( fileStream.tellp() == 0 )
{
DtpRuntimeRow::Header(fileStream);
}

fileStream << *this;
}
};

} // namespace egoa::IO

#endif // EGOA__IO__STATISTICS__DTP_RUNTIME_ROW_HPP

0 comments on commit f321e9c

Please sign in to comment.