[26] | 1 | /*************************************************************************/ |
---|
| 2 | /* */ |
---|
| 3 | /* Form a set of rules from a decision tree */ |
---|
| 4 | /* ---------------------------------------- */ |
---|
| 5 | /* */ |
---|
| 6 | /*************************************************************************/ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #include "defns.i" |
---|
| 10 | #include "types.i" |
---|
| 11 | #include "extern.i" |
---|
| 12 | #include "rulex.i" |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | ItemNo *TargetClassFreq, /* [Boolean] */ |
---|
| 16 | *Errors, /* [Condition] */ |
---|
| 17 | *Total; /* [Condition] */ |
---|
| 18 | |
---|
| 19 | float *Pessimistic, /* [Condition] */ |
---|
| 20 | *Actual, /* [Condition] */ |
---|
| 21 | *CondSigLevel; /* [Condition] */ |
---|
| 22 | |
---|
| 23 | Boolean **CondSatisfiedBy, /* [Condition][ItemNo] */ |
---|
| 24 | *Deleted; /* [Condition] */ |
---|
| 25 | |
---|
| 26 | DiscrValue *SingleValue; /* [Attribute] */ |
---|
| 27 | |
---|
| 28 | Condition *Stack; |
---|
| 29 | |
---|
| 30 | short MaxDisjuncts, |
---|
| 31 | MaxDepth; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | /*************************************************************************/ |
---|
| 36 | /* */ |
---|
| 37 | /* Form a ruleset from decision tree t */ |
---|
| 38 | /* */ |
---|
| 39 | /*************************************************************************/ |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | FormRules(t) |
---|
| 43 | /* ---------- */ |
---|
| 44 | Tree t; |
---|
| 45 | { |
---|
| 46 | short i; |
---|
| 47 | |
---|
| 48 | /* Find essential parameters and allocate storage */ |
---|
| 49 | |
---|
| 50 | MaxDepth = 0; |
---|
| 51 | MaxDisjuncts = 0; |
---|
| 52 | |
---|
| 53 | TreeParameters(t, 0); |
---|
| 54 | |
---|
| 55 | Actual = (float *) calloc(MaxDepth+2, sizeof(float)); |
---|
| 56 | Total = (ItemNo *) calloc(MaxDepth+2, sizeof(ItemNo)); |
---|
| 57 | Errors = (ItemNo *) calloc(MaxDepth+2, sizeof(ItemNo)); |
---|
| 58 | Pessimistic = (float *) calloc(MaxDepth+2, sizeof(float)); |
---|
| 59 | |
---|
| 60 | CondSigLevel = (float *) calloc(MaxDepth+2, sizeof(float)); |
---|
| 61 | |
---|
| 62 | TargetClassFreq = (ItemNo *) calloc(2, sizeof(ItemNo)); |
---|
| 63 | |
---|
| 64 | Deleted = (Boolean *) calloc(MaxDepth+2, sizeof(Boolean)); |
---|
| 65 | CondSatisfiedBy = (char **) calloc(MaxDepth+2, sizeof(char *)); |
---|
| 66 | Stack = (Condition *) calloc(MaxDepth+2, sizeof(Condition)); |
---|
| 67 | |
---|
| 68 | ForEach(i, 0, MaxDepth+1) |
---|
| 69 | { |
---|
| 70 | CondSatisfiedBy[i] = (char *) calloc(MaxItem+1, sizeof(char)); |
---|
| 71 | Stack[i] = (Condition) malloc(sizeof(struct CondRec)); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | SingleValue = (DiscrValue *) calloc(MaxAtt+1, sizeof(DiscrValue)); |
---|
| 75 | |
---|
| 76 | InitialiseRules(); |
---|
| 77 | |
---|
| 78 | /* Extract and prune disjuncts */ |
---|
| 79 | |
---|
| 80 | Scan(t, 0); |
---|
| 81 | |
---|
| 82 | /* Deallocate storage */ |
---|
| 83 | |
---|
| 84 | ForEach(i, 0, MaxDepth+1) |
---|
| 85 | { |
---|
| 86 | cfree(CondSatisfiedBy[i]); |
---|
| 87 | cfree(Stack[i]); |
---|
| 88 | } |
---|
| 89 | cfree(Deleted); |
---|
| 90 | cfree(CondSatisfiedBy); |
---|
| 91 | cfree(Stack); |
---|
| 92 | |
---|
| 93 | cfree(Actual); |
---|
| 94 | cfree(Total); |
---|
| 95 | cfree(Errors); |
---|
| 96 | cfree(Pessimistic); |
---|
| 97 | |
---|
| 98 | cfree(CondSigLevel); |
---|
| 99 | |
---|
| 100 | cfree(TargetClassFreq); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | /*************************************************************************/ |
---|
| 106 | /* */ |
---|
| 107 | /* Find the maximum depth and the number of leaves in tree t */ |
---|
| 108 | /* with initial depth d */ |
---|
| 109 | /* */ |
---|
| 110 | /*************************************************************************/ |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | TreeParameters(t, d) |
---|
| 114 | /* --------------- */ |
---|
| 115 | Tree t; |
---|
| 116 | short d; |
---|
| 117 | { |
---|
| 118 | DiscrValue v; |
---|
| 119 | |
---|
| 120 | if ( t->NodeType ) |
---|
| 121 | { |
---|
| 122 | ForEach(v, 1, t->Forks) |
---|
| 123 | { |
---|
| 124 | TreeParameters(t->Branch[v], d+1); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | /* This is a leaf */ |
---|
| 130 | |
---|
| 131 | if ( d > MaxDepth ) MaxDepth = d; |
---|
| 132 | MaxDisjuncts++; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | /*************************************************************************/ |
---|
| 139 | /* */ |
---|
| 140 | /* Extract disjuncts from tree t at depth d, and process them */ |
---|
| 141 | /* */ |
---|
| 142 | /*************************************************************************/ |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | Scan(t, d) |
---|
| 146 | /* ---- */ |
---|
| 147 | Tree t; |
---|
| 148 | short d; |
---|
| 149 | { |
---|
| 150 | DiscrValue v; |
---|
| 151 | short i; |
---|
| 152 | Condition *Term; |
---|
| 153 | Test x, FindTest(); |
---|
| 154 | |
---|
| 155 | if ( t->NodeType ) |
---|
| 156 | { |
---|
| 157 | d++; |
---|
| 158 | |
---|
| 159 | x = (Test) malloc(sizeof(struct TestRec)); |
---|
| 160 | x->NodeType = t->NodeType; |
---|
| 161 | x->Tested = t->Tested; |
---|
| 162 | x->Forks = t->Forks; |
---|
| 163 | x->Cut = ( t->NodeType == ThreshContin ? t->Cut : 0 ); |
---|
| 164 | if ( t->NodeType == BrSubset ) |
---|
| 165 | { |
---|
| 166 | x->Subset = (Set *) calloc(t->Forks + 1, sizeof(Set)); |
---|
| 167 | ForEach(v, 1, t->Forks) |
---|
| 168 | { |
---|
| 169 | x->Subset[v] = t->Subset[v]; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | Stack[d]->CondTest = FindTest(x); |
---|
| 174 | |
---|
| 175 | ForEach(v, 1, t->Forks) |
---|
| 176 | { |
---|
| 177 | Stack[d]->TestValue = v; |
---|
| 178 | Scan(t->Branch[v], d); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | else |
---|
| 182 | if ( t->Items >= 1 ) |
---|
| 183 | { |
---|
| 184 | /* Leaf of decision tree - construct the set of |
---|
| 185 | conditions associated with this leaf and prune */ |
---|
| 186 | |
---|
| 187 | Term = (Condition *) calloc(d+1, sizeof(Condition)); |
---|
| 188 | ForEach(i, 1, d) |
---|
| 189 | { |
---|
| 190 | Term[i] = (Condition) malloc(sizeof(struct CondRec)); |
---|
| 191 | Term[i]->CondTest = Stack[i]->CondTest; |
---|
| 192 | Term[i]->TestValue = Stack[i]->TestValue; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | PruneRule(Term, d, t->Leaf); |
---|
| 196 | |
---|
| 197 | cfree(Term); |
---|
| 198 | } |
---|
| 199 | } |
---|