[26] | 1 | /*************************************************************************/ |
---|
| 2 | /* */ |
---|
| 3 | /* Main routine, c4.5 */ |
---|
| 4 | /* ------------------ */ |
---|
| 5 | /* */ |
---|
| 6 | /*************************************************************************/ |
---|
| 7 | |
---|
| 8 | #include "defns.i" |
---|
| 9 | #include "types.i" |
---|
| 10 | |
---|
[67] | 11 | #include <omp.h> |
---|
[26] | 12 | |
---|
| 13 | |
---|
[67] | 14 | /* External data, described in extern.i */ |
---|
[26] | 15 | |
---|
[67] | 16 | short MaxAtt, MaxClass, MaxDiscrVal = 2; |
---|
[26] | 17 | |
---|
[67] | 18 | ItemNo MaxItem; |
---|
[26] | 19 | |
---|
[67] | 20 | Description *Item; |
---|
[26] | 21 | |
---|
[67] | 22 | DiscrValue *MaxAttVal; |
---|
[26] | 23 | |
---|
[67] | 24 | char *SpecialStatus; |
---|
[26] | 25 | |
---|
[67] | 26 | String *ClassName, *AttName, **AttValName, FileName = "DF"; |
---|
[26] | 27 | |
---|
[67] | 28 | short VERBOSITY = 0, TRIALS = 10; |
---|
| 29 | |
---|
| 30 | Boolean GAINRATIO = true, SUBSET = false, BATCH = true, UNSEENS = false, |
---|
[26] | 31 | PROBTHRESH = false; |
---|
| 32 | |
---|
[67] | 33 | ItemNo MINOBJS = 2, WINDOW = 0, INCREMENT = 0; |
---|
[26] | 34 | |
---|
[67] | 35 | float CF = 0.25; |
---|
[26] | 36 | |
---|
[67] | 37 | Tree *Pruned; |
---|
[26] | 38 | |
---|
[67] | 39 | Boolean AllKnown = true; |
---|
[26] | 40 | |
---|
[67] | 41 | main(Argc, Argv) |
---|
| 42 | /* ---- */ |
---|
| 43 | int Argc;char *Argv[]; { |
---|
| 44 | int o; |
---|
| 45 | extern char *optarg; |
---|
| 46 | extern int optind; |
---|
| 47 | Boolean FirstTime = true; |
---|
| 48 | short Best, BestTree(); |
---|
[26] | 49 | |
---|
[67] | 50 | PrintHeader("decision tree generator"); |
---|
[26] | 51 | |
---|
[106] | 52 | /* |
---|
[105] | 53 | (void) omp_set_dynamic(0); |
---|
| 54 | if (omp_get_dynamic()) {printf("Warning: dynamic adjustment of threads has been set\n");} |
---|
| 55 | (void) omp_set_num_threads(2); |
---|
[106] | 56 | */ |
---|
[26] | 57 | |
---|
[105] | 58 | printf("nr threads = %d\n", omp_get_max_threads()); |
---|
[67] | 59 | /* Process options */ |
---|
| 60 | |
---|
| 61 | while ((o = getopt(Argc, Argv, "f:bupv:t:w:i:gsm:c:")) != EOF) { |
---|
| 62 | if (FirstTime) { |
---|
| 63 | printf("\n Options:\n"); |
---|
| 64 | FirstTime = false; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | switch (o) { |
---|
| 68 | case 'f': |
---|
| 69 | FileName = optarg; |
---|
| 70 | printf("\tFile stem <%s>\n", FileName); |
---|
| 71 | break; |
---|
| 72 | case 'b': |
---|
| 73 | BATCH = true; |
---|
| 74 | printf("\tWindowing disabled (now the default)\n"); |
---|
| 75 | break; |
---|
| 76 | case 'u': |
---|
| 77 | UNSEENS = true; |
---|
| 78 | printf("\tTrees evaluated on unseen cases\n"); |
---|
| 79 | break; |
---|
| 80 | case 'p': |
---|
| 81 | PROBTHRESH = true; |
---|
| 82 | printf("\tProbability thresholds used\n"); |
---|
| 83 | break; |
---|
| 84 | case 'v': |
---|
| 85 | VERBOSITY = atoi(optarg); |
---|
| 86 | printf("\tVerbosity level %d\n", VERBOSITY); |
---|
| 87 | break; |
---|
| 88 | case 't': |
---|
| 89 | TRIALS = atoi(optarg); |
---|
| 90 | printf("\tWindowing enabled with %d trials\n", TRIALS); |
---|
| 91 | Check(TRIALS, 1, 10000) |
---|
| 92 | ; |
---|
| 93 | BATCH = false; |
---|
| 94 | break; |
---|
| 95 | case 'w': |
---|
| 96 | WINDOW = atoi(optarg); |
---|
| 97 | printf("\tInitial window size of %d items\n", WINDOW); |
---|
| 98 | Check(WINDOW, 1, 1000000) |
---|
| 99 | ; |
---|
| 100 | BATCH = false; |
---|
| 101 | break; |
---|
| 102 | case 'i': |
---|
| 103 | INCREMENT = atoi(optarg); |
---|
| 104 | printf("\tMaximum window increment of %d items\n", INCREMENT); |
---|
| 105 | Check(INCREMENT, 1, 1000000) |
---|
| 106 | ; |
---|
| 107 | BATCH = false; |
---|
| 108 | break; |
---|
| 109 | case 'g': |
---|
| 110 | GAINRATIO = false; |
---|
| 111 | printf("\tGain criterion used\n"); |
---|
| 112 | break; |
---|
| 113 | case 's': |
---|
| 114 | SUBSET = true; |
---|
| 115 | printf("\tTests on discrete attribute groups\n"); |
---|
| 116 | break; |
---|
| 117 | case 'm': |
---|
| 118 | MINOBJS = atoi(optarg); |
---|
| 119 | printf("\tSensible test requires 2 branches with >=%d cases\n", |
---|
| 120 | MINOBJS); |
---|
| 121 | Check(MINOBJS, 1, 1000000) |
---|
| 122 | ; |
---|
| 123 | break; |
---|
| 124 | case 'c': |
---|
| 125 | CF = atof(optarg); |
---|
| 126 | printf("\tPruning confidence level %g%%\n", CF); |
---|
| 127 | Check(CF, Epsilon, 100) |
---|
| 128 | ; |
---|
| 129 | CF /= 100; |
---|
| 130 | break; |
---|
| 131 | case '?': |
---|
| 132 | printf("unrecognised option\n"); |
---|
| 133 | exit(1); |
---|
| 134 | } |
---|
[26] | 135 | } |
---|
| 136 | |
---|
[67] | 137 | /* Initialise */ |
---|
[26] | 138 | |
---|
[67] | 139 | GetNames(); |
---|
[83] | 140 | //printf(">>>the force is with us\n"); |
---|
[67] | 141 | GetData(".data"); |
---|
[83] | 142 | //printf(">>>the force is with us222\n"); |
---|
[67] | 143 | printf("\nRead %d cases (%d attributes) from %s.data\n", MaxItem + 1, |
---|
| 144 | MaxAtt + 1, FileName); |
---|
[26] | 145 | |
---|
[67] | 146 | /* Build decision trees */ |
---|
[26] | 147 | |
---|
[67] | 148 | if (BATCH) { |
---|
| 149 | TRIALS = 1; |
---|
[83] | 150 | OneTree_Discr(); |
---|
| 151 | //OneTree(); |
---|
[67] | 152 | Best = 0; |
---|
| 153 | } else { |
---|
| 154 | Best = BestTree(); |
---|
| 155 | } |
---|
[26] | 156 | |
---|
[67] | 157 | /* Soften thresholds in best tree */ |
---|
[26] | 158 | |
---|
[67] | 159 | if (PROBTHRESH) { |
---|
| 160 | printf("Softening thresholds"); |
---|
| 161 | if (!BATCH) |
---|
| 162 | printf(" for best tree from trial %d", Best); |
---|
| 163 | printf("\n"); |
---|
| 164 | SoftenThresh(Pruned[Best]); |
---|
| 165 | printf("\n"); |
---|
| 166 | PrintTree(Pruned[Best]); |
---|
| 167 | } |
---|
[26] | 168 | |
---|
[67] | 169 | /* Save best tree */ |
---|
[26] | 170 | |
---|
[67] | 171 | if (BATCH || TRIALS == 1) { |
---|
| 172 | printf("\nTree saved\n"); |
---|
| 173 | } else { |
---|
| 174 | printf("\nBest tree from trial %d saved\n", Best); |
---|
| 175 | } |
---|
| 176 | SaveTree(Pruned[Best], ".tree"); |
---|
[26] | 177 | |
---|
[67] | 178 | /* Evaluation */ |
---|
[26] | 179 | |
---|
[67] | 180 | printf("\n\nEvaluation on training data (%d items):\n", MaxItem + 1); |
---|
| 181 | Evaluate(false, Best); |
---|
[26] | 182 | |
---|
[67] | 183 | if (UNSEENS) { |
---|
| 184 | GetData(".test"); |
---|
| 185 | printf("\nEvaluation on test data (%d items):\n", MaxItem + 1); |
---|
| 186 | Evaluate(true, Best); |
---|
| 187 | } |
---|
[26] | 188 | |
---|
[67] | 189 | exit(0); |
---|
[26] | 190 | } |
---|