-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClustering.java
More file actions
344 lines (290 loc) · 9.31 KB
/
Clustering.java
File metadata and controls
344 lines (290 loc) · 9.31 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
package force;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import force.Analysis.GeneralAnalysis.OccurrenceCounting;
import force.Help.ArraylistSort;
import force.Help.Report;
public class Clustering {
class ProposedGroups {
public Point2D getGroupCenter() {
return groupCenter;
}
public void setGroupCenter(Point2D groupCenter) {
this.groupCenter = groupCenter;
}
public void setaGroup(ArrayList<Point2D> aGroup) {
this.aGroup = aGroup;
}
public ArrayList<Point2D> getaGroup() {
return aGroup;
}
String groupName;
double aggregateEntrope;
ArrayList<Point2D> aGroup = new ArrayList<>();
Point2D groupCenter;
ArrayList<Double> aGroupStaistic = new ArrayList<>();
}
public class GroupingRequirements {
public double getTotalCoverage() {
return totalCoverage;
}
String groupingName;
double totalCoverage;
double eachGroupMemberNumber;
public double getEachGroupMemberNumber() {
return eachGroupMemberNumber;
}
public GroupingRequirements(String groupingName, double totalCoverage,
int eachGroupMemberNumber) {
super();
this.groupingName = groupingName;
this.totalCoverage = totalCoverage;
this.eachGroupMemberNumber = eachGroupMemberNumber;
}
public String getGroupingName() {
return groupingName;
}
public void setGroupingName(String groupingName) {
this.groupingName = groupingName;
}
}
public class RawData {
ArrayList<Point2D> rawData = new ArrayList<>();
String rawDataName;
public RawData(ArrayList<Double> xCoordinate,
ArrayList<Double> yCoordinate, String rawDataName) {
this.rawDataName = rawDataName;
ArrayList<Point2D> p2d = new ArrayList<>();
for (int i = 0; i < xCoordinate.size(); i++) {
Point2D aPoint = new Point2D.Double(xCoordinate.get(i),
yCoordinate.get(i));
p2d.add(aPoint);
}
rawData = p2d;
}
public ArrayList<Point2D> getRawData() {
return rawData;
}
public void setRawData(ArrayList<Point2D> rawData) {
this.rawData = rawData;
}
public String getRawDataName() {
return rawDataName;
}
public void setRawDataName(String rawDataName) {
this.rawDataName = rawDataName;
}
public void reportRawData() throws IOException {
Report r = new Help().new Report("raw data.txt");
r.report("all 2d points, first line for X, second line for Y\n X: ");
for (int i = 0; i < rawData.size(); i++) {
r.reportHorizontal(rawData.get(i).getX());
}
r.report("\n Y: ");
for (int i = 0; i < rawData.size(); i++) {
r.reportHorizontal(rawData.get(i).getY());
}
}
}
public class Cluster {
boolean isExpanded = false;
double currentCoverage;
ArraylistSort groupSort = new Help().new ArraylistSort();
GroupingRequirements clusterGroupingRequirements;
RawData clusterRawData;
ClusteringStatistic clusterClusteringStatistic;
ArrayList<ProposedGroups> groupList = new ArrayList<>();
ArrayList<ProposedGroups> tempGroupList = new ArrayList<>();
ArrayList<Point2D> coveredPoints = new ArrayList<>();
ArrayList<Double> alpgTotalEntrope = new ArrayList<>();
public Cluster(GroupingRequirements gr, RawData rd,
ClusteringStatistic cs) {
clusterClusteringStatistic = cs;
clusterGroupingRequirements = gr;
clusterRawData = rd;
}
public void doClustering() throws IOException, InterruptedException {
clusterClusteringStatistic.calculateStatistic(clusterRawData);
clusterClusteringStatistic.reportClusteringStatistic();
clusterClusteringStatistic.reportHowSorted();
propseGroup_Fisrt();
if (ifSatisfied()) {
System.out.println("the first step works.");
Thread.sleep(5111);
return;
} else {
isExpanded = true;
expandTheGroup();
currentCoverage = (double) coveredPoints.size()
/ (double) clusterRawData.getRawData().size();
System.out.println("the second step works.");
Thread.sleep(5111);
}
}
public void reportClustering() throws IOException {
Report r = new Help().new Report("clustering results.txt");
r.report("\n\n*******************\nclustering results are here:");
r.report("required minimum group member:,"
+ clusterGroupingRequirements.getEachGroupMemberNumber()
+ "\nrequired coverage percentage:,"
+ clusterGroupingRequirements.getTotalCoverage()
+ "\ntotal number of data:,"
+ clusterRawData.getRawData().size()
+ "\ncurrent coverage:," + currentCoverage);
if (isExpanded == true) {
r.report("clustering status:,Expanded clustering");
} else {
r.report("clustering status:,Non-expanded clustering");
}
r.report("***************************\n");
for (int i = 0; i < groupList.size(); i++) {
r.report("Group number:," + i);
for (Point2D aPoinInGroup : groupList.get(i).getaGroup()) {
r.report("x:," + aPoinInGroup.getX() + " ,Y:,"
+ aPoinInGroup.getY());
}
}
}
void expandTheGroup() {
for (ProposedGroups aPG : tempGroupList) {
for (int i = 0; i < aPG.getaGroup().size(); i++) {
for (ProposedGroups aDeterminedPG : groupList) {
if (aDeterminedPG.getaGroup().contains(
aPG.getaGroup().get(i))) {
for (Point2D aP : aPG.getaGroup()) {
if (coveredPoints.contains(aP) == false) {
aDeterminedPG.getaGroup().add(aP);
coveredPoints.add(aP);
}
}
}
}
}
}
}
public int coveredPoints() {
int a = 0;
for (ProposedGroups p : groupList) {
for (Point2D a2Dpoint : p.getaGroup()) {
a++;
}
}
System.out.println("total points covered:" + a);
return a;
}
private boolean ifSatisfied() throws InterruptedException {
currentCoverage = (double) coveredPoints.size()
/ (double) clusterRawData.getRawData().size();
System.out.println("thrashhold:"
+ clusterGroupingRequirements.totalCoverage);
System.out.println("actual percentage:" + currentCoverage);
Thread.sleep(2000);
if (currentCoverage >= clusterGroupingRequirements.totalCoverage) {
return true;
} else {
return false;
}
}
void propseGroup_Fisrt() {
for (int i = 0; i < clusterClusteringStatistic
.getClustringStatistic().size(); i++) {
double totalEntrope = 0;
for (int j = 0; j < clusterGroupingRequirements
.getEachGroupMemberNumber(); j++) {
totalEntrope = totalEntrope
+ clusterClusteringStatistic
.getClustringStatistic().get(i).get(j);
}
alpgTotalEntrope.add(totalEntrope);
}
groupSort.sortArraylistMembers(alpgTotalEntrope);
groupSort.howSorted();
for (int i = 0; i < groupSort.getHowSorted().size(); i++) {
ArrayList<Double> original = this.clusterClusteringStatistic
.getClustringStatistic().get(
groupSort.getHowSorted().get(i));
ArrayList<Point2D> tempP2D = new ArrayList<>();
int count = 0;
for (int j = 0; j < clusterGroupingRequirements
.getEachGroupMemberNumber(); j++) {
Point2D aPoint = clusterRawData.getRawData().get(
clusterClusteringStatistic.howSorted.get(
groupSort.getHowSorted().get(i)).get(j));
if (coveredPoints.contains(aPoint) == false) {
tempP2D.add(aPoint);
count++;
}
}
ProposedGroups pg;
Point2D aGoupCenter = clusterRawData.getRawData().get(
groupSort.getHowSorted().get(i));
pg = new ProposedGroups();
pg.setGroupCenter(aGoupCenter);
for (Point2D point2d : tempP2D) {
pg.getaGroup().add(point2d);
}
if (count == clusterGroupingRequirements
.getEachGroupMemberNumber()) {
for (Point2D onePoint2d : pg.getaGroup()) {
coveredPoints.add(onePoint2d);
}
groupList.add(pg);
} else {
tempGroupList.add(pg);
}
}
}
}
public class ClusteringStatistic {
RawData rawData;
ArraylistSort als = null;
String clustringStatisticName;
ArrayList<ArrayList<Double>> clustringStatistic = new ArrayList<>();
ArrayList<ArrayList<Integer>> howSorted = new ArrayList<>();
public void reportClusteringStatistic() throws IOException {
Report r = new Help().new Report(
"reportSortedClusteringStatistic.txt");
r.reportD2(clustringStatistic);
}
public void reportHowSorted() throws IOException {
Report r = new Help().new Report(
"reportClustringStatisticHowSorted.txt");
r.reportI2(howSorted);
}
public void calculateStatistic(RawData rd) throws IOException {
rawData = rd;
for (int i = 0; i < rd.getRawData().size(); i++) {
Point2D center = rd.getRawData().get(i);
ArrayList<Double> distances = new ArrayList<>();
for (int j = 0; j < rd.getRawData().size(); j++) {
Point2D target = rd.getRawData().get(j);
double distance = Math.sqrt(Math.pow(
target.getY() - center.getY(), 2)
+ Math.pow(target.getX() - center.getX(), 2));
distances.add(distance);
}
als = new Help().new ArraylistSort();
als.sortArraylistMembers(distances);
als.howSorted();
als.reportSortedlist();
clustringStatistic.add(als.getSortedArraylist());
howSorted.add(als.getHowSorted());
}
}
public String getClustringStatisticName() {
return clustringStatisticName;
}
public void setClustringStatisticName(String clustringStatisticName) {
this.clustringStatisticName = clustringStatisticName;
}
public ArrayList<ArrayList<Double>> getClustringStatistic() {
return clustringStatistic;
}
public void setClustringStatistic(
ArrayList<ArrayList<Double>> clustringStatistic) {
this.clustringStatistic = clustringStatistic;
}
}
}