1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * OLM.java |
---|
19 | * Copyright (C) 2009 TriDat Tran |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.classifiers.rules; |
---|
24 | |
---|
25 | import weka.classifiers.Classifier; |
---|
26 | import weka.classifiers.AbstractClassifier; |
---|
27 | import weka.classifiers.Evaluation; |
---|
28 | import java.io.*; |
---|
29 | import java.util.*; |
---|
30 | import weka.core.*; |
---|
31 | import weka.core.Capabilities.Capability; |
---|
32 | import weka.core.TechnicalInformation.Field; |
---|
33 | import weka.core.TechnicalInformation.Type; |
---|
34 | |
---|
35 | /** |
---|
36 | * |
---|
37 | <!-- globalinfo-start --> |
---|
38 | * This class is an implementation of the Ordinal Learning Method (OLM).<br/> |
---|
39 | * Further information regarding the algorithm and variants can be found in:<br/> |
---|
40 | * <br/> |
---|
41 | * Arie Ben-David (1992). Automatic Generation of Symbolic Multiattribute Ordinal Knowledge-Based DSSs: methodology and Applications. Decision Sciences. 23:1357-1372. |
---|
42 | * <p/> |
---|
43 | <!-- globalinfo-end --> |
---|
44 | * |
---|
45 | <!-- technical-bibtex-start --> |
---|
46 | * BibTeX: |
---|
47 | * <pre> |
---|
48 | * @article{Ben-David1992, |
---|
49 | * author = {Arie Ben-David}, |
---|
50 | * journal = {Decision Sciences}, |
---|
51 | * pages = {1357-1372}, |
---|
52 | * title = {Automatic Generation of Symbolic Multiattribute Ordinal Knowledge-Based DSSs: methodology and Applications}, |
---|
53 | * volume = {23}, |
---|
54 | * year = {1992} |
---|
55 | * } |
---|
56 | * </pre> |
---|
57 | * <p/> |
---|
58 | <!-- technical-bibtex-end --> |
---|
59 | * |
---|
60 | <!-- options-start --> |
---|
61 | * Valid options are: <p/> |
---|
62 | * |
---|
63 | * <pre> -R <integer> |
---|
64 | * The resolution mode. Valid values are: |
---|
65 | * 0 for conservative resolution, 1 for random resolution, 2 for average, and 3 for no resolution. (default 0).</pre> |
---|
66 | * |
---|
67 | * <pre> -C <integer> |
---|
68 | * The classification mode. Valid values are: |
---|
69 | * 0 for conservative classification, 1 for nearest neighbour classification. (default 0).</pre> |
---|
70 | * |
---|
71 | * <pre> -U <size> |
---|
72 | * SSet maximum size of rule base |
---|
73 | * (default: -U <number of examples>)</pre> |
---|
74 | * |
---|
75 | <!-- options-end --> |
---|
76 | * |
---|
77 | * @author TriDat Tran |
---|
78 | * @version $Revision: 5928 $ |
---|
79 | */ |
---|
80 | public class OLM extends AbstractClassifier |
---|
81 | implements OptionHandler, TechnicalInformationHandler { |
---|
82 | |
---|
83 | /** |
---|
84 | * For serialization |
---|
85 | */ |
---|
86 | private static final long serialVersionUID = -381974207649598344L; |
---|
87 | |
---|
88 | //protected Instance ist; |
---|
89 | protected int printR; |
---|
90 | protected int numExamples; |
---|
91 | |
---|
92 | |
---|
93 | /* The conflict resolution modes */ |
---|
94 | public static final int RESOLUTION_NONE = 3; |
---|
95 | public static final int RESOLUTION_AVERAGE = 2; |
---|
96 | public static final int RESOLUTION_RANDOM = 1; |
---|
97 | public static final int RESOLUTION_CONSERVATIVE = 0; |
---|
98 | public static final Tag [] TAGS_RESOLUTION = { |
---|
99 | new Tag(RESOLUTION_NONE, "No conflict resolution"), |
---|
100 | new Tag(RESOLUTION_AVERAGE, "Resolution using average"), |
---|
101 | new Tag(RESOLUTION_RANDOM, "Random resolution"), |
---|
102 | new Tag(RESOLUTION_CONSERVATIVE, "Conservative resolution") |
---|
103 | }; |
---|
104 | |
---|
105 | /** The conflict resolution mode */ |
---|
106 | protected int m_resolutionMode = RESOLUTION_CONSERVATIVE; |
---|
107 | |
---|
108 | /* The classification modes */ |
---|
109 | public static final int CLASSIFICATION_CONSERVATIVE = 1; |
---|
110 | public static final int CLASSIFICATION_NEARESTNEIGHBOUR = 0; |
---|
111 | public static final Tag[] TAGS_CLASSIFICATION = { |
---|
112 | new Tag(CLASSIFICATION_NEARESTNEIGHBOUR, "Nearest neighbour classification"), |
---|
113 | new Tag(CLASSIFICATION_CONSERVATIVE, "Conservative classification") |
---|
114 | }; |
---|
115 | |
---|
116 | /** The classification mode */ |
---|
117 | protected int m_classificationMode = CLASSIFICATION_CONSERVATIVE; |
---|
118 | |
---|
119 | protected int upperBaseLimit = -1; |
---|
120 | protected int randSeed = 0; |
---|
121 | protected Random rand = new Random(0); |
---|
122 | |
---|
123 | protected boolean print_msg = false; |
---|
124 | |
---|
125 | /** |
---|
126 | * Returns default capabilities of the classifier. |
---|
127 | * |
---|
128 | * @return the capabilities of this classifier |
---|
129 | */ |
---|
130 | public Capabilities getCapabilities() { |
---|
131 | Capabilities result = super.getCapabilities(); |
---|
132 | result.disableAll(); |
---|
133 | |
---|
134 | // attributes |
---|
135 | result.enable(Capability.NOMINAL_ATTRIBUTES); |
---|
136 | result.enable(Capability.NUMERIC_ATTRIBUTES); |
---|
137 | result.enable(Capability.MISSING_VALUES); |
---|
138 | |
---|
139 | // class |
---|
140 | result.enable(Capability.NOMINAL_CLASS); |
---|
141 | result.enable(Capability.MISSING_CLASS_VALUES); |
---|
142 | |
---|
143 | // instances |
---|
144 | result.setMinimumNumberInstances(1); |
---|
145 | |
---|
146 | return result; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Returns a string describing the classifier. |
---|
151 | * @return a description suitable for displaying in the |
---|
152 | * explorer/experimenter gui |
---|
153 | */ |
---|
154 | public String globalInfo() { |
---|
155 | return "This class is an implementation of the Ordinal Learning " |
---|
156 | + "Method (OLM).\n" |
---|
157 | + "Further information regarding the algorithm and variants " |
---|
158 | + "can be found in:\n\n" |
---|
159 | + getTechnicalInformation().toString(); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Returns an instance of a TechnicalInformation object, containing |
---|
164 | * detailed information about the technical background of this class, |
---|
165 | * e.g., paper reference or book this class is based on. |
---|
166 | * |
---|
167 | * @return the technical information about this class |
---|
168 | */ |
---|
169 | public TechnicalInformation getTechnicalInformation() { |
---|
170 | TechnicalInformation result; |
---|
171 | TechnicalInformation additional; |
---|
172 | |
---|
173 | result = new TechnicalInformation(Type.ARTICLE); |
---|
174 | result.setValue(Field.AUTHOR, "Arie Ben-David"); |
---|
175 | result.setValue(Field.YEAR, "1992"); |
---|
176 | result.setValue(Field.TITLE, "Automatic Generation of Symbolic Multiattribute Ordinal Knowledge-Based DSSs: methodology and Applications"); |
---|
177 | result.setValue(Field.JOURNAL, "Decision Sciences"); |
---|
178 | result.setValue(Field.PAGES, "1357-1372"); |
---|
179 | result.setValue(Field.VOLUME, "23"); |
---|
180 | |
---|
181 | return result; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | /** |
---|
186 | * Classifies a given instance. |
---|
187 | * |
---|
188 | * @param inst the instance to be classified |
---|
189 | * @return the classification |
---|
190 | */ |
---|
191 | public double classifyInstance(Instance inst) { |
---|
192 | return olmrules.classify(inst); |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * Returns an enumeration describing the available options |
---|
197 | * Valid options are: |
---|
198 | * @return an enumeration of all the available options |
---|
199 | */ |
---|
200 | public Enumeration listOptions() { |
---|
201 | Vector newVector = new Vector(3); |
---|
202 | |
---|
203 | newVector.addElement(new Option( |
---|
204 | "\tThe resolution mode. Valid values are:\n" + |
---|
205 | "\t0 for conservative resolution, 1 for random resolution," + |
---|
206 | "\t2 for average, and 3 for no resolution. (default 0).", |
---|
207 | "R", 1, "-R <integer>")); |
---|
208 | |
---|
209 | newVector.addElement(new Option( |
---|
210 | "\tThe classification mode. Valid values are:\n" + |
---|
211 | "\t0 for conservative classification, 1 for nearest neighbour classification." + |
---|
212 | " (default 0).", |
---|
213 | "C", 1, "-C <integer>")); |
---|
214 | |
---|
215 | newVector.addElement(new Option("\tSSet maximum size of rule base\n" + |
---|
216 | "\t(default: -U <number of examples>)","U", 1, "-U <size>")); |
---|
217 | |
---|
218 | return newVector.elements(); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Parses a given list of options. |
---|
223 | * |
---|
224 | <!-- options-start --> |
---|
225 | * Valid options are: <p/> |
---|
226 | * |
---|
227 | * <pre> -R <integer> |
---|
228 | * The resolution mode. Valid values are: |
---|
229 | * 0 for conservative resolution, 1 for random resolution, 2 for average, and 3 for no resolution. (default 0).</pre> |
---|
230 | * |
---|
231 | * <pre> -C <integer> |
---|
232 | * The classification mode. Valid values are: |
---|
233 | * 0 for conservative classification, 1 for nearest neighbour classification. (default 0).</pre> |
---|
234 | * |
---|
235 | * <pre> -U <size> |
---|
236 | * SSet maximum size of rule base |
---|
237 | * (default: -U <number of examples>)</pre> |
---|
238 | * |
---|
239 | <!-- options-end --> |
---|
240 | * |
---|
241 | * @param options the list of options as an array of strings |
---|
242 | * @exception Exception if an option is not supported |
---|
243 | */ |
---|
244 | public void setOptions(String[] options) throws Exception { |
---|
245 | String resolutionMode = Utils.getOption('R', options); |
---|
246 | if (resolutionMode.length() > 0) { |
---|
247 | setResolutionMode(new SelectedTag(Integer.parseInt(resolutionMode), |
---|
248 | TAGS_RESOLUTION)); |
---|
249 | } |
---|
250 | |
---|
251 | String classificationMode = Utils.getOption('C', options); |
---|
252 | if (classificationMode.length() > 0) { |
---|
253 | setClassificationMode(new SelectedTag(Integer.parseInt(classificationMode), |
---|
254 | TAGS_CLASSIFICATION)); |
---|
255 | } |
---|
256 | |
---|
257 | String upperBase = Utils.getOption('U', options); |
---|
258 | if (upperBase.length() != 0) |
---|
259 | upperBaseLimit = Integer.parseInt(upperBase); |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Gets the current settings of the Classifier. |
---|
264 | * |
---|
265 | * @return an array of strings suitable for passing to setOptions |
---|
266 | */ |
---|
267 | public String [] getOptions() { |
---|
268 | String [] options = new String [6]; |
---|
269 | int current = 0; |
---|
270 | |
---|
271 | if(upperBaseLimit == -1) upperBaseLimit = numExamples; |
---|
272 | |
---|
273 | options[current++] = "-R"; options[current++] = "" + m_resolutionMode; |
---|
274 | options[current++] = "-C"; options[current++] = "" + m_classificationMode; |
---|
275 | options[current++] = "-U"; options[current++] = "" + upperBaseLimit; |
---|
276 | |
---|
277 | return options; |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Returns the tip text for this property |
---|
282 | * @return tip text for this property suitable for |
---|
283 | * displaying in the explorer/experimenter gui |
---|
284 | */ |
---|
285 | public String resolutionModeTipText() { |
---|
286 | return "The resolution mode to use."; |
---|
287 | } |
---|
288 | |
---|
289 | /** |
---|
290 | * Sets the resolution mode. |
---|
291 | * |
---|
292 | * @param newMethod the new evaluation mode. |
---|
293 | */ |
---|
294 | public void setResolutionMode(SelectedTag newMethod) { |
---|
295 | |
---|
296 | if (newMethod.getTags() == TAGS_RESOLUTION) { |
---|
297 | m_resolutionMode = newMethod.getSelectedTag().getID(); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * Gets the resolution mode. |
---|
303 | * |
---|
304 | * @return the evaluation mode. |
---|
305 | */ |
---|
306 | public SelectedTag getResolutionMode() { |
---|
307 | |
---|
308 | return new SelectedTag(m_resolutionMode, TAGS_RESOLUTION); |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * Sets the classification mode. |
---|
313 | * |
---|
314 | * @param newMethod the new classification mode. |
---|
315 | */ |
---|
316 | public void setClassificationMode(SelectedTag newMethod) { |
---|
317 | m_classificationMode = newMethod.getSelectedTag().getID(); |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * Gets the classification mode. |
---|
322 | * |
---|
323 | * @return the classiciation mode |
---|
324 | */ |
---|
325 | public SelectedTag getClassificationMode() { |
---|
326 | return new SelectedTag(m_classificationMode, TAGS_CLASSIFICATION); |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * Returns the tip text for this property |
---|
331 | * @return tip text for this property suitable for |
---|
332 | * displaying in the explorer/experimenter gui |
---|
333 | */ |
---|
334 | public String classificationModeTipText() { |
---|
335 | return "The classification mode to use."; |
---|
336 | } |
---|
337 | |
---|
338 | /** |
---|
339 | * Returns the tip text for this property |
---|
340 | * @return tip text for this property suitable for |
---|
341 | * displaying in the explorer/experimenter gui |
---|
342 | */ |
---|
343 | public String ruleSizeTipText() { |
---|
344 | return "Set the rule base size\n" + |
---|
345 | "0 - unlimited\n"; |
---|
346 | } |
---|
347 | |
---|
348 | public int getRuleSize(){ return upperBaseLimit;} |
---|
349 | public void setRuleSize(int s){ upperBaseLimit = s;} |
---|
350 | |
---|
351 | /** |
---|
352 | * Class to store CISE (Consistent and Irredundant Set of Examples) rules |
---|
353 | */ |
---|
354 | private class OLMRules implements Serializable{ |
---|
355 | private Vector rules; |
---|
356 | |
---|
357 | /** |
---|
358 | * Constructor |
---|
359 | */ |
---|
360 | public OLMRules() |
---|
361 | { |
---|
362 | rules = new Vector(); |
---|
363 | } |
---|
364 | |
---|
365 | public int distance(Instance inst1, Instance inst2) |
---|
366 | { |
---|
367 | double values1[] = inst1.toDoubleArray(); |
---|
368 | double values2[] = inst2.toDoubleArray(); |
---|
369 | int classindex = inst1.classIndex(); |
---|
370 | int numAtt = inst1.numAttributes(); |
---|
371 | int dist = 0; |
---|
372 | |
---|
373 | for(int i=0; i < numAtt; i++) |
---|
374 | { |
---|
375 | if(i != classindex) |
---|
376 | dist += Math.abs(values1[i] - values2[i]); |
---|
377 | } |
---|
378 | |
---|
379 | return dist; |
---|
380 | } |
---|
381 | |
---|
382 | public Instance averageRule(Instance inst1, Instance inst2) |
---|
383 | { |
---|
384 | Instance inst = inst1; |
---|
385 | double values1[] = inst1.toDoubleArray(); |
---|
386 | double values2[] = inst2.toDoubleArray(); |
---|
387 | int classindex = inst1.classIndex(); |
---|
388 | int numAtt = inst1.numAttributes(); |
---|
389 | |
---|
390 | for(int i=0; i < numAtt; i++) |
---|
391 | { |
---|
392 | inst.setValue(i,Math.round((values1[i] + values2[i])/2)); |
---|
393 | } |
---|
394 | |
---|
395 | return inst; |
---|
396 | } |
---|
397 | |
---|
398 | public void printRules() |
---|
399 | { |
---|
400 | Instance inst; |
---|
401 | for(int i=0; i < rules.size(); i++) |
---|
402 | { |
---|
403 | inst = (Instance)rules.elementAt(i); |
---|
404 | System.out.print(i+": "); |
---|
405 | System.out.println(inst.toString()); |
---|
406 | } |
---|
407 | } |
---|
408 | /** |
---|
409 | * Checks if the input (non-class) attributes in inst1 is greater |
---|
410 | * than in inst2. |
---|
411 | * |
---|
412 | * @param inst1 Instance1 |
---|
413 | * @param inst2 Instance2 |
---|
414 | */ |
---|
415 | private boolean isGreaterInput(Instance inst1, Instance inst2) |
---|
416 | { |
---|
417 | double values1[] = inst1.toDoubleArray(); |
---|
418 | double values2[] = inst2.toDoubleArray(); |
---|
419 | int classindex = inst1.classIndex(); |
---|
420 | int numAtt = inst1.numAttributes(); |
---|
421 | |
---|
422 | for(int i=0; i < numAtt; i++) |
---|
423 | { |
---|
424 | if(i!= classindex && values1[i] < values2[i]) |
---|
425 | return false; |
---|
426 | } |
---|
427 | return true; |
---|
428 | } |
---|
429 | |
---|
430 | private boolean isEqualInput(Instance inst1, Instance inst2) |
---|
431 | { |
---|
432 | double values1[] = inst1.toDoubleArray(); |
---|
433 | double values2[] = inst2.toDoubleArray(); |
---|
434 | int classindex = inst1.classIndex(); |
---|
435 | int numAtt = inst1.numAttributes(); |
---|
436 | |
---|
437 | for(int i=0; i < numAtt; i++) |
---|
438 | { |
---|
439 | if(i!= classindex && values1[i] != values2[i]) |
---|
440 | return false; |
---|
441 | } |
---|
442 | return true; |
---|
443 | } |
---|
444 | |
---|
445 | private boolean isGreaterOutput(Instance inst1, Instance inst2) |
---|
446 | { |
---|
447 | return (inst1.toDoubleArray())[inst1.classIndex()] > |
---|
448 | (inst2.toDoubleArray())[inst2.classIndex()]; |
---|
449 | } |
---|
450 | |
---|
451 | private boolean isEqualOutput(Instance inst1, Instance inst2) |
---|
452 | { |
---|
453 | return (inst1.toDoubleArray())[inst1.classIndex()] == |
---|
454 | (inst2.toDoubleArray())[inst2.classIndex()]; |
---|
455 | } |
---|
456 | |
---|
457 | private void fillMissing(Instance inst) |
---|
458 | { |
---|
459 | ; |
---|
460 | } |
---|
461 | |
---|
462 | public void addRule(Instance inst) |
---|
463 | { |
---|
464 | // add new rule? |
---|
465 | boolean addr = true; |
---|
466 | boolean b = false; |
---|
467 | int classindex = inst.classIndex(); |
---|
468 | // Fill in missing values. |
---|
469 | fillMissing(inst); |
---|
470 | // Compare E with each rule in CISE |
---|
471 | for(int i=0; i < rules.size(); i++) |
---|
472 | { |
---|
473 | b = false; |
---|
474 | // Checks of Redudancies. |
---|
475 | if(isEqualOutput(inst, (Instance)rules.elementAt(i))) |
---|
476 | { |
---|
477 | // Is E redundant : i.e EI(1) > EI(2) and EO(1) = EO(2) |
---|
478 | if(isGreaterInput(inst, (Instance)rules.elementAt(i))) |
---|
479 | { |
---|
480 | // E is redundant w.r.t rule i, we discard E |
---|
481 | addr = false; |
---|
482 | if(print_msg) |
---|
483 | System.out.println(inst.toString() + " is (1) redundant wrt " + |
---|
484 | ((Instance)rules.elementAt(i)).toString()); |
---|
485 | continue; |
---|
486 | } |
---|
487 | else if(isGreaterInput((Instance)rules.elementAt(i), inst)) |
---|
488 | { |
---|
489 | if(print_msg) |
---|
490 | System.out.println(((Instance)rules.elementAt(i)).toString() + |
---|
491 | " is (2) redundant wrt " + inst.toString()); |
---|
492 | // rule i is redundant w.r.t E, discard rule i |
---|
493 | rules.removeElementAt(i); |
---|
494 | i--; |
---|
495 | continue; |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | // is E inconsistent and has a higher output? |
---|
500 | if(isGreaterInput((Instance)rules.elementAt(i), inst) && |
---|
501 | !isGreaterOutput((Instance)rules.elementAt(i), inst)) |
---|
502 | { |
---|
503 | |
---|
504 | // Conservative |
---|
505 | if (m_resolutionMode == RESOLUTION_CONSERVATIVE) |
---|
506 | { |
---|
507 | // discard E |
---|
508 | addr = false; |
---|
509 | } |
---|
510 | // Random |
---|
511 | if (m_resolutionMode == RESOLUTION_RANDOM) |
---|
512 | { |
---|
513 | // select random rule to keep |
---|
514 | if(rand.nextBoolean()) |
---|
515 | { |
---|
516 | addr = addr || true; |
---|
517 | rules.removeElementAt(i); |
---|
518 | i--; |
---|
519 | } |
---|
520 | else |
---|
521 | addr = false; |
---|
522 | } |
---|
523 | // No Conflict Resolution, ignore new rule |
---|
524 | if (m_resolutionMode == RESOLUTION_NONE) |
---|
525 | { |
---|
526 | addr = false; |
---|
527 | } |
---|
528 | // Average |
---|
529 | if (m_resolutionMode == RESOLUTION_AVERAGE) |
---|
530 | { |
---|
531 | // create 'average rule' |
---|
532 | if(print_msg) |
---|
533 | System.out.print(inst.toString() + " - " + |
---|
534 | ((Instance)rules.elementAt(i)).toString()); |
---|
535 | inst = averageRule(inst, (Instance)rules.elementAt(i)); |
---|
536 | System.out.println(" : Average : " + inst.toString()); |
---|
537 | // Remove current rule |
---|
538 | rules.removeElementAt(i); |
---|
539 | // test average rule |
---|
540 | addr = true; |
---|
541 | i = 0; |
---|
542 | } |
---|
543 | continue; |
---|
544 | } |
---|
545 | // is E inconsistent and has a lower output? |
---|
546 | if(isGreaterInput(inst, (Instance)rules.elementAt(i)) && |
---|
547 | !isGreaterOutput(inst, (Instance)rules.elementAt(i))) |
---|
548 | { |
---|
549 | // Conservative |
---|
550 | if (m_resolutionMode == RESOLUTION_CONSERVATIVE) |
---|
551 | { |
---|
552 | // discard rule i |
---|
553 | if(print_msg) |
---|
554 | System.out.println("Discard rule "+ |
---|
555 | ((Instance)rules.elementAt(i)).toString()); |
---|
556 | b = true; |
---|
557 | rules.removeElementAt(i); |
---|
558 | i--; |
---|
559 | } |
---|
560 | // Random |
---|
561 | if (m_resolutionMode == RESOLUTION_RANDOM) |
---|
562 | { |
---|
563 | // select random rule to keep |
---|
564 | if(rand.nextBoolean()) |
---|
565 | { |
---|
566 | addr = addr || true; |
---|
567 | rules.removeElementAt(i); |
---|
568 | i--; |
---|
569 | } |
---|
570 | else |
---|
571 | addr = false; |
---|
572 | } |
---|
573 | // No Conflict Resolution, ignore new rule |
---|
574 | if (m_resolutionMode == RESOLUTION_NONE) |
---|
575 | { |
---|
576 | addr = false; |
---|
577 | } |
---|
578 | // Average |
---|
579 | if (m_resolutionMode == RESOLUTION_AVERAGE) |
---|
580 | { |
---|
581 | // create 'average rule' |
---|
582 | if(print_msg) |
---|
583 | System.out.print(inst.toString() + " - " + |
---|
584 | ((Instance)rules.elementAt(i)).toString()); |
---|
585 | inst = averageRule(inst, (Instance)rules.elementAt(i)); |
---|
586 | if(print_msg) |
---|
587 | System.out.println(" : Average : " + inst.toString()); |
---|
588 | // Remove current rule |
---|
589 | rules.removeElementAt(i); |
---|
590 | // test average rule |
---|
591 | addr = true; |
---|
592 | i = 0; |
---|
593 | } |
---|
594 | continue; |
---|
595 | } |
---|
596 | // check if the rule is inconsistent |
---|
597 | if(isEqualInput(inst,(Instance)rules.elementAt(i))) |
---|
598 | { |
---|
599 | if(isGreaterOutput(inst,(Instance)rules.elementAt(i))) |
---|
600 | { |
---|
601 | // Conservative |
---|
602 | if (m_resolutionMode == RESOLUTION_CONSERVATIVE) |
---|
603 | { |
---|
604 | // discard E |
---|
605 | addr = false; |
---|
606 | } |
---|
607 | // random |
---|
608 | if (m_resolutionMode == RESOLUTION_RANDOM) |
---|
609 | { |
---|
610 | // select random rule to keep |
---|
611 | if(rand.nextBoolean()) |
---|
612 | { |
---|
613 | addr = addr || true; |
---|
614 | rules.removeElementAt(i); |
---|
615 | i--; |
---|
616 | } |
---|
617 | else |
---|
618 | addr = false; |
---|
619 | } |
---|
620 | // No Conflict Resolution, ignore new rule |
---|
621 | if (m_resolutionMode == RESOLUTION_NONE) |
---|
622 | { |
---|
623 | addr = false; |
---|
624 | } |
---|
625 | // Average |
---|
626 | if (m_resolutionMode == RESOLUTION_AVERAGE) |
---|
627 | { |
---|
628 | // create 'average rule' |
---|
629 | if(print_msg) |
---|
630 | System.out.print(inst.toString() + " - " + |
---|
631 | ((Instance)rules.elementAt(i)).toString()); |
---|
632 | inst = averageRule(inst, (Instance)rules.elementAt(i)); |
---|
633 | if(print_msg) |
---|
634 | System.out.println(" : 2Average : " + inst.toString()); |
---|
635 | // Remove current rule |
---|
636 | rules.removeElementAt(i); |
---|
637 | // test average rule |
---|
638 | addr = true; |
---|
639 | i = 0; |
---|
640 | } |
---|
641 | continue; |
---|
642 | } |
---|
643 | else if(isGreaterOutput((Instance)rules.elementAt(i),inst)) |
---|
644 | { |
---|
645 | |
---|
646 | // Conservative |
---|
647 | if (m_resolutionMode == RESOLUTION_CONSERVATIVE) |
---|
648 | { |
---|
649 | //discard rule i |
---|
650 | rules.removeElementAt(i); |
---|
651 | i--; |
---|
652 | } |
---|
653 | //random |
---|
654 | if (m_resolutionMode == RESOLUTION_RANDOM) |
---|
655 | { |
---|
656 | // select random rule to keep |
---|
657 | if(rand.nextBoolean()) |
---|
658 | { |
---|
659 | addr = addr || true; |
---|
660 | rules.removeElementAt(i); |
---|
661 | i--; |
---|
662 | } |
---|
663 | else |
---|
664 | addr = false; |
---|
665 | } |
---|
666 | // No Conflict Resolution, ignore new rule |
---|
667 | if (m_resolutionMode == RESOLUTION_NONE) |
---|
668 | { |
---|
669 | addr = false; |
---|
670 | } |
---|
671 | // Average |
---|
672 | if (m_resolutionMode == RESOLUTION_AVERAGE) |
---|
673 | { |
---|
674 | // create 'average rule' |
---|
675 | if(print_msg) |
---|
676 | System.out.print(inst.toString() + " - " + |
---|
677 | ((Instance)rules.elementAt(i)).toString()); |
---|
678 | inst = averageRule(inst, (Instance)rules.elementAt(i)); |
---|
679 | if(print_msg) |
---|
680 | System.out.println(" : Average : " + inst.toString()); |
---|
681 | // Remove current rule |
---|
682 | rules.removeElementAt(i); |
---|
683 | // test average rule |
---|
684 | addr = true; |
---|
685 | i = 0; |
---|
686 | } |
---|
687 | continue; |
---|
688 | } |
---|
689 | } |
---|
690 | } |
---|
691 | |
---|
692 | if(b) System.out.println("broke out of loop totally!!"); |
---|
693 | // insert the new rule if it has not been discarded, based on |
---|
694 | // output order (decreasing order) |
---|
695 | // System.out.println("Adding Rule"); |
---|
696 | int i = 0; |
---|
697 | double output = inst.toDoubleArray()[classindex]; |
---|
698 | |
---|
699 | // Check Rule Base Limit |
---|
700 | if(addr && ( upperBaseLimit <= 0 || upperBaseLimit > rules.size())) |
---|
701 | { |
---|
702 | while(i < rules.size() && |
---|
703 | (((Instance)rules.elementAt(i)).toDoubleArray()) |
---|
704 | [classindex] > output) i++; |
---|
705 | |
---|
706 | if(i == rules.size()) |
---|
707 | rules.addElement(inst); |
---|
708 | else if(i == 0) |
---|
709 | rules.insertElementAt(inst, 0); |
---|
710 | else |
---|
711 | rules.insertElementAt(inst, i); |
---|
712 | } |
---|
713 | return; |
---|
714 | } |
---|
715 | |
---|
716 | public double classify(Instance inst) |
---|
717 | { |
---|
718 | Instance tInst; |
---|
719 | |
---|
720 | // fill in missing values |
---|
721 | fillMissing(inst); |
---|
722 | |
---|
723 | // Conservative |
---|
724 | if (m_classificationMode == CLASSIFICATION_CONSERVATIVE) |
---|
725 | { |
---|
726 | for(int i=0; i < rules.size(); i++) |
---|
727 | { |
---|
728 | tInst = (Instance)rules.elementAt(i); |
---|
729 | if(isGreaterInput(inst, tInst)) |
---|
730 | { |
---|
731 | return (tInst.toDoubleArray())[inst.classIndex()]; |
---|
732 | } |
---|
733 | } |
---|
734 | |
---|
735 | return (((Instance)rules.lastElement()).toDoubleArray()) |
---|
736 | [inst.classIndex()]; |
---|
737 | } |
---|
738 | // Nearest Neightbour |
---|
739 | int cDist = -1; |
---|
740 | int elem = -1; |
---|
741 | if (m_classificationMode == CLASSIFICATION_NEARESTNEIGHBOUR) |
---|
742 | { |
---|
743 | for(int i=0; i < rules.size(); i++) |
---|
744 | { |
---|
745 | tInst = (Instance)rules.elementAt(i); |
---|
746 | if(cDist == -1 || (distance(inst, tInst) < cDist)) |
---|
747 | { |
---|
748 | cDist = distance(inst, tInst); |
---|
749 | elem = i; |
---|
750 | } |
---|
751 | if(print_msg) |
---|
752 | System.out.println(((Instance)rules.elementAt(i)).toString() + |
---|
753 | " - " + |
---|
754 | inst.toString() + |
---|
755 | ": Distance is " + distance(inst,tInst)); |
---|
756 | } |
---|
757 | if(print_msg) |
---|
758 | System.out.println(((Instance)rules.elementAt(elem)).toString() + |
---|
759 | " is closest to " + |
---|
760 | inst.toString()); |
---|
761 | |
---|
762 | return (((Instance)rules.elementAt(elem)).toDoubleArray()) |
---|
763 | [inst.classIndex()]; |
---|
764 | } |
---|
765 | |
---|
766 | return 0; |
---|
767 | } |
---|
768 | } |
---|
769 | |
---|
770 | private OLMRules olmrules; |
---|
771 | /** |
---|
772 | * Generates the classifier. |
---|
773 | * |
---|
774 | * @param data the data to be used |
---|
775 | * @exception Exception if the classifier can't built successfully |
---|
776 | */ |
---|
777 | public void buildClassifier(Instances data) throws Exception |
---|
778 | { |
---|
779 | // can classifier handle the data? |
---|
780 | getCapabilities().testWithFail(data); |
---|
781 | |
---|
782 | data = new Instances(data); |
---|
783 | numExamples = data.numInstances(); |
---|
784 | Enumeration e = data.enumerateInstances(); |
---|
785 | |
---|
786 | // Checks on data not implemented. |
---|
787 | |
---|
788 | // reset random generator to produce the same results each time |
---|
789 | rand = new Random(0); |
---|
790 | // Options |
---|
791 | if(print_msg) |
---|
792 | System.out.println("Resolution mode: " + m_resolutionMode); |
---|
793 | if(print_msg) |
---|
794 | System.out.println("Classification: " + m_classificationMode); |
---|
795 | if(print_msg) |
---|
796 | System.out.println("Rule size: " + upperBaseLimit); |
---|
797 | |
---|
798 | // initialize rules set. |
---|
799 | olmrules = new OLMRules(); |
---|
800 | int i = 0; |
---|
801 | // fill in rules. |
---|
802 | if(print_msg) |
---|
803 | System.out.println("Printing Rule Process"); |
---|
804 | while(e.hasMoreElements()) |
---|
805 | { |
---|
806 | Instance ins = (Instance)e.nextElement(); |
---|
807 | if(print_msg) |
---|
808 | System.out.println("Trying to add (" + |
---|
809 | ins.toString() + ") Rule"); |
---|
810 | olmrules.addRule(ins); |
---|
811 | if(print_msg) |
---|
812 | System.out.println("Result:"); |
---|
813 | if(print_msg) |
---|
814 | olmrules.printRules(); |
---|
815 | i++; |
---|
816 | |
---|
817 | // System.out.println("Added rule " + i); |
---|
818 | } |
---|
819 | //System.out.println("Rule set built!!"); |
---|
820 | |
---|
821 | // print rule set: |
---|
822 | |
---|
823 | } |
---|
824 | |
---|
825 | /** |
---|
826 | * Prints a description of the classifier. |
---|
827 | * |
---|
828 | * @return a description of the classifier as a string |
---|
829 | */ |
---|
830 | public String toString() { |
---|
831 | return "OLM"; |
---|
832 | } |
---|
833 | |
---|
834 | /** |
---|
835 | * Returns the revision string. |
---|
836 | * |
---|
837 | * @return the revision |
---|
838 | */ |
---|
839 | public String getRevision() { |
---|
840 | return RevisionUtils.extract("$Revision: 5928 $"); |
---|
841 | } |
---|
842 | |
---|
843 | /** |
---|
844 | * Main method for testing this class |
---|
845 | */ |
---|
846 | public static void main(String[] args) { |
---|
847 | |
---|
848 | runClassifier(new OLM(), args); |
---|
849 | } |
---|
850 | } |
---|
851 | |
---|