-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algo_fuzzy_RFU_tree.java
840 lines (739 loc) · 33.9 KB
/
Algo_fuzzy_RFU_tree.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
import java.io.*;
import java.util.*;
/**
* This is an implementation of the fuzzy-RFU-tree algorithm.
* "Mining Valuable Fuzzy Patterns via the RFM Model", Yanlin Qi et al.
* published at 2022 IEEE International Conference on Data Mining Workshops (ICDMW)
* @author Yanlin Qi, HIT, China
*/
public class Algo_fuzzy_RFU_tree{
/**
* the maximum memory usage
*/
private double maxMemory = 0;
/**
* the time the algorithm started
*/
private long startTimestamp = 0;
private long startTimestamp1 = 0;
private long endTimestamp3 = 0;
private long endTimestamp4 = 0;
private long endTimestamp5 = 0;
/**
* the time the algorithm terminated
*/
private long endTimestamp1 = 0;
/**
* the time the algorithm terminated
*/
private long endTimestamp2 = 0;
/**
* the number of RHUIs generated
*/
private int huiCount = 0;
/**
* the number of PRHUIs generated
*/
private int pRHUIsCount;
private double totalRevenue = 0.0;
private BufferedWriter writer3;
private String dataset;
private double minRecencyThreshold = 0d;
private double minUtilityThreshold=0.0;
private double minFrequencyThreshold=0.0;
private double minUtilityRatio=0.0;
private double totalUtility1 = 0d;
/**
* the number of transactions
*/
private int transCurrent = 0;
/**
* delay recency rate
*/
double decayThreshold = 0.05;
/**
* time current
*/
//double timeCurrent = 115;//125
/**
* writer to write the output file
*/
private BufferedWriter writer = null;
private BufferedWriter writer1 = null;
/**
* Structure to store the potential RHUIs
*/
private List<Itemset> pRHUIs = new ArrayList<Itemset>();
// private Itemset Itemset_val =null;
/**
* item-utility
*/
List fuzzydatabase = new ArrayList();
HashMap<String, Double> itemsUtil = new HashMap<String,Double>();
/**
* item-time
*/
HashMap<Integer, Double> tidTime = new HashMap<Integer,Double>();//项对应的时间
/**
* item-tids
*/
Map<String, TreeSet<Integer>> mapItemTIDS = new HashMap<String, TreeSet<Integer>>();
/**
* To activate debug mode
*/
private final boolean DEBUG = false;
/**
* the path of input file
*/
private String pathInput = null;
private Object maxF,minF,maxR,minR,maxU,minU;
private int traverse_num = 0;
private int recursive_num = 0;
Map<String, Double> mapItemToTWU = new HashMap<String, Double>();
// We create a map to store the Frequency value of each item
Map<String, Double> mapItemToFrequency = new HashMap<String, Double>();
// We create a map to store the Recency value of each item
Map<String, Double> mapItemToRecency = new HashMap<String, Double>();
/**
* Method to run the algorithm
*
* @param input path for reading the input file
* @param utilityTableInput path for item-utility file
* @param timeTableInput path for item-time file
* @param output path for writing the output file
* @param minFrequencyRate the minimum FrequencyRate to calculate minimum Frequency
* @param minUtility the minimum utility
* @param decay the user defined decay
* @param minRecency the minimum recency
*
* @throws IOException exception if error while reading or writing the file
*/
public void runAlgorithm(String dataset, BufferedWriter writer3, String input, String utilityTableInput, String timeTableInput, String output, String output1,
boolean useRate, double minFrequencyRate, int minUtility, double minUtilityRatio, double decay,double minRecency,double timeCurrent) throws IOException {
checkMemory();
startTimestamp = System.currentTimeMillis();
pathInput = input;
decayThreshold = decay;
maxMemory = 0;
writer = new BufferedWriter(new FileWriter(output));
writer1 = new BufferedWriter(new FileWriter(output1));
this.writer3=writer3;
this.dataset=dataset;
this.minUtilityThreshold=minUtility;
this.minFrequencyThreshold=minFrequencyRate;
this.minRecencyThreshold=minRecency;
this.minUtilityRatio=minUtilityRatio;
this.totalUtility1=totalUtility1;
// We create a map to store the TWU of each item
// Map item and its utility
Load_UtilityTable(utilityTableInput);
// Map tranctions and its time
Load_TimeTable(timeTableInput);
BufferedReader myInput = null;
String thisLine;
FFIMiner ffi = new FFIMiner();
Map<String, Double> ItemSup = ffi.init(input);
Map<Integer, Map<String, Double>> fuzzyTranactions = ffi.fuzzyTranactions;
endTimestamp3 = System.currentTimeMillis();
String true_item;
String true_level;
Set<String> fuzzyitems = ItemSup.keySet();
List<String> true_items = new ArrayList<String>();
Map<String,String> mapItemToLevel =new HashMap<String,String>();
Map<Integer,Double> MapTidToTWU =new HashMap<Integer,Double>();
Map<Integer,Double> MapTidToRecency=new HashMap<Integer, Double>();
int tid1 = 0;
try {
startTimestamp1 = System.currentTimeMillis();
myInput = new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
myInput.mark((int) new File(input).length() + 1);
for (String fuzzyitem : fuzzyitems) {
String[] itemAndlevel = fuzzyitem.split("\\.");
true_item = itemAndlevel[0];
true_level = itemAndlevel[1];
mapItemToLevel.put(true_item,true_level);
}
while ((thisLine = myInput.readLine()) != null) {
Double transaction_utility = 0d;
Double SumRecency = 0d, currentRecency = 0d;
Double twu = 0d;
String[] ItemAndQual = thisLine.split(" ");
for (String itemAndQualOne : ItemAndQual) {
String[] tmp1 = itemAndQualOne.split(",");
String item = tmp1[0];
double count = Double.parseDouble(tmp1[1]);
transaction_utility += itemsUtil.get(item) * count;
}
for (String itemAndQualOne : ItemAndQual) {
String[] tmp = itemAndQualOne.split(",");
if (tmp.length == 2) {
String item = tmp[0];
String level = mapItemToLevel.get(item);
String fuzzyitem = item + "." + level;
if(fuzzyitems.contains(fuzzyitem)) {
if (fuzzyTranactions.get(tid1 + 1).get(fuzzyitem) != 0) {
mapItemToFrequency.put(item, ItemSup.get(fuzzyitem));
twu = mapItemToTWU.get(item);
twu = (twu == null) ? transaction_utility : twu + transaction_utility;
mapItemToTWU.put(item, twu);
SumRecency = mapItemToRecency.get(item);
currentRecency = Math.pow((1 - decayThreshold), (timeCurrent - tidTime.get(tid1))/1000.0);
SumRecency = (SumRecency == null) ? currentRecency : SumRecency + currentRecency;
mapItemToRecency.put(item, SumRecency);
TreeSet<Integer> Tids = mapItemTIDS.get(item);
if (Tids == null) {
Tids = new TreeSet<Integer>();
}
Tids.add(tid1);
mapItemTIDS.put(item, Tids);
}
}
}
}
MapTidToTWU.put(tid1,transaction_utility);
MapTidToRecency.put(tid1,currentRecency);
tid1++;
}
int length = mapItemToFrequency.size();
Object[] obj = mapItemToFrequency.values().toArray();
Arrays.sort(obj);
maxF=obj[length-1];
minF=obj[0];
Object[] obj1 = mapItemToRecency.values().toArray();
Arrays.sort(obj1);
maxR=obj1[length-1];
minR=obj1[0];
Object[] obj2 = mapItemToTWU.values().toArray();
Arrays.sort(obj2);
maxU=obj2[length-1];
minU=obj2[0];
} catch (Exception e) {
e.printStackTrace();
} finally {
if (myInput != null) {
myInput.close();
}
}
// second database scan generate revised transaction and build the initial global IHUP-Tree
try {
RFMPTree tree = new RFMPTree();
myInput = new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
int tid2 = 0;
List<Item> RFT1P=new ArrayList<Item>();
ArrayList<String> RFT1item=new ArrayList<String>();
while ((thisLine = myInput.readLine()) != null) {
List<Item> revisedTransaction = new ArrayList<Item>();
String[] ItemAndQual = thisLine.split(" ");
Map<String, Double> FitemAndFvalue = fuzzyTranactions.get(tid2 + 1);
double transaction_utility = 0d;
for (String itemAndQualOne : ItemAndQual) {
String[] tmp = itemAndQualOne.split(",");
String item = tmp[0];
double count = Double.parseDouble(tmp[1]);
String level=mapItemToLevel.get(item);
String fuzzy_item=item+"."+level;
if(FitemAndFvalue.get(fuzzy_item)!=0) {
if (mapItemToTWU.get(item) >= minUtilityThreshold && mapItemToRecency.get(item) >= minRecencyThreshold && mapItemToFrequency.get(item) >= minFrequencyThreshold) {
transaction_utility += itemsUtil.get(item) * count;
}
}
}
for (String itemAndQualOne : ItemAndQual) {
String[] tmp = itemAndQualOne.split(",");
String item = tmp[0];
String fuzzyitem = item + "." + mapItemToLevel.get(item);
double fuzzyFvalue = FitemAndFvalue.get(fuzzyitem);
if (fuzzyFvalue != 0) {
if (mapItemToTWU.get(item) >= minUtilityThreshold && mapItemToRecency.get(item) >= minRecencyThreshold && mapItemToFrequency.get(item) >= minFrequencyThreshold) {
double currentRecency = MapTidToRecency.get(tid2);
Item element = new Item(item, transaction_utility, currentRecency, fuzzyFvalue);
revisedTransaction.add(element);
RFT1item.add(item);
RFT1item = getSingle(RFT1item);
}
}
}
revisedTransaction.sort(new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
// return compareItemsAsc(o1.name, o2.name, mapItemToTWU);
return compareItemsDesc(o1.name, o2.name, mapItemToFrequency);
}
});
tree.addTransaction(revisedTransaction);
tid2++;
}
tree.createHeaderList(mapItemToFrequency);
checkMemory();
if (DEBUG) {
System.out.println("GLOBAL TREE" +
"\n mapITEM-FRE : " + mapItemToFrequency +
"\n mapITEM-TWU : " + mapItemToTWU +
"\n mapITEM-REC : " + mapItemToRecency +
"\n" + tree.toString());
}
//********************************//
// We create the header table for the global IHUP-Tree,// Mine tree with UP-Growth with 2 strategies DLU and DLN
endTimestamp1 = System.currentTimeMillis();
ihup(tree, new String[0]);
// check the memory usage again and close the file.
checkMemory();
} catch(Exception e){
e.printStackTrace();
} finally{
if (myInput != null) {
myInput.close();
}
}
pRHUIsCount = pRHUIs.size();
checkMemory();
endTimestamp4 = System.currentTimeMillis();
try {
myInput = new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
// Third database scan to calculate the exact utility of each PRHUIs and output RFM-patterns.
int tid3 = 0;
while ((thisLine = myInput.readLine()) != null) {
List<Item> revisedTransaction = new ArrayList<Item>(); //revisedTransaction,一行代表一项的值,有几行说明这个事件中有几项
List<String> revisedTransactionIds = new ArrayList<>();//里面装的是项。
double transactionRecency = Math.pow((1 - decayThreshold), (timeCurrent - tidTime.get(tid3))/1000.0);
String[] ItemAndQual = thisLine.split(" ");
for (String itemAndQualOne : ItemAndQual) {
String[] tmp = itemAndQualOne.split(",");
if(mapItemToLevel.keySet().contains(tmp[0])) {
String item = tmp[0];
String level = mapItemToLevel.get(item);
String fuzzyitem =item+"."+level;
double fuzzyFval = fuzzyTranactions.get(tid3+1).get(fuzzyitem);
if(fuzzyFval!=0){
if (mapItemToFrequency.get(item) >= minFrequencyThreshold && mapItemToTWU.get(item) >= minUtilityThreshold && mapItemToRecency.get(item) >= minRecencyThreshold) {
double utility = itemsUtil.get(tmp[0]) * Double.parseDouble(tmp[1]);
Item element = new Item(item, utility, transactionRecency, fuzzyFval);
revisedTransaction.add(element);
}
}
}
}
revisedTransaction.sort(new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
// return compareItemsAsc(o1.name, o2.name, mapItemToTWU);
return compareItemsDesc(o1.name, o2.name, mapItemToFrequency);
}
});
for(Item element : revisedTransaction){
String item = element.name;
revisedTransactionIds.add(item);
}
for (Itemset itemset : pRHUIs) {
if (itemset.size() > revisedTransaction.size()) {
continue;
}
updateExactUtility(revisedTransaction, revisedTransactionIds, itemset);
}
tid3++;
}
checkMemory();
} catch (Exception e) {
e.printStackTrace();
}
endTimestamp5 = System.currentTimeMillis();
int number=0;
for (Itemset itemset : pRHUIs) {
ArrayList<String> buffer = new ArrayList<String>();
for (int i = 0; i < itemset.size(); i++) {
buffer.add(itemset.get(i));
}
if (itemset.getExactUtility() >= minUtilityThreshold && itemset.getExactFrequency() >= minFrequencyRate && itemset.getExactRecency() >= minRecency ) {
number++;
writeOut(itemset);
}
}
// check the memory usage again
checkMemory();
// record end time
endTimestamp2 = System.currentTimeMillis();
pRHUIs.clear();
// CLOSE OUTPUT FILE
writer.close();
writer1.close();
}
public static ArrayList getSingle(ArrayList list){
ArrayList newList = new ArrayList();
Iterator it = list.iterator();
while(it.hasNext()){
Object obj = it.next();
if(!newList.contains(obj)){
newList.add(obj);
}
}
return newList;
}
private void Load_TimeTable(String timeTableInput) {
try {
String thisline;
BufferedReader br = new BufferedReader(new FileReader(timeTableInput));
while ((thisline = br.readLine()) != null) {
String[] tmp = thisline.split(",");
int tid = Integer.parseInt(tmp[0]);
Double time = Double.parseDouble(tmp[1]);
tidTime.put(tid, time);
}
br.close();
} catch (Exception e) {
System.out.println("Error about loading the time table (in ConnectionTextFile.java): " + e.toString());
}
}
/**
* Loading Utility Table
*
* @param utility_table_input the path of utiltiy_table file
*/
public void Load_UtilityTable(String utility_table_input) {
try {
String thisline;
BufferedReader br = new BufferedReader(new FileReader(utility_table_input)); // �_�n
while ((thisline = br.readLine()) != null) {
String[] tmp = thisline.split(",");
String item = tmp[0];
Double profit = Double.parseDouble(tmp[1]);
itemsUtil.put(item, profit);
}
br.close();
} catch (Exception e) {
System.out.println("Eity table (in rror about loading the utilConnectionTextFile.java): " + e.toString());
}
}
/**
* compare item with descending order
*
* @param item1 item A
* @param item2 item B
* @param map item-utility
*
* @return o1-o2 ascending | o2-o1 descending
*/
private int compareItemsDesc(String item1, String item2, Map<String, Double> map) {
double compare = map.get(item2) - map.get(item1);
// if the same, use the lexical order otherwise use the TWU
return Double.compare(compare, 0.0);
}
private int compareItemsAsc(String item1, String item2, Map<String, Double> map) {
double compare = map.get(item1) - map.get(item2);
// if the same, use the lexical order otherwise use the TWU
return Double.compare(compare, 0.0);
}
/**
* Mine UP-Tree recursively
*
* @param tree IHUPTree to mine
* @param prefix the prefix itemset
*/
private void ihup(RFMPTree tree, String[] prefix) throws IOException {
for (int i = tree.headerList.size() - 1; i >= 0; i--) {
String item = tree.headerList.get(i);
// ===== CALCULATE SUM OF ITEM NODE UTILITY =====
TreeNode pathCPB = tree.mapItemNodes.get(item);
double pathCPBFrequency = 0.0;
double path_minif=0.0;
double pathCPBRecency = 0.0;
double pathCPBUtility = 0.0;
while(pathCPB != null) {
pathCPBFrequency += pathCPB.count;
pathCPBRecency += pathCPB.recency;
pathCPBUtility += pathCPB.nodeUtility;
pathCPB = pathCPB.nodeLink;
}
if (pathCPBFrequency >= minFrequencyThreshold && pathCPBRecency >= minRecencyThreshold && pathCPBUtility >= minUtilityThreshold) {
String[] newPrefix = new String[prefix.length + 1];
System.arraycopy(prefix, 0, newPrefix, 0, prefix.length);
newPrefix[prefix.length] = item;
Itemset Itemset_val=new Itemset(newPrefix);
Itemset_val.utility = pathCPBUtility;
Itemset_val.frequency = pathCPBFrequency;
Itemset_val.recency = pathCPBRecency;
savePHUI(newPrefix, Itemset_val.recency);
writeOut1(Itemset_val);
traverse_num += 1; //
// ===== CREATE THE LOCAL TREE =====
RFMPTree localTree = createLocalTree(tree, item);
if (DEBUG) {
System.out.println("\nLOCAL TREE for projection by:" + ((prefix == null) ? "" : Arrays.toString(prefix) + ",") + item + "\n" + localTree.toString());
}
if (localTree.headerList.size() > 0) {
recursive_num += 1;
ihup(localTree, newPrefix);
}
}
}
}
/**
* createLocalTree
*
* @param tree the construct tree
* @param item current item (local root)
*
* @return a local tree (the root is parameter 'item')
*/
private RFMPTree createLocalTree(RFMPTree tree, String item) {
// It is a subdatabase which consists of the set of prefix paths
List<List<TreeNode>> prefixPaths = new ArrayList<List<TreeNode>>();
TreeNode path = tree.mapItemNodes.get(item);
// map to store path utility of local items in CPB
final Map<String, Double> itemPathUtility = new HashMap<String, Double>();
// map to store path recency of local items in CPB
final Map<String, Double> itemPathRecency = new HashMap<String, Double>();
// map to store path frequency of local items in CPB
final Map<String, Double> itemPathFrequency= new HashMap<String, Double>();
while (path != null) {
// get the node utility,frequency,recency of the item
double nodeutility = path.nodeUtility;
double nodeFrequency = path.count;//
double nodeRecency = path.recency;
// if the path is not just the root node
if (!path.parent.name.equals("-1")) {
// create the prefix-path
List<TreeNode> prefixPath = new ArrayList<TreeNode>();
// add this node.
prefixPath.add(path); // NOTE: we add it just to keep its utility,
// actually it should not be part of the prefixPath
TreeNode parentnode = path.parent;
while (!parentnode.name.equals("-1")) {
prefixPath.add(parentnode);
Double pu = itemPathUtility.get(parentnode.name);
pu = (pu == null) ? nodeutility : pu + nodeutility;
itemPathUtility.put(parentnode.name, pu);
//*****************************
// pr - path recency
Double pr = itemPathRecency.get(parentnode.name);
pr = (pr == null) ? nodeRecency : pr + nodeRecency;
itemPathRecency.put(parentnode.name, pr);
//*****************************
// pf - path frequency
Double pf = itemPathFrequency.get(parentnode.name);
pf = (pf == null) ? nodeFrequency : pf + nodeFrequency;
itemPathFrequency.put(parentnode.name, pf);
parentnode = parentnode.parent;
}
// add the path to the list of prefix-paths
prefixPaths.add(prefixPath);
}
// We will look for the next prefix-path
path = path.nodeLink;
}
if (DEBUG) {
System.out.println("\n\n\nPREFIXPATHS:");
for (List<TreeNode> prefixPath : prefixPaths) {
for (TreeNode node : prefixPath) {
System.out.println(" " + node);
}
System.out.println(" --");
}
}
// Calculate the Utility of each item in the prefixpath
RFMPTree localTree = new RFMPTree();
for (List<TreeNode> prefixPath : prefixPaths) {
double pathUtility = prefixPath.get(0).nodeUtility;
double pathFrequency = prefixPath.get(0).count;
double pathRecency = prefixPath.get(0).recency;
List<String> localPath = new ArrayList<String>();
// for each node in the prefixpath,
// except the first one, we count the frequency
for (int j = 1; j < prefixPath.size(); j++) {
TreeNode node = prefixPath.get(j);//ArrayList<TreeNode>
if (itemPathFrequency.get(node.name) >= minFrequencyThreshold && itemPathUtility.get(node.name) >= minUtilityThreshold && itemPathRecency.get(node.name) >= minRecencyThreshold) {
localPath.add(node.name);
}
}
if (DEBUG) {
System.out.println(" path utility " + pathUtility);
}
// we reorganize local path in decending order of path frequency
localPath.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// compare the frequency of the items
//return compareItemsAsc(o1, o2, mapItemToTWU);
return compareItemsDesc(o1, o2, itemPathFrequency);
}
});
localTree.addLocalTransaction(localPath, pathFrequency, pathRecency, pathUtility);
}
// We create the local header table for the tree item - CPB
localTree.createHeaderList(itemPathFrequency);
//System.out.println(localTree.root);
//System.out.println(localTree.headerList);
return localTree;
}
/**
* Save a PHUI in the list of PRHUIs
*
* @param itemset the itemset array
*/
private void savePHUI(String[] itemset,double recency) {
Itemset itemsetObj = new Itemset(itemset);
// System.out.println(itemsetObj.getItemset());
//按照字典序排序
itemsetObj.recency=recency;
// Arrays.sort(itemset);
pRHUIs.add(itemsetObj);
}
//重新计算正确的 Mvalue
public void updateExactUtility(List<Item> transaction, List<String> revisedTransactionIds, Itemset itemset) {
double utility = 0.0;
// double recency = 0.0;
double frequency=0.0;
// List frequencys=new ArrayList();
List<Double> frequencys = new ArrayList<>();
if (ContainsAll(revisedTransactionIds,itemset.getItemset())){//
for (int i = 0; i < itemset.size(); i++) {
String itemI = itemset.get(i);//ItemI是项集中的每个项
int index = revisedTransactionIds.indexOf(itemI);//得到在revisedTransactionIds中项为4的索引
utility += transaction.get(index).utility;
frequency = transaction.get(index).frequency;
frequencys.add(frequency);
//frequency = Double.min(transaction.get(index).frequency,frequency);
}
// recency += transaction.get(0).recency;
}
itemset.increaseUtility(utility);
// itemset.increaseRecency(recency);
double minValue;
if (frequencys.size()!=0) {
minValue = frequencys.get(0);
for (int i = 0; i < frequencys.size(); i++) {
if (i == frequencys.size() - 1) {
continue;
}
double next = frequencys.get(i + 1);
if (minValue > next) {
minValue = next;
}
}
} else {
minValue = 0d;
}
itemset.increaseFrequency(minValue);
// itemset.increaseFrequency(itemset.minFrequency(frequencys));
}
private boolean ContainsAll(List<String> revisedTransactionIds, String[] itemset) {
boolean res=true;
for (String transId:itemset) {
if (!revisedTransactionIds.contains(transId)){
res = false;
}
}
return res;
}
/**
* Write a HUI to the output file
*
* @param HUI itemset which users need
*
* @throws IOException
*/
private void writeOut(Itemset HUI) throws IOException {
huiCount++;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < HUI.size(); i++) {
buffer.append(HUI.get(i));
buffer.append(' ');
}
buffer.append("," +" #Util: "+ HUI.getExactUtility());
buffer.append(" #Freq: " + String.format("%.3f",HUI.getExactFrequency()));
buffer.append(" #Re: " + String.format("%.3f", HUI.getExactRecency()));
writer.write(buffer.toString());
writer.newLine();
}
private void writeOut1(Itemset HUI) throws IOException {
// increase the number of high utility itemsets found
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < HUI.size(); i++) {
buffer.append(HUI.get(i));
buffer.append(' ');
}
// buffer.append("," + HUI.getExactUtility());
buffer.append("," +" #Util: "+ HUI.getExactUtility());
buffer.append(" #Freq: " + String.format("%.3f",HUI.getExactFrequency()));
buffer.append(" #Re: " + String.format("%.3f", HUI.getExactRecency()));
writer1.write(buffer.toString());
writer1.newLine();
}
/**
* Method to check the memory usage and keep the maximum memory usage.
*/
private void checkMemory() {
// get the current memory usage
double currentMemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024d / 1024d;
// if higher than the maximum until now replace the maximum with the current memory usage
if (currentMemory > maxMemory) {
maxMemory = currentMemory;
}
}
public void writeout_all_minRe() throws IOException{
java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
StringBuilder buffer3 = new StringBuilder();
buffer3.append(dataset);
buffer3.append(" ");
buffer3.append(minRecencyThreshold);
buffer3.append(" ");
buffer3.append((endTimestamp2 - startTimestamp)/1000.0 );
buffer3.append(" ");
buffer3.append(huiCount);
buffer3.append(" ");
buffer3.append(pRHUIsCount);
// write to file
writer3.write(buffer3.toString());
writer3.newLine();
writer3.flush();
}
public void writeout_all_minSup() throws IOException{
java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
StringBuilder buffer3 = new StringBuilder();
buffer3.append(dataset);
buffer3.append(" ");
buffer3.append(minFrequencyThreshold);
buffer3.append(" ");
buffer3.append((endTimestamp2 - startTimestamp)/1000.0 );
buffer3.append(" ");
buffer3.append(huiCount);
buffer3.append(" ");
buffer3.append(pRHUIsCount);
writer3.write(buffer3.toString());
writer3.newLine();
writer3.flush();
}
public void writeout_all_minUtil() throws IOException{
java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
StringBuilder buffer3 = new StringBuilder();
buffer3.append(dataset);
buffer3.append(" ");
buffer3.append(minUtilityThreshold);
buffer3.append(" ");
buffer3.append((endTimestamp2 - startTimestamp)/1000.0 );
buffer3.append(" ");
buffer3.append(huiCount);
buffer3.append(" ");
buffer3.append(pRHUIsCount);
writer3.write(buffer3.toString());
writer3.newLine();
writer3.flush();
}
/**
* Print statistics about the latest execution to System.out.
*/
public void printStats(double min_support,int minUtility, double minRecency) {
java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
System.out.println("============= fuzzy-RFU-tree !!! =============");
System.out.println("dataset: "+ dataset);
System.out.println("minFval: "+ minFrequencyThreshold+", "+"minRval: "+ minRecencyThreshold +", "+"minUval: "+ minUtilityThreshold);
System.out.println("Fval: "+String.format("%.1f", minF)+"~"+String.format("%.1f", maxF)+" , "+"Rval: "+String.format("%.1f", minR)+"~"+String.format("%.1f", maxR)+" , "+"Uval: "+String.format("%.1f", minU)+"~"+String.format("%.1f", maxU));
System.out.println(" Total time: " + (endTimestamp2 - startTimestamp) / 1000.0 + " s");
System.out.println(" Memory: " + df.format(maxMemory) + " MB");
System.out.println(" true_count: " + huiCount);
System.out.println(" candidates: " + pRHUIsCount);
System.out.println(" fuzzy time: "+ (endTimestamp3 - startTimestamp)/ 1000.0 + " s" );
System.out.println(" Preprocessing time: "+ (endTimestamp1 - startTimestamp)/ 1000.0 + " s" );
System.out.println(" mining time: "+ (endTimestamp4 - endTimestamp1)/ 1000.0 + " s" );
System.out.println("Phase-II: "+ (endTimestamp5 - endTimestamp4)/ 1000.0 + " s" );
}
}