Skip to content

Commit 320e530

Browse files
author
linyiqun
committed
粗糙集属性约简算法
粗糙集属性约简算法
1 parent f6cb943 commit 320e530

File tree

6 files changed

+930
-0
lines changed

6 files changed

+930
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package DataMining_RoughSets;
2+
3+
/**
4+
* ´Ö²Ú¼¯Ô¼¼òËã·¨
5+
* @author lyq
6+
*
7+
*/
8+
public class Client {
9+
public static void main(String[] args){
10+
String filePath = "C:\\Users\\lyq\\Desktop\\icon\\input.txt";
11+
12+
RoughSetsTool tool = new RoughSetsTool(filePath);
13+
tool.findingReduct();
14+
}
15+
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package DataMining_RoughSets;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
6+
/**
7+
* 知识系统
8+
*
9+
* @author lyq
10+
*
11+
*/
12+
public class KnowledgeSystem {
13+
// 知识系统内的集合
14+
ArrayList<RecordCollection> ksCollections;
15+
16+
public KnowledgeSystem(ArrayList<RecordCollection> ksCollections) {
17+
this.ksCollections = ksCollections;
18+
}
19+
20+
/**
21+
* 获取集合的上近似集合
22+
*
23+
* @param rc
24+
* 原始集合
25+
* @return
26+
*/
27+
public RecordCollection getUpSimilarRC(RecordCollection rc) {
28+
RecordCollection resultRc = null;
29+
ArrayList<String> nameArray;
30+
ArrayList<String> targetArray;
31+
ArrayList<RecordCollection> copyRcs = new ArrayList<>();
32+
ArrayList<RecordCollection> deleteRcs = new ArrayList<>();
33+
targetArray = rc.getRecordNames();
34+
35+
// 做一个集合拷贝
36+
for (RecordCollection recordCollection : ksCollections) {
37+
copyRcs.add(recordCollection);
38+
}
39+
40+
for (RecordCollection recordCollection : copyRcs) {
41+
nameArray = recordCollection.getRecordNames();
42+
43+
if (strIsContained(targetArray, nameArray)) {
44+
removeOverLaped(targetArray, nameArray);
45+
deleteRcs.add(recordCollection);
46+
47+
if (resultRc == null) {
48+
resultRc = recordCollection;
49+
} else {
50+
// 进行并运算
51+
resultRc = resultRc.unionCal(recordCollection);
52+
}
53+
54+
if (targetArray.size() == 0) {
55+
break;
56+
}
57+
}
58+
}
59+
//去除已经添加过的集合
60+
copyRcs.removeAll(deleteRcs);
61+
62+
if (targetArray.size() > 0) {
63+
// 说明已经完全还未找全上近似的集合
64+
for (RecordCollection recordCollection : copyRcs) {
65+
nameArray = recordCollection.getRecordNames();
66+
67+
if (strHasOverlap(targetArray, nameArray)) {
68+
removeOverLaped(targetArray, nameArray);
69+
70+
if (resultRc == null) {
71+
resultRc = recordCollection;
72+
} else {
73+
// 进行并运算
74+
resultRc = resultRc.unionCal(recordCollection);
75+
}
76+
77+
if (targetArray.size() == 0) {
78+
break;
79+
}
80+
}
81+
}
82+
}
83+
84+
return resultRc;
85+
}
86+
87+
/**
88+
* 获取集合的下近似集合
89+
*
90+
* @param rc
91+
* 原始集合
92+
* @return
93+
*/
94+
public RecordCollection getDownSimilarRC(RecordCollection rc) {
95+
RecordCollection resultRc = null;
96+
ArrayList<String> nameArray;
97+
ArrayList<String> targetArray;
98+
targetArray = rc.getRecordNames();
99+
100+
for (RecordCollection recordCollection : ksCollections) {
101+
nameArray = recordCollection.getRecordNames();
102+
103+
if (strIsContained(targetArray, nameArray)) {
104+
removeOverLaped(targetArray, nameArray);
105+
106+
if (resultRc == null) {
107+
resultRc = recordCollection;
108+
} else {
109+
// 进行并运算
110+
resultRc = resultRc.unionCal(recordCollection);
111+
}
112+
113+
if (targetArray.size() == 0) {
114+
break;
115+
}
116+
}
117+
}
118+
119+
return resultRc;
120+
}
121+
122+
/**
123+
* 判断2个字符数组之间是否有交集
124+
*
125+
* @param str1
126+
* 字符列表1
127+
* @param str2
128+
* 字符列表2
129+
* @return
130+
*/
131+
public boolean strHasOverlap(ArrayList<String> str1, ArrayList<String> str2) {
132+
boolean hasOverlap = false;
133+
134+
for (String s1 : str1) {
135+
for (String s2 : str2) {
136+
if (s1.equals(s2)) {
137+
hasOverlap = true;
138+
break;
139+
}
140+
}
141+
142+
if (hasOverlap) {
143+
break;
144+
}
145+
}
146+
147+
return hasOverlap;
148+
}
149+
150+
/**
151+
* 判断字符集str2是否完全包含于str1中
152+
*
153+
* @param str1
154+
* @param str2
155+
* @return
156+
*/
157+
public boolean strIsContained(ArrayList<String> str1, ArrayList<String> str2) {
158+
boolean isContained = false;
159+
int count = 0;
160+
161+
for (String s : str2) {
162+
if (str1.contains(s)) {
163+
count++;
164+
}
165+
}
166+
167+
if (count == str2.size()) {
168+
isContained = true;
169+
}
170+
171+
return isContained;
172+
}
173+
174+
/**
175+
* 字符列表移除公共元素
176+
*
177+
* @param str1
178+
* @param str2
179+
*/
180+
public void removeOverLaped(ArrayList<String> str1, ArrayList<String> str2) {
181+
ArrayList<String> deleteStrs = new ArrayList<>();
182+
183+
for (String s1 : str1) {
184+
for (String s2 : str2) {
185+
if (s1.equals(s2)) {
186+
deleteStrs.add(s1);
187+
break;
188+
}
189+
}
190+
}
191+
192+
// 进行公共元素的移除
193+
str1.removeAll(deleteStrs);
194+
}
195+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package DataMining_RoughSets;
2+
3+
import java.text.MessageFormat;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 数据记录,包含这条记录所有属性
10+
*
11+
* @author lyq
12+
*
13+
*/
14+
public class Record {
15+
// 记录名称
16+
private String name;
17+
// 记录属性键值对
18+
private HashMap<String, String> attrValues;
19+
20+
public Record(String name, HashMap<String, String> attrValues) {
21+
this.name = name;
22+
this.attrValues = attrValues;
23+
}
24+
25+
public String getName() {
26+
return this.name;
27+
}
28+
29+
/**
30+
* 此数据是否包含此属性值
31+
*
32+
* @param attr
33+
* 待判断属性值
34+
* @return
35+
*/
36+
public boolean isContainedAttr(String attr) {
37+
boolean isContained = false;
38+
39+
if (attrValues.containsValue(attr)) {
40+
isContained = true;
41+
}
42+
43+
return isContained;
44+
}
45+
46+
/**
47+
* 判断数据记录是否是同一条记录,根据数据名称来判断
48+
*
49+
* @param record
50+
* 目标比较对象
51+
* @return
52+
*/
53+
public boolean isRecordSame(Record record) {
54+
boolean isSame = false;
55+
56+
if (this.name.equals(record.name)) {
57+
isSame = true;
58+
}
59+
60+
return isSame;
61+
}
62+
63+
/**
64+
* 数据的决策属性分类
65+
*
66+
* @return
67+
*/
68+
public String getRecordDecisionClass() {
69+
String value = null;
70+
71+
value = attrValues.get(RoughSetsTool.DECISION_ATTR_NAME);
72+
73+
return value;
74+
}
75+
76+
/**
77+
* 根据约简属性输出决策规则
78+
*
79+
* @param reductAttr
80+
* 约简属性集合
81+
*/
82+
public String getDecisionRule(ArrayList<String> reductAttr) {
83+
String ruleStr = "";
84+
String attrName = null;
85+
String value = null;
86+
String decisionValue;
87+
88+
decisionValue = attrValues.get(RoughSetsTool.DECISION_ATTR_NAME);
89+
ruleStr += "属性";
90+
for (Map.Entry entry : this.attrValues.entrySet()) {
91+
attrName = (String) entry.getKey();
92+
value = (String) entry.getValue();
93+
94+
if (attrName.equals(RoughSetsTool.DECISION_ATTR_NAME)
95+
|| reductAttr.contains(attrName) || value.equals(name)) {
96+
continue;
97+
}
98+
99+
ruleStr += MessageFormat.format("{0}={1},", attrName, value);
100+
}
101+
ruleStr += "他的分类为" + decisionValue;
102+
103+
return ruleStr;
104+
}
105+
}

0 commit comments

Comments
 (0)