HOG2
StatUtil.cpp
Go to the documentation of this file.
1 /*
2  * $Id: statUtil.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 11/9/05.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include "StatUtil.h"
13 #include <math.h>
14 #include <cstdio>
15 
16 inline double max(double a, double b)
17 { if (a > b) return a; return b; }
18 
23 void setupAverageRatio(StatCollection *stats, char *stat1, char *stat2)
24 {
25  stats->ClearAllStats();
26  stats->ClearFilters();
27  stats->AddIncludeFilter(stat1);
28  stats->AddIncludeFilter(stat2);
29  stats->AddIncludeFilter("simulationTime");
30 }
31 
38 {
39 // stats->PrintStatsTable();
40 
41  int stat1Type = -1, stat2Type = -1;
42  std::vector<const statistics *> stat1;
43  std::vector<const statistics *> stat2;
44  int currOwner = -1;
45 
46  for (int x = 0; x < stats->GetNumStats(); x++)
47  {
48  const statistics *s = stats->GetStatNum(x);
49  if (s->owner == stats->LookupOwner("unitSimulation"))
50  {
51  while (stat1.size() > stat2.size())
52  stat1.pop_back();
53  while (stat2.size() > stat1.size())
54  stat2.pop_back();
55  continue;
56  }
57  if (currOwner == -1)
58  {
59  currOwner = s->owner;
60  printf("Collecting stats for %s\n", stats->LookupOwnerID(currOwner));
61  }
62  if (s->owner != currOwner)
63  continue;
64 
65  if (stat1Type == -1)
66  {
67  stat1Type = s->category;
68  stat1.push_back(s);
69  }
70  else if (stat1Type == s->category)
71  {
72  stat1.push_back(s);
73  }
74  else if (stat2Type == -1)
75  {
76  stat2Type = s->category;
77  stat2.push_back(s);
78  }
79  else if (stat2Type == s->category)
80  {
81  stat2.push_back(s);
82  }
83  }
84 
85  while (stat1.size() > stat2.size())
86  stat1.pop_back();
87  while (stat2.size() > stat1.size())
88  stat2.pop_back();
89 
90  double sum1=0.0, sum2=0.0;
91 
92  for (unsigned int x = 0; x < stat1.size(); x++)
93  {
94  if (stat1[x]->sType == longStored)
95  sum1 += (double)stat1[x]->value.lval;
96  else
97  sum1 += stat1[x]->value.fval;
98 
99  if (stat2[x]->sType == longStored)
100  sum2 += (double)stat2[x]->value.lval;
101  else
102  sum2 += stat2[x]->value.fval;
103  }
104 
105 // printf("Final table of values:\n");
106 // for (unsigned int x = 0; x < stat1.size(); x++)
107 // {
108 // if (stat1[x]->sType == longStored)
109 // printf("%6d\t", stat1[x]->value.lval);
110 // else
111 // printf("%1.4f\t", stat1[x]->value.fval);
112 //
113 // if (stat2[x]->sType == longStored)
114 // printf("%6d\n", stat2[x]->value.lval);
115 // else
116 // printf("%1.4f\n", stat2[x]->value.fval);
117 // }
118 
119 
120  printf("Measuring ratio between %s and %s as collected by %s\n",
121  stats->lookupCategoryID(stat1Type),
122  stats->lookupCategoryID(stat2Type),
123  stats->LookupOwnerID(currOwner));
124  printf("%d stats collected\n%s average %1.4e\n%s average %1.4e\n",
125  (int)stat1.size(), stats->lookupCategoryID(stat1Type),
126  (double)sum1/stat1.size(), stats->lookupCategoryID(stat2Type),
127  (double)sum2/stat2.size());
128  printf("Ratios:\n%s / %s: %1.4e\n%s / %s: %1.4e\n",
129  stats->lookupCategoryID(stat1Type), stats->lookupCategoryID(stat2Type),
130  sum1/sum2,
131  stats->lookupCategoryID(stat2Type), stats->lookupCategoryID(stat1Type),
132  sum2/sum1);
133 }
134 
135 
140 double SumStatEntries(StatCollection *stats, const char *category, const char *owner)
141 {
142  double sum = 0.0;
143  int catID, ownerID;
144  catID = stats->LookupCategory(category);
145  ownerID = stats->LookupOwner(owner);
146 
147  for (int x = 0; x < (int)stats->GetNumStats(); x++)
148  {
149  if ((stats->GetStatNum(x)->category == catID) && (stats->GetStatNum(x)->owner == ownerID))
150  {
151  if (stats->GetStatNum(x)->sType == floatStored)
152  sum += stats->GetStatNum(x)->value.fval;
153  else
154  sum += (double)stats->GetStatNum(x)->value.lval;
155  }
156  }
157 
158  return sum;
159 }
160 
161 double maxStatEntries(StatCollection *stats, const char *category, const char *owner)
162 {
163  double maxval = -9999999999.9;
164  int catID, ownerID;
165  catID = stats->LookupCategory(category);
166  ownerID = stats->LookupOwner(owner);
167 
168  for (int x = 0; x < (int)stats->GetNumStats(); x++)
169  {
170  if ((stats->GetStatNum(x)->category == catID) && (stats->GetStatNum(x)->owner == ownerID))
171  {
172  if (stats->GetStatNum(x)->sType == floatStored)
173  maxval = max(maxval, stats->GetStatNum(x)->value.fval);
174  else
175  maxval = max(maxval, (double)stats->GetStatNum(x)->value.lval);
176  }
177  }
178  return maxval;
179 }
180 
182 long unsigned countStatEntries(StatCollection *stats, const char *category, const char *owner)
183 {
184  long unsigned count = 0;
185  int catID, ownerID;
186  catID = stats->LookupCategory(category);
187  ownerID = stats->LookupOwner(owner);
188 
189  for (int x = 0; x < (int)stats->GetNumStats(); x++)
190  if ((stats->GetStatNum(x)->category == catID) && (stats->GetStatNum(x)->owner == ownerID))
191  count++;
192 
193  return count;
194 }
195 
196 double averageStatEntries(StatCollection *stats, const char *category, const char *owner)
197 {
198  double sum = 0.0;
199  double count = 0.0;
200  int catID, ownerID;
201  catID = stats->LookupCategory(category);
202  ownerID = stats->LookupOwner(owner);
203 
204  for (int x = 0; x < (int)stats->GetNumStats(); x++)
205  {
206  if ((stats->GetStatNum(x)->category == catID) && (stats->GetStatNum(x)->owner == ownerID))
207  {
208  if (stats->GetStatNum(x)->sType == floatStored)
209  sum += stats->GetStatNum(x)->value.fval;
210  else
211  sum += (double)stats->GetStatNum(x)->value.lval;
212  count++;
213  }
214  }
215  if (count > 0)
216  return sum/count;
217  return 0;
218 }
219 
220 double stdevStatEntries(StatCollection *stats, const char *category, const char *owner)
221 {
222  double average = averageStatEntries(stats, category, owner);
223  double stdev = 0;
224  double count = 0;
225  int catID, ownerID;
226  catID = stats->LookupCategory(category);
227  ownerID = stats->LookupOwner(owner);
228 
229  for (int x = 0; x < (int)stats->GetNumStats(); x++)
230  {
231  if ((stats->GetStatNum(x)->category == catID) && (stats->GetStatNum(x)->owner == ownerID))
232  {
233  if (stats->GetStatNum(x)->sType == floatStored)
234  stdev += (average-stats->GetStatNum(x)->value.fval)*
235  (average-stats->GetStatNum(x)->value.fval);
236  else
237  stdev += (average-(double)stats->GetStatNum(x)->value.lval)*
238  (average-(double)stats->GetStatNum(x)->value.lval);
239  count++;
240  }
241  }
242  if (count > 0)
243  return sqrt(stdev/count);
244  return 0;
245 }
StatUtil.h
floatStored
@ floatStored
Definition: StatCollection.h:19
if
if(state==GLUT_DOWN)
Definition: GLUThog.cpp:244
StatCollection::LookupOwnerID
const char * LookupOwnerID(int id) const
Given a owner ID, return the text description.
Definition: StatCollection.cpp:182
statistics::owner
int owner
Definition: StatCollection.h:23
statistics::sType
storedType sType
Definition: StatCollection.h:25
StatCollection::LookupCategory
int LookupCategory(const char *category) const
Given a category, look up the ID.
Definition: StatCollection.cpp:241
statValue::fval
double fval
Definition: StatCollection.h:18
stdevStatEntries
double stdevStatEntries(StatCollection *stats, const char *category, const char *owner)
Definition: StatUtil.cpp:220
StatCollection::ClearAllStats
void ClearAllStats()
Remove all stat entries from the collection.
Definition: StatCollection.cpp:148
setupAverageRatio
void setupAverageRatio(StatCollection *stats, char *stat1, char *stat2)
Setup the stats so a ratio between two stats for each unit can be properly measured.
Definition: StatUtil.cpp:23
SumStatEntries
double SumStatEntries(StatCollection *stats, const char *category, const char *owner)
Sum the values of all stat entries for the same (category, owner).
Definition: StatUtil.cpp:140
StatCollection::ClearFilters
void ClearFilters()
Clear any filters being used for stat entry.
Definition: StatCollection.cpp:220
StatCollection::lookupCategoryID
const char * lookupCategoryID(int id) const
Given a category ID, return the text description.
Definition: StatCollection.cpp:174
StatCollection::GetStatNum
const statistics * GetStatNum(int which) const
Return the nth stat which has been collected.
Definition: StatCollection.cpp:166
max
double max(double a, double b)
Definition: StatUtil.cpp:16
statistics::category
int category
Definition: StatCollection.h:23
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
statistics
Definition: StatCollection.h:21
statistics::value
statValue value
Definition: StatCollection.h:24
statValue::lval
long lval
Definition: StatCollection.h:18
maxStatEntries
double maxStatEntries(StatCollection *stats, const char *category, const char *owner)
Definition: StatUtil.cpp:161
averageStatEntries
double averageStatEntries(StatCollection *stats, const char *category, const char *owner)
Definition: StatUtil.cpp:196
StatCollection::AddIncludeFilter
void AddIncludeFilter(const char *category)
Definition: StatCollection.cpp:197
longStored
@ longStored
Definition: StatCollection.h:19
StatCollection::GetNumStats
int GetNumStats() const
The number of stats collected so far.
Definition: StatCollection.cpp:158
countStatEntries
long unsigned countStatEntries(StatCollection *stats, const char *category, const char *owner)
Count the number of state instances in the stat collection.
Definition: StatUtil.cpp:182
measureAverageRatio
void measureAverageRatio(StatCollection *stats)
Measures the ration between the two stats set-up from setupAverageRatio.
Definition: StatUtil.cpp:37
StatCollection::LookupOwner
int LookupOwner(const char *owner) const
Given an owner, look up the ID.
Definition: StatCollection.cpp:275