-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGSP.java
More file actions
461 lines (242 loc) · 9.11 KB
/
GSP.java
File metadata and controls
461 lines (242 loc) · 9.11 KB
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
package gsp;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
//
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
//
public class GSP {
private ArrayList<Sequence> c; //长度为i的候选序列模式
private ArrayList<Sequence> l; //长度为i的序列模式
private ArrayList<Sequence> result;
private SeqDB db;
private int support;
//////////////////////////
///////////////////////////
/**
* 构造方法
* 在实例化GSP对象时,同时赋值支持度
* 并获取序列集和初始化序列模式结果
* @param support 支持度
*/
public GSP(int support) {
this.support = support; //赋值支持度
this.db = new SeqDB(); //从SeqDB类中获取设置好的序列集
this.result = new ArrayList<Sequence>(); //初始化序列模式结果对象
}
/**
* 产生序列模式
* 核心方法,在该方法中调用连接和剪枝操作,并将最后获得的序列模式放到result中
* @return 序列模式
*/
public ArrayList getSequences() {
long start = System.currentTimeMillis();
//调用初始化方法
initialize();
try(PrintStream ps = new PrintStream(new FileOutputStream("D:\\data.txt",true))){
System.setOut(ps);
System.out.println("序列模式L(1) 为:" +l);
}catch(IOException e){
e.printStackTrace();
}
// System.out.println(".................................................");
for (int i = 0; i < l.size(); i++) {
//产生进行连接操作后的候选集
genCandidate();
if (!(c.size() > 0)) {
break;
}
// System.out.println("剪枝前候选集的大小为:"+c.size()+" 候选集c为:"+c);
//进行剪枝操作
pruneC();
// System.out.println("剪枝后候选集的大小为:"+c.size()+" 候选集c为:"+c);
//产生序列模式
generateL();
try(PrintStream ps = new PrintStream(new FileOutputStream("D:\\data.txt",true))){
System.setOut(ps);
System.out.println("序列模式L(" + (i + 2) + ") 为:" +l);
}catch(IOException e){
e.printStackTrace();
}
addToResult(l);
// System.out.println(".................................................");
}
long end = System.currentTimeMillis();
// System.out.println("计算花费时间" + (end - start) + "毫秒!");
return this.result;
}
/*
* 初始化方法
* 获取设置好的序列集
*/
private void initialize() {
Map<Integer, Integer> can = new HashMap<Integer, Integer>();
//对于序列集中的所有序列
for (Sequence s : db.getSeqs()) {
//对于序列中的所有项目集
for (Element e : s.getElements()) {
//对于项目集中的所有项目
for (int i : e.getItems()) {
//比较项目的出现次数,并计数,用于与支持度比较
if (can.containsKey(i)) {
int count = can.get(i).intValue() + 1;
can.put(i, count);
} else {
can.put(i, 1);
}
}
}
}
this.l = new ArrayList<Sequence>();
//对于产生的候选集,如果支持度大于最小支持度阈值,则添加到序列模式L中
for (int i : can.keySet()) {
if (can.get(i).intValue() >= support) {
Element e = new Element(new int[] {i});
Sequence seq = new Sequence();
seq.addElement(e);
this.l.add(seq);
}
}
//将第一次频繁序列模式加入结果集中
this.addToResult(l);
}
/*
* 产生经过连接操作后的候选集
*
*/
private void genCandidate() {
this.c = new ArrayList<Sequence>();
//对于种子集L进行连接操作
for (int i = 0; i < this.l.size(); i++) {
for (int j = i; j < this.l.size(); j++) {
this.joinAndInsert(l.get(i), l.get(j));
if (i != j) {
this.joinAndInsert(l.get(j), l.get(i));
}
}
}
}
/*
* 对种子集进行连接操作
*/
private void joinAndInsert(Sequence s1, Sequence s2) {
Sequence s, st;
//去除第一个元素
Element ef = s1.getElement(0).getWithoutFistItem();
//去除最后一个元素
Element ee = s2.getElement(s2.size() - 1).getWithoutLastItem();
int i = 0, j = 0;
if (ef.size() == 0) {
i++;
}
for (; i < s1.size() && j < s2.size(); i++, j++) {
Element e1, e2;
if (i == 0) {
e1 = ef;
} else {
e1 = s1.getElement(i);
}
if (j == s2.size() - 1) {
e2 = ee;
} else {
e2 = s2.getElement(j);
}
if (!e1.equalsTo(e2)) {
return;
}
} //end of for
s = new Sequence(s1);
//将s2的最后一个元素添加到s中
(s.getElement(s.size() - 1)).addItem(s2.getElement(s2.size() - 1).
getLastItem());
//如果候选集中没有s,则添加到候选集
if (s.notInSeqs(c)) {
c.add(s);
}
st = new Sequence(s1);
//将s2的最后一个元素添加到st中
st.addElement(new Element(new int[] {s2.getElement(s2.size() - 1).
getLastItem()}));
//如果候选集中没有st,则添加到候选集
if (st.notInSeqs(c)) {
c.add(st);
}
return;
}
/*
* 剪枝操作
* 看每个候选序列的连续子序列是不是频繁序列
* 采用逐个取元素,只去其中一个项目,然后看是不是有相应的频繁序列在l中。
* 如果元素只有一个项目,则去除该元素做相应判断。
*/
private void pruneC() {
Sequence s;
//对于序列中的所有元素
for (int i = 0; i < this.c.size();i++) {
s = c.get(i);
//对于元素中的所有项目
for (int j = 0; j < s.size(); j++) {
Element ce = s.getElement(j);
boolean prune=false;
//只有一个元素的情况
if (ce.size() == 1) {
s.removeElement(j);
//如果子序列不是序列模式,则将它从候选序列模式中删除,否则添加
if (s.notInSeqs(this.l)) {
prune=true;
}
s.insertElement(j, ce);
} else {
for (int k = 0; k < ce.size(); k++) {
int item=ce.removeItem(k);
//如果子序列不是序列模式,则将它从候选序列模式中删除。否则添加
if (s.notInSeqs(this.l)) {
prune=true;
}
ce.addItem(item);
}
}
//如果剪枝,则将该序列删除
if(prune){
c.remove(i);
i--;
break;
}
}
}
}
/*
* 生成序列模式L
* 用于已经经过连接和剪枝操作后的后选序列集
*/
private void generateL() {
this.l = new ArrayList<Sequence>();
for (Sequence s : db.getSeqs()) {
for (Sequence seq : this.c) {
if (seq.isSubsequenceOf(s)) {
//支持度计数
seq.incrementSupport();
}
}
}
for (Sequence seq : this.c) {
//大于最小支持度阈值的放到序列模式中
if (seq.getSupport() >= this.support) {
this.l.add(seq);
}
}
}
/*
* 将该频繁序列模式加入结果中
*/
private void addToResult(ArrayList<Sequence> l) {
for (int i = 0; i < l.size(); i++) {
this.result.add(l.get(i));
}
}