1 | /*************************************************************************/ |
---|
2 | /* */ |
---|
3 | /* Main routine, c4.5 */ |
---|
4 | /* ------------------ */ |
---|
5 | /* */ |
---|
6 | /*************************************************************************/ |
---|
7 | |
---|
8 | #include "defns.i" |
---|
9 | #include "types.i" |
---|
10 | |
---|
11 | #include <omp.h> |
---|
12 | |
---|
13 | |
---|
14 | /* External data, described in extern.i */ |
---|
15 | |
---|
16 | short MaxAtt, MaxClass, MaxDiscrVal = 2; |
---|
17 | |
---|
18 | ItemNo MaxItem; |
---|
19 | |
---|
20 | Description *Item; |
---|
21 | |
---|
22 | DiscrValue *MaxAttVal; |
---|
23 | |
---|
24 | char *SpecialStatus; |
---|
25 | |
---|
26 | String *ClassName, *AttName, **AttValName, FileName = "DF"; |
---|
27 | |
---|
28 | short VERBOSITY = 0, TRIALS = 10; |
---|
29 | |
---|
30 | Boolean GAINRATIO = true, SUBSET = false, BATCH = true, UNSEENS = false, |
---|
31 | PROBTHRESH = false; |
---|
32 | |
---|
33 | ItemNo MINOBJS = 2, WINDOW = 0, INCREMENT = 0; |
---|
34 | |
---|
35 | float CF = 0.25; |
---|
36 | |
---|
37 | Tree *Pruned; |
---|
38 | |
---|
39 | Boolean AllKnown = true; |
---|
40 | |
---|
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(); |
---|
49 | |
---|
50 | PrintHeader("decision tree generator"); |
---|
51 | |
---|
52 | /* |
---|
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); |
---|
56 | */ |
---|
57 | |
---|
58 | printf("nr threads = %d\n", omp_get_max_threads()); |
---|
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 | } |
---|
135 | } |
---|
136 | |
---|
137 | /* Initialise */ |
---|
138 | |
---|
139 | GetNames(); |
---|
140 | //printf(">>>the force is with us\n"); |
---|
141 | GetData(".data"); |
---|
142 | //printf(">>>the force is with us222\n"); |
---|
143 | printf("\nRead %d cases (%d attributes) from %s.data\n", MaxItem + 1, |
---|
144 | MaxAtt + 1, FileName); |
---|
145 | |
---|
146 | /* Build decision trees */ |
---|
147 | |
---|
148 | if (BATCH) { |
---|
149 | TRIALS = 1; |
---|
150 | OneTree_Discr(); |
---|
151 | //OneTree(); |
---|
152 | Best = 0; |
---|
153 | } else { |
---|
154 | Best = BestTree(); |
---|
155 | } |
---|
156 | |
---|
157 | /* Soften thresholds in best tree */ |
---|
158 | |
---|
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 | } |
---|
168 | |
---|
169 | /* Save best tree */ |
---|
170 | |
---|
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"); |
---|
177 | |
---|
178 | /* Evaluation */ |
---|
179 | |
---|
180 | printf("\n\nEvaluation on training data (%d items):\n", MaxItem + 1); |
---|
181 | Evaluate(false, Best); |
---|
182 | |
---|
183 | if (UNSEENS) { |
---|
184 | GetData(".test"); |
---|
185 | printf("\nEvaluation on test data (%d items):\n", MaxItem + 1); |
---|
186 | Evaluate(true, Best); |
---|
187 | } |
---|
188 | |
---|
189 | exit(0); |
---|
190 | } |
---|