-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFIMiner.java
215 lines (195 loc) · 7.93 KB
/
FFIMiner.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
/**
* This is an implementation of the Fuzzy-RFU-tree algorithm.
*
* @author Yanlin Qi, HIT, China
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class FFIMiner {
static List<Map<String,Double>> FFLs=new ArrayList<Map<String, Double>>();
static Map<Integer,Map<String,Double>> tranactions=new HashMap<Integer, Map<String, Double>>();
static Map<Integer,Map<String,Double>> fuzzyTranactions=new HashMap<Integer, Map<String, Double>>();
static Map<String,Map<String,Double>> ScarMap=new HashMap<String,Map<String,Double>>();
static Map<String, Double> maxScarMap=new HashMap<String, Double>();
static List<FuzzList> FLs=new ArrayList<FuzzList>();
static Map<String,Double> init(String input) throws IOException {
loadTranstions(input);
return getMaxScar();
}
static void loadTranstions(String input) throws IOException {
String trans_database_input = input;
String thisline;
BufferedReader br = new BufferedReader(new FileReader(trans_database_input));
int tid=0;
List<Double> count = new ArrayList<Double>();
double max = 11.0;
double min = 1.0;
while ((thisline = br.readLine()) !=null) {
tid++;
String[] itemAndQual = thisline.split(" ");
for (String itemAndQualOne : itemAndQual) {
String[] tmp = itemAndQualOne.split(",");
if (tmp.length == 2) {
count.add(Double.parseDouble(tmp[1]));
}
}
}
max = Collections.max(count);
min = Collections.min(count);
BufferedReader br1 = new BufferedReader(new FileReader(trans_database_input));
int tid1=0;
while ((thisline = br1.readLine()) !=null) {
tid1++;
Map<String, Double> tranOne = new HashMap<String, Double>();
Map<String, Double> fuzzTranOne = new LinkedHashMap<String, Double>();//LinkedHashMap有序
String[] itemAndQual = thisline.split(" ");
for (String itemAndQualOne : itemAndQual) {
String[] tmp = itemAndQualOne.split(",");
if (tmp.length==2 && tmp[1] != null) {
tranOne.put(tmp[0], Double.parseDouble(tmp[1]));
Map<String, Double> temp = fuzzyTriangular(tmp[0], 1.5*max, 0.5*min, 3, Double.parseDouble(tmp[1]));//1.5*max, 0.5*min,
for (String key : temp.keySet()) {
fuzzTranOne.put(key, temp.get(key));
}
if (ScarMap.isEmpty() || null == ScarMap.get(tmp[0])) {
ScarMap.put(tmp[0], temp);
} else {
Map<String, Double> map = ScarMap.get(tmp[0]);
for (String key : temp.keySet()) {
double sup = map.get(key);
sup += temp.get(key);
map.put(key, sup);
}
}
}
tranactions.put(tid1, tranOne);
fuzzyTranactions.put(tid1, fuzzTranOne);
}
}
br.close();
}
static Map<String,Double> getMaxScar(){
Map<String,Double> res = new HashMap<String, Double>();
for (String item: ScarMap.keySet()) {
Double maxScar = -1.0;
String maxScarFuzz = "";
for (String fuzzItem : ScarMap.get(item).keySet()) {
double tmp = ScarMap.get(item).get(fuzzItem);
if (tmp > maxScar){
maxScar = tmp;
maxScarFuzz = fuzzItem;
}
}
res.put(maxScarFuzz, maxScar);
}
res = sortMap(res);
return res;
}
static FuzzList geneList(){
ArrayList<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(getMaxScar().entrySet());//??为什么list直接被赋值了?
HashMap<String,Double> itemAndfuzzy =new HashMap<>();
HashMap<Integer,HashMap<String,Double>> newtrans=new HashMap<>();
FuzzList fuzzList = new FuzzList();
for (int i = 0; i < list.size(); i++) {
String key=list.get(i).getKey();
fuzzList.getFuzzyItems().add(key);
for (Integer ts:fuzzyTranactions.keySet()) {
Element element=new Element(ts);
Map<String,Double> fuzzValue = fuzzyTranactions.get(ts);
if(fuzzValue.containsKey(key) && Double.compare(fuzzValue.get(key),0.0)==1){
element.setIF(fuzzValue.get(key));
element.setFuzzyitem(key);
itemAndfuzzy.put(key,element.IF);
fuzzList.getList().add(element);
}
newtrans.put(ts,itemAndfuzzy);
}
FLs.add(fuzzList);
}
return fuzzList;
}
static Map<String,Double> fuzzyTriangular(String item, double max, double min, int regions, double x){
Map<String,Double> res=new LinkedHashMap<String,Double>();
double[] low=new double[regions];
double[] mid=new double[regions];
double[] high=new double[regions];
double fuzzyValue = 0.0;
double s=(max-min)/(regions-1);
for (int i=0;i<regions;i++){
mid[i]=min+s*i;
low[i]=mid[i]-s;
high[i]=mid[i]+s;
if (i == 0) {
fuzzyValue=fuzzyCore2(low[i], mid[i], high[i], x);//代入三角隶属函数计算值
res.put(item + ".L",fuzzyValue);
} else if (i == 1) {
fuzzyValue=fuzzyCore(low[i], mid[i], high[i], x);
res.put(item + ".M",fuzzyValue);
} else if (i == 2) {
fuzzyValue=fuzzyCore1(low[i], mid[i], high[i], x);
res.put(item + ".H",fuzzyValue);
}
}
return res;
}
static double fuzzyCore(double low, double mid, double high, double x){
double res=0.0;
if (x<=low){
res=0.0;
}else if (x<=mid){
res=(float)((x-low)/(mid-low));
}else if (x<=high){
res=(float)((high-x)/(high-mid));
}else {
res=0.0;
}
return res;
}
static double fuzzyCore1(double low, double mid, double high, double x){
double res=0.0;
if (x<=low){
res=0.0;
}else if (x <= mid){
res=(float)((x-low)/(mid-low));
}else {
res= 1.0;
}
return res;
}
static double fuzzyCore2(double low, double mid, double high, double x){
double res=0.0;
if (x<=mid){
res = 1.0;
}else if (x <= high){
res =(float)((high-x)/(high-mid));
}else {
res= 0.0;
}
return res;
}
public static Map sortMap(Map oldMap) {
ArrayList<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(oldMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
@Override
public int compare(Map.Entry<String, Double> arg0,
Map.Entry<String, Double> arg1) {
int res=Double.compare(arg0.getValue(),arg1.getValue());
if(res>0)
{
return 1;
}else if (res<0){
return -1;
}else {
return 0;
}
}
});
Map newMap = new LinkedHashMap();
for (int i = 0; i < list.size(); i++) {
newMap.put(list.get(i).getKey(), list.get(i).getValue());
}
return newMap;
}
}