Parasol Planning Library (PPL)
MetricUtils.h
Go to the documentation of this file.
1 #ifndef PMPL_METRIC_UTILS_H_
2 #define PMPL_METRIC_UTILS_H_
3 
4 #include "Utilities/Average.h"
5 #include "Utilities/ClockClass.h"
6 
7 #ifndef _PARALLEL
8 #include <containers/sequential/graph/algorithms/connected_components.h>
9 #endif
10 
11 #include <cstddef>
12 #include <iomanip>
13 #include <iostream>
14 #include <fstream>
15 #include <map>
16 #include <string>
17 #include <tuple>
18 #include <utility>
19 #include <vector>
20 
21 
29 class StatClass final {
30 
31  public:
32 
33  StatClass();
34 
35  void ClearStats();
36 
37  int IncNumCollDetCalls(const std::string& _cdName,
38  const std::string& _callName);
39  size_t GetIsCollTotal();
40  void IncCfgIsColl(const std::string& _callName);
41 
42  int IncLPConnections(const std::string& _lpName , int _incr = 1);
43  int IncLPAttempts(const std::string& _lpName, int _incr = 1);
44  int IncLPCollDetCalls(const std::string& _lpName, int _incr = 1);
45 
46  void IncNodesGenerated(const std::string& _samplerName, size_t _incr = 1);
47  void IncNodesAttempted(const std::string& _samplerName, size_t _incr = 1);
48 
49  //Clock Accessors
50  void ClearClock(const std::string& _name);
51  void StartClock(const std::string& _name);
52  void StopClock(const std::string& _name);
53  void StopPrintClock(const std::string& _name, std::ostream& _os);
54  void PrintClock(const std::string& _name, std::ostream& _os);
55  double GetSeconds(const std::string& _name);
56  int GetUSeconds(const std::string& _name);
57 
58  // Statistics Accessors/Modifiers
59  double GetStat(const std::string& _s);
60  void SetStat(const std::string& _s, const double _v);
61  void IncStat(const std::string& _s, const double _v = 1);
62 
63  // Average Accessors
64  Average<double>& GetAverage(const std::string& _s);
65 
66  // Histories
67  std::vector<double>& GetHistory(const std::string& _s);
68  void AddToHistory(const std::string& _s, double _v);
69  void WriteHistory(const std::string& _s);
70 
71  void SetAuxDest(const std::string& _s);
72 
73  void PrintAllStats(std::ostream& _os);
74 
75  template <typename RoadmapType>
76  void PrintAllStats(std::ostream& _os, RoadmapType* _rmap = nullptr);
77 
78  template<class GraphType>
79  void DisplayCCStats(std::ostream& _os, GraphType&);
80 
81  // m_lpInfo represents information about the Local Planners, referenced by
82  // name
83  // m_lpInfo.first is the name of the Local Planner
84  // m_lpInfo.second.get<0>() is the # of LP attempts
85  // m_lpInfo.second.get<1>() is the # of LP connections (successes)
86  // m_lpInfo.second.get<2>() is the # of LP collision detection calls
87  std::map<std::string, std::tuple<size_t, size_t, size_t> > m_lpInfo;
88  std::map<std::string, size_t> m_collDetCountByName;
89 
90  std::map<std::string, ClockClass> m_clockMap;
91 
94  std::map<std::string, size_t> m_isCollByName;
95  size_t m_isCollTotal;
96 
97 
98  //m_samplerInfo represents sampler nodes attempted and generated
99  // map<string, pair<int, int> > represents a mapping between the sampler name
100  // and first the number of attempted samples, then the number of generated samples
101  // that is, pair.first = attempts, pair.second = generated (successful attempts)
102  std::map<std::string, std::pair<size_t, size_t>> m_samplerInfo;
103 
104  private:
105 
108 
109  std::map<std::string, size_t> m_numCollDetCalls;
110  std::map<std::string, double> m_stats;
111  std::map<std::string, Average<double>> m_averages;
112  std::map<std::string, std::vector<double>> m_histories;
113  std::string m_auxFileDest;
114 
116 
117 };
118 
119 
124 class MethodTimer final {
125 
128 
129  StatClass* const m_stats;
130  const std::string m_label;
131 
133 
134  public:
135 
138 
142  MethodTimer(StatClass* const _stats, const std::string& _label);
143 
145  ~MethodTimer();
146 
148 
149 };
150 
151 
152 template <typename RoadmapType>
153 void
155 PrintAllStats(std::ostream& _os, RoadmapType* _rmap) {
156  if(_rmap) {
157  // Output roadmap statistics.
158  _os << "Roadmap Statistics:\n"
159  << "\n Number of Nodes: " << _rmap->get_num_vertices()
160  << "\n Number of Edges: " << _rmap->get_num_edges()
161  << std::endl;
162  DisplayCCStats(_os, *_rmap);
163  }
164 
165  PrintAllStats(_os);
166 }
167 
168 
169 template <class GraphType>
170 void
172 DisplayCCStats(std::ostream& _os, GraphType& _g) {
173  #ifndef _PARALLEL
174  // Compute the CC information.
175  typedef typename GraphType::vertex_descriptor VID;
176 
177  stapl::sequential::vector_property_map<GraphType, size_t> cMap;
178  std::vector<std::pair<size_t, VID>> ccs;
179 
180  stapl::sequential::get_cc_stats(_g, cMap, ccs);
181 
182  size_t ccnum = 0;
183  _os << "\n There are " << ccs.size() << " connected components:";
184  for(auto cc : ccs)
185  _os << "\n CC[" << ccnum++ << "]: " << cc.first
186  << " (vid " << cc.second << ")";
187  _os << std::endl;
188 
189  #else
190  _os << "WARNING: CCs not computed, called sequential implementation of CC stats \n" ;
191  #endif
192 }
193 
194 #endif
Definition: MetricUtils.h:124
MethodTimer(StatClass *const _stats, const std::string &_label)
Definition: MetricUtils.cpp:332
~MethodTimer()
Destruction stops the named clock.
Definition: MetricUtils.cpp:339
Definition: MetricUtils.h:29
void DisplayCCStats(std::ostream &_os, GraphType &)
Definition: MetricUtils.h:172
double GetStat(const std::string &_s)
Definition: MetricUtils.cpp:162
std::map< std::string, ClockClass > m_clockMap
Definition: MetricUtils.h:90
int IncNumCollDetCalls(const std::string &_cdName, const std::string &_callName)
Definition: MetricUtils.cpp:28
StatClass()
Definition: MetricUtils.cpp:9
void WriteHistory(const std::string &_s)
Definition: MetricUtils.cpp:204
std::map< std::string, size_t > m_isCollByName
Definition: MetricUtils.h:94
void IncNodesGenerated(const std::string &_samplerName, size_t _incr=1)
Definition: MetricUtils.cpp:76
void StopClock(const std::string &_name)
Definition: MetricUtils.cpp:102
std::vector< double > & GetHistory(const std::string &_s)
Definition: MetricUtils.cpp:190
void SetStat(const std::string &_s, const double _v)
Definition: MetricUtils.cpp:169
void StopPrintClock(const std::string &_name, std::ostream &_os)
Definition: MetricUtils.cpp:112
void IncStat(const std::string &_s, const double _v=1)
Definition: MetricUtils.cpp:176
void PrintAllStats(std::ostream &_os)
Definition: MetricUtils.cpp:220
Average< double > & GetAverage(const std::string &_s)
Definition: MetricUtils.cpp:183
int IncLPAttempts(const std::string &_lpName, int _incr=1)
Definition: MetricUtils.cpp:62
std::map< std::string, size_t > m_collDetCountByName
Definition: MetricUtils.h:88
void SetAuxDest(const std::string &_s)
Definition: MetricUtils.cpp:213
double GetSeconds(const std::string &_name)
Definition: MetricUtils.cpp:142
size_t m_isCollTotal
Definition: MetricUtils.h:95
int IncLPConnections(const std::string &_lpName, int _incr=1)
Definition: MetricUtils.cpp:55
size_t GetIsCollTotal()
Definition: MetricUtils.cpp:40
int IncLPCollDetCalls(const std::string &_lpName, int _incr=1)
Definition: MetricUtils.cpp:69
void PrintClock(const std::string &_name, std::ostream &_os)
Definition: MetricUtils.cpp:122
void ClearClock(const std::string &_name)
Definition: MetricUtils.cpp:132
void AddToHistory(const std::string &_s, double _v)
Definition: MetricUtils.cpp:197
void IncCfgIsColl(const std::string &_callName)
Definition: MetricUtils.cpp:47
void IncNodesAttempted(const std::string &_samplerName, size_t _incr=1)
Definition: MetricUtils.cpp:83
void StartClock(const std::string &_name)
Definition: MetricUtils.cpp:90
void ClearStats()
Definition: MetricUtils.cpp:16
std::map< std::string, std::pair< size_t, size_t > > m_samplerInfo
Definition: MetricUtils.h:102
std::map< std::string, std::tuple< size_t, size_t, size_t > > m_lpInfo
Definition: MetricUtils.h:87
int GetUSeconds(const std::string &_name)
Definition: MetricUtils.cpp:152