0001 classdef (ConstructOnLoad = true) CBR < handle
0002
0003
0004
0005 properties (Access = private)
0006
0007
0008
0009 Model;
0010 Case_Base;
0011
0012
0013
0014
0015 vk;
0016 dc;
0017 method;
0018
0019
0020
0021
0022 block;
0023
0024
0025
0026 test;
0027 veins;
0028 classepredit;
0029
0030 param;
0031
0032 treuoutliers;
0033 end
0034
0035 methods
0036
0037 function obj=CBR(varargin)
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 if nargin == 0
0073 obj.Case_Base=[];
0074
0075
0076 obj.vk=[];
0077 obj.dc=[];
0078 obj.method=[];
0079
0080 obj.block =0;
0081
0082 obj.test = [];
0083 obj.veins=[];
0084 obj.classepredit=[];
0085 obj.param=[];
0086 else
0087
0088 model = varargin{1};
0089 C_B = varargin{2};
0090
0091 if nargin == 2
0092 resp = 'NO';
0093 else
0094 resp = varargin{3};
0095 end
0096 z=whos('model');
0097 if(not(strcmp(z.class,'PCA')))
0098
0099 error('CBR\CBR: model parameter must be an object of type PCA');
0100 end
0101
0102 z=whos('C_B');
0103 if(not(strcmp(z.class,'DATA')))
0104
0105 error('CBR\CBR: C_B parameter must be an object of type DATA');
0106 end
0107
0108 obj.Model=model.copy();
0109
0110 z=whos('resp');
0111 if strcmp(z.class,'char')
0112 obj.treuoutliers = resp;
0113 else
0114 error('CBR\CBR: resp parameter must be char type');
0115 end
0116
0117 outs = obj.Model.getOutLiers();
0118
0119 while(sum(outs) > 0)
0120 if isempty(obj.treuoutliers)
0121 obj.treuoutliers = questdlg('There are outliers do you want to remove them?','Attention!','YES','NO','PLOT','YES');
0122 end
0123 switch obj.treuoutliers
0124 case ''
0125 outs = 1;
0126 obj.treuoutliers=[];
0127 case 'YES'
0128 obj.Model.deleteBatch(outs);
0129 outs=obj.Model.getOutLiers();
0130 case 'NO'
0131 outs = 0;
0132 case 'PLOT'
0133 obj.Model.plotOutLiers();
0134 outs = 1;
0135 obj.treuoutliers=[];
0136 otherwise
0137 error('CBR\CBR: resp parameter must be char type and valid ones are: YES, NO, PLOT');
0138 end
0139
0140 end
0141
0142
0143
0144
0145 obj.Case_Base=obj.Model.project(C_B.copy());
0146
0147
0148 obj.vk=[];
0149 obj.dc=[];
0150 obj.method=[];
0151
0152 obj.block =0;
0153
0154 obj.test = [];
0155 obj.veins=[];
0156 obj.classepredit=[];
0157 obj.param=[];
0158
0159 end
0160 end
0161
0162 function objo=copy(obj)
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 objo = CBR();
0178
0179
0180
0181 objo.Model = obj.Model.copy();
0182 objo.Case_Base = obj.Case_Base.copy();
0183
0184 objo.vk = obj.vk;
0185 objo.dc = obj.dc;
0186 objo.method = obj.method;
0187
0188 objo.block = obj.block;
0189
0190 objo.test = obj.test;
0191 objo.veins = obj.veins;
0192 objo.classepredit = obj.classepredit;
0193
0194 objo.param = obj.param;
0195
0196 objo.treuoutliers = obj.treuoutliers;
0197 end
0198
0199
0200 function m=getCaseBase(obj)
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 m=obj.Case_Base;
0214 end
0215 function model=getModel(obj)
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226 model=obj.Model.copy;
0227 end
0228 function out = isOptimized(obj)
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240 out=obj.block;
0241 end
0242 function out = getVK(obj)
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 out = obj.vk;
0262 end
0263 function out = getDC(obj)
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 out=obj.dc;
0281 end
0282 function out = getMethod(obj)
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 out=obj.method;
0300 end
0301 function out = getParam(obj)
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319 out=obj.param;
0320 end
0321
0322
0323 function addCase(obj,dad,class,nom)
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341 obj.Case_Base.addBatch(dad,class,nom);
0342
0343
0344
0345 obj.test = [];
0346 obj.veins=[];
0347 obj.classepredit=[];
0348 end
0349 function deleteCase(obj,i)
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363 obj.Case_Base.deleteBatch(i);
0364
0365
0366
0367 obj.test = [];
0368 obj.veins=[];
0369 obj.classepredit=[];
0370 end
0371 function [dad clas nom] = getCase(obj,i)
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390 [dad clas nom] = obj.Case_Base.getBatch(i);
0391 end
0392
0393
0394 function addCaseToModel(obj,dad,class,nom)
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412 if(obj.block == 0)
0413 obj.Model.addBatch(dad,class,nom);
0414
0415
0416 obj.test = [];
0417 obj.veins=[];
0418 obj.classepredit=[];
0419
0420
0421 obj.Case_Base=obj.Model.project(obj.Case_Base.getDATA());
0422
0423
0424 outs = obj.Model.getOutLiers();
0425
0426 while(sum(outs) > 0)
0427 if isempty(obj.treuoutliers)
0428 obj.treuoutliers = questdlg('There are outliers do you want to remove them?','Attention!','YES','NO','PLOT','YES');
0429 end
0430 switch obj.treuoutliers
0431 case ''
0432 outs = 1;
0433 obj.treuoutliers=[];
0434 case 'YES'
0435 obj.Model.deleteBatch(outs);
0436 outs=obj.Model.getOutLiers();
0437 case 'NO'
0438 outs = 1;
0439 obj.treuoutliers=[];
0440 case 'PLOT'
0441 obj.Model.plotOutLiers();
0442 end
0443
0444 end
0445
0446 else
0447 error('CBR\addCaseToModel: You can''t change your model if you made an optimitzation .');
0448 end
0449 end
0450 function deleteCaseFromModel(obj,i)
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462 if(obj.block == 0)
0463 obj.Model.deleteBatch(i);
0464
0465
0466
0467 obj.test = [];
0468 obj.veins=[];
0469 obj.classepredit=[];
0470
0471
0472 obj.Case_Base=obj.Model.project(obj.Case_Base.getDATA());
0473
0474 outs = obj.Model.getOutLiers();
0475
0476 while(sum(outs) > 0)
0477 if isempty(obj.treuoutliers)
0478 obj.treuoutliers = questdlg('There are outliers do you want to remove them?','Attention!','YES','NO','PLOT','YES');
0479 end
0480 switch obj.treuoutliers
0481 case ''
0482 outs = 1;
0483 obj.treuoutliers=[];
0484 case 'YES'
0485 obj.Model.deleteBatch(outs);
0486 outs=obj.Model.getOutLiers();
0487 case 'NO'
0488 outs = 0;
0489 case 'PLOT'
0490 obj.Model.plotOutLiers();
0491 outs = 1;
0492 obj.treuoutliers=[];
0493 end
0494
0495 end
0496 else
0497 error('CBR\addCaseToModel: You can''t change your model if you made an optimitzation .');
0498 end
0499
0500 end
0501
0502
0503
0504 function [index Test_set]= retrieve(obj,dades,ivk,idc,iparam)
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548 z=whos('dades');
0549 if(not(strcmp(z.class,'DATA')))
0550
0551 error('CBR\retrieve: dades parameter must be an object of type DATA');
0552 end
0553
0554
0555 if nargin >2
0556
0557 z=whos('ivk');
0558 if(not(strcmp(z.class,'double')))
0559
0560 error('CBR\retrieve: The parameter ivk must be of type Double');
0561 end
0562
0563 if (obj.block ==1) && sum(obj.vk ~= ivk)~=0
0564 error('CBR\retrieve: vk parameter must not be changed when the case base has been optimited. Remember, is not necesary to call the method retreave with all the parameters.');
0565 else
0566 obj.vk=ivk;
0567 end
0568
0569
0570 z=whos('idc');
0571 if(not(strcmp(z.class,'double')))
0572
0573 error('CBR\retrieve: dc parameter must be of type Double');
0574 end
0575 if (obj.block ==1) && (obj.dc~=idc)
0576 error('CBR\retrieve: dc parameter must not be changed when the case base has been optimited. Remember, is not necesary to call the method retreave with all the parameters.');
0577 else
0578 obj.dc=idc;
0579 end
0580
0581 if obj.dc ==17 || obj.dc == 18
0582 if obj.block == 1 && obj.param~=iparam
0583 error('CBR\reuse: This parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method reuse with all the parameters.');
0584 else
0585 if exist('iparam','var')
0586 obj.param=iparam;
0587 else
0588 error('CBR\reuse:This distance needs an extra parameter!');
0589 end
0590 end
0591 end
0592 end
0593
0594 if isempty(obj.vk)
0595 error('CBR\retrieve: vk parameter never had a value. Remember, you can not use the short form.');
0596 end
0597 if isempty(obj.dc)
0598 error('CBR\retrieve: dc parameter never had a value. Remember, you can not use the short form.');
0599 end
0600
0601
0602 if obj.dc ==17 || obj.dc == 18
0603 if isempty(obj.param)
0604
0605 error('CBR\reuse:This distance needs an extra parameter!');
0606 end
0607 end
0608
0609 obj.classepredit=[];
0610
0611
0612 if nargout ~= 0
0613
0614 obj.test = obj.Model.project(dades);
0615 Test_set = obj.test;
0616
0617
0618
0619 obj.vk(obj.vk>obj.Case_Base.getBatchCount())=obj.Case_Base.getBatchCount();
0620
0621 CBR_retrieve(obj);
0622
0623
0624
0625
0626
0627
0628
0629 index = obj.veins.indexs_k2;
0630 else
0631
0632 obj.test = obj.Model.project(dades);
0633
0634
0635
0636 obj.vk(obj.vk>obj.Case_Base.getBatchCount())=obj.Case_Base.getBatchCount();
0637
0638 CBR_retrieve(obj);
0639 end
0640
0641 end
0642 function classe = reuse(obj,imethod)
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668 if nargin > 1
0669
0670 z=whos('imethod');
0671 if(not(strcmp(z.class,'double')))
0672
0673 error('CBR\reuse: imethod parameter must be of type Double.');
0674 end
0675 if obj.block ==1 && obj.method~=imethod
0676 error('CBR\reuse: method parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method reuse with all the parameters.');
0677 else
0678 obj.method=imethod;
0679 end
0680
0681 end
0682
0683
0684 if isempty(obj.method)
0685
0686 error('CBR\retrieve: method parameter never had a value. Remember, you can not use the short form.');
0687 end
0688
0689
0690 if isempty(obj.test)
0691
0692 error('CBR\reuse:You can''t use reuse. Use retrieve first!!');
0693 end
0694
0695
0696
0697
0698 CBR_reuse(obj);
0699
0700
0701 if nargout >0
0702 classe=obj.classepredit;
0703 end
0704 end
0705 function [matri fallen]=revise(obj)
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723 if isempty(obj.test)
0724
0725 error('CBR\revise: You can''t use revise. Use retrieve first!');
0726 end
0727 if isempty(obj.classepredit)
0728
0729 error('CBR\revise: You can''t use revise. Use reuse first!');
0730 end
0731
0732
0733
0734 [matri fallen]=CBR_revise(obj);
0735 end
0736 function retain(obj,fallen)
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750 obj.CBR_retain(fallen);
0751 end
0752
0753
0754 function IBUdG(obj,nelem,descarta,ivk,idc,imethod,iparam)
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816 if nargin >3
0817 z=whos('ivk');
0818 if(not(strcmp(z.class,'double')))
0819
0820 error('CBR\IBUdG: vk parameter must be of type Double.');
0821 end
0822 if (obj.block ==1) && sum(obj.vk ~= ivk)~=0
0823 error('CBR\IBUdG: vk parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
0824 else
0825 obj.vk=ivk;
0826 end
0827
0828 z=whos('idc');
0829 if(not(strcmp(z.class,'double')))
0830
0831 error('CBR\IBUdG: dc parameter must be of type Double.');
0832 end
0833 if obj.block ==1 && obj.dc~=idc
0834 error('CBR\IBUdG: dc parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
0835 else
0836 obj.dc=idc;
0837 end
0838
0839 z=whos('imethod');
0840 if(not(strcmp(z.class,'double')))
0841
0842 error('CBR\IBUdG: method parameter must be of type Double.');
0843 end
0844 if obj.block ==1 && obj.method~=imethod
0845 error('CBR\IBUdG: method parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
0846 else
0847 obj.method=imethod;
0848 end
0849
0850 if obj.dc ==17 || obj.dc == 18
0851 if obj.block == 1 && obj.param~=iparam
0852 error('CBR\IBUdG: This parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method reuse with all the parameters.');
0853 else
0854 if exist('iparam','var')
0855 obj.param=iparam;
0856 else
0857 error('CBR\IBUdG:This distance needs an extra parameter!');
0858 end
0859 end
0860 end
0861 end
0862
0863
0864 if obj.dc ==17 || obj.dc == 18
0865 if isempty(obj.param)
0866
0867 error('CBR\IBUdG:This distance needs an extra parameter!');
0868 end
0869 end
0870
0871 if isempty(obj.vk) || isempty(obj.dc) || isempty(obj.method)
0872 error('CBR\IBUdG: At least 1 of them (vk , dc , method) never had a value. You can''t use the short form.');
0873 end
0874
0875 obj.block=1;
0876 Calcula_IB_UdG(obj,nelem,descarta);
0877 end
0878 function IB3(obj,ivk,idc,imethod,iparam)
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929
0930
0931
0932
0933
0934
0935
0936 if nargin >1
0937 z=whos('ivk');
0938 if(not(strcmp(z.class,'double')))
0939
0940 error('CBR\IB3: vk parameter must be of type Double.');
0941 end
0942 if (obj.block ==1) && sum(obj.vk ~= ivk)~=0
0943 error('CBR\IB3: vk parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
0944 else
0945 obj.vk=ivk;
0946 end
0947
0948 z=whos('idc');
0949 if(not(strcmp(z.class,'double')))
0950
0951 error('CBR\IB3: dc parameter must be of type Double.');
0952 end
0953 if obj.block ==1 && obj.dc~=idc
0954 error('CBR\IB3: dc parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
0955 else
0956 obj.dc=idc;
0957 end
0958
0959 z=whos('imethod');
0960 if(not(strcmp(z.class,'double')))
0961
0962 error('CBR\IB3: method parameter must be of type Double.');
0963 end
0964 if obj.block ==1 && obj.method~=imethod
0965 error('CBR\IB3: method parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
0966 else
0967 obj.method=imethod;
0968 end
0969
0970 if obj.dc ==17 || obj.dc == 18
0971 if obj.block == 1 && obj.param~=iparam
0972 error('CBR\IB3: This parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method reuse with all the parameters.');
0973 else
0974 if exist('iparam','var')
0975 obj.param=iparam;
0976 else
0977 error('CBR\IB3:This distance needs an extra parameter!');
0978 end
0979 end
0980 end
0981 end
0982
0983 if isempty(obj.vk) || isempty(obj.dc) || isempty(obj.method)
0984 error('CBR\IB3: At least 1 of them (vk , dc , method) never had a value. You can''t use the short form.');
0985 end
0986
0987
0988 if obj.dc ==17 || obj.dc == 18
0989 if isempty(obj.param)
0990
0991 error('CBR\IB2:This distance needs an extra parameter!');
0992 end
0993 end
0994 obj.block=1;
0995
0996 Calcula_IB3(obj)
0997 end
0998 function IB2(obj,ivk,idc,imethod,iparam)
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045 if nargin >1
1046 z=whos('ivk');
1047 if(not(strcmp(z.class,'double')))
1048
1049 error('CBR\IB2: vk parameter must be of type Double.');
1050 end
1051 if (obj.block ==1) && sum(obj.vk ~= ivk)~=0
1052 error('CBR\IB2: vk parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
1053 else
1054 obj.vk=ivk;
1055 end
1056
1057 z=whos('idc');
1058 if(not(strcmp(z.class,'double')))
1059
1060 error('CBR\IB2: dc parameter must be of type Double.');
1061 end
1062 if obj.block ==1 && obj.dc~=idc
1063 error('CBR\IB2: dc parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
1064 else
1065 obj.dc=idc;
1066 end
1067 z=whos('imethod');
1068 if(not(strcmp(z.class,'double')))
1069
1070 error('CBR\IB2: method parameter must be of type Double.');
1071 end
1072 if obj.block ==1 && obj.method~=imethod
1073 error('CBR\IB2: method parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
1074 else
1075 obj.method=imethod;
1076 end
1077
1078 if obj.dc ==17 || obj.dc == 18
1079 if obj.block == 1 && obj.param~=iparam
1080 error('CBR\IB2: This parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method reuse with all the parameters.');
1081 else
1082 if exist('iparam','var')
1083 obj.param=iparam;
1084 else
1085 error('CBR\IB2:This distance needs an extra parameter!');
1086 end
1087 end
1088 end
1089 end
1090
1091 if isempty(obj.vk) || isempty(obj.dc) || isempty(obj.method)
1092 error('CBR\IB2: At least 1 of them (vk , dc , method) never had a value. You can''t use the short form.');
1093 end
1094
1095 if obj.dc ==17 || obj.dc == 18
1096 if isempty(obj.param)
1097
1098 error('CBR\IB2:This distance needs an extra parameter!');
1099 end
1100 end
1101
1102 Calcula_IB2(obj);
1103 obj.block=1;
1104 end
1105 function DROP4(obj,ivk,idc,imethod,iparam)
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150 if nargin >1
1151 z=whos('ivk');
1152 if(not(strcmp(z.class,'double')))
1153
1154 error('CBR\DROP4: vk parameter must be of type Double.');
1155 end
1156 if (obj.block ==1) && sum(obj.vk ~= ivk)~=0
1157 error('CBR\DROP4: vk parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
1158 else
1159 obj.vk=ivk;
1160 end
1161
1162 z=whos('idc');
1163 if(not(strcmp(z.class,'double')))
1164
1165 error('CBR\DROP4: dc parameter must be of type Double.');
1166 end
1167 if obj.block ==1 && obj.dc~=idc
1168 error('CBR\DROP4: dc parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
1169 else
1170 obj.dc=idc;
1171 end
1172 z=whos('imethod');
1173 if(not(strcmp(z.class,'double')))
1174
1175 error('CBR\DROP4: method parameter must be of type Double.');
1176 end
1177 if obj.block ==1 && obj.method~=imethod
1178 error('CBR\DROP4: method parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method IBUdG with all the parameters, you can use the short form.');
1179 else
1180 obj.method=imethod;
1181 end
1182
1183 if obj.dc ==17 || obj.dc == 18
1184 if obj.block == 1 && obj.param~=iparam
1185 error('CBR\DROP4: This parameter must not be changed when the case base has been optimited. Remember that is not necessary to call the method reuse with all the parameters.');
1186 else
1187 if exist('iparam','var')
1188 obj.param=iparam;
1189 else
1190 error('CBR\DROP4:This distance needs an extra parameter!');
1191 end
1192 end
1193 end
1194 end
1195
1196 if isempty(obj.vk) || isempty(obj.dc) || isempty(obj.method)
1197 error('CBR\DROP4: At least 1 of them (vk , dc , method) never had a value. You can''t use the short form.');
1198 end
1199
1200
1201 if obj.dc ==17 || obj.dc == 18
1202 if isempty(obj.param)
1203
1204 error('CBR\DROP4:This distance needs an extra parameter!');
1205 end
1206 end
1207 obj.block=1;
1208
1209 ordena(obj);
1210 calcula_DROP4(obj);
1211 end
1212
1213
1214
1215
1216
1217
1218 function openvar(nom,~)
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229 x=whos('nom');
1230 if strcmp(x.class,'CBR')
1231
1232 else
1233 if(evalin('base',['size(' nom ');']) == [1 1])
1234 Menu_CBR(nom)
1235 end
1236 end
1237 end
1238
1239 function disp(obj)
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249 disp('Model')
1250 obj.Model.disp()
1251
1252 disp(' ')
1253
1254 disp('Case Base')
1255 obj.Case_Base.disp()
1256
1257 end
1258
1259 function s = saveobj(obj)
1260
1261
1262
1263
1264
1265
1266
1267 s.Model = obj.Model;
1268 s.Case_Base = obj.Case_Base;
1269
1270 s.vk = obj.vk;
1271 s.dc = obj.dc;
1272 s.method = obj.method;
1273
1274 s.block = obj.block;
1275
1276 s.test = obj.test;
1277 s.veins = obj.veins;
1278 s.classepredit = obj.classepredit;
1279
1280 s.param = obj.param;
1281
1282 s.treuoutliers = obj.treuoutliers;
1283 end
1284 end
1285
1286 methods (Static)
1287 function obj = loadobj(obj)
1288
1289
1290
1291
1292
1293
1294 if isstruct(obj)
1295
1296 newObj = CBR();
1297
1298 newObj.Model = obj.Model;
1299 newObj.Case_Base = obj.Case_Base;
1300
1301 newObj.vk = obj.vk;
1302 newObj.dc = obj.dc;
1303 newObj.method = obj.method;
1304
1305 newObj.block = obj.block;
1306
1307 newObj.test = obj.test;
1308 newObj.veins = obj.veins;
1309 newObj.classepredit = obj.classepredit;
1310
1311 newObj.param = obj.param;
1312
1313 newObj.treuoutliers = obj.treuoutliers;
1314 obj = newObj;
1315 end
1316 end
1317 end
1318
1319 methods (Access = private)
1320
1321 function CBR_retrieve(obj)
1322
1323
1324 switch (obj.dc)
1325 case 1
1326
1327 testgT=obj.test.getT();
1328 CBgT=obj.Case_Base.getT();
1329
1330
1331
1332 k = min(obj.vk);
1333
1334 distancia = zeros(size(CBgT,1),size(testgT,1));
1335
1336 rep = repmat(obj.Model.getLambda()',size(CBgT,1),1);
1337
1338 for i = 1:size(testgT,1)
1339 distancia(:,i) = sqrt(sum(((CBgT - repmat(testgT(i,:),size(CBgT,1),1) ).^2 ./ rep),2));
1340 end
1341
1342 [ordenats_k1,indexs_k1] = sort(distancia,1);
1343
1344 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1345 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1346 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1347 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1348
1349 case 2
1350
1351 tegT2=obj.test.getT2();
1352 CBgT2=obj.Case_Base.getT2();
1353
1354 k = min(obj.vk);
1355
1356
1357 distancia = zeros(length(CBgT2),length(tegT2));
1358
1359 for i = 1:length(tegT2)
1360 distancia(:,i) = abs(CBgT2 - repmat(tegT2(i),length(CBgT2),1));
1361 end
1362
1363 [ordenats_k1,indexs_k1] = sort(distancia,1);
1364
1365 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1366 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1367 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1368 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1369
1370 case 3
1371
1372 tegQ=obj.test.getQ();
1373 CBgQ=obj.Case_Base.getQ;
1374
1375 k = min(obj.vk);
1376
1377 distancia = zeros(length(CBgQ),length(tegQ));
1378
1379 for i = 1:length(tegQ)
1380 distancia(:,i) = abs(CBgQ - repmat(tegQ(i),length(CBgQ),1));
1381 end
1382
1383 [ordenats_k1,indexs_k1] = sort(distancia,1);
1384
1385 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1386 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1387 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1388 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1389
1390 case 4
1391
1392 tegQ=obj.test.getQ();
1393 CBgQ=obj.Case_Base.getQ;
1394 tegT2=obj.test.getT2();
1395 CBgT2=obj.Case_Base.getT2();
1396
1397
1398 k1 = obj.vk(1); k2 = obj.vk(2);
1399 if k2 > k1
1400 k2=k1;
1401 end
1402
1403 distancia1 = zeros(length(CBgQ),length(tegQ));
1404 distancia2 = zeros(k1,length(tegQ));
1405
1406 for i = 1:length(tegQ)
1407 distancia1(:,i) = abs(CBgQ - repmat(tegQ(i),length(CBgQ),1));
1408 end
1409
1410 [ordenats_k1,indexs_k1] = sort(distancia1,1);
1411
1412 obj.veins.ordenats_k1 = ordenats_k1(1:k1,:);
1413 obj.veins.indexs_k1 = indexs_k1(1:k1,:);
1414
1415
1416 for i = 1:length(tegT2)
1417 casos_restants = CBgT2(indexs_k1(1:k1,i));
1418 distancia2(:,i) = abs(casos_restants - repmat(tegT2(i),k1,1));
1419 end
1420
1421 [ordenats_k2,indexs_k2] = sort(distancia2,1);
1422
1423 obj.veins.indexs_k2 = zeros(k2,length(tegT2));
1424 for i = 1:length(tegT2)
1425 obj.veins.indexs_k2(:,i) = indexs_k1(indexs_k2(1:k2,i),i);
1426 end
1427 obj.veins.ordenats_k2 = ordenats_k2(1:k2,:);
1428
1429 case 5
1430
1431 CBgQ=obj.Case_Base.getQ;
1432 CBgT=obj.Case_Base.getT();
1433 tegT=obj.test.getT;
1434 tegQ=obj.test.getQ();
1435
1436 k1 = obj.vk(1); k2 = obj.vk(2);
1437 if k2 > k1
1438 k2=k1;
1439 end
1440
1441 distancia1 = zeros(length(CBgQ),length(tegQ));
1442 distancia2 = zeros(k1,size(tegT,1));
1443
1444 for i = 1:length(tegQ)
1445 distancia1(:,i) = abs(CBgQ - repmat(tegQ(i),length(CBgQ),1));
1446 end
1447
1448 [ordenats_k1,indexs_k1] = sort(distancia1,1);
1449
1450 obj.veins.ordenats_k1 = ordenats_k1(1:k1,:);
1451 obj.veins.indexs_k1 = indexs_k1(1:k1,:);
1452
1453 rep=repmat(obj.Model.getLambda()',k1,1);
1454 for i = 1:size(tegT,1)
1455 casos_restants = CBgT(indexs_k1(1:k1,i),:);
1456 distancia2(:,i) = sqrt(sum(((casos_restants - repmat(tegT(i,:),k1,1)).^2) ./ rep ,2));
1457 end
1458
1459 [ordenats_k2,indexs_k2] = sort(distancia2,1);
1460
1461 obj.veins.indexs_k2 = zeros(k2,length(tegQ));
1462 for i = 1:size(tegT,1)
1463 obj.veins.indexs_k2(:,i) = indexs_k1(indexs_k2(1:k2,i),i);
1464 end
1465 obj.veins.ordenats_k2 = ordenats_k2(1:k2,:);
1466
1467 case 6
1468
1469 CBgT2=obj.Case_Base.getT2;
1470 tegT2=obj.test.getT2;
1471
1472 k1 = obj.vk(1); k2 = obj.vk(2);
1473 if k2 > k1
1474 k2=k1;
1475 end
1476
1477 distancia1 = zeros(length(CBgT2),length(tegT2));
1478 distancia2 = zeros(k1,size(tegT2,1));
1479
1480 for i = 1:length(tegT2)
1481 distancia1(:,i) = abs(CBgT2 - repmat(tegT2(i),length(CBgT2),1));
1482 end
1483
1484 [ordenats_k1,indexs_k1] = sort(distancia1,1);
1485
1486 obj.veins.ordenats_k1 = ordenats_k1(1:k1,:);
1487 obj.veins.indexs_k1 = indexs_k1(1:k1,:);
1488
1489 CBgT=obj.Case_Base.getT();
1490 tegT=obj.test.getT;
1491
1492 aux=CBgT(indexs_k1,:);
1493 rep=repmat(obj.Model.getLambda()',k1,1);
1494 for i = 1:size(tegT,1)
1495 casos_restants = aux(indexs_k1(1:k1,i),:);
1496 distancia2(:,i) = sqrt(sum((casos_restants - repmat(tegT(i,:),k1,1)).^2 ./ rep ,2) );
1497 end
1498
1499 [ordenats_k2,indexs_k2] = sort(distancia2,1);
1500
1501 obj.veins.indexs_k2 = zeros(k2,length(tegT2));
1502 for i = 1:size(tegT,1)
1503 obj.veins.indexs_k2(:,i) = indexs_k1(indexs_k2(1:k2,i),i);
1504 end
1505 obj.veins.ordenats_k2 = ordenats_k2(1:k2,:);
1506
1507 case 7
1508
1509 tegcQ=obj.test.getQContribution();
1510 CBgcQ=obj.Case_Base.getQContribution();
1511
1512 k = min(obj.vk);
1513
1514 d1=size(CBgcQ,1);
1515 distancia = zeros(d1,size(tegcQ,1));
1516
1517
1518 for i = 1:size(tegcQ,1)
1519 distancia(:,i) = sqrt( sum( (CBgcQ - repmat(tegcQ(i,:),[d1 1]) ).^2, 2));
1520 end
1521
1522
1523 [ordenats_k1,indexs_k1] = sort(distancia,1);
1524
1525 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1526 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1527 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1528 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1529
1530 case 8
1531
1532 tegcT2=obj.test.getT2Contribution();
1533 CBgcT2=obj.Case_Base.getT2Contribution();
1534
1535 k = min(obj.vk);
1536
1537 d1=size(CBgcT2,1);
1538 distancia = zeros(d1,size(tegcT2,1));
1539
1540
1541 for i = 1:size(tegcT2,1)
1542 distancia(:,i) = sqrt( sum( (CBgcT2 - repmat(tegcT2(i,:),[d1 1]) ).^2, 2));
1543 end
1544
1545
1546
1547 [ordenats_k1,indexs_k1] = sort(distancia,1);
1548
1549 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1550 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1551 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1552 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1553
1554 case 9
1555
1556 tegcQ=obj.test.getQContribution();
1557 CBgcQ=obj.Case_Base.getQContribution();
1558
1559 k = min(obj.vk);
1560
1561 d1=size(CBgcQ,1);
1562 distancia = zeros(d1,size(tegcQ,1));
1563
1564
1565 if(obj.Case_Base.getBatchCount()<2)
1566
1567
1568 for i = 1:size(tegcQ,1)
1569 distancia(:,i) = sqrt(sum(CBgcQ.^2 - repmat(tegcQ(i,:).^2,[d1 1]),2));
1570 end
1571 else
1572
1573 rep = (repmat(obj.Case_Base.getQContributionLimitStd(),[d1 1])).^2;
1574 for i = 1:size(tegcQ,1)
1575 distancia(:,i) =sqrt( sum( (CBgcQ - repmat(tegcQ(i,:), [d1 1])).^2 ./ rep, 2) );
1576
1577 end
1578 end
1579
1580 [ordenats_k1,indexs_k1] = sort(distancia,1);
1581
1582 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1583 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1584 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1585 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1586
1587 case 10
1588
1589 tegcQ=obj.test.getT2Contribution();
1590 CBgcQ=obj.Case_Base.getT2Contribution();
1591
1592 k = min(obj.vk);
1593
1594 d1=size(CBgcQ,1);
1595 distancia = zeros(d1,size(tegcQ,1));
1596
1597
1598 if(obj.Case_Base.getBatchCount()<2)
1599
1600
1601 for i = 1:size(tegcQ,1)
1602 distancia(:,i) = sqrt(sum(CBgcQ.^2 - repmat(tegcQ(i,:).^2,[d1 1]),2));
1603 end
1604 else
1605
1606 rep = (repmat(obj.Case_Base.getT2ContributionLimitStd(),[d1 1])).^2;
1607 for i = 1:size(tegcQ,1)
1608 distancia(:,i) =sqrt( sum( (CBgcQ - repmat(tegcQ(i,:), [d1 1])).^2 ./ rep, 2) );
1609
1610 end
1611 end
1612
1613 [ordenats_k1,indexs_k1] = sort(distancia,1);
1614
1615 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1616 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1617 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1618 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1619
1620 case 11
1621
1622 tegT2=obj.test.getT2();
1623 CBgT2=obj.Case_Base.getT2();
1624 tegQ=obj.test.getQ();
1625 CBgQ=obj.Case_Base.getQ();
1626
1627 k = min(obj.vk);
1628
1629 distancia1 = zeros(length(CBgT2),length(tegT2));
1630
1631
1632 for i = 1:length(tegT2)
1633 distancia1(:,i) = abs(CBgT2 - repmat(tegT2(i),length(CBgT2),1));
1634 end
1635
1636
1637
1638 distancia2 = zeros(length(CBgQ),length(tegQ));
1639
1640 for i = 1:length(tegQ)
1641 distancia2(:,i) = abs(CBgQ - repmat(tegQ(i),length(CBgQ),1) );
1642 end
1643
1644 distancia=sqrt(distancia1.^2+distancia2.^2);
1645
1646 [ordenats_k1,indexs_k1] = sort(distancia,1);
1647
1648 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1649 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1650 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1651 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1652
1653 case 12
1654
1655 tegcQ=obj.test.getQContribution();
1656 tegcQ = tegcQ > repmat((obj.test.getQContributionLimitMean + obj.test.getQContributionLimitStd * 3),[size(tegcQ,1) 1] );
1657 CBgcQ=obj.Case_Base.getQContribution();
1658 CBgcQ= CBgcQ> repmat((obj.Case_Base.getQContributionLimitMean + obj.Case_Base.getQContributionLimitStd * 3),[size(CBgcQ,1) 1] );
1659
1660
1661 k = min(obj.vk);
1662
1663 d1=size(CBgcQ,1);
1664 distancia = zeros(d1,size(tegcQ,1));
1665
1666
1667 for i = 1:size(tegcQ,1)
1668 distancia(:,i)=sum(repmat(tegcQ(i,:),[d1 1]) ~= CBgcQ,2);
1669 end
1670 distancia = distancia / size(tegcQ,2);
1671
1672 [ordenats_k1,indexs_k1] = sort(distancia,1);
1673
1674 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1675 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1676 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1677 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1678
1679 case 13
1680
1681 tegcT2=obj.test.getT2Contribution();
1682 tegcT2 = tegcT2 > repmat((obj.test.getT2ContributionLimitMean + obj.test.getT2ContributionLimitStd * 3),[size(tegcT2,1) 1] );
1683 CBgcT2=obj.Case_Base.getT2Contribution();
1684 CBgcT2= CBgcT2> repmat((obj.Case_Base.getT2ContributionLimitMean + obj.Case_Base.getT2ContributionLimitStd * 3),[size(CBgcT2,1) 1] );
1685
1686
1687 k = min(obj.vk);
1688
1689 d1=size(CBgcT2,1);
1690 distancia = zeros(d1,size(tegcT2,1));
1691
1692
1693 for i = 1:size(tegcT2,1)
1694 distancia(:,i)=sum(repmat(tegcT2(i,:),[d1 1]) ~= CBgcT2,2);
1695 end
1696 distancia = distancia / size(tegcT2,2);
1697
1698 [ordenats_k1,indexs_k1] = sort(distancia,1);
1699
1700 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1701 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1702 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1703 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1704
1705 case 14
1706
1707 tegcQ=obj.test.getQContribution();
1708 tegcQ = tegcQ > repmat((obj.test.getQContributionLimitMean + obj.test.getQContributionLimitStd * 3),[size(tegcQ,1) 1] );
1709 CBgcQ=obj.Case_Base.getQContribution();
1710 CBgcQ= CBgcQ> repmat((obj.Case_Base.getQContributionLimitMean + obj.Case_Base.getQContributionLimitStd * 3),[size(CBgcQ,1) 1] );
1711 dfases=repmat(obj.test.getDATA.getStagesDurations(),1,obj.test.getVariableCount);
1712 if isempty(dfases)
1713 error('There is no value in Fases. You can use this distance method without Fases value.')
1714 end
1715 index = 1;
1716 cdfases=cumsum(dfases);
1717 tegcQ=double(tegcQ);
1718 CBgcQ = double(CBgcQ);
1719 for i=1:length(dfases)
1720 tegcQ(:,i) = sum(tegcQ(:,index:cdfases(i)),2);
1721 CBgcQ(:,i) = sum(CBgcQ(:,index:cdfases(i)),2);
1722 index = index + dfases(i);
1723 end
1724 tegcQ(:,(length(dfases)+1):end) = [];
1725 CBgcQ(:,(length(dfases)+1):end) = [];
1726
1727 k = min(obj.vk);
1728
1729 d1=size(CBgcQ,1);
1730 distancia = zeros(d1,size(tegcQ,1));
1731
1732
1733 for i = 1:size(tegcQ,1)
1734 distancia(:,i)=abs(sum(repmat(tegcQ(i,:),[d1 1]) - CBgcQ,2));
1735 end
1736 distancia = distancia / max(cdfases);
1737
1738 [ordenats_k1,indexs_k1] = sort(distancia,1);
1739
1740 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1741 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1742 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1743 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1744
1745 case 15
1746
1747 tegcQ=obj.test.getT2Contribution();
1748 tegcQ = tegcQ > repmat((obj.test.getT2ContributionLimitMean + obj.test.getT2ContributionLimitStd * 3),[size(tegcQ,1) 1] );
1749 CBgcQ=obj.Case_Base.getT2Contribution();
1750 CBgcQ= CBgcQ> repmat((obj.Case_Base.getT2ContributionLimitMean + obj.Case_Base.getT2ContributionLimitStd * 3),[size(CBgcQ,1) 1] );
1751 dfases=repmat(obj.test.getDATA.getStagesDurations(),1,obj.test.getVariableCount);
1752 if isempty(dfases)
1753 error('There is no value in Fases. You can use this distance method without Fases value.')
1754 end
1755 index = 1;
1756 cdfases=cumsum(dfases);
1757 tegcQ=double(tegcQ);
1758 CBgcQ = double(CBgcQ);
1759 for i=1:length(dfases)
1760 tegcQ(:,i) = sum(tegcQ(:,index:cdfases(i)),2);
1761 CBgcQ(:,i) = sum(CBgcQ(:,index:cdfases(i)),2);
1762 index = index + dfases(i);
1763 end
1764 tegcQ(:,(length(dfases)+1):end) = [];
1765 CBgcQ(:,(length(dfases)+1):end) = [];
1766
1767 k = min(obj.vk);
1768
1769 d1=size(CBgcQ,1);
1770 distancia = zeros(d1,size(tegcQ,1));
1771
1772
1773 for i = 1:size(tegcQ,1)
1774 distancia(:,i)=abs(sum(repmat(tegcQ(i,:),[d1 1]) - CBgcQ,2));
1775 end
1776 distancia = distancia / max(cdfases);
1777
1778 [ordenats_k1,indexs_k1] = sort(distancia,1);
1779
1780 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1781 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1782 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1783 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1784
1785 case 16
1786
1787 testgT=obj.test.getT();
1788 CBgT=obj.Case_Base.getT();
1789
1790
1791
1792 k = min(obj.vk);
1793
1794 distancia = zeros(size(CBgT,1),size(testgT,1));
1795
1796
1797
1798 for i = 1:size(testgT,1)
1799 distancia(:,i) = sqrt(sum(((CBgT - repmat(testgT(i,:),size(CBgT,1),1) ).^2 ),2));
1800 end
1801
1802 [ordenats_k1,indexs_k1] = sort(distancia,1);
1803
1804 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1805 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1806 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1807 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1808
1809 case 17
1810
1811 tegcQ=obj.test.getQContribution();
1812 tegcQ = tegcQ > repmat((obj.test.getQContributionLimitMean + obj.test.getQContributionLimitStd * 3),[size(tegcQ,1) 1] );
1813 CBgcQ=obj.Case_Base.getQContribution();
1814 CBgcQ= CBgcQ> repmat((obj.Case_Base.getQContributionLimitMean + obj.Case_Base.getQContributionLimitStd * 3),[size(CBgcQ,1) 1] );
1815 dfases=repmat(obj.test.getDATA.getStagesDurations(),1,obj.test.getVariableCount);
1816 if isempty(dfases)
1817 error('There is no value in Fases. You can use this distance method without Fases value.')
1818 end
1819 index = 1;
1820 cdfases=cumsum(dfases);
1821 tegcQ=double(tegcQ);
1822 CBgcQ = double(CBgcQ);
1823 for i=1:length(dfases)
1824 tegcQ(:,i) = sum(tegcQ(:,index:cdfases(i)),2);
1825 CBgcQ(:,i) = sum(CBgcQ(:,index:cdfases(i)),2);
1826 index = index + dfases(i);
1827 end
1828 tegcQ(:,(length(dfases)+1):end) = [];
1829 CBgcQ(:,(length(dfases)+1):end) = [];
1830
1831 k = min(obj.vk);
1832
1833 d1=size(CBgcQ,1);
1834 distancia = zeros(d1,size(tegcQ,1));
1835
1836
1837 tegcQ = tegcQ > repmat((dfases*obj.param),size(tegcQ,1),1 );
1838 CBgcQ = CBgcQ > repmat((dfases*obj.param),size(CBgcQ,1),1 );
1839
1840
1841 for i = 1:size(tegcQ,1)
1842 distancia(:,i)=abs(sum(repmat(tegcQ(i,:),[d1 1]) - CBgcQ,2));
1843 end
1844 distancia = distancia / size(tegcQ,2);
1845
1846 [ordenats_k1,indexs_k1] = sort(distancia,1);
1847
1848 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1849 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1850 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1851 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1852
1853 case 18
1854
1855 tegcQ=obj.test.getT2Contribution();
1856 tegcQ = tegcQ > repmat((obj.test.getT2ContributionLimitMean + obj.test.getT2ContributionLimitStd * 3),[size(tegcQ,1) 1] );
1857 CBgcQ=obj.Case_Base.getT2Contribution();
1858 CBgcQ= CBgcQ> repmat((obj.Case_Base.getT2ContributionLimitMean + obj.Case_Base.getT2ContributionLimitStd * 3),[size(CBgcQ,1) 1] );
1859 dfases=repmat(obj.test.getDATA.getStagesDurations(),1,obj.test.getVariableCount);
1860 if isempty(dfases)
1861 error('There is no value in Fases. You can use this distance method without Fases value.')
1862 end
1863 index = 1;
1864 cdfases=cumsum(dfases);
1865 tegcQ=double(tegcQ);
1866 CBgcQ = double(CBgcQ);
1867 for i=1:length(dfases)
1868 tegcQ(:,i) = sum(tegcQ(:,index:cdfases(i)),2);
1869 CBgcQ(:,i) = sum(CBgcQ(:,index:cdfases(i)),2);
1870 index = index + dfases(i);
1871 end
1872 tegcQ(:,(length(dfases)+1):end) = [];
1873 CBgcQ(:,(length(dfases)+1):end) = [];
1874
1875 k = min(obj.vk);
1876
1877 d1=size(CBgcQ,1);
1878 distancia = zeros(d1,size(tegcQ,1));
1879
1880
1881
1882 tegcQ = tegcQ > repmat((dfases *obj.param),size(tegcQ,1),1 );
1883 CBgcQ = CBgcQ > repmat((dfases *obj.param),size(CBgcQ,1),1 );
1884
1885
1886 for i = 1:size(tegcQ,1)
1887 distancia(:,i)=abs(sum(repmat(tegcQ(i,:),[d1 1]) - CBgcQ,2));
1888 end
1889 distancia = distancia / max(cdfases);
1890
1891 [ordenats_k1,indexs_k1] = sort(distancia,1);
1892
1893 obj.veins.ordenats_k1 = ordenats_k1(1:k,:);
1894 obj.veins.indexs_k1 = indexs_k1(1:k,:);
1895 obj.veins.ordenats_k2 = obj.veins.ordenats_k1;
1896 obj.veins.indexs_k2 = obj.veins.indexs_k1;
1897
1898
1899
1900
1901
1902
1903 end
1904 end
1905 function CBR_reuse(obj)
1906
1907
1908 if(obj.vk(2)==1)
1909
1910 obj.classepredit= obj.Case_Base.getClass(obj.veins.indexs_k2(1,:));
1911 else
1912
1913 vks = min(min(obj.vk),size(obj.veins.indexs_k2,1));
1914 switch (obj.method)
1915 case 1
1916
1917 classes=obj.Case_Base.getClass();
1918 n_classes=max(classes);
1919
1920 classes_i = classes(obj.veins.indexs_k2(1:vks,:));
1921
1922
1923 pes = 1./ obj.veins.ordenats_k2(1:vks,:);
1924 if size(classes_i,1)==1
1925 classes_i=classes_i';
1926 end
1927 if size(pes,1)==1
1928 pes=pes';
1929 end
1930 freq=hist(classes(obj.veins.indexs_k2(1:vks,:)),1:n_classes);
1931 if size(freq,1)==1
1932 freq=freq';
1933 end
1934
1935 if obj.test.getBatchCount == 1
1936 pes=pes.*freq(classes_i);
1937 else
1938 pes=pes.* (freq(classes_i + repmat((((1:obj.test.getBatchCount)-1).*n_classes),size(classes_i,1),1)));
1939
1940 end
1941
1942 maxi=zeros(1,obj.test.getBatchCount);
1943
1944
1945 obj.classepredit=zeros(1,obj.test.getBatchCount);
1946
1947 for i=1:n_classes
1948 if size(classes_i,1)>1
1949 index=classes_i(1:vks,:)==i;
1950 else
1951 index=(classes_i(1:vks)==i)';
1952 end
1953 maxpes = pes .* index;
1954
1955 maxpes(isnan(maxpes)) = 0;
1956
1957 pesos=sum(maxpes);
1958 obj.classepredit(pesos > maxi) = i;
1959 maxi(pesos > maxi) = pesos(pesos > maxi);
1960 end
1961
1962 case 2
1963
1964 classes=obj.Case_Base.getClass();
1965 n_classes=max(classes);
1966 if n_classes == 1
1967 obj.classepredit=ones(1,obj.test.getBatchCount);
1968 else
1969 vks = max(1,min(min(hist(classes,1:n_classes)),vks));
1970 if isempty(vks)
1971 obj.classepredit=-ones(1,obj.test.getBatchCount);
1972 elseif(vks == 1)
1973 obj.classepredit=classes(obj.veins.indexs_k2(1:vks,:));
1974 else
1975 freq=hist(classes(obj.veins.indexs_k2(1:vks,:)),1:n_classes);
1976 [~,obj.classepredit] = max(freq);
1977 end
1978 end
1979
1980 case 3
1981
1982 classes=obj.Case_Base.getClass();
1983 n_classes=max(classes);
1984
1985 classes_i = classes(obj.veins.indexs_k2(1:vks,:));
1986
1987
1988 pes = 1./ obj.veins.ordenats_k2(1:vks,:);
1989
1990
1991 maxi=zeros(1,obj.test.getBatchCount);
1992
1993
1994 obj.classepredit=zeros(1,obj.test.getBatchCount);
1995
1996 for i=1:n_classes
1997 if size(classes_i,1)>1
1998 index=classes_i(1:vks,:)==i;
1999 else
2000 index=(classes_i(1:vks)==i)';
2001 end
2002 maxpes = pes .* index;
2003
2004 maxpes(isnan(maxpes)) = 0;
2005
2006 pesos=sum(maxpes);
2007 obj.classepredit(pesos > maxi) = i;
2008 maxi(pesos > maxi) = pesos(pesos > maxi);
2009 end
2010
2011 otherwise
2012 error('CBR/reuse: Choose a valid method!');
2013
2014 end
2015 end
2016
2017 end
2018 function CBR_retain(obj,fallen)
2019
2020
2021 if( sum(fallen)~=0)
2022
2023
2024
2025 if(size(fallen,1)~=1) && (size(fallen,2)~=1)
2026 error('CBR/Retain: the class vector sise is not valid. A vector is needed');
2027 end
2028
2029 [dades class noms]=obj.test.getBatch(fallen);
2030
2031 obj.Case_Base.addBatch(dades,class,noms);
2032 end
2033 end
2034 function [matri fallen] = CBR_revise(obj)
2035
2036
2037 if isempty(obj.test.getClass())
2038
2039 c=input('Give the new class vector: exemple [ 1 1 1 2 ]\n');
2040 obj.test.setClass(c);
2041 end
2042
2043
2044
2045 matri = compute_extended_confusion_matrix(obj.classepredit,obj.test.getClass(), length(unique(obj.Case_Base.getClass())));
2046 fallen=(obj.classepredit~=obj.test.getClass());
2047 end
2048
2049 function Calcula_IB_UdG(obj, nelem, descarta)
2050
2051
2052 if descarta > nelem
2053 nelem=descarta;
2054 end
2055
2056
2057
2058
2059 segur=obj.vk;
2060 obj.vk=[obj.Case_Base.getBatchCount() obj.Case_Base.getBatchCount()];
2061 obj.retrieve(obj.Case_Base.getDATA());
2062 obj.vk=segur;
2063 classes=obj.Case_Base.getClass();
2064 resu=classes(obj.veins.indexs_k1);
2065
2066
2067
2068
2069 enemics=(resu~=repmat(resu(1,:),size(resu,1),1));
2070 [~,IX]=sort(enemics,'descend');
2071 x2=1:obj.Case_Base.getBatchCount();
2072 x2=repmat(x2,nelem,1);
2073 x1=IX(1:nelem,:);
2074
2075
2076
2077 bons=ones(size(unique(classes)))*nelem;
2078 indexcentres=zeros(size(unique(classes)));
2079 sumaenemics=sum(enemics(2:nelem,:),1);
2080 for i=1 : obj.Case_Base.getBatchCount()
2081 if(bons(classes(i))>sumaenemics(i))
2082 bons(classes(i))=sumaenemics(i);
2083 indexcentres(classes(i))=i;
2084 end
2085 end
2086 temp=unique(obj.veins.indexs_k2(sub2ind(size(obj.veins.indexs_k2),x1,x2)));
2087 temp2=unique(obj.veins.indexs_k2(sub2ind(size(obj.veins.indexs_k2),x1(1:descarta,:),x2(1:descarta,:))));
2088 temp=setdiff(temp,temp2);
2089 if size(temp,1)~=1
2090 temp=temp';
2091 end
2092
2093 indexcentres=setdiff(indexcentres,indexcentres(unique(classes(temp))));
2094 index = unique([temp indexcentres]);
2095
2096
2097
2098
2099 obj.Case_Base.deleteBatch(setdiff(1:obj.Case_Base.getBatchCount(),index));
2100
2101
2102 end
2103 function calcula_DROP4(obj)
2104
2105
2106 C_B_vella=obj.Case_Base.getDATA();
2107 cla=C_B_vella.getClass();
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117 treuresoroll(obj,0.7,min(obj.vk));
2118
2119
2120
2121
2122
2123
2124 segur=obj.vk;
2125 obj.vk=[C_B_vella.getBatchCount() C_B_vella.getBatchCount()];
2126 obj.retrieve(C_B_vella);
2127 obj.vk=segur;
2128
2129
2130
2131 classpredit = reuse(obj);
2132 encertats=sum(classpredit == cla);
2133
2134
2135
2136 for i=1:obj.Case_Base.getBatchCount()
2137
2138 [dades , clas , noms]=obj.Case_Base.getBatch(1);
2139 obj.Case_Base.deleteBatch(1);
2140
2141
2142
2143
2144
2145
2146
2147
2148 vkseg = obj.vk;
2149 obj.vk(1) = min(obj.vk(1),obj.Case_Base.getBatchCount);
2150 obj.vk(2) = min(obj.vk(2),obj.Case_Base.getBatchCount);
2151
2152 if (obj.dc <4 || obj.dc >6)
2153
2154
2155
2156 x=obj.veins.indexs_k2(:);
2157 ins=x==1;
2158
2159
2160 temporal=(reshape(x(not(ins))',size(obj.veins.indexs_k2,1)-1,size(obj.veins.indexs_k2,2)))-1;
2161
2162
2163 x(ins)=obj.Case_Base.getBatchCount()+2;
2164 x=x-1;
2165 segur_i_k2=reshape(x,size(obj.veins.indexs_k2,1),size(obj.veins.indexs_k2,2));
2166
2167 segur_o_k2=obj.veins.ordenats_k2;
2168
2169
2170 obj.veins.indexs_k2=temporal;
2171
2172
2173 x=obj.veins.ordenats_k2(:);
2174 obj.veins.ordenats_k2=reshape(x(not(ins))',size(obj.veins.ordenats_k2,1)-1,size(obj.veins.ordenats_k2,2));
2175
2176 reuse(obj);
2177
2178 else
2179
2180
2181
2182 x=obj.veins.indexs_k1(:);
2183 ins=x==1;
2184
2185
2186 temporal=(reshape(x(not(ins))',size(obj.veins.indexs_k1,1)-1,size(obj.veins.indexs_k1,2)))-1;
2187
2188
2189 x(ins)=obj.Case_Base.getBatchCount()+2;
2190 x=x-1;
2191 segur_i_k1=reshape(x,size(obj.veins.indexs_k1,1),size(obj.veins.indexs_k1,2));
2192
2193 segur_o_k1=obj.veins.ordenats_k1;
2194
2195
2196 obj.veins.indexs_k1=temporal;
2197
2198
2199 x=obj.veins.ordenats_k1(:);
2200 obj.veins.ordenats_k1=reshape(x(not(ins))',size(obj.veins.ordenats_k1,1)-1,size(obj.veins.ordenats_k1,2));
2201
2202
2203
2204 x=obj.veins.indexs_k2(:);
2205 ins=x==1;
2206
2207
2208 temporal=(reshape(x(not(ins))',size(obj.veins.indexs_k2,1)-1,size(obj.veins.indexs_k2,2)))-1;
2209
2210
2211 x(ins)=obj.Case_Base.getBatchCount()+2;
2212 x=x-1;
2213 segur_i_k2=reshape(x,size(obj.veins.indexs_k2,1),size(obj.veins.indexs_k2,2));
2214
2215 segur_o_k2=obj.veins.ordenats_k2;
2216
2217
2218 obj.veins.indexs_k2=temporal;
2219
2220
2221 x=obj.veins.ordenats_k2(:);
2222 obj.veins.ordenats_k2=reshape(x(not(ins))',size(obj.veins.ordenats_k2,1)-1,size(obj.veins.ordenats_k2,2));
2223
2224
2225
2226
2227 ind=obj.veins.indexs_k1(1:obj.vk(1),:);
2228
2229
2230
2231 orde = zeros(obj.vk(2),length(obj.veins.ordenats_k1));
2232 inde = zeros(obj.vk(2),length(obj.veins.ordenats_k1));
2233
2234
2235 for buc=1:size(ind,1)
2236
2237 orde(buc,:)=(obj.veins.ordenats_k1(obj.veins.indexs_k2==repmat(ind(buc,:),size(obj.veins.indexs_k2,1),1)))';
2238 inde(buc,:)=(obj.veins.indexs_k1(obj.veins.indexs_k2==repmat(ind(buc,:),size(obj.veins.indexs_k2,1),1)))';
2239 end
2240
2241
2242 s1 = obj.veins.indexs_k2;
2243 s2 = obj.veins.ordenats_k2;
2244
2245 obj.veins.indexs_k2 = inde;
2246 obj.veins.ordenats_k2 = orde;
2247
2248
2249 reuse(obj);
2250
2251 obj.veins.indexs_k2 = s1;
2252 obj.veins.ordenats_k2 = s2;
2253 end
2254
2255
2256
2257
2258 obj.vk = vkseg;
2259
2260 if length(unique(obj.Case_Base.getClass)) ~= length(unique(C_B_vella.getClass)) || sum(obj.classepredit == cla) < encertats
2261 obj.Case_Base.addBatch(dades,clas,noms);
2262 if (obj.dc <4 || obj.dc >6)
2263
2264 obj.veins.indexs_k2=segur_i_k2;
2265 obj.veins.ordenats_k2=segur_o_k2;
2266 else
2267
2268
2269 obj.veins.indexs_k1=segur_i_k1;
2270 obj.veins.ordenats_k1=segur_o_k1;
2271
2272 obj.veins.indexs_k2=segur_i_k2;
2273 obj.veins.ordenats_k2=segur_o_k2;
2274 end
2275
2276
2277
2278 else
2279
2280
2281 encertats=sum(obj.classepredit == cla);
2282 end
2283 end
2284 end
2285 function Calcula_IB3(obj)
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321 Old_CD = obj.Case_Base.copy();
2322 obj.Case_Base.deleteBatch(1:obj.Case_Base.getBatchCount());
2323 dades=obj.Case_Base.getDATA.copy();
2324
2325 taula=struct('encerts',0,'errors',0,'classe',0,'index',0);
2326
2327 for x=1 : Old_CD.getBatchCount()
2328 [dad cla noms]=Old_CD.getBatch(x);
2329 if x~=1
2330
2331
2332
2333 dades.addBatch(dad,cla,noms);
2334 copy=obj.vk;
2335 retrieve(obj,dades);
2336 Sim = reuse(obj);
2337 obj.vk=copy;
2338 dades.deleteBatch(1);
2339
2340
2341
2342
2343
2344
2345
2346
2347 acceptables=(taula.encerts/(taula.encerts+taula.errors))>0.9;
2348 candidat = obj.veins.indexs_k2(acceptables);
2349
2350 if(not(isempty(candidat)))
2351 Sim = taula.classe(candidat(1));
2352
2353 else
2354
2355 end
2356
2357
2358
2359
2360
2361
2362
2363 if Sim ~= cla
2364
2365 obj.Case_Base.addBatch(dad,cla,noms);
2366 taula.encerts=[taula.encerts 1];
2367 taula.errors=[taula.errors 0];
2368 taula.classe=[taula.classe cla];
2369 taula.index=[taula.index x];
2370
2371 taula.errors(obj.veins.indexs_k2(1))=taula.errors(obj.veins.indexs_k2(1))+1;
2372 else
2373 taula.encerts(obj.veins.indexs_k2(1))=taula.encerts(obj.veins.indexs_k2(1))+1;
2374 end
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390 enemics= taula.encerts./(taula.encerts + taula.errors);
2391
2392 enemicsclase=zeros(max(unique(taula.classe)),1);
2393 for z=unique(taula.classe)
2394 enemicsclase(z)=mean(enemics(taula.classe==z));
2395 end
2396
2397
2398 indexs=enemics < (enemicsclase(taula.classe) * 0.7)';
2399 obj.Case_Base.deleteBatch(indexs);
2400
2401 in=logical(1:length(taula.encerts));
2402 in(indexs)=0;
2403
2404 taula.encerts=taula.encerts(in);
2405 taula.errors=taula.errors(in);
2406 taula.classe=taula.classe(in);
2407 taula.index=taula.index(in);
2408
2409 else
2410 obj.Case_Base.addBatch(dad,cla,noms);
2411 taula.encerts=1;
2412 taula.errors=0;
2413 taula.classe=cla;
2414 taula.index=1;
2415 end
2416 end
2417 end
2418 function Calcula_IB2(obj)
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439 Old_CD = obj.Case_Base.copy();
2440 obj.Case_Base.deleteBatch(1:obj.Case_Base.getBatchCount());
2441 dades=obj.Case_Base.getDATA.copy();
2442 for x=1 : Old_CD.getBatchCount()
2443 [dad cla noms]=Old_CD.getBatch(x);
2444 if x~=1
2445 dades.addBatch(dad,cla,noms);
2446 copia=obj.vk;
2447 retrieve(obj,dades);
2448 Sim = reuse(obj);
2449 obj.vk=copia;
2450 dades.deleteBatch(1);
2451 if Sim ~= cla
2452
2453 obj.Case_Base.addBatch(dad,cla,noms);
2454 end
2455 else
2456 obj.Case_Base.addBatch(dad,cla,noms);
2457 end
2458 end
2459 end
2460
2461 function ordena(obj)
2462
2463
2464
2465
2466
2467 if obj.Case_Base.getBatchCount > 1 && length(unique(obj.Case_Base.getClass())) > 1
2468
2469 segur=obj.vk;
2470 obj.vk=[obj.Case_Base.getDATA().getBatchCount() obj.Case_Base.getDATA().getBatchCount()];
2471 obj.retrieve(obj.Case_Base.getDATA());
2472 obj.vk=segur;
2473
2474 classes=obj.Case_Base.getClass();
2475 resu=classes(obj.veins.indexs_k2);
2476 enemics=(resu~=repmat(resu(1,:),size(resu,1),1));
2477 enemics= repmat((1:size(enemics,1)),size(enemics,2),1)'.*enemics;
2478 enemics(enemics==0)=inf;
2479 enemics=sort(enemics);
2480 enemics=enemics(1,:);
2481 enemics=obj.veins.ordenats_k2(sub2ind(size(obj.veins.ordenats_k2),enemics,1:size(obj.veins.ordenats_k2,2)));
2482 [~,index]=sort(enemics);
2483 obj.Case_Base.getDATA().shuffle(fliplr(index));
2484 end
2485
2486 end
2487 function treuresoroll(obj,i,nelem)
2488
2489
2490
2491 copy=obj.vk;
2492 obj.retrieve(obj.Case_Base.getDATA());
2493 obj.vk=copy;
2494 classes=obj.Case_Base.getClass();
2495 resu=classes(obj.veins.indexs_k2);
2496 enemics=(resu~=repmat(resu(1,:),size(resu,1),1));
2497 sumaenemics=sum(enemics(2:nelem,:),1)./nelem;
2498 indexs=sumaenemics>=i;
2499 obj.Case_Base.deleteBatch(indexs);
2500 end
2501
2502 end
2503
2504 end