-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEPCH.java
executable file
·1800 lines (1524 loc) · 79.1 KB
/
EPCH.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* EPCH.java
*
* @author Andres Leon Suarez Cetrulo (suarezcetrulo at gmail dot com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package moa.classifiers.meta;
import com.github.javacliparser.FloatOption;
import com.github.javacliparser.FlagOption;
import com.github.javacliparser.IntOption;
import com.github.javacliparser.StringOption;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import moa.core.DoubleVector;
import moa.core.InstanceExample;
import moa.core.Measurement;
import moa.options.ClassOption;
import moa.classifiers.AbstractClassifier;
import moa.classifiers.Classifier;
import moa.classifiers.MultiClassClassifier;
import moa.classifiers.core.driftdetection.ChangeDetector;
import moa.classifiers.igngsvm.gng.GNG;
import moa.classifiers.igngsvm.gng.GUnit;
import moa.evaluation.BasicClassificationPerformanceEvaluator;
import moa.evaluation.DynamicWindowClassificationPerformanceEvaluator;
import moa.evaluation.LearningPerformanceEvaluator;
import com.yahoo.labs.samoa.instances.Instance;
import com.yahoo.labs.samoa.instances.Instances;
/**
* Evolving Pool of Classifiers with History
*
* @author Andres Leon Suarez Cetrulo (suarezcetrulo at gmail dot com)
* @version $Revision: 1 $
*/
public class EPCH extends AbstractClassifier implements MultiClassClassifier {
@Override
public String getPurposeString() {
return "EPCH from Suarez-Cetrulo et al.";
}
private static final long serialVersionUID = 1L;
/////////////
// Options
// -------
public ClassOption baseLearnerOption = new ClassOption("baseLearner", 'l', "Classifier to train.", Classifier.class,
"trees.HoeffdingTree -e 1000000 -g 200 -c 0"); // default params for hoeffding trees
public IntOption ensembleSizeOption = new IntOption("ensembleSize", 's',
"[HARDCODED TO 1 BY NOW EVEN IF IT CHANGES] The number of learners.", 1, 1, Integer.MAX_VALUE);
public FloatOption lambdaOption = new FloatOption("lambda", 'a', "The lambda parameter for bagging.", 6.0, 1.0,
Float.MAX_VALUE);
public ClassOption driftDetectionMethodOption = new ClassOption("driftDetectionMethod", 'x',
"Change detector for drifts and its parameters", ChangeDetector.class, "ADWINChangeDetector -a 1.0E-5");
public ClassOption warningDetectionMethodOption = new ClassOption("warningDetectionMethod", 'p',
"Change detector for warnings (start training bkg learner)", ChangeDetector.class,
"ADWINChangeDetector -a 1.0E-4");
public FlagOption disableWeightedVote = new FlagOption("disableWeightedVote", 'w', "Should use weighted voting?");
public FlagOption disableDriftDetectionOption = new FlagOption("disableDriftDetection", 'u',
"Should use drift detection? If disabled then bkg learner is also disabled");
// EPCH needs to have a warning always that the drifts are enabled, as the topologies grow during the warning window
// public FlagOption disableBackgroundLearnerOption = new FlagOption("disableBackgroundLearner", 'q',
// "Should use bkg learner? If disabled then reset tree immediately.");
public FlagOption disableRecurringDriftDetectionOption = new FlagOption("disableRecurringDriftDetection", 'r',
"Should save old learners to compare against in the future? If disabled then recurring concepts are not handled explicitely.");
public FlagOption rememberConceptWindowOption = new FlagOption("rememberConceptWindow", 'i',
"Should remember last window size when retrieving a concept? If disabled then retrieved concepts will have a default window size.");
public IntOption defaultWindowOption = new IntOption("defaultWindow", 'd',
"Number of rows by default in Dynamic Sliding Windows.", 50, 1, Integer.MAX_VALUE);
public IntOption windowIncrementsOption = new IntOption("windowIncrements", 'c',
"Size of the increments or decrements in Dynamic Sliding Windows.", 1, 1, Integer.MAX_VALUE);
public IntOption minWindowSizeOption = new IntOption("minWindowSize", 'z',
"Minimum window size in Dynamic Sliding Windows.", 5, 1, Integer.MAX_VALUE);
public IntOption windowResizePolicyOption = new IntOption("windowResizePolicy", 'y',
"Policy to update the size of the window. Ordered by complexity, being 0 the simplest one and 3 the one with most complexity.",
0, 0, 2);
public FloatOption thresholdOption = new FloatOption("thresholdOption", 't',
"Decision threshold for recurring concepts (-1 = threshold option disabled).", 0.65, -1, Float.MAX_VALUE);
public FlagOption resizeAllWindowsOption = new FlagOption("resizeAllWindows", 'b',
"Should the comparison windows for old learners be also dynamic?");
public StringOption eventsLogFileOption = new StringOption("eventsLogFile", 'e',
"File path to export events as warnings and drifts", "./EPCH_events_log.txt");
public FlagOption disableEventsLogFileOption = new FlagOption("disableEventsLogFile", 'g',
"Should export event logs to analyze them in the future? If disabled then events are not logged.");
public IntOption logLevelOption = new IntOption("eventsLogFileLevel", 'h',
"0 only logs drifts; 1 logs drifts + warnings; 2 logs every data example", 1, 0, 2);
public ClassOption evaluatorOption = new ClassOption("baseClassifierEvaluator", 'f',
"Classification performance evaluation method in each base classifier for voting.",
LearningPerformanceEvaluator.class, "BasicClassificationPerformanceEvaluator");
public IntOption driftDecisionMechanismOption = new IntOption("driftDecisionMechanism", 'k',
"0 does not take into account the performance active base classifier explicitely, at the time of the drift; " +
"1 takes into consideration active classifiers", 2, 0, 2);
public IntOption warningWindowSizeThresholdOption = new IntOption("WarningWindowSizeThreshold", 'ñ',
"Threshold for warning window size that defines a false a alarm.", 300, 1, Integer.MAX_VALUE);
public FloatOption distThresholdOption = new FloatOption("distThresholdOption", 'ç',
"Max distance allowed between topologies to be considered part of the same group.", 100000, 0, Float.MAX_VALUE);
public FlagOption resetTopologyInMerginOption = new FlagOption("resetTopologyInMergin", '€',
"Should the topology be trained from scratch after a drift signal is raised?");
// Options for the topology: TODO (these should come from a meta class)
public IntOption topologyLambdaOption = new IntOption("topologyLambda", 'o', "Topology Lambda", 100);
public IntOption maxAgeOption = new IntOption("maxAge", 'm', "MaximumAge", 200);
public FloatOption alfaOption = new FloatOption("alfa", '$', "Alfa", 0.5);
public FloatOption constantOption = new FloatOption("d", '&', "d", 0.995);
public FloatOption BepsilonOption = new FloatOption("epsilonB", '@', "EpsilonB", 0.2);
public FloatOption NepsilonOption = new FloatOption("epsilonN", 'j', "EpsilonN", 0.006);
public IntOption stoppingCriteriaOption = new IntOption("stoppingCriteria", 'v', "Stopping criteria", 100);
// public FloatOption stopPercentageOption = new FloatOption("stopPercentageOption", 'P',
// "Stopping criteria as percentage (if 0, the static stopping criteria is )", 0, 0, 100.0);
public FlagOption classNotAsAnAttributeInTopologyOption = new FlagOption("classNotAsAnAttributeInTopology", 'q',
"Should the class be considered as a feature in the topology?");
//////////
protected EPCHBaseLearner[] ensemble;
protected long instancesSeen;
protected int subspaceSize;
// Window statistics
protected double lastError;
// Warning and Drifts
public long lastDriftOn;
public long lastWarningOn;
// Drift and warning detection
protected ChangeDetector driftDetectionMethod;
protected ChangeDetector warningDetectionMethod;
protected int numberOfDriftsDetected;
protected int numberOfWarningsDetected;
PrintWriter eventsLogFile;
public int logLevel;
Topology topology;
Topology newTopology;
Instances W;
ConceptHistory CH;
int CHid;
int groupId;
///////////////////////////////////////
//
// TRAINING AND TESTING OF THE ENSEMBLE
// Data Management and Prediction modules are here.
// All other modules also are orchestrated from here.
// -----------------------------------
@Override
public void resetLearningImpl() {
// Reset attributes
this.ensemble = null;
this.subspaceSize = 0;
this.instancesSeen = 0;
this.topology = null;
this.newTopology = null;
this.CHid = 0;
this.groupId = 0;
// Reset warning and drift detection related attributes
this.lastDriftOn = 0;
this.lastWarningOn = 0;
this.numberOfDriftsDetected = 0;
this.numberOfWarningsDetected = 0;
// Init Drift Detector
if (!this.disableDriftDetectionOption.isSet()) {
this.driftDetectionMethod = ((ChangeDetector) getPreparedClassOption(this.driftDetectionMethodOption)).copy();
}
// Init Drift Detector for Warning detection.
// if (!this.disableBackgroundLearnerOption.isSet()) {
this.warningDetectionMethod = ((ChangeDetector) getPreparedClassOption(this.warningDetectionMethodOption)).copy();
this.W = new Instances(); // list of training examples during warning window.
//}
}
/**
* In EPCH, this method performs the actions of the classifier manager. Thus, in
* this method, warning and drift detection are performed. This method also send
* instances to the ensemble classifiers. Or to the single active classifier if
* ensemble size = 1 (default).
*
* New BKG classifiers and switching from and to CH may also need to be here.
*
* Steps followed:
* ----------------
* - 0 Initialization
* - 1 If the concept history is ready and it contains old classifiers, then test the training instance
* in each old classifier's internal evaluator to know how their errors compare against bkg one.
* - 2 Update error in active classifier.
* - 3 Update error in background classifier's internal evaluator.
* - 4 Train each base classifier, orchestrating drifts and switching of classifiers.
* - 5 update the topology (this is done as long as there is no active warnings).
*
* The method below implements the following lines of the algorithm:
* - Line 1: start topology
* - Lines 2-3: initialize ensemble (create base classifiers) and lists.
* The rest of the lines of the algorithm are triggered from here using the method
* 'trainBaseClassifierOnInstance' (Lines 4-35).
*/
@Override
public void trainOnInstanceImpl(Instance instance) {
++this.instancesSeen;
// Step 0: Initialization
if (!this.disableDriftDetectionOption.isSet() && this.topology == null) {
this.topology = new Topology(this.topologyLambdaOption, this.alfaOption, this.maxAgeOption,
this.constantOption, this.BepsilonOption, this.NepsilonOption, this.classNotAsAnAttributeInTopologyOption); // algorithm line 1
this.topology.resetLearningImpl();
} System.out.println("prototypes created in topology:" + this.topology.getNumberOfPrototypesCreated());
if (this.ensemble == null) initEnsemble(instance); // algorithm lines 2-3
// Step 1: Update error in concept history learners
if (!this.disableRecurringDriftDetectionOption.isSet() && this.CH != null
&& this.CH.getWarnings().containsValue(true) && this.CH.size() > 0) {
this.CH.updateHistoryErrors(instance);
} // Steps 2-4: Iterate through the ensemble for following steps (active and bkg classifiers)
// for (int lPos = 0; i < this.ensemble.length; i++) { // TODO. the design will change when supporting many active classifiers
int lPos = 0; // learner position in an ensemble
DoubleVector vote = new DoubleVector(this.ensemble[lPos].getVotesForInstance(instance));
InstanceExample example = new InstanceExample(instance);
this.ensemble[lPos].evaluator.addResult(example, vote.getArrayRef()); // Step 2: Testing in active classifier
if (!disableRecurringDriftDetectionOption.isSet()) { // Step 3: Update error in background classifier's
if (this.ensemble[lPos].bkgLearner != null && this.ensemble[lPos].bkgLearner.internalWindowEvaluator != null
&& this.ensemble[lPos].bkgLearner.internalWindowEvaluator
.containsIndex(this.ensemble[lPos].bkgLearner.indexOriginal)) {
DoubleVector bkgVote = new DoubleVector(this.ensemble[lPos].bkgLearner.getVotesForInstance(instance));
// Update both active and bkg classifier internal evaluators
this.ensemble[lPos].bkgLearner.internalWindowEvaluator.addResult(example, bkgVote.getArrayRef());
this.ensemble[lPos].internalWindowEvaluator.addResult(example, vote.getArrayRef());
}
} trainBaseClassifierOnInstance(lPos, instance, this.instancesSeen); // Step 4: Train each base classifier
// } // TODO. the design will change when supporting many active classifiers (read below)
// TODO: uncomment the line below and comment it out in trainBaseClassifierOnInstance()
// this.topology.trainOnInstanceImpl(instance); // Step 5 (Lines 13-15)
// TODO: topology should always be at this level
// also this shouldn't be done if there are active warnings, so these should also be moved to these level
// When supporting many active classifiers, we may need to have a learner for warning and drift detection.
// this learner would not be affected by any bagging and so on, it may be taken into account for votes or not (TBD).
}
/**
* Train base classifiers, track warning and drifts, and orchestrate the comparisons and replacement of classifiers.
* This function is orchestrated from the main EPCH class as drifts and warnings are only accessible from here.
*
* The next line of the algorithm is implemented below:
* Line 4-6: ClassifierTrain(c, x, y) -> // Train c on the current instance (x, y).
* The rest of the lines of the algorithm are triggered from, here by:
* - warningDetection: Lines 7-21
* - topology.trainOnInstanceImpl: Lines 13-15 if warning detection is disabled
* - driftDetection: Lines 22-35
*
* The next steps are followed:
* - Step 1 Train base classifier (Lines 4-6 of algorithm)
* - Step 2 Check for drifts and warnings only if drift detection is enabled
* - Step 2.1 Check for warning only if useBkgLearner is active.
* - Step 2.1.1 Otherwise update the topology (this is done as long as there is no active warnings).
* - Step 2.2 Check for drift
* - Step 3: Log training event
*/
public void trainBaseClassifierOnInstance(int ensemblePos, Instance instance, long instancesSeen) {
// Step 1: Train base classifier (Lines 4-6)
this.ensemble[ensemblePos].trainOnInstance(instance, instancesSeen);
// Step 2: Check for drifts and warnings only if drift detection is enabled
if (!this.disableDriftDetectionOption.isSet()) { // && !this.ensemble[ensemblePos].isBackgroundLearner)
boolean correctlyClassifies = this.ensemble[ensemblePos].correctlyClassifies(instance);
// Step 2.1: Check for warning only if useBkgLearner is active. The topology gets updated either way
// if (!this.disableBackgroundLearnerOption.isSet()) // in EPCH warning detection should always be active
detectWarning(ensemblePos, instance, correctlyClassifies); //else
this.topology.trainOnInstanceImpl(instance); // Step 2.1.1 (Lines 13-15)
// TODO: this design won't work for multiple classifiers, the training of the topology should be at the upper level
// TODO: topology should not be in this function.
// it should be in the previous one as otherwise we'll send X copies of the same instance each time,
// depending on the number of classifiers
// Step 2.2: Check for drift
detectDrift(ensemblePos, correctlyClassifies);
} // Step 3: Register training example in log
if (this.eventsLogFile != null && this.logLevelOption.getValue() >= 2)
logEvent(getTrainExampleEvent(ensemblePos));
}
@Override
public double[] getVotesForInstance(Instance instance) {
Instance testInstance = instance.copy();
if (this.ensemble == null) initEnsemble(testInstance);
DoubleVector combinedVote = new DoubleVector();
for (int i = 0; i < this.ensemble.length; ++i) {
DoubleVector vote = new DoubleVector(this.ensemble[i].getVotesForInstance(testInstance));
if (vote.sumOfValues() > 0.0) {
vote.normalize();
double acc = this.ensemble[i].evaluator.getPerformanceMeasurements()[1].getValue();
if (!this.disableWeightedVote.isSet() && acc > 0.0) {
for (int v = 0; v < vote.numValues(); ++v) vote.setValue(v, vote.getValue(v) * acc);
}
combinedVote.addValues(vote);
}
}
return combinedVote.getArrayRef();
}
@Override
public boolean isRandomizable() {
return true;
}
@Override
public void getModelDescription(StringBuilder arg0, int arg1) {
}
@Override
protected Measurement[] getModelMeasurementsImpl() {
// TODO. add the same to RCARF, getting a sum of all warnings/drifts in the ensemble
// (just add a loop through ensemble[i].numberOfDriftsDetected) in rcarf
List<Measurement> measurementList = new LinkedList<Measurement>();
measurementList.add(new Measurement("Change detected", this.numberOfDriftsDetected));
measurementList.add(new Measurement("Warning detected", this.numberOfWarningsDetected));
// this.numberOfDriftsDetected = 0;
// this.numberOfWarningsDetected = 0;
return measurementList.toArray(new Measurement[measurementList.size()]);
}
protected void initEnsemble(Instance instance) {
// Init the ensemble.
int ensembleSize = 1; // this.ensembleSizeOption.getValue(); TODO: undo this once the design is ready for many active learners.
this.ensemble = new EPCHBaseLearner[ensembleSize];
BasicClassificationPerformanceEvaluator classificationEvaluator = (BasicClassificationPerformanceEvaluator)
getPreparedClassOption(this.evaluatorOption);
// Only initialize the Concept History if the handling of recurring concepts is enabled
if (!this.disableRecurringDriftDetectionOption.isSet()) CH = new ConceptHistory(this.distThresholdOption.getValue());
//if (!this.disableBackgroundLearnerOption.isSet()) // (EPCH needs warnings to be enabled)
this.W = (Instances) instance.copy().dataset();
this.W.delete();
try { // Start events logging and print headers
if (this.disableEventsLogFileOption.isSet()) {
this.eventsLogFile = null;
} else {
this.eventsLogFile = new PrintWriter(this.eventsLogFileOption.getValue());
logEvent(getEventHeaders());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Classifier learner = (Classifier) getPreparedClassOption(this.baseLearnerOption);
learner.resetLearning();
for (int i = 0; i < ensembleSize; ++i) {
this.ensemble[i] = new EPCHBaseLearner(i, (Classifier) learner.copy(),
(BasicClassificationPerformanceEvaluator) classificationEvaluator.copy(), this.instancesSeen,
// !this.disableBackgroundLearnerOption.isSet(), // these are still needed con the level below (now this is true in EPCH)
!this.disableDriftDetectionOption.isSet(), // these are still needed con the level below
false, // isbkglearner
!this.disableRecurringDriftDetectionOption.isSet(),
false, // first classifier is not in the CH.
new Window(this.defaultWindowOption.getValue(), this.windowIncrementsOption.getValue(),
this.minWindowSizeOption.getValue(), this.thresholdOption.getValue(),
this.rememberConceptWindowOption.isSet() ? true : false,
this.resizeAllWindowsOption.isSet() ? true : false, windowResizePolicyOption.getValue()),
null, // Windows start at NULL
this.warningWindowSizeThresholdOption.getValue());
}
}
///////////////////////////////////////
//
// CLASSIFIER MANAGEMENT MODULE
// Divided into three parts:
// - Warning and drift handling (detection)
// - Actions in case of drift
// - Identification of new best group, classifier and trigger switch between classifiers.
// -----------------------------------
// WARNING AND DRIFT HANDLING
/**
* This method implements all the actions that happen when the warning detection is enabled.
*
* Some of the following lines of the algorithm EPCH are implemented here:
* - Lines 7-9: False Alarms handling at buffer W level (Case 2)
* - Line 10: if (size(W) 𝝐 (𝟏, 𝝁)) ->In warning window
* - Line 11: Train the background classifier
* - Line 12: Add instance to the buffer of instances during warning
* - Line 13-15: Update centroids / prototypes.
* - Line 16-20: If a warning is detected, start warning window and clear buffer W.
*
* The steps followed for this can be seen below:
* - Step 1 Update warning detection adding latest error /*********** warning detection ***********
* - Step 2 Check for False Alarm (Case 2) - Lines 7-10
* - Step 3 If the classifier is in the warning window, train the bkg classifier and add the current instance to W.
* - Step 3.1 Otherwise update the topology (the topology does not update during warning)
* - Step 4 Check if there was a change (warning signal). If so, start warning window;
* In case of false alarm this triggers warning again and the bkg learner gets replaced.
* - Step 4.1 Update the warning detection object for the current object.
* This effectively resets changes made to the object while it was still a bkglearner.
* - Step 4.2 Start warning window.
*
*/
protected void detectWarning(int ensemblePos, Instance instance, boolean correctlyClassifies) {
// Step 1: Update the WARNING detection method
this.warningDetectionMethod.input(correctlyClassifies ? 0 : 1);
// Step 2: Check for False Alarm case 2 (Lines 7-9)
if (this.W.size() >= this.warningWindowSizeThresholdOption.getValue()) resetWarningWindow(ensemblePos); // Line 8
// Step 3: Either warning window training/buffering or topology update (Lines 10-15)
if (this.W.size() >= 1 && this.W.size() < this.warningWindowSizeThresholdOption.getValue()) { // &&
// this.ensemble[ensemblePos].bkgLearner != null) { // when W is in that range, bkgLearner != null (tested), so the condition is not required
this.ensemble[ensemblePos].bkgLearner.classifier.trainOnInstance(instance); // Line 11
this.W.add(instance); // Line 12
} else this.topology.trainOnInstanceImpl(instance); // Step 3.1: Lines 13-15 (TODO: should we feed a given instance to GNG many times?)
// Step 4: line 16: warning detected?
if (this.warningDetectionMethod.getChange()) {
resetWarningWindow(ensemblePos); // Step 4.1 (Line 19)
startWarningWindow(ensemblePos); //Step 4.2
}
}
protected void resetWarningWindow(int ensemblePos){
this.ensemble[ensemblePos].bkgLearner = null; // Lines 8 and 19
this.ensemble[ensemblePos].internalWindowEvaluator = null;
this.ensemble[ensemblePos].tmpCopyOfClassifier = null;
this.warningDetectionMethod = ((ChangeDetector) getPreparedClassOption(this.warningDetectionMethodOption)).copy(); // restart warning
this.W.delete(); // Lines 8 and 19 (it also initializes the object W)
this.CH.decreaseNumberOfWarnings(ensemblePos); // update applicable concepts
}
/**
* This starts the warning window event
*
* The next steps are followed:
* - 1 Update last error and make a backup of the current classifier in a concept object
* (the active one will be in use until the Drift is confirmed).
* - 2 Update of objects with warning.
* - 3 If the concept internal evaluator has been initialized for any other classifier on warning,
* add window size and last error of current classifier on warning.
* Otherwise, initialize a new internal evaluator for the concept
* */
protected void startWarningWindow(int ensemblePos) { // TODO: review this as I've moved things.
this.lastWarningOn = this.instancesSeen;
this.numberOfWarningsDetected++;
// Step 1 Update last error and make a backup of the current classifier
if (!this.disableRecurringDriftDetectionOption.isSet()) {
this.ensemble[ensemblePos].saveCurrentConcept(this.instancesSeen); // line 17: Save a tmp copy of c as snapshot
}
// Step 2: Update of objects with warning.
if (!disableRecurringDriftDetectionOption.isSet()) {
// this.ensemble[ensemblePos].internalWindowEvaluator = null; (replaced anyway by the next line) (TODO: why would this be in RCARF?)
this.ensemble[ensemblePos].createInternalEvaluator();
this.CH.increaseNumberOfWarnings(ensemblePos, this.ensemble[ensemblePos], this.lastError);
}
if (this.eventsLogFile != null && this.logLevelOption.getValue() >= 1) logEvent(getWarningEvent(ensemblePos)); // Log this
// Step 3: Create background Classifier
this.ensemble[ensemblePos].createBkgClassifier(this.lastWarningOn); // line 18: create background classifier
}
/**
* This method selects the next concept classifier and closest group topology when a drift is raised.
* Pselected is: a new P (Pn) in case of bkgDrift; Pc in case of false alarm; and Ph in case of recurring drift.
*
* The next steps are followed:
* - 0 Set false in case of drift at false as default.
* Included for cases where driftDecisionMechanism > 0 and recurring drifts are enabled.
* - 1 Compare DT results using Window method and pick the best one between CH and bkg classifier.
* It returns the best classifier in the object of the bkgLearner if there is not another base classifier
* with lower error than active classifier (and driftDecisionMechanism > 0), then a false alarm is raised.
* This step belong to line 32 in the algorithm: c = FindClassifier(c, b, GH) -> Assign best transition to next state.
* - 2 Orchestrate all the actions if the drift is confirmed and there is not a false alarm.
* - 3 Decrease amount of warnings in concept history and from evaluators
* - 4 reset base learner
*
* Lines of the algorithm Lines 22-25 are implemented here:
* -----------
* Insertion in CH (Lines 24-28)
* line 24: get prototypes from topology
* line 25: Group for storing old state
* line 26-28: create a placeholder for a group represented by 'tmpPrototypes'
* Retrieval from CH and refresh (lines 29-35)
* line 29: push current classifier to Gc
* line 30: Update topology on Gc
* line 31: Group for retrieval of next state
* lines 32-33: In method switchActiveClassifier
* line 34: Add the examples during warning to a new topology.
* line 35: Empty list W
*/
protected void detectDrift(int ensemblePos, boolean correctlyClassifies) {
/*********** drift detection ***********/
// Update the DRIFT detection method
this.driftDetectionMethod.input(correctlyClassifies ? 0 : 1);
// Check if there was a change
if (this.driftDetectionMethod.getChange()) { // line 22-23 drift detected?
this.lastDriftOn = this.instancesSeen;
this.numberOfDriftsDetected++;
boolean falseAlarm = false; // Set false alarms (case 1) at false as default
// Retrieval from CH
if (!this.disableRecurringDriftDetectionOption.isSet()) // step 1
// Start retrieval from CH (TODO: W should only be used if warning detection is enabled. same for topologies?)
falseAlarm = switchActiveClassifier(ensemblePos, this.CH.findGroup(this.W)); // lines 31-33 of the algorithm
else if (this.eventsLogFile != null && this.logLevelOption.getValue() >= 1)
logEvent(getBkgDriftEvent(ensemblePos)); // TODO. Refactor this logEvent function so always
// it's inside of 'registerDrift' and not wrapping it
if (!falseAlarm) { // Step 2
// Insertion in CH (Lines 24-27)
Instances tmpPrototypes = this.topology.getPrototypes(); // line 24
int previousGroup = this.CH.findGroup(tmpPrototypes); // line 25
if (this.CH.size() == 0 || previousGroup == -1) { // line 26
previousGroup = this.groupId++;
this.CH.createNewGroup(previousGroup, tmpPrototypes, this.newTopology); // line 27
} pushToConceptHistory(ensemblePos, previousGroup); // lines 29-30
if (!this.disableRecurringDriftDetectionOption.isSet())
this.CH.decreaseNumberOfWarnings(ensemblePos); //, previousGroup); // step 3
this.ensemble[ensemblePos].reset(); // reset base classifier (step 4)
if (this.newTopology != null) this.topology = updateExtraTopology(this.newTopology, this.W); // line 34
this.W.delete(); // line 35
}
}
}
protected void pushToConceptHistory(int ensemblePos, int previousGroup) {
// Move copy of active classifier made before warning to Concept History.
this.ensemble[ensemblePos].tmpCopyOfClassifier.addHistoryID(this.CHid++); // ConceptHistory.nextID())
this.CH.addLearnerToGroup(previousGroup, this.ensemble[ensemblePos].tmpCopyOfClassifier); // line 29
this.CH.setGroupTopology(previousGroup, mergeTopologies(this.CH.getTopologyFromGroup(previousGroup))); // line 30
}
// DRIFT ACTIONS
/***
* Register false alarm an update variables consequently
* (both the active classifier and the ensemble topology will remain being the same)
*/
protected boolean registerDriftFalseAlarm(int ensemblePos) {
if (this.eventsLogFile != null && this.logLevel >= 0) logEvent(getFalseAlarmEvent(ensemblePos));
this.newTopology = null; // then Pn = Pc (line 33)
return true;
}
/***
* Register recurring drift an update variables consequently
* Copy the best recurring learner in the history group passed, and the topology of this group.
*/
protected void registerRecurringDrift(int ensemblePos, Integer indexOfBestRanked, int historyGroup) {
if (this.eventsLogFile != null && this.logLevel >= 0)
logEvent(getRecurringDriftEvent(indexOfBestRanked, historyGroup, ensemblePos));
this.ensemble[ensemblePos].bkgLearner = this.CH.copyConcept(historyGroup, indexOfBestRanked);
this.newTopology = this.CH.getTopologyFromGroup(historyGroup); // then Pn = Ph (line 33)
}
/***
* Register background drift an update variables consequently
* Pselected is a new P in case of background drift
*/
protected void registerBkgDrift(int ensemblePos) {
// Register background drift
if (this.eventsLogFile != null && this.logLevel >= 0)
logEvent(getBkgDriftEvent(ensemblePos));
this.newTopology = new Topology(this.topologyLambdaOption, this.alfaOption, this.maxAgeOption,
this.constantOption, this.BepsilonOption, this.NepsilonOption, this.classNotAsAnAttributeInTopologyOption); // line 33
this.newTopology.resetLearningImpl();
}
// IDENTIFICATION OF NEXT STATE
/**
* This method ranks all applicable base classifiers in the Concept History (CH)
* It also selects the next classifier to be active, or it raises a false alarm
* if the drift should be reconsidered.
*
* It implements the lines 32-33 of the algorithm.
* lines 32-33: get topology of the group (methods 'registerDrift...') and retrieve the best classifier
*
* -----------------------------------------------------------------------------------------------------------------
* False alarms depend on the drift decision mechanism
* -----------------------------------------------------------------------------------------------------------------
*
* When driftDecisionMechanism == 0, if bkgLearner == null, false alarms cannot
* be raised. A comparison against CH is not possible as there is no bkg learner
* trained. In this case, a drift signal has been raised and it cannot be
* stopped without false alarms. A bkg drift applies as only option available.
*
* When drift decision mechanism == 1 or 2, then false alarms are taken into
* consideration for drifts (the warning will be still active even if a false
* alarm is raised for a drift in the same active classifier). If the background
* learner is NULL, we consider that the drift signal may have been caused by a
* too sensitive drift detection parameterization. In this case, it's clearly
* too soon to change the active classifier. Therefore we raise a drift signal.
*
* When drift decision mechanism == 2, we also raise a false alarm when the
* active classifier obtains less error than the bkg classifier and all of the
* classifiers from the CH.
*
* -----------------------------------------------------------------------------------------------------------------
* If the active classifier is not the best available choice / false alarm is
* raised, the following logic applies:
* -----------------------------------------------------------------------------------------------------------------
* If bkgBetterThanCHbaseClassifier == False, the minimum error of the base
* classifiers in the CH is not lower than the error of the bkg classifier.
* Then, register background drift.
*
* If CHranking.size() == 0, no applicable concepts for the active classifier in
* the concept history. Then, we register background drift. Otherwise, a
* recurring drift is the best option.
*
* @param historyGroup
*
*/
protected boolean switchActiveClassifier(int ensemblePos, int historyGroup) {
int indexOfBestRanked = -1;
double errorOfBestRanked = -1.0;
HashMap<Integer, Double> ranking = new HashMap<Integer, Double> ();
// 1 Raise a false alarm for the drift if the background learner is not ready (Case 1)
if (this.driftDecisionMechanismOption.getValue() > 0 && this.ensemble[ensemblePos].bkgLearner == null)
return registerDriftFalseAlarm(ensemblePos);
// 2 Retrieve best applicable classifier from Concept History (if a CH group applies)
if (historyGroup != -1) ranking = rankConceptHistoryClassifiers(ensemblePos, historyGroup);
if (ranking.size() > 0) {
indexOfBestRanked = getMinKey(ranking); // find index of concept with lowest value (error)
errorOfBestRanked = Collections.min(ranking.values());
}
// 3 Compare this against the background classifier and make the decision.
if (this.driftDecisionMechanismOption.getValue() == 2) {
if (activeBetterThanBKGbaseClassifier(ensemblePos)) {
if (ranking.size() > 0 && !activeBetterThanCHbaseClassifier(ensemblePos, errorOfBestRanked))
registerRecurringDrift(ensemblePos, indexOfBestRanked, historyGroup);
// False alarm if active classifier is still the best one and when there are no applicable concepts.
else return registerDriftFalseAlarm(ensemblePos);
} else {
if (ranking.size() > 0 && bkgBetterThanCHbaseClassifier(ensemblePos, errorOfBestRanked))
registerRecurringDrift(ensemblePos, indexOfBestRanked, historyGroup);
else registerBkgDrift(ensemblePos);
}
// Drift decision mechanism == 0 or 1 (in an edge case where the bkgclassifier is still NULL, we ignore the comparisons) (Case 1)
} else {
if (ranking.size() > 0 && this.ensemble[ensemblePos].bkgLearner != null
&& bkgBetterThanCHbaseClassifier(ensemblePos, errorOfBestRanked))
registerRecurringDrift(ensemblePos, indexOfBestRanked, historyGroup);
else
registerBkgDrift(ensemblePos);
} return false; // No false alarms raised at this point
}
protected Topology trainNewWithOldTopology(Instances instances, Instances instances2) {
Topology top = new Topology(this.topologyLambdaOption, this.alfaOption, this.maxAgeOption,
this.constantOption, this.BepsilonOption, this.NepsilonOption, this.classNotAsAnAttributeInTopologyOption); // line 2
top.resetLearningImpl();
top = updateExtraTopology(top, instances); // feed first old prototypes from group
top = updateExtraTopology(top, instances2); // feed new topology to be merged
return top;
}
/**
* The next line of the algorithm EPCH is implemented by this method.
* line 30: Update topology on Gc
* */
protected Topology mergeTopologies(Topology CHtop) {
if (this.resetTopologyInMerginOption.isSet()) {
// way 1: old prototypes will be given less importance in this merging mechanism
return trainNewWithOldTopology(CHtop.getPrototypes(), this.topology.getPrototypes());
} else {
// way 2: old prototypes may be more important here due to the way how GNG works
return updateExtraTopology(CHtop, this.topology.getPrototypes());
}
}
/**
* This auxiliary function updates either old or new topologies that will be merged with, compared with, or will replace to the current one.
* */
protected Topology updateExtraTopology(Topology top, Instances w2) {
/*
int trainType = 0;
if(stopPercentageOption.getValue() > 0)
top.stoppingCriteriaOption.setValue((int)((this.stopPercentageOption.getValue() * (double) w2.size()) / 100.0));
if(trainType==0) {
// We add them several times till achieving the stopping criteria as in iGNGSVM
// The effect of this would be a GNG topology that may not be able to keep expanding, which may be undesired in EPCH.
for (int i=0; top.getNumberOfPrototypesCreated()<top.stoppingCriteriaOption.getValue(); i++){
top.trainOnInstanceImpl((Instance) w2.get(i));
if(i+1==w2.numInstances()) i = -1;
}
} else { */
// we add them once
for (int instPos = 0; instPos < w2.size(); instPos++) {
top.trainOnInstanceImpl(w2.get(instPos));
}
// }
return top; // if topology (Pc) is global, then we don´t need to return this here
}
/* Compute distances between Instances as seen in GNG for arrays (GUnit objects). **/
public double dist(Instance w1,Instance w2){
double sum = 0;
for (int i = 0; i < w1.numAttributes(); i++) {
sum += Math.pow(w1.value(i)-w2.value(i),2);
}
return Math.sqrt(sum);
}
/**
* This function ranks the best concepts from a given group of the Concept History
* -----------------------------------
* This only takes into consideration Concepts sent to the Concept History
* after the current classifier raised a warning (see this consideration in reset*)
*
* The Concept History owns only one learner per historic concept.
* But each learner has a different window size and error.
*
* this.indexOriginal - pos of this classifier with active warning in ensemble
*/
protected HashMap<Integer, Double> rankConceptHistoryClassifiers(int ensemblePos, int historyGroup) {
HashMap<Integer, Double> CHranking = new HashMap<Integer, Double>();
for (Concept auxConcept : this.CH.getConceptsFromGroup(historyGroup))
if (auxConcept.ConceptLearner.internalWindowEvaluator != null
&& auxConcept.ConceptLearner.internalWindowEvaluator.containsIndex(ensemblePos)) { // TODO: check that ensemblePos is the right one
CHranking.put(auxConcept.getHistoryIndex(),
((DynamicWindowClassificationPerformanceEvaluator) auxConcept.ConceptLearner.internalWindowEvaluator)
.getFractionIncorrectlyClassified(ensemblePos));
}
return CHranking;
}
/**
* Aux method for getting the best classifier (used to rank concepts from a group in the CH)
* */
protected Integer getMinKey(Map<Integer, Double> map) {
Integer minKey = null;
double minValue = Double.MAX_VALUE;
for (Integer key : map.keySet()) {
double value = map.get(key);
if (value < minValue) {
minValue = value;
minKey = key;
}
} return minKey;
}
protected boolean activeBetterThanBKGbaseClassifier(int ensemblePos) {
// If drift decision mechanism is == 2
return (((DynamicWindowClassificationPerformanceEvaluator) this.ensemble[ensemblePos].internalWindowEvaluator)
.getFractionIncorrectlyClassified(this.ensemble[ensemblePos].indexOriginal) <= ((DynamicWindowClassificationPerformanceEvaluator)
this.ensemble[ensemblePos].bkgLearner.internalWindowEvaluator).getFractionIncorrectlyClassified(this.ensemble[ensemblePos].bkgLearner.indexOriginal));
// TODO: can we use ensemblePos instead of indexOriginal (how does this evolve when retrieving classifiers from the CH)?
// this.indexOriginal - pos of this classifier with active warning in ensemble
// return ((this.ensemble[ensemblePos].evaluator.getFractionIncorrectlyClassified() <= ((DynamicWindowClassificationPerformanceEvaluator)
// this.ensemble[ensemblePos].bkgLearner.internalWindowEvaluator).getFractionIncorrectlyClassified(this.ensemble[ensemblePos].bkgLearner.indexOriginal)));
}
protected boolean activeBetterThanCHbaseClassifier(int ensemblePos, double bestFromCH) {
// If drift decision mechanism is == 2
return (((DynamicWindowClassificationPerformanceEvaluator) this.ensemble[ensemblePos].internalWindowEvaluator)
.getFractionIncorrectlyClassified(this.ensemble[ensemblePos].indexOriginal) <= bestFromCH);
// TODO: can we use ensemblePos instead of indexOriginal (how does this evolve when retrieving classifiers from the CH)?
// this.indexOriginal - pos of this classifier with active warning in ensemble
// return (this.ensemble[ensemblePos].evaluator.getFractionIncorrectlyClassified() <= bestFromCH); (old comparison)
}
protected boolean bkgBetterThanCHbaseClassifier(int ensemblePos, double bestFromCH) {
// this.bkgLearner.indexOriginal - pos of bkg classifier if it becomes active in the ensemble (always same pos than the active)
return (bestFromCH <= ((DynamicWindowClassificationPerformanceEvaluator) this.ensemble[ensemblePos].bkgLearner.internalWindowEvaluator)
.getFractionIncorrectlyClassified(this.ensemble[ensemblePos].bkgLearner.indexOriginal));
}
///////////////////////////////////////
//
// LOGGING FUNCTIONS
// -----------------------------------
public Event getTrainExampleEvent(int indexOriginal) {
String[] eventLog = { String.valueOf(instancesSeen), "Train example", String.valueOf(indexOriginal),
String.valueOf(this.ensemble[indexOriginal].evaluator.getPerformanceMeasurements()[1].getValue()),
this.warningDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
this.driftDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
String.valueOf(this.instancesSeen), String.valueOf(this.ensemble[indexOriginal].evaluator.getFractionIncorrectlyClassified()),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings().size(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getNumberOfActiveWarnings(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings(): "N/A"),
"N/A", "N/A", "N/A" };
return (new Event(eventLog));
}
public Event getWarningEvent(int indexOriginal) {
// System.out.println();
System.out.println("-------------------------------------------------");
System.out.println("WARNING ON IN MODEL #"+indexOriginal+". Warning flag status (activeClassifierPos, Flag): "+CH.getWarnings());
System.out.println("CONCEPT HISTORY STATE AND APPLICABLE FROM THIS WARNING IS: "+CH.keySet().toString());
System.out.println("-------------------------------------------------");
// System.out.println();
String[] warningLog = { String.valueOf(this.lastWarningOn), "WARNING-START", // event
String.valueOf(indexOriginal),
String.valueOf(this.ensemble[indexOriginal].evaluator.getPerformanceMeasurements()[1].getValue()),
this.warningDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
this.driftDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
String.valueOf(this.instancesSeen), String.valueOf(this.ensemble[indexOriginal].evaluator.getFractionIncorrectlyClassified()),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings().size(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getNumberOfActiveWarnings(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings(): "N/A"),
!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.keySet().toString(): "N/A",
"N/A", "N/A" };
// 1279,1,WARNING-START,0.74,{F,T,F;F;F;F},...
return (new Event(warningLog));
}
public Event getBkgDriftEvent(int indexOriginal) {
System.out.println("DRIFT RESET IN MODEL #"+indexOriginal+" TO NEW BKG MODEL #"+this.ensemble[indexOriginal].bkgLearner.indexOriginal);
String[] eventLog = {
String.valueOf(this.lastDriftOn), "DRIFT TO BKG MODEL", String.valueOf(indexOriginal),
String.valueOf(this.ensemble[indexOriginal].evaluator.getPerformanceMeasurements()[1].getValue()),
this.warningDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
this.driftDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
String.valueOf(this.instancesSeen), String.valueOf(this.ensemble[indexOriginal].evaluator.getFractionIncorrectlyClassified()),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings().size(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getNumberOfActiveWarnings(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings(): "N/A"),
"N/A", "N/A", "N/A" };
return (new Event(eventLog));
}
public Event getRecurringDriftEvent(Integer indexOfBestRankedInCH, int group, int indexOriginal) {
System.out.println(indexOfBestRankedInCH); // TODO: debugging
System.out.println("RECURRING DRIFT RESET IN POSITION #"+indexOriginal+" TO MODEL #"+
CH.get(indexOfBestRankedInCH).groupList.get(indexOfBestRankedInCH).ensembleIndex);
// +this.bkgLearner.indexOriginal);
String[] eventLog = { String.valueOf(this.lastDriftOn), "RECURRING DRIFT", String.valueOf(indexOriginal),
String.valueOf(this.ensemble[indexOriginal].evaluator.getPerformanceMeasurements()[1].getValue()),
this.warningDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
this.driftDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
String.valueOf(this.instancesSeen), String.valueOf(this.ensemble[indexOriginal].evaluator.getFractionIncorrectlyClassified()),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings().size(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getNumberOfActiveWarnings(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings(): "N/A"),
"N/A",
String.valueOf(this.CH.get(group).groupList.get(indexOfBestRankedInCH).ensembleIndex),
String.valueOf(this.CH.get(group).groupList.get(indexOfBestRankedInCH).createdOn) };
return (new Event(eventLog));
}
public Event getFalseAlarmEvent(int indexOriginal) {
System.out.println("FALSE ALARM IN MODEL #"+indexOriginal);
String[] eventLog = { String.valueOf(this.lastDriftOn), "FALSE ALARM ON DRIFT SIGNAL",
String.valueOf(indexOriginal),
String.valueOf(this.ensemble[indexOriginal].evaluator.getPerformanceMeasurements()[1].getValue()),
this.warningDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
this.driftDetectionMethodOption.getValueAsCLIString().replace("ADWINChangeDetector -a ", ""),
String.valueOf(this.instancesSeen), String.valueOf(this.ensemble[indexOriginal].evaluator.getFractionIncorrectlyClassified()),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings().size(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getNumberOfActiveWarnings(): "N/A"),
String.valueOf(!this.disableRecurringDriftDetectionOption.isSet() ? this.CH.getWarnings(): "N/A"),
"N/A", "N/A", "N/A" };
return (new Event(eventLog));
}
// General auxiliar methods for logging events
public Event getEventHeaders() {
String[] headers = { "instance_number", "event_type", "affected_position", // former 'classifier'
"voting_weight", // voting weight for the three that presents an event.
"warning_setting", "drift_setting", "affected_classifier_created_on", "error_percentage",
"amount_of_classifiers", "amount_of_active_warnings", "classifiers_on_warning", "applicable_concepts",
"recurring_drift_to_history_id", "recurring_drift_to_classifier_created_on" };
return (new Event(headers));
}
/**
* Method to register events such as Warning and Drifts in the event log file.
*/
public void logEvent(Event eventDetails) {
// Log processed instances, warnings and drifts in file of events
// # instance, event, affected_position, affected_classifier_id last-error, #classifiers;#active_warnings; classifiers_on_warning,
// applicable_concepts_from_here, recurring_drift_to_history_id, drift_to_classifier_created_on
this.eventsLogFile.println(String.join(";", eventDetails.getInstanceNumber(), eventDetails.getEvent(),
eventDetails.getAffectedPosition(), eventDetails.getVotingWeigth(), // of the affected position
eventDetails.getWarningSetting(), // WARNING SETTING of the affected position.
eventDetails.getDriftSetting(), // DRIFT SETTING of the affected position.
eventDetails.getCreatedOn(), // new, affected_classifier_was_created_on
eventDetails.getLastError(),
eventDetails.getNumberOfClassifiers(),
eventDetails.getNumberOfActiveWarnings(), // #active_warnings
eventDetails.getClassifiersOnWarning(), // toString of list of classifiers in warning
eventDetails.getListOfApplicableConcepts(), // applicable_concepts_from_here
eventDetails.getRecurringDriftToClassifierID(), // recurring_drift_to_history_id
eventDetails.getDriftToClassifierCreatedOn()));
this.eventsLogFile.flush();
}
///////////////////////////////////////
//
// AUX CLASSES
// -----------------------------------
/**
* Inner class that represents a single tree member of the ensemble. It contains
* some analysis information, such as the numberOfDriftsDetected,
*/
protected final class EPCHBaseLearner {
public int indexOriginal;
public long createdOn;
public Classifier classifier;
public boolean isBackgroundLearner;
public boolean isOldLearner; // only for reference
// public boolean useBkgLearner; // (now always true in EPCH)
// these flags are still necessary at this level
public boolean useDriftDetector;
public boolean useRecurringLearner;
// Bkg learner
protected EPCHBaseLearner bkgLearner;
// Copy of main classifier at the beginning of the warning window for its copy in the Concept History
protected Concept tmpCopyOfClassifier;
// Statistics
public BasicClassificationPerformanceEvaluator evaluator;
// Internal statistics
public DynamicWindowClassificationPerformanceEvaluator internalWindowEvaluator; // for bkg and CH classifiers
protected double lastError;
protected Window windowProperties;