[22] | 1 | /*************************************************************************/ |
---|
| 2 | /* */ |
---|
| 3 | /* Classify items interactively using a set of rules */ |
---|
| 4 | /* ------------------------------------------------- */ |
---|
| 5 | /* */ |
---|
| 6 | /*************************************************************************/ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #include "defns.i" |
---|
| 10 | #include "types.i" |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | /* External data */ |
---|
| 14 | |
---|
| 15 | short MaxAtt, MaxClass, MaxDiscrVal; |
---|
| 16 | |
---|
| 17 | ItemNo MaxItem; |
---|
| 18 | |
---|
| 19 | Description *Item; |
---|
| 20 | |
---|
| 21 | DiscrValue *MaxAttVal; |
---|
| 22 | |
---|
| 23 | String *ClassName, |
---|
| 24 | *AttName, |
---|
| 25 | **AttValName, |
---|
| 26 | FileName = "DF"; |
---|
| 27 | |
---|
| 28 | char *SpecialStatus; |
---|
| 29 | |
---|
| 30 | short VERBOSITY = 0, |
---|
| 31 | TRACE = 0; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | Boolean FirstTime = true; |
---|
| 35 | |
---|
| 36 | PR *Rule; |
---|
| 37 | |
---|
| 38 | RuleNo NRules = 0, |
---|
| 39 | *RuleIndex; |
---|
| 40 | |
---|
| 41 | short RuleSpace = 0; |
---|
| 42 | |
---|
| 43 | ClassNo DefaultClass; |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | /* The interview module uses a more complex description of an |
---|
| 47 | case called a "Range Description" with lower and upper bounds. |
---|
| 48 | For further information, see consult.c */ |
---|
| 49 | |
---|
| 50 | typedef struct ValRange *RangeDescRec; |
---|
| 51 | |
---|
| 52 | struct ValRange |
---|
| 53 | { |
---|
| 54 | Boolean Known, Asked; |
---|
| 55 | float LowerBound, UpperBound, *Probability; |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | RangeDescRec RangeDesc; |
---|
| 59 | |
---|
| 60 | float Confidence; |
---|
| 61 | |
---|
| 62 | #define MINCF 0.50 /* minimum cf for useable rule */ |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | /*************************************************************************/ |
---|
| 66 | /* */ |
---|
| 67 | /* Find the best rule for the current case. */ |
---|
| 68 | /* Note: leave probability in Confidence */ |
---|
| 69 | /* */ |
---|
| 70 | /*************************************************************************/ |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | RuleNo BestRule() |
---|
| 74 | /* -------- */ |
---|
| 75 | { |
---|
| 76 | RuleNo r; |
---|
| 77 | float cf, RuleStrength(); |
---|
| 78 | |
---|
| 79 | Confidence = 0.0; |
---|
| 80 | |
---|
| 81 | ForEach(r, 1, NRules) |
---|
| 82 | { |
---|
| 83 | cf = RuleStrength(Rule[r]); |
---|
| 84 | |
---|
| 85 | if ( cf > 0.3 ) |
---|
| 86 | { |
---|
| 87 | Confidence = cf; |
---|
| 88 | return r; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | return 0; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | /*************************************************************************/ |
---|
| 98 | /* */ |
---|
| 99 | /* Given a rule, determine the strength with which we can conclude */ |
---|
| 100 | /* that the current case belongs to the class specified by the RHS */ |
---|
| 101 | /* of the rule */ |
---|
| 102 | /* */ |
---|
| 103 | /*************************************************************************/ |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | float RuleStrength(Rule) |
---|
| 107 | /* ------------ */ |
---|
| 108 | PR Rule; |
---|
| 109 | { |
---|
| 110 | short d; |
---|
| 111 | float RuleProb=1.0, ProbSatisfied(); |
---|
| 112 | |
---|
| 113 | ForEach(d, 1, Rule.Size) |
---|
| 114 | { |
---|
| 115 | RuleProb *= ProbSatisfied(Rule.Lhs[d]); |
---|
| 116 | if ( RuleProb < MINCF ) |
---|
| 117 | { |
---|
| 118 | return 0.0; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return ( (1 - Rule.Error) * RuleProb ); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | /*************************************************************************/ |
---|
| 128 | /* */ |
---|
| 129 | /* Determine the probability of the current case description */ |
---|
| 130 | /* satisfying the given condition */ |
---|
| 131 | /* */ |
---|
| 132 | /*************************************************************************/ |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | float ProbSatisfied(c) |
---|
| 136 | /* ------------- */ |
---|
| 137 | Condition c; |
---|
| 138 | { |
---|
| 139 | Attribute a; |
---|
| 140 | char v; |
---|
| 141 | float AddProb=0.0; |
---|
| 142 | Test t; |
---|
| 143 | DiscrValue i; |
---|
| 144 | |
---|
| 145 | t = c->CondTest; |
---|
| 146 | a = t->Tested; |
---|
| 147 | v = c->TestValue; |
---|
| 148 | |
---|
| 149 | CheckValue(a, Nil); |
---|
| 150 | |
---|
| 151 | if ( ! RangeDesc[a].Known ) |
---|
| 152 | { |
---|
| 153 | return 0.0; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | switch ( t->NodeType ) |
---|
| 157 | { |
---|
| 158 | case BrDiscr: /* test of discrete attribute */ |
---|
| 159 | |
---|
| 160 | return RangeDesc[a].Probability[v]; |
---|
| 161 | |
---|
| 162 | case ThreshContin: /* test of continuous attribute */ |
---|
| 163 | |
---|
| 164 | if ( RangeDesc[a].UpperBound <= t->Cut ) |
---|
| 165 | { |
---|
| 166 | return ( v == 1 ? 1.0 : 0.0 ); |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | if ( RangeDesc[a].LowerBound > t->Cut ) |
---|
| 170 | { |
---|
| 171 | return ( v == 2 ? 1.0 : 0.0 ); |
---|
| 172 | } |
---|
| 173 | else |
---|
| 174 | if ( v == 1 ) |
---|
| 175 | { |
---|
| 176 | return (t->Cut - RangeDesc[a].LowerBound) / |
---|
| 177 | (RangeDesc[a].UpperBound - RangeDesc[a].LowerBound); |
---|
| 178 | } |
---|
| 179 | else |
---|
| 180 | { |
---|
| 181 | return (RangeDesc[a].UpperBound - t->Cut) / |
---|
| 182 | (RangeDesc[a].UpperBound - RangeDesc[a].LowerBound); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | case BrSubset: /* subset test on discrete attribute */ |
---|
| 186 | |
---|
| 187 | ForEach(i, 1, MaxAttVal[a]) |
---|
| 188 | { |
---|
| 189 | if ( In(i, t->Subset[v]) ) |
---|
| 190 | { |
---|
| 191 | AddProb += RangeDesc[a].Probability[i]; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | return AddProb; |
---|
| 195 | |
---|
| 196 | } |
---|
| 197 | return 0.0; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | /*************************************************************************/ |
---|
| 203 | /* */ |
---|
| 204 | /* Process a single case */ |
---|
| 205 | /* */ |
---|
| 206 | /*************************************************************************/ |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | InterpretRuleset() |
---|
| 210 | /* ---------------- */ |
---|
| 211 | { |
---|
| 212 | char Reply; |
---|
| 213 | Attribute a; |
---|
| 214 | RuleNo r; |
---|
| 215 | |
---|
| 216 | /* Initialise */ |
---|
| 217 | |
---|
| 218 | ForEach(a, 0, MaxAtt) |
---|
| 219 | { |
---|
| 220 | RangeDesc[a].Asked = false; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | if ( FirstTime ) |
---|
| 224 | { |
---|
| 225 | FirstTime = false; |
---|
| 226 | printf("\n"); |
---|
| 227 | } |
---|
| 228 | else |
---|
| 229 | { |
---|
| 230 | printf("\n-------------------------------------------\n\n"); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /* Find the first rule that fires on the item */ |
---|
| 234 | |
---|
| 235 | if ( r = BestRule() ) |
---|
| 236 | { |
---|
| 237 | printf("\nDecision:\n"); |
---|
| 238 | printf("\t%s", ClassName[Rule[r].Rhs]); |
---|
| 239 | |
---|
| 240 | if ( Confidence < 1.0 ) |
---|
| 241 | { |
---|
| 242 | printf(" CF = %.2f", Confidence); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | printf("\n"); |
---|
| 246 | } |
---|
| 247 | else |
---|
| 248 | { |
---|
| 249 | printf("\nDecision:\n"); |
---|
| 250 | printf("\t%s (default class)\n", ClassName[DefaultClass]); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /* Prompt for what to do next */ |
---|
| 254 | |
---|
| 255 | while ( true ) |
---|
| 256 | { |
---|
| 257 | printf("\nRetry, new case or quit [r,n,q]: "); |
---|
| 258 | Reply = getchar(); |
---|
| 259 | SkipLine(Reply); |
---|
| 260 | switch ( Reply ) |
---|
| 261 | { |
---|
| 262 | case 'r': return; |
---|
| 263 | case 'n': Clear(); return; |
---|
| 264 | case 'q': exit(0); |
---|
| 265 | default: printf("Please enter 'r', 'n' or 'q'"); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | /*************************************************************************/ |
---|
| 273 | /* */ |
---|
| 274 | /* Main routine for classifying items using a set of rules */ |
---|
| 275 | /* */ |
---|
| 276 | /*************************************************************************/ |
---|
| 277 | |
---|
| 278 | main(Argc, Argv) |
---|
| 279 | /* ---- */ |
---|
| 280 | int Argc; |
---|
| 281 | char *Argv[]; |
---|
| 282 | { |
---|
| 283 | int o; |
---|
| 284 | extern char *optarg; |
---|
| 285 | extern int optind; |
---|
| 286 | Attribute a; |
---|
| 287 | RuleNo r; |
---|
| 288 | |
---|
| 289 | PrintHeader("production rule interpreter"); |
---|
| 290 | |
---|
| 291 | /* Process options */ |
---|
| 292 | |
---|
| 293 | while ( (o = getopt(Argc, Argv, "tvf:")) != EOF ) |
---|
| 294 | { |
---|
| 295 | switch (o) |
---|
| 296 | { |
---|
| 297 | case 't': TRACE = 1; |
---|
| 298 | break; |
---|
| 299 | case 'v': VERBOSITY = 1; |
---|
| 300 | break; |
---|
| 301 | case 'f': FileName = optarg; |
---|
| 302 | break; |
---|
| 303 | case '?': printf("unrecognised option\n"); |
---|
| 304 | exit(1); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | /* Initialise */ |
---|
| 309 | |
---|
| 310 | GetNames(); |
---|
| 311 | GetRules(); |
---|
| 312 | |
---|
| 313 | if ( TRACE ) |
---|
| 314 | { |
---|
| 315 | ForEach(r, 1, NRules) |
---|
| 316 | { |
---|
| 317 | PrintRule(r); |
---|
| 318 | } |
---|
| 319 | printf("\nDefault class: %s\n", ClassName[DefaultClass]); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | /* Allocate value ranges */ |
---|
| 323 | |
---|
| 324 | RangeDesc = (struct ValRange *) calloc(MaxAtt+1, sizeof(struct ValRange)); |
---|
| 325 | |
---|
| 326 | ForEach(a, 0, MaxAtt) |
---|
| 327 | { |
---|
| 328 | if ( MaxAttVal[a] ) |
---|
| 329 | { |
---|
| 330 | RangeDesc[a].Probability = |
---|
| 331 | (float *) calloc(MaxAttVal[a]+1, sizeof(float)); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | /* Consult */ |
---|
| 336 | |
---|
| 337 | Clear(); |
---|
| 338 | while ( true ) |
---|
| 339 | { |
---|
| 340 | InterpretRuleset(); |
---|
| 341 | } |
---|
| 342 | } |
---|