averages.c
Go to the documentation of this file.
1 #include "averages.h"
2 
3 #include "error.h"
4 #include "algebra.h"
5 #include "defines.h"
6 
7 #include <math.h>
8 #include <time.h>
9 #include <unistd.h>
10 
21 void InitAverages(unsigned int m,boolean useCharts,boolean useSamples,
22  unsigned int maxIt,Taverages *av)
23 {
24  av->maxExperiments=m;
25  av->nSuccess=0;
26 
27  NEW(av->times,m,double);
28  NEW(av->memory,m,double);
29  NEW(av->pathLength,m,double);
30  NEW(av->pathCost,m,double);
31 
32  if (useCharts)
33  { NEW(av->nCharts,m,double); }
34  else
35  av->nCharts=NULL;
36 
37  if (useSamples)
38  { NEW(av->nSamples,m,double); }
39  else
40  av->nSamples=NULL;
41 
42  av->maxIt=maxIt;
43 
44  if (maxIt!=NO_UINT)
45  {
46  unsigned int i;
47 
48  NEW(av->time,maxIt,double);
49  NEW(av->timeStd,maxIt,double);
50 
51  NEW(av->cost,maxIt,double);
52  NEW(av->costStd,maxIt,double);
53  NEW(av->ncost,maxIt,unsigned int);
54 
55  for(i=0;i<maxIt;i++)
56  {
57  av->time[i]=0;
58  av->timeStd[i]=0;
59  av->cost[i]=0;
60  av->costStd[i]=0;
61  av->ncost[i]=0;
62  }
63  }
64  else
65  {
66  av->time=NULL;
67  av->timeStd=NULL;
68 
69  av->cost=NULL;
70  av->costStd=NULL;
71  av->ncost=NULL;
72  }
73 }
74 
75 void NewSuccesfulExperiment(double t,unsigned int mem,double pl,double pc,
76  unsigned int nc,unsigned int ns,
77  double *time,double *cost,Taverages *av)
78 {
79  double m;
80 
81  if (av->nSuccess>=av->maxExperiments)
82  Error("More succesful experiments than maximum experiments");
83 
84  fprintf(stderr,"Data about the execution:\n");
85  av->times[av->nSuccess]=t;
86  fprintf(stderr," Elapsed time: %f sec\n",t);
87 
88  m=((double)mem)/(1024.0*1024.0);
89  av->memory[av->nSuccess]=m;
90  fprintf(stderr," Used memory : %f Mb\n",m);
91 
92  av->pathLength[av->nSuccess]=pl;
93  fprintf(stderr," Path length : %f\n",pl);
94 
95  av->pathCost[av->nSuccess]=pc;
96  fprintf(stderr," Path cost : %f\n",pc);
97 
98 
99  if (av->nCharts!=NULL)
100  {
101  av->nCharts[av->nSuccess]=nc;
102  fprintf(stderr," Num charts : %u\n",nc);
103  }
104 
105  if (av->nSamples!=NULL)
106  {
107  av->nSamples[av->nSuccess]=ns;
108  fprintf(stderr," Num samples : %u\n",ns);
109  }
110 
111  if (av->maxIt!=NO_UINT)
112  {
113  if ((time==NULL)||(cost==NULL))
114  {
115  /* Time and cost are expected but they are not
116  actually given -> delete them. They would
117  be used/expected any more. */
118  free(av->time);
119  free(av->timeStd);
120  free(av->cost);
121  free(av->costStd);
122  free(av->ncost);
123 
124  av->maxIt=NO_UINT;
125  }
126  else
127  {
128  unsigned int i;
129 
130  for(i=0;i<av->maxIt;i++)
131  {
132  av->time[i]+=time[i];
133  av->timeStd[i]+=(time[i]*time[i]);
134 
135  if (cost[i]>-0.5)
136  {
137  /* If we have cost data at this iteration */
138  av->cost[i]+=cost[i];
139  av->costStd[i]+=(cost[i]*cost[i]);
140  av->ncost[i]++;
141  }
142  }
143  }
144  }
145 
146  av->nSuccess++;
147 }
148 
149 void PrintAveragesHeader(FILE *f,int argc, char **arg,Taverages *av)
150 {
151  unsigned int i;
152  time_t t;
153  char hostname[150];
154  char *path;
155 
156  fprintf(f,"\n\n%% **************************************************\n");
157  t=time(NULL);
158  gethostname(hostname,50);
159  path=getcwd(NULL,0);
160  fprintf(f,"Date : %s",ctime(&t));
161  fprintf(f,"Machine : %s\n",hostname);
162  fprintf(f,"Path : %s\n",path);
163  free(path);
164  fprintf(f,"Command line: ");
165  for(i=0;i<argc;i++)
166  fprintf(f,"%s ",arg[i]);
167  fprintf(f,"\n");
168 }
169 
170 void PrintAverages(FILE *f,Taverages *av)
171 {
172  fprintf(f,"%% **************************************************\n");
173  fprintf(f,"Execution averages over %u repetitions\n",av->maxExperiments);
174  fprintf(f," Success ratio: %u / %u = %g\n",av->nSuccess,av->maxExperiments,
175  (double)av->nSuccess/(double)av->maxExperiments);
176 
177  if (av->nSuccess>0)
178  {
179  unsigned int i;
180  double mt,ml,mc,ms;
181 
182  if (av->maxIt!=NO_UINT)
183  {
184  /* Print statistics about time and cost per iteration */
185  fprintf(f,"****************************************************\n");
186  fprintf(f,"It Mean_time Std_time Mean_cost Std_cost N_cost\n");
187 
188  /* Compute the mean of time/cost */
189  for (i=0;i<av->maxIt;i++)
190  {
191  av->time[i]/=(double)av->nSuccess;
192  if (av->ncost[i]>0)
193  av->cost[i]/=(double)av->ncost[i];
194  }
195  /* Compute the standard deviation of time/cost */
196  /*
197  (1/n)*\sum (x_i-m_i)^2 = (1/n)*\sum (x_i^2-2*x_i*m_i+m_i^2)=
198  (1/n)*\sum x_i^2 - (1/n)*2*m_i*\sum x_i + m_i^2 = (1/n)*\sum x_i^2 - m_i^2
199  */
200  for (i=0;i<av->maxIt;i++)
201  {
202  av->timeStd[i]=sqrt(av->timeStd[i]/(double)av->nSuccess-pow(av->time[i],2));
203  if (av->ncost[i]>0)
204  av->costStd[i]=sqrt(av->costStd[i]/(double)av->ncost[i]-pow(av->cost[i],2));
205  }
206 
207  /* print the results */
208  for (i=0;i<av->maxIt;i++)
209  {
210  if ((i%ITERATIONS_BETWEEN_DATA)==0)
211  {
212  fprintf(f, "%u %g %g ",i,av->time[i],av->timeStd[i]);
213  if (av->ncost[i]==0)
214  fprintf(stderr, "0 0 0\n");
215  else
216  fprintf(stderr, "%g %g %g\n",av->cost[i],av->costStd[i],(double)av->ncost[i]/(double)av->maxExperiments);
217  }
218  }
219  fprintf(f,"**************************************************\n");
220  }
221 
222  /* Print the rest of statistics (exec time, path lenght,...) */
223  mt=Mean(av->nSuccess,av->times);
224  fprintf(f," Time (CPU sec) \n");
225  fprintf(f," Mean : %g\n",mt);
226  fprintf(f," Std dev : %g\n",StdDev(av->nSuccess,mt,av->times));
227 
228  mt=Mean(av->nSuccess,av->memory);
229  fprintf(f," Memory (Mb) \n");
230  fprintf(f," Mean : %g\n",mt);
231  fprintf(f," Std dev : %g\n",StdDev(av->nSuccess,mt,av->memory));
232 
233  ml=Mean(av->nSuccess,av->pathLength);
234  fprintf(f," Path length\n");
235  fprintf(f," Mean : %g\n",ml);
236  fprintf(f," Std dev : %g\n",StdDev(av->nSuccess,ml,av->pathLength));
237 
238  ml=Mean(av->nSuccess,av->pathCost);
239  fprintf(f," Path Cost\n");
240  fprintf(f," Mean : %g\n",ml);
241  fprintf(f," Std dev : %g\n",StdDev(av->nSuccess,ml,av->pathCost));
242 
243  if (av->nCharts!=NULL)
244  {
245  mc=Mean(av->nSuccess,av->nCharts);
246  fprintf(f," Charts\n");
247  fprintf(f," Mean : %g\n",mc);
248  fprintf(f," Std dev : %g\n",StdDev(av->nSuccess,mc,av->nCharts));
249  }
250 
251  if (av->nSamples!=NULL)
252  {
253  ms=Mean(av->nSuccess,av->nSamples);
254  fprintf(f," Samples\n");
255  fprintf(f," Mean : %g\n",ms);
256  fprintf(f," Std dev : %g\n",StdDev(av->nSuccess,ms,av->nSamples));
257  }
258  }
259  else
260  fprintf(f," Not enough successes to generate statistics\n");
261 }
262 
264 {
265  free(av->times);
266  free(av->memory);
267  free(av->pathLength);
268  free(av->pathCost);
269 
270  if (av->nCharts!=NULL)
271  free(av->nCharts);
272 
273  if (av->nSamples!=NULL)
274  free(av->nSamples);
275 
276  if (av->maxIt!=NO_UINT)
277  {
278  free(av->time);
279  free(av->timeStd);
280  free(av->cost);
281  free(av->costStd);
282  free(av->ncost);
283  }
284 }
unsigned int * ncost
Definition: averages.h:53
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
void NewSuccesfulExperiment(double t, unsigned int mem, double pl, double pc, unsigned int nc, unsigned int ns, double *time, double *cost, Taverages *av)
Adds data of a new experiment.
Definition: averages.c:75
#define ITERATIONS_BETWEEN_DATA
Iterations between printed data on time/cost.
Definition: averages.h:24
double * cost
Definition: averages.h:51
unsigned int nSuccess
Definition: averages.h:34
void Error(const char *s)
General error function.
Definition: error.c:80
Error and warning functions.
double * pathLength
Definition: averages.h:38
void PrintAverages(FILE *f, Taverages *av)
Prints the averages of a set of experiments.
Definition: averages.c:170
void PrintAveragesHeader(FILE *f, int argc, char **arg, Taverages *av)
Prints a header to the averages results.
Definition: averages.c:149
Definitions of constants and macros used in several parts of the cuik library.
double * timeStd
Definition: averages.h:49
Auxiliary functions to deal averages of path planner executions.
double * memory
Definition: averages.h:37
double * times
Definition: averages.h:36
double StdDev(unsigned int s, double m, double *v)
Computes the standard deviation.
double * nCharts
Definition: averages.h:41
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
double * nSamples
Definition: averages.h:42
Structure to store expeeriment results.
Definition: averages.h:32
double * pathCost
Definition: averages.h:39
double * costStd
Definition: averages.h:52
void DeleteAverages(Taverages *av)
Deletes the space used by a set of averages.
Definition: averages.c:263
double * time
Definition: averages.h:48
unsigned int maxExperiments
Definition: averages.h:33
unsigned int maxIt
Definition: averages.h:44
void InitAverages(unsigned int m, boolean useCharts, boolean useSamples, unsigned int maxIt, Taverages *av)
Initializes a set of averages.
Definition: averages.c:21
double Mean(unsigned int s, double *v)
Computes the mean.