mageec  0.1.0
MAchine Guided Energy Efficient Compilation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
mcost.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 /* Read variable misclassification costs */
30 /* ------------------------------------- */
31 /* */
32 /*************************************************************************/
33 
34 
35 #include "defns.i"
36 #include "extern.i"
37 
38 
39 void GetMCosts(FILE *Cf)
40 /* --------- */
41 {
42  ClassNo Pred, Real, p, r;
43  char Name[1000];
44  CaseNo i;
45  float Val, Sum=0;
46 
47  LineNo = 0;
48 
49  /* Read entries from cost file */
50 
51  while ( ReadName(Cf, Name, 1000, ':') )
52  {
53  if ( ! (Pred = Which(Name, ClassName, 1, MaxClass)) )
54  {
55  Error(BADCOSTCLASS, Name, "");
56  }
57 
58  if ( ! ReadName(Cf, Name, 1000, ':') ||
59  ! (Real = Which(Name, ClassName, 1, MaxClass)) )
60  {
61  Error(BADCOSTCLASS, Name, "");
62  }
63 
64  if ( ! ReadName(Cf, Name, 1000, ':') ||
65  sscanf(Name, "%f", &Val) != 1 || Val < 0 )
66  {
67  Error(BADCOST, "", "");
68  Val = 1;
69  }
70 
71  if ( Pred > 0 && Real > 0 && Pred != Real && Val != 1 )
72  {
73  /* Have a non-trivial cost entry */
74 
75  if ( ! MCost )
76  {
77  /* Set up cost matrices */
78 
79  MCost = Alloc(MaxClass+1, float *);
80  ForEach(p, 1, MaxClass)
81  {
82  MCost[p] = Alloc(MaxClass+1, float);
83  ForEach(r, 1, MaxClass)
84  {
85  MCost[p][r] = ( p == r ? 0.0 : 1.0 );
86  }
87  }
88  }
89 
90  MCost[Pred][Real] = Val;
91  }
92  }
93  fclose(Cf);
94 
95  /* Don't need weights etc. for predict or interpret, or
96  if not using cost weighting */
97 
98  if ( ! (CostWeights = MaxClass == 2 && MaxCase >= 0 && MCost) )
99  {
100  return;
101  }
102 
103  /* Determine class frequency distribution */
104 
105  ClassFreq = AllocZero(MaxClass+1, double);
106 
107  if ( CWtAtt )
108  {
109  AvCWt = 1; /* relative weights not yet set */
110  ForEach(i, 0, MaxCase)
111  {
112  ClassFreq[Class(Case[i])] += RelCWt(Case[i]);
113  }
114  }
115  else
116  {
117  ForEach(i, 0, MaxCase)
118  {
119  ClassFreq[Class(Case[i])]++;
120  }
121  }
122 
123  /* Find normalised weight multipliers */
124 
125  WeightMul = Alloc(3, float);
126 
127  Sum = (ClassFreq[1] * MCost[2][1] + ClassFreq[2] * MCost[1][2]) /
128  (ClassFreq[1] + ClassFreq[2]);
129 
130  WeightMul[1] = MCost[2][1] / Sum;
131  WeightMul[2] = MCost[1][2] / Sum;
132 
133  /* Adjust MINITEMS to take account of case reweighting */
134 
135  MINITEMS *= Min(WeightMul[1], WeightMul[2]);
136 
138 }