mageec  0.1.0
MAchine Guided Energy Efficient Compilation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
report.c
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Copyright 2010 Rulequest Research Pty Ltd. */
4 /* */
5 /* This file is part of C5.0 GPL Edition, a single-threaded version */
6 /* of C5.0 release 2.07. */
7 /* */
8 /* C5.0 GPL Edition is free software: you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License as */
10 /* published by the Free Software Foundation, either version 3 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* C5.0 GPL Edition is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU General Public License */
19 /* (gpl.txt) along with C5.0 GPL Edition. If not, see */
20 /* */
21 /* <http://www.gnu.org/licenses/>. */
22 /* */
23 /*************************************************************************/
24 
25 
26 
27 /*************************************************************************/
28 /* */
29 /* Program to produce average results from an xval */
30 /* ----------------------------------------------- */
31 /* */
32 /*************************************************************************/
33 
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 void PrintSummary(float **Val, int No, char *Title);
39 float SE(float sum, float sumsq, int no);
40 
41 int Boost=0, Composite=0, Costs=0, Rules;
42 
43 #define SIZE 0
44 #define ERRP 1
45 #define COST 2
46 
47 
48 int main(int argc, char *argv[])
49 /* ---- */
50 {
51  char Line[100], *p;
52  int Cases, Folds, Repeats, f, r, i, N,
53  Size=0, Errs=0, Form, OK;
54  float ***Raw, **Average=0, FX, Tests, Cost=0;
55 
56  sscanf(argv[1], "%d", &Cases);
57  sscanf(argv[2], "%d", &Folds);
58  sscanf(argv[3], "%d", &Repeats);
59  sscanf(argv[4], "%d", &Rules);
60 
61  /* Assemble all data */
62 
63  Raw = (float ***) calloc(Repeats, sizeof(float **));
64  if ( Repeats > 1 )
65  {
66  Average = (float **) calloc(Repeats, sizeof(float *));
67  }
68 
69  /* Determine input type from the first line */
70 
71  fgets(Line, 100, stdin);
72 
73  /* Count the numbers on the line */
74 
75  N = 0;
76  for ( p = Line ; *p ; )
77  {
78  if ( isdigit(*p) )
79  {
80  N++;
81  while ( isdigit(*p) || *p == '.' ) p++;
82  }
83  else
84  {
85  p++;
86  }
87  }
88 
89  if ( ! memcmp(Line, "boost", 5) )
90  {
91  Boost = 1;
92  Costs = ( N == 3 );
93  }
94  else
95  if ( ! memcmp(Line, "composite", 9) )
96  {
97  Composite = 1;
98  Rules = 0;
99  Costs = ( N == 4 );
100  }
101  else
102  {
103  Costs = ( N == 4 );
104  }
105  Form = ( Composite ? 2 + Costs : Costs );
106 
107  for ( r = 0 ; r < Repeats ; r++ )
108  {
109  Raw[r] = (float **) calloc(Folds, sizeof(float *));
110  if ( Repeats > 1 )
111  {
112  Average[r] = (float *) calloc(3, sizeof(float));
113  }
114 
115  for ( f = 0 ; f < Folds ; f++ )
116  {
117  Raw[r][f] = (float *) calloc(3, sizeof(float));
118 
119  if ( r + f != 0 && ! fgets(Line, 100, stdin) )
120  {
121  printf("\nExpecting %d lines\n", Folds * Repeats);
122  exit(1);
123  }
124 
125  Tests = Cases / Folds + ( f >= Folds - Cases % Folds);
126 
127  if ( ! memcmp(Line, "boost", 5) )
128  {
129  Boost = 1;
130 
131  switch ( Form )
132  {
133  case 0:
134  N = sscanf(Line, "boost %d (%f%%)", &Errs, &FX);
135  OK = ( N == 2 );
136  break;
137 
138  case 1:
139  N = sscanf(Line, "boost %d (%f%%) %f", &Errs, &FX, &Cost);
140  OK = ( N == 3 );
141  }
142  }
143  else
144  {
145  switch ( Form )
146  {
147  case 0:
148  N = sscanf(Line, "%d %d (%f%%)", &Size, &Errs, &FX);
149  OK = ( N == 3 );
150  break;
151 
152  case 1:
153  N = sscanf(Line, "%d %d (%f%%) %f",
154  &Size, &Errs, &FX, &Cost);
155  OK = ( N == 4 );
156  break;
157 
158  case 2:
159  N = sscanf(Line+18, "%d %d (%f%%) %f",
160  &Size, &Errs, &FX, &Cost);
161  OK = ( N == 4 );
162  break;
163  }
164  }
165 
166  if ( ! OK )
167  {
168  printf("\nCannot parse line\n\t%s", Line);
169  exit(1);
170  }
171 
172  Raw[r][f][SIZE] = Size;
173  Raw[r][f][ERRP] = (100.0 * Errs) / Tests;
174  Raw[r][f][COST] = Cost;
175 
176  if ( Average )
177  {
178  for ( i = 0 ; i < 3 ; i++ )
179  {
180  Average[r][i] += Raw[r][f][i];
181  }
182  }
183  }
184 
185  if ( Average )
186  {
187  for ( i = 0 ; i < 3 ; i++ )
188  {
189  Average[r][i] /= Folds;
190  }
191  }
192  }
193 
194  /* Check that amount of data is correct */
195 
196  if ( fgets(Line, 100, stdin) )
197  {
198  printf("\nExpecting %d lines\n", Folds * Repeats * 2);
199  exit(1);
200  }
201 
202  if ( Average )
203  {
204  PrintSummary(Average, Repeats, "XVal");
205  }
206  else
207  {
208  PrintSummary(Raw[SIZE], Folds, "Fold");
209  }
210 
211  return 0;
212 }
213 
214 
215 char
216  *StdP[] = { " Decision Tree ",
217  " ---------------- ",
218  " Size Errors " },
219 
220  *StdPC[] = { " Decision Tree ",
221  " ----------------------- ",
222  " Size Errors Cost " },
223 
224  *Extra[] = { " Rules ",
225  " ----------------",
226  " No Errors" },
227 
228  *ExtraC[]= { " Rules ",
229  " -----------------------",
230  " No Errors Cost" };
231 
232 void PrintSummary(float **Val, int No, char *Title)
233 /* ------------ */
234 {
235  int i, j;
236  float Sum[3], SumSq[3];
237 
238  for ( i = 0 ; i < 3 ; i++ )
239  {
240  Sum[i] = SumSq[i] = 0;
241  }
242 
243  for ( i = 0 ; i <= 2 ; i++ )
244  {
245  switch ( i )
246  {
247  case 0:
248  printf("\n\t%s ", Title);
249  break;
250 
251  case 1:
252  printf("\t---- ");
253  break;
254 
255  case 2:
256  printf("\t ");
257  }
258 
259  printf("%s\n", ( Composite ?
260  ( Costs ? ExtraC[i] : Extra[i] ) :
261  Rules ?
262  ( Costs ? ExtraC[i] : Extra[i] ) :
263  ( Costs ? StdPC[i] : StdP[i] ) ));
264  }
265  printf("\n");
266 
267  for ( j = 0 ; j < No ; j++ )
268  {
269  for ( i = 0 ; i < 3 ; i++ )
270  {
271  Sum[i] += Val[j][i];
272  SumSq[i] += Val[j][i] * Val[j][i];
273  }
274 
275  printf("\t%3d ", j+1);
276 
277  if ( Boost )
278  {
279  printf(" *");
280  }
281  else
282  {
283  printf("%8.1f", Val[j][SIZE]);
284  }
285 
286  printf(" %4.1f%% ", Val[j][ERRP]);
287 
288  if ( Costs )
289  {
290  printf("%5.2f ", Val[j][COST]);
291  }
292 
293  printf("\n");
294  }
295 
296  printf("\n\tMean ");
297 
298  if ( Boost )
299  {
300  printf(" ");
301  }
302  else
303  {
304  printf("%8.1f", Sum[SIZE] / No);
305  }
306 
307  printf(" %4.1f%% ", Sum[ERRP] / No);
308 
309  if ( Costs )
310  {
311  printf("%5.2f ", Sum[COST] / No);
312  }
313 
314  printf("\n\tSE ");
315 
316  if ( Boost )
317  {
318  printf(" ");
319  }
320  else
321  {
322  printf("%8.1f", SE(Sum[SIZE], SumSq[SIZE], No));
323  }
324 
325  printf(" %4.1f%% ", SE(Sum[ERRP], SumSq[ERRP], No));
326 
327  if ( Costs )
328  {
329  printf("%5.2f ", SE(Sum[COST], SumSq[COST], No));
330  }
331 
332  printf("\n");
333 }
334 
335 
336 
337 float SE(float sum, float sumsq, int no)
338 /* -- */
339 {
340  float mean;
341 
342  mean = sum / no;
343 
344  return sqrt( ((sumsq - no * mean * mean) / (no - 1)) / no );
345 }