-
Notifications
You must be signed in to change notification settings - Fork 0
/
OATESTest.java
653 lines (515 loc) · 24.4 KB
/
OATESTest.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
/*
* Example of command line:
* EvaluatePrequentialRegression -l (meta.OATES -c (threshold.ProductivitySplitClusterer -t 2.85;6.6) -d (/Users/llm11/Leandro's Files/Work/Approaches/MOA-2016.04-leandro/promise_nominal_edited0_sorted_timestamp.arff) -q 500) -s (ArffFileStream -f (/Users/llm11/Leandro's Files/Work/Approaches/MOA-2016.04-leandro/cocomonasa_nominal_edited_timestamp.arff) -c 17) -f 1 -o (/Users/llm11/Leandro's Files/Work/Approaches/MOA-2016.04-leandro/tmp.pred) -O (/Users/llm11/Leandro's Files/Work/Approaches/MOA-2016.04-leandro/tmp2.pred)
*/
package moa.classifiers.meta;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import com.yahoo.labs.samoa.instances.Attribute;
import com.yahoo.labs.samoa.instances.AttributesInformation;
import com.yahoo.labs.samoa.instances.DenseInstance;
import com.yahoo.labs.samoa.instances.Instance;
import com.yahoo.labs.samoa.instances.InstanceInformation;
import com.yahoo.labs.samoa.instances.Instances;
import junit.framework.TestCase;
import junitx.util.PrivateAccessor;
import moa.cluster.Clustering;
import moa.clusterers.streamkm.StreamKM;
import moa.streams.ArffFileStream;
public class OATESTest extends TestCase {
public OATESTest() {
super();
}
public OATESTest(String name) {
super(name);
}
private ArffFileStream wcDataStream;
private String wcDataSetFileName = "test_wc_data.arff";
private String ccDataSetFileName = "test_cc_data.arff";
private String ccDataSetFileNameEmpty = "test_cc_data_empty.arff";
private int effIndex = 2; // note that this class index starts with 0
private int sizeIndex = 1;
private Instances dataset;
private OATES oates, oates2; //dycom uses ProductivitySplitClusterer, and dycom2 uses StreamKM
private OATES oatesNoCC; // same as oates, but without any cc data stream
protected void setUp() throws Exception {
super.setUp();
wcDataStream = new ArffFileStream(wcDataSetFileName, effIndex);
oates = new OATES();
oates.baseLearnerOption.setValueViaCLIString("moa.classifiers.lazy.kNN -k 1");
oates.mappingFunctionOption.setValueViaCLIString("moa.classifiers.meta.SimpleLinearMappingFunction -r 0.1");
oates.clustererOption.setValueViaCLIString("moa.clusterers.threshold.ProductivitySplitClusterer -t \"1.0;2.0\" -e " + effIndex + " -s " + sizeIndex);
oates.ccDataStreamFile.setValueViaCLIString(ccDataSetFileName);
oates.betaWC.setValue(0.6);
oates.betaCC.setValue(0.5);
oates.period.setValue(1);
oates.wcPastInstancesQueueSize.setValue(3);
oates.prepareForUse();
oates.resetLearning();
oates2 = new OATES();
oates2.baseLearnerOption.setValueViaCLIString("moa.classifiers.lazy.kNN -k 1");
oates2.mappingFunctionOption.setValueViaCLIString("moa.classifiers.meta.SimpleLinearMappingFunction -r 0.1");
oates2.clustererOption.setValueViaCLIString("moa.clusterers.clustream.WithKmeans -h 10 -k 3 -m 10");
oates2.ccDataStreamFile.setValueViaCLIString(ccDataSetFileName);
oates2.betaWC.setValue(0.5);
oates2.betaCC.setValue(0.5);
oates2.period.setValue(1);
oates2.wcPastInstancesQueueSize.setValue(3);
oates2.prepareForUse();
oates2.resetLearning();
oatesNoCC = new OATES();
oatesNoCC.baseLearnerOption.setValueViaCLIString("moa.classifiers.lazy.kNN -k 1");
oatesNoCC.mappingFunctionOption.setValueViaCLIString("moa.classifiers.meta.SimpleLinearMappingFunction -r 0.1");
oatesNoCC.clustererOption.setValueViaCLIString("moa.clusterers.threshold.ProductivitySplitClusterer -t \"1.0;2.0\" -e " + effIndex + " -s " + sizeIndex);
oatesNoCC.ccDataStreamFile.setValueViaCLIString(ccDataSetFileNameEmpty);
oatesNoCC.betaWC.setValue(0.6);
oatesNoCC.betaCC.setValue(0.5);
oatesNoCC.period.setValue(1);
oatesNoCC.wcPastInstancesQueueSize.setValue(3);
oatesNoCC.prepareForUse();
oatesNoCC.resetLearning();
FileReader fr=null;
try {
fr = new FileReader(wcDataSetFileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dataset = new Instances(fr,6,effIndex+1); // WARNING!!! The "Instances" class is initialised with class attribute starting from 1, and later on in the code the class attribute of each instance is actually starting from 0
// It seems that the class ArffLoader is to blame for that.
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testResetLearning() {
assertTrue(oates.mappingFunctions != null);
assertTrue(oates.weights != null);
assertTrue(oates.learners != null);
assertTrue(oates.wcPastInstancesQueue != null);
assertTrue(oates.ccPastInstancesQueue != null);
// When using ProductivitySplitClusterer, the clusters will be initialized before training starts
assertEquals(3,oates.mappingFunctions.size());
assertEquals(4,oates.learners.size());
assertEquals(4,oates.weights.size());
assertEquals(0,oates.ccPastInstancesQueue.size());
assertEquals(0,oates.wcPastInstancesQueue.size());
assertFalse(oates.isCCClassIndexSet);
for (int i=0; i<3; ++i) {
assertEquals(oates.learners.get(i),((MappingFunction) oates.mappingFunctions.get(i)).getCCLearner());
}
assertTrue(oates2.clusterer.getClusteringResult() != null);
assertEquals(3,((moa.clusterers.clustream.WithKmeans) oates2.clusterer).kOption.getValue());
assertEquals(0,oates2.clusterer.getClusteringResult().size());
assertTrue(oates2.mappingFunctions != null);
assertTrue(oates2.weights != null);
assertTrue(oates2.learners != null);
assertTrue(oates2.wcPastInstancesQueue != null);
assertTrue(oates2.ccPastInstancesQueue != null);
// When using StremKM, the clusters will not be initialized before training starts
assertEquals(0,oates2.mappingFunctions.size());
assertEquals(1,oates2.learners.size());
assertEquals(1,oates2.weights.size());
assertEquals(0,oates2.ccPastInstancesQueue.size());
assertEquals(0,oates2.wcPastInstancesQueue.size());
assertFalse(oates2.isCCClassIndexSet);
assertFalse(oatesNoCC.ccDataStream.hasMoreInstances());
assertFalse(oatesNoCC.learners.get(0).trainingHasStarted());
assertFalse(oatesNoCC.learners.get(1).trainingHasStarted());
assertFalse(oatesNoCC.learners.get(2).trainingHasStarted());
}
public void testIndexClusterInstanceBelongsTo() {
Clustering clusters = oates.clusterer.getClusteringResult();
double [] atts = {1,2,3};
DenseInstance ccInst = new DenseInstance(1, atts);
assertEquals(1,oates.indexClusterInstanceBelongsTo(clusters, ccInst));
double []atts2 = {1,3,2};
ccInst = new DenseInstance(1, atts2);
assertEquals(0,oates.indexClusterInstanceBelongsTo(clusters, ccInst));
double []atts3 = {1,3,10};
ccInst = new DenseInstance(1, atts3);
assertEquals(2,oates.indexClusterInstanceBelongsTo(clusters, ccInst));
}
public void testTrainOnCCInstance() {
// The first tests are with dycom rather than dycom2. This means that the trainOnCCInstance method will not result in a new cluster being created
double [] attvals = {1,2,3,4};
DenseInstance ccInst = new DenseInstance(1, attvals);
ccInst.setDataset(dataset);
oates.trainOnCCInstance(ccInst);
assertEquals(3,oates.mappingFunctions.size());
assertEquals(4,oates.learners.size());
assertEquals(4,oates.weights.size());
ccInst.deleteAttributeAt(3);
// Delete attribute from dataset, as some base learners will make use of the information from the dataset.
List<Attribute> atts = null;
List<Integer> indexes = null;
AttributesInformation attinfo = null;
try {
InstanceInformation instanceinfo = (InstanceInformation) PrivateAccessor.getField(dataset, "instanceInformation");
attinfo = (AttributesInformation) PrivateAccessor.getField(instanceinfo, "attributesInformation");
atts = (List<Attribute>) PrivateAccessor.getField(attinfo, "attributes");
indexes = (List<Integer>) PrivateAccessor.getField(attinfo, "indexValues");
} catch (NoSuchFieldException e) {
System.err.println("Error while trying to delete attribute from dataset.");
e.printStackTrace();
}
Attribute attbk = atts.remove(atts.size()-1); // remove last attribute, which corresponds to the timestamp
Integer indexbk = indexes.remove(indexes.size()-1);
attinfo.setAttributes(atts, indexes);
assertTrue(oates.learners.get(1).trainingHasStarted());
assertEquals(3.0,oates.learners.get(1).getVotesForInstance(ccInst)[0]);
atts.add(attbk);
indexes.add(indexbk);
attinfo.setAttributes(atts, indexes);
attvals[0] = 1;
attvals[1] = 3;
attvals[2] = 2;
attvals[3] = 5;
ccInst = new DenseInstance(1, attvals);
ccInst.setDataset(dataset);
oates.trainOnCCInstance(ccInst);
assertEquals(3,oates.mappingFunctions.size());
assertEquals(4,oates.learners.size());
assertEquals(4,oates.weights.size());
ccInst.deleteAttributeAt(3);
// Delete attribute from dataset, as some base learners will make use of the information from the dataset.
atts = null;
indexes = null;
attinfo = null;
try {
InstanceInformation instanceinfo = (InstanceInformation) PrivateAccessor.getField(dataset, "instanceInformation");
attinfo = (AttributesInformation) PrivateAccessor.getField(instanceinfo, "attributesInformation");
atts = (List<Attribute>) PrivateAccessor.getField(attinfo, "attributes");
indexes = (List<Integer>) PrivateAccessor.getField(attinfo, "indexValues");
} catch (NoSuchFieldException e) {
System.err.println("Error while trying to delete attribute from dataset.");
e.printStackTrace();
}
attbk = atts.remove(atts.size()-1); // remove last attribute, which corresponds to the timestamp
indexbk = indexes.remove(indexes.size()-1);
attinfo.setAttributes(atts, indexes);
assertTrue(oates.learners.get(0).trainingHasStarted());
assertEquals(2.0,oates.learners.get(0).getVotesForInstance(ccInst)[0]);
atts.add(attbk);
indexes.add(indexbk);
attinfo.setAttributes(atts, indexes);
assertFalse(oates.learners.get(2).trainingHasStarted());
attvals[0] = 1.1;
attvals[1] = 3.1;
attvals[2] = 2.1;
attvals[3] = 5;
ccInst = new DenseInstance(1, attvals);
ccInst.setDataset(dataset);
ccInst.deleteAttributeAt(3);
// Delete attribute from dataset, as some base learners will make use of the information from the dataset.
atts = null;
indexes = null;
attinfo = null;
try {
InstanceInformation instanceinfo = (InstanceInformation) PrivateAccessor.getField(dataset, "instanceInformation");
attinfo = (AttributesInformation) PrivateAccessor.getField(instanceinfo, "attributesInformation");
atts = (List<Attribute>) PrivateAccessor.getField(attinfo, "attributes");
indexes = (List<Integer>) PrivateAccessor.getField(attinfo, "indexValues");
} catch (NoSuchFieldException e) {
System.err.println("Error while trying to delete attribute from dataset.");
e.printStackTrace();
}
attbk = atts.remove(atts.size()-1); // remove last attribute, which corresponds to the timestamp
indexbk = indexes.remove(indexes.size()-1);
attinfo.setAttributes(atts, indexes);
assertTrue(oates.learners.get(0).trainingHasStarted());
assertEquals(2.0,oates.learners.get(0).getVotesForInstance(ccInst)[0]);
atts.add(attbk);
indexes.add(indexbk);
attinfo.setAttributes(atts, indexes);
// The second set of tests are with dycom2. This means that the trainOnCCInstance method will result in a new cluster being created
attvals[0] = 1;
attvals[1] = 3;
attvals[2] = 2;
attvals[3] = 5;
ccInst = new DenseInstance(1, attvals);
ccInst.setDataset(dataset);
for (int i=0; i<20; ++i)
oates2.trainOnCCInstance(ccInst);
// There will be only one cluster, because the clustering algorithm has been trained with 20 copies of the same instance
assertEquals(1,oates2.mappingFunctions.size());
assertEquals(2,oates2.learners.size());
assertEquals(2,oates2.weights.size());
ccInst.deleteAttributeAt(3);
// Delete attribute from dataset, as some base learners will make use of the information from the dataset.
atts = null;
indexes = null;
attinfo = null;
try {
InstanceInformation instanceinfo = (InstanceInformation) PrivateAccessor.getField(dataset, "instanceInformation");
attinfo = (AttributesInformation) PrivateAccessor.getField(instanceinfo, "attributesInformation");
atts = (List<Attribute>) PrivateAccessor.getField(attinfo, "attributes");
indexes = (List<Integer>) PrivateAccessor.getField(attinfo, "indexValues");
} catch (NoSuchFieldException e) {
System.err.println("Error while trying to delete attribute from dataset.");
e.printStackTrace();
}
attbk = atts.remove(atts.size()-1); // remove last attribute, which corresponds to the timestamp
indexbk = indexes.remove(indexes.size()-1);
attinfo.setAttributes(atts, indexes);
int trainingStarted = -1;
for (int i=0; i<oates2.learners.size(); ++i) {
if(oates2.learners.get(i).trainingHasStarted())
trainingStarted = i;
}
assertTrue(trainingStarted != -1);
assertEquals(2.0,oates2.learners.get(trainingStarted).getVotesForInstance(ccInst)[0]);
atts.add(attbk);
indexes.add(indexbk);
attinfo.setAttributes(atts, indexes);
// Try to make k-means algorithm create all 3 clusters
for (int i=0; i<10; ++i) {
attvals[0] = i;
attvals[1] = i;
attvals[2] = i;
attvals[3] = i;
ccInst = new DenseInstance(1, attvals);
ccInst.setDataset(dataset);
oates2.trainOnCCInstance(ccInst);
}
assertEquals(3,oates2.mappingFunctions.size());
assertEquals(4,oates2.learners.size());
assertEquals(4,oates2.weights.size());
}
public void testUpdateWeights() {
for (int i=0; i<oates.weights.size(); ++i)
assertEquals(1.0, oates.weights.get(i));
double [] attvals = {1,2,3,4};
DenseInstance inst = new DenseInstance(1, attvals);
inst.setDataset(dataset);
oates.makeChecks(inst);
List<Attribute> atts = new ArrayList<Attribute>();
for (int i=0; i<inst.numAttributes()-1; ++i) {
atts.add(inst.attribute(i));
}
Instances datasetNoTimeStamp = new Instances(inst.dataset().getRelationName(), atts, 0);
datasetNoTimeStamp.setClassIndex(inst.classIndex());
inst.deleteAttributeAt(3);
inst.setDataset(datasetNoTimeStamp);
oates.wcPastInstancesQueue.add(inst);
oates.learners.get(1).trainOnInstance(inst);
attvals = new double[3];
attvals[0] = 5;
attvals[1] = 6;
attvals[2] = 7;
inst = new DenseInstance(1, attvals);
inst.setDataset(datasetNoTimeStamp);
oates.learners.get(0).trainOnInstance(inst);
oates.updateWeights();
assertEquals(1.0/1.5,oates.weights.get(1));
assertEquals(0.5/1.5,oates.weights.get(0));
assertEquals(1.0,oates.weights.get(2));
assertEquals(1.0,oates.weights.get(3));
attvals[0] = 10;
attvals[1] = 11;
attvals[2] = 12;
inst = new DenseInstance(1, attvals);
inst.setDataset(datasetNoTimeStamp);
oates.learners.get(3).trainOnInstance(inst);
oates.updateWeights();
assertEquals(1.0/2.1,oates.weights.get(1));
assertEquals(0.5/2.1,oates.weights.get(0));
assertEquals(0.6/2.1,oates.weights.get(3));
assertEquals(1.0,oates.weights.get(2));
}
public void testTrainMappingFunctions() {
for (int i=0; i<oates.mappingFunctions.size(); ++i)
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(i)).getB());
double [] attvals = {1,2,3,4};
DenseInstance inst = new DenseInstance(1, attvals);
inst.setDataset(dataset);
oates.makeChecks(inst);
List<Attribute> atts = new ArrayList<Attribute>();
for (int i=0; i<inst.numAttributes()-1; ++i) {
atts.add(inst.attribute(i));
}
Instances datasetNoTimeStamp = new Instances(inst.dataset().getRelationName(), atts, 0);
datasetNoTimeStamp.setClassIndex(inst.classIndex());
inst.deleteAttributeAt(3);
inst.setDataset(datasetNoTimeStamp);
oates.wcPastInstancesQueue.add(inst);
oates.learners.get(1).trainOnInstance(inst);
assertTrue(oates.learners.get(1).trainingHasStarted());
double []attvals2 = {10,20,30};
DenseInstance inst2 = new DenseInstance(1, attvals2);
inst2.setDataset(dataset);
oates.wcPastInstancesQueue.add(inst2);
oates.trainMappingFunctions();
assertEquals(1.9,((SimpleLinearMappingFunction)oates.mappingFunctions.get(1)).getB());
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(0)).getB());
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(2)).getB());
oates.learners.get(1).trainOnInstance(inst2);
oates.trainMappingFunctions();
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(1)).getB());
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(0)).getB());
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(2)).getB());
// wc instances will include only <15,25,35> and <150,250,350>, whereas CC lerner 1 will have been trained on <1,2,3> and <10,20,30>
oates.wcPastInstancesQueue.clear();
double []attvals3 = {15,25,35};
DenseInstance inst3 = new DenseInstance(1, attvals3);
inst3.setDataset(dataset);
oates.wcPastInstancesQueue.add(inst3);
double []attvals4 = {150,250,350};
DenseInstance inst4 = new DenseInstance(1, attvals4);
inst4.setDataset(dataset);
oates.wcPastInstancesQueue.add(inst4);
oates.trainMappingFunctions();
assertEquals(Math.round(2.216666666666667 * 1000000),Math.round(((SimpleLinearMappingFunction)oates.mappingFunctions.get(1)).getB() * 1000000));
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(0)).getB());
assertEquals(1.0,((SimpleLinearMappingFunction)oates.mappingFunctions.get(2)).getB());
}
public void testTrainOnInstanceImpl() {
oates.resetLearning();
assertEquals(4,oates.learners.size());
oates.trainOnInstanceImpl(wcDataStream.nextInstance().instance);
assertEquals(1,oates.wcPastInstancesQueue.size());
assertTrue(oates.learners.get(3).trainingHasStarted());
int numCCModelsTrained = 0, numMappingModelsTrained = 0;
for (int i=0; i<3; ++i) {
if (oates.learners.get(i).trainingHasStarted()) {
numCCModelsTrained++;
numMappingModelsTrained++;
}
}
// Only 1 CC model should have been trained, as only 1 CC instance had timestamp before the WC instance
assertEquals(1,numCCModelsTrained);
assertEquals(1,numMappingModelsTrained);
assertEquals(0.5/1.5,oates.weights.get(3));
oates.trainOnInstanceImpl(wcDataStream.nextInstance().instance);
assertEquals(2,oates.wcPastInstancesQueue.size());
assertTrue(oates.learners.get(3).trainingHasStarted());
int numCCInstsTrainingCCModel = 0, numCCInstsTrainingMappingModel = 0;
for (int i=0; i<3; ++i) {
if (oates.learners.get(i).trainingHasStarted()) {
numCCInstsTrainingCCModel += oates.learners.get(i).trainingWeightSeenByModel();
numCCInstsTrainingMappingModel += oates.mappingFunctions.get(i).trainingWeightSeenByModel();
}
}
// All 3 CC training instances should have been used for training, as all of them had timestamp before the WC instance
assertEquals(3,numCCInstsTrainingCCModel);
while (wcDataStream.hasMoreInstances()) {
Instance inst = wcDataStream.nextInstance().instance;
oates.trainOnInstanceImpl(inst);
assertEquals(3,oates.wcPastInstancesQueue.size());
assertEquals(inst.classValue(),oates.wcPastInstancesQueue.get(2).classValue());
}
numCCInstsTrainingCCModel = 0; numCCInstsTrainingMappingModel = 0;
for (int i=0; i<3; ++i) {
if (oates.learners.get(i).trainingHasStarted()) {
numCCInstsTrainingCCModel += oates.learners.get(i).trainingWeightSeenByModel();
numCCInstsTrainingMappingModel += oates.mappingFunctions.get(i).trainingWeightSeenByModel();
}
}
// No more than the previous 3 CC training instances should have been used for training, as there are no other CC instances
assertEquals(3,numCCInstsTrainingCCModel);
wcDataStream.restart();
while(wcDataStream.hasMoreInstances()) {
Instance inst = wcDataStream.nextInstance().instance;
inst.deleteAttributeAt(inst.numAttributes()-1);
assertEquals(inst.classValue(),oates.learners.get(3).getVotesForInstance(inst)[0]);
}
// Case where there is no cc instance
oatesNoCC.resetLearning();
wcDataStream.restart();
oatesNoCC.trainOnInstanceImpl(wcDataStream.nextInstance().instance);
assertTrue(oatesNoCC.learners.get(3).trainingHasStarted());
assertFalse(oatesNoCC.learners.get(0).trainingHasStarted());
assertFalse(oatesNoCC.learners.get(1).trainingHasStarted());
assertFalse(oatesNoCC.learners.get(2).trainingHasStarted());
for (int i=0; i<4; ++i)
assertEquals(1.0, oatesNoCC.weights.get(i));
}
public void testGetVotesForInstance() {
oates.resetLearning();
assertEquals(4,oates.learners.size());
double [] attvals = {1,2,3,4};
DenseInstance inst = new DenseInstance(1, attvals);
inst.setDataset(dataset);
oates.makeChecks(inst);
List<Attribute> atts = new ArrayList<Attribute>();
for (int i=0; i<inst.numAttributes()-1; ++i) {
atts.add(inst.attribute(i));
}
Instances datasetNoTimeStamp = new Instances(inst.dataset().getRelationName(), atts, 0);
datasetNoTimeStamp.setClassIndex(inst.classIndex());
Instance instNoTimeStamp = inst.copy();
instNoTimeStamp.deleteAttributeAt(3);
instNoTimeStamp.setDataset(datasetNoTimeStamp);
//dycom.wcPastInstancesQueue.add(inst);
// Train one CC model
oates.learners.get(1).trainOnInstance(instNoTimeStamp);
assertTrue(oates.learners.get(1).trainingHasStarted());
double [] attvals2 = {10,20,30};
DenseInstance instNoTimeStamp2 = new DenseInstance(1, attvals2);
instNoTimeStamp2.setDataset(datasetNoTimeStamp);
// Train the WC model
oates.learners.get(3).trainOnInstance(instNoTimeStamp2);
assertTrue(oates.learners.get(3).trainingHasStarted());
assertFalse(oates.learners.get(0).trainingHasStarted());
assertFalse(oates.learners.get(2).trainingHasStarted());
// Ensure that no further CC training will take place in OATES, besides the training done manually above
while(oates.ccDataStream.hasMoreInstances())
oates.ccDataStream.nextInstance();
// Add one instance to the wc queue, to be used to update OATES' weights when asking to get the votes
// The CC mapped model will give a correct prediction to this instance
// The WC model should have its weight multiplied by 0.6
// The new weights should be 1/1.6 for the CC model 1, and 0.6/1.6 for the WC model
oates.wcPastInstancesQueue.add(instNoTimeStamp);
assertEquals(Math.round((3 * 1/1.6 + 30 * 0.6/1.6)*1000000), Math.round(oates.getVotesForInstance(inst)[0]*1000000));
}
public void testTrainCCModels() {
// Case where all CC data should be used for training
double [] attvals = {1,2,3,4};
DenseInstance inst = new DenseInstance(1, attvals);
inst.setDataset(dataset);
oates.resetLearning();
oates.makeChecks(inst);
oates.trainCCModels(inst);
int numCCInstancesTrained = 0;
for (int i=0; i<oates.learners.size(); i++)
numCCInstancesTrained += oates.learners.get(i).trainingWeightSeenByModel();
assertEquals(3, numCCInstancesTrained);
assertEquals(0, oates.ccPastInstancesQueue.size());
// Case where only 1 CC instance should be used for training
double [] attvals2 = {1,2,3,2};
DenseInstance inst2 = new DenseInstance(1, attvals2);
inst2.setDataset(dataset);
oates.resetLearning();
oates.makeChecks(inst2);
oates.trainCCModels(inst2);
numCCInstancesTrained = 0;
for (int i=0; i<oates.learners.size(); i++)
numCCInstancesTrained += oates.learners.get(i).trainingWeightSeenByModel();
assertEquals(1, numCCInstancesTrained);
assertEquals(1, oates.ccPastInstancesQueue.size());
assertTrue(oates.ccDataStream.hasMoreInstances());
// Case where only 0 CC instances should be used for training
double [] attvals3 = {1,2,3,0};
DenseInstance inst3 = new DenseInstance(1, attvals3);
inst3.setDataset(dataset);
oates.resetLearning();
oates.makeChecks(inst3);
oates.trainCCModels(inst3);
numCCInstancesTrained = 0;
for (int i=0; i<oates.learners.size(); i++)
numCCInstancesTrained += oates.learners.get(i).trainingWeightSeenByModel();
assertEquals(0, numCCInstancesTrained);
assertEquals(1, oates.ccPastInstancesQueue.size());
assertTrue(oates.ccDataStream.hasMoreInstances());
// Case where there are no CC instances
oatesNoCC.resetLearning();
oatesNoCC.makeChecks(inst);
assertFalse(oatesNoCC.ccDataStream.hasMoreInstances());
oatesNoCC.trainCCModels(inst);
assertFalse(oatesNoCC.learners.get(0).trainingHasStarted());
assertFalse(oatesNoCC.learners.get(1).trainingHasStarted());
assertFalse(oatesNoCC.learners.get(2).trainingHasStarted());
assertEquals(1.0, oatesNoCC.weights.get(3));
}
}