3
3
import gov .sandia .cognition .learning .algorithm .clustering .AffinityPropagation ;
4
4
import gov .sandia .cognition .math .DivergenceFunction ;
5
5
6
+ import java .io .IOException ;
6
7
import java .util .ArrayList ;
7
8
import java .util .Date ;
8
9
import java .util .LinkedHashMap ;
15
16
import org .apache .commons .math3 .ml .clustering .DBSCANClusterer ;
16
17
import org .apache .commons .math3 .ml .clustering .DoublePoint ;
17
18
import org .apache .commons .math3 .ml .clustering .FuzzyKMeansClusterer ;
18
- import org .apache .commons .math3 .ml .clustering .KMeansPlusPlusClusterer ;
19
19
import org .apache .commons .math3 .ml .clustering .MultiKMeansPlusPlusClusterer ;
20
20
import org .apache .commons .math3 .ml .distance .DistanceMeasure ;
21
+ import org .battelle .clodhopper .tuple .ArrayTupleList ;
22
+ import org .battelle .clodhopper .tuple .TupleList ;
23
+ import org .battelle .clodhopper .xmeans .XMeansClusterer ;
24
+ import org .battelle .clodhopper .xmeans .XMeansParams ;
21
25
26
+ import br .ufu .facom .lsi .prefrec .clusterer .apache .KMeansPlusPlusClusterer ;
22
27
import br .ufu .facom .lsi .prefrec .clusterer .distance .CosineDistance ;
23
28
import br .ufu .facom .lsi .prefrec .clusterer .distance .MyEuclideanDistance ;
24
29
import br .ufu .facom .lsi .prefrec .clusterer .distance .MySimilarityDivergence ;
@@ -58,7 +63,7 @@ public void execute(PrefMatrixScorer pms, String[] args) {
58
63
dm );
59
64
60
65
List <Cluster <DoublePoint >> clusters = dbscan
61
- .cluster (toDoublePointList (pms ));
66
+ .cluster (toDoublePointList (pms , 0 ));
62
67
System .out .println (clusters .size ());
63
68
this .cluster = clusterVectorToMatrix (clusters , pms );
64
69
break ;
@@ -68,16 +73,17 @@ public void execute(PrefMatrixScorer pms, String[] args) {
68
73
Integer .parseInt (args [1 ]), Double .parseDouble (args [2 ]));
69
74
70
75
List <CentroidCluster <DoublePoint >> clustersFuzzy = fkmc
71
- .cluster (toDoublePointList (pms ));
76
+ .cluster (toDoublePointList (pms , 0 ));
72
77
73
78
this .cluster = clusterVectorToMatrix (clustersFuzzy , pms );
74
79
break ;
75
80
76
81
case "AFFINITY" :
77
82
AffinityPropagation <DoublePoint > ap = new AffinityPropagation <>();
83
+ int indx = Integer .parseInt (args [1 ]);
78
84
DivergenceFunction <DoublePoint , DoublePoint > df = new MySimilarityDivergence ();
79
85
ap .setDivergence (df );
80
- ap .learn (toDoublePointList (pms ));
86
+ ap .learn (toDoublePointList (pms , indx ));
81
87
82
88
List <gov .sandia .cognition .learning .algorithm .clustering .cluster .CentroidCluster <DoublePoint >> clustersAff = ap
83
89
.getResult ();
@@ -89,35 +95,79 @@ public void execute(PrefMatrixScorer pms, String[] args) {
89
95
case "KMEANSPLUSPLUS" :
90
96
MyEuclideanDistance distance = new MyEuclideanDistance ();
91
97
KMeansPlusPlusClusterer <DoublePoint > kMeanPlusPlus = new KMeansPlusPlusClusterer <>(
92
- 12 , -1 , distance );
98
+ 5 , -1 , distance );
93
99
List <CentroidCluster <DoublePoint >> clustersMKmeansPlusPlus = kMeanPlusPlus
94
- .cluster (toDoublePointList (pms ));
100
+ .cluster (toDoublePointList (pms , 0 ));
95
101
this .cluster = clusterVectorToMatrix (clustersMKmeansPlusPlus ,
96
102
pms );
97
103
break ;
98
104
99
105
case "MULTIKMEANS" :
100
- KMeansPlusPlusClusterer <DoublePoint > kMean = new KMeansPlusPlusClusterer <>(
106
+ org . apache . commons . math3 . ml . clustering . KMeansPlusPlusClusterer <DoublePoint > kMean = new org . apache . commons . math3 . ml . clustering . KMeansPlusPlusClusterer <>(
101
107
12 );
102
- MultiKMeansPlusPlusClusterer <DoublePoint > mKMeans = new MultiKMeansPlusPlusClusterer <>(
108
+ MultiKMeansPlusPlusClusterer <DoublePoint > mKMeans = new MultiKMeansPlusPlusClusterer <DoublePoint >(
103
109
kMean , 200 );
104
110
List <CentroidCluster <DoublePoint >> clustersMKmeans = mKMeans
105
- .cluster (toDoublePointList (pms ));
111
+ .cluster (toDoublePointList (pms , 0 ));
106
112
this .cluster = clusterVectorToMatrix (clustersMKmeans , pms );
107
113
break ;
108
114
109
- default :
115
+ case "XMEANS" :
116
+ int tupleLength = pms .getMatrixMap ().entrySet ().size ();
117
+ Long key = pms .getMatrixMap ().keySet ().iterator ().next ();
118
+ int tupleCount = pms .getMatrixMap ().get (key )[0 ].length ;
119
+ tupleCount = tupleCount * tupleCount ;
120
+ double [] data = new double [tupleLength * tupleCount ];
121
+ int counter = 0 ;
122
+ for (Long k : pms .getMatrixMap ().keySet ()) {
123
+ Double [][] d = pms .getMatrixMap ().get (k );
124
+ matrixLength = d .length ;
125
+ for (int i = 0 ; i < matrixLength ; i ++) {
126
+ for (int j = 0 ; j < d [i ].length ; j ++) {
127
+ if (d [i ][j ] == null ) {
128
+ data [counter ] = 0.0 ;
129
+ } else {
130
+ data [counter ] = d [i ][j ];
131
+ }
132
+ counter ++;
133
+ }
134
+ }
135
+ }
136
+
137
+ TupleList tupleData = new ArrayTupleList (tupleLength ,
138
+ tupleCount , data );
139
+
140
+ XMeansParams .Builder builder = new XMeansParams .Builder ();
141
+ XMeansParams params = builder .minClusters (3 ).maxClusters (10 )
142
+ .build ();
143
+ XMeansClusterer xmeans = new XMeansClusterer (tupleData , params );
144
+
145
+ Thread t = new Thread (xmeans );
146
+ t .start ();
147
+
148
+ List <org .battelle .clodhopper .Cluster > clusterResult = null ;
149
+ try {
150
+ // This blocks!
151
+ clusterResult = xmeans .get ();
152
+ } catch (Exception e ) {
153
+ e .printStackTrace ();
154
+ }
155
+
156
+ this .cluster = clusterToMatrix (clusterResult , pms );
110
157
158
+ break ;
159
+ default :
160
+ // No cluster
111
161
Map <Long , List <Map <Long , Double [][]>>> matrix = new LinkedHashMap <>();
112
162
113
163
List <Map <Long , Double [][]>> clusterList = new ArrayList <>();
114
164
for (Long id : pms .getMatrixMap ().keySet ()) {
115
-
165
+
116
166
Map <Long , Double [][]> userMatrix = new LinkedHashMap <>();
117
167
Double [][] matrixDouble = pms .getMatrixMap ().get (id );
118
168
userMatrix .put (id , matrixDouble );
119
169
clusterList .add (userMatrix );
120
-
170
+
121
171
}
122
172
matrix .put (0L , clusterList );
123
173
this .cluster = matrix ;
@@ -129,6 +179,18 @@ public void execute(PrefMatrixScorer pms, String[] args) {
129
179
}
130
180
}
131
181
182
+ private Map <Long , List <Map <Long , Double [][]>>> clusterToMatrix (
183
+ List <org .battelle .clodhopper .Cluster > clusterResult ,
184
+ PrefMatrixScorer pms ) {
185
+
186
+ Map <Long , List <Map <Long , Double [][]>>> matrix = new LinkedHashMap <>();
187
+
188
+ // TODO
189
+
190
+ return matrix ;
191
+
192
+ }
193
+
132
194
private Map <Long , List <Map <Long , Double [][]>>> clusterAffinityVectorToMatrix (
133
195
List <gov .sandia .cognition .learning .algorithm .clustering .cluster .CentroidCluster <DoublePoint >> clusters ,
134
196
PrefMatrixScorer pms ) {
@@ -145,16 +207,6 @@ private Map<Long, List<Map<Long, Double[][]>>> clusterAffinityVectorToMatrix(
145
207
Long id = userScoreDoublePoint .get (dp );
146
208
Double [][] matrixDouble = pms .getMatrixMap ().get (id );
147
209
148
- // for(int k = 0; k < matrixDouble.length; k++){
149
- // for(int h = 0; h < matrixDouble[k].length; h++){
150
-
151
- // System.out.print(matrixDouble[k][h]);
152
- // System.out.print(" ");
153
-
154
- // }
155
- // System.out.println();
156
- // }
157
-
158
210
userMatrix .put (id , matrixDouble );
159
211
clusterList .add (userMatrix );
160
212
}
@@ -204,24 +256,36 @@ private <T extends Cluster<DoublePoint>> Map<Long, List<Map<Long, Double[][]>>>
204
256
205
257
}
206
258
207
- private List <DoublePoint > toDoublePointList (PrefMatrixScorer pms ) {
259
+ private List <DoublePoint > toDoublePointList (PrefMatrixScorer pms , int indx )
260
+ throws IOException {
208
261
209
262
List <DoublePoint > result = new ArrayList <>();
263
+ // BufferedWriter bw = new BufferedWriter(new
264
+ // FileWriter("matrix_round"+indx));
210
265
211
266
for (Long key : pms .getMatrixMap ().keySet ()) {
212
267
Double [][] d = pms .getMatrixMap ().get (key );
213
268
List <Double > pointList = new ArrayList <>();
269
+
214
270
matrixLength = d .length ;
271
+ // int count=0;
272
+ // Double[] rawRm = new Double[d.length*d.length];
215
273
for (int i = 0 ; i < matrixLength ; i ++) {
216
- for (int j = i +1 ; j < d [i ].length ; j ++) {//teste era j=i+1
274
+
275
+ for (int j = i + 1 ; j < d [i ].length ; j ++) {// teste era j=i+1
217
276
if (d [i ][j ] == null ) {
218
277
d [i ][j ] = 0.0 ;
219
278
pointList .add (d [i ][j ]);
220
279
} else {
221
280
pointList .add (d [i ][j ]);
222
281
}
282
+ // rawRm[count] = d[i][j];
283
+ // bw.write(String.valueOf(rawRm[count]) + ";");
284
+ // count++;
285
+
223
286
}
224
287
}
288
+ // bw.write("\n");
225
289
// Create double point and add to the result list
226
290
Double [] ds = pointList .toArray (new Double [pointList .size ()]);
227
291
DoublePoint dp = new DoublePoint (ArrayUtils .toPrimitive (ds ));
@@ -230,6 +294,8 @@ private List<DoublePoint> toDoublePointList(PrefMatrixScorer pms) {
230
294
// the matrix score
231
295
userScoreDoublePoint .put (dp , key );
232
296
}
297
+ // bw.flush();
298
+ // bw.close();
233
299
return result ;
234
300
}
235
301
0 commit comments