Skip to content

Commit 0717b84

Browse files
MylleneMyllene
Myllene
authored and
Myllene
committed
2 parents 2d20b4c + 07fb44c commit 0717b84

File tree

9 files changed

+690
-42
lines changed

9 files changed

+690
-42
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<artifactId>cprefminermulti</artifactId>
6767
<version>1.0.4</version>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.battelle</groupId>
71+
<artifactId>clodhopper-core</artifactId>
72+
<version>1.0.0</version>
73+
</dependency>
6974
</dependencies>
7075

7176
<build>

prefrec-execution/src/main/java/br/ufu/facom/lsi/prefrec/execution/crossvalidation/CrossValidation.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.io.IOException;
77
import java.nio.file.Files;
88
import java.nio.file.Paths;
9-
109
import java.nio.file.StandardOpenOption;
1110
import java.util.ArrayList;
1211
import java.util.HashMap;
@@ -62,16 +61,19 @@ public void execute() {
6261
Clusterer clusterer = new Clusterer();
6362
//clusterer.execute(representer.getPrefMatrixScorer(), new String[]
6463
//{
65-
//"DBSCAN", "euclidian", "0.1", "4" });
64+
//"DBSCAN", "cosine", "1", "4" });
6665
//clusterer.execute(representer.getPrefMatrixScorer(),
6766
//new String[] { "MULTIKMEANS" });
6867
//clusterer.execute(representer.getPrefMatrixScorer(),
69-
// new String[] { "AFFINITY" });
68+
//new String[] { "KMEANSPLUSPLUS" });
69+
clusterer.execute(representer.getPrefMatrixScorer(),
70+
new String[] { "AFFINITY" ,String.valueOf(i)});
71+
72+
// clusterer.execute(representer.getPrefMatrixScorer(), new String[]
73+
// {
74+
// "FUZZY", "4", "1.2" });
75+
//clusterer.execute(representer.getPrefMatrixScorer(), new String[]{"XMEANS"});
7076

71-
clusterer.execute(representer.getPrefMatrixScorer(), new String[]
72-
{
73-
"FUZZY", "4", "1.2" });
74-
7577
//clusterer.execute(representer.getPrefMatrixScorer(), new String[]{""});
7678

7779
Agregator agregator = new Agregator(clusterer.getCluster());
@@ -86,7 +88,7 @@ public void execute() {
8688
try {
8789

8890
miner.buildModels(new ArrayList<Double[][]>(agregator
89-
.getConcensualMatrixMap().keySet()));
91+
.getConcensualMatrixMap().keySet()), itemList);
9092
} catch (Exception e1) {
9193
e1.printStackTrace();
9294
}

prefrec-pref-clusterer/src/main/java/br/ufu/facom/lsi/prefrec/clusterer/Clusterer.java

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import gov.sandia.cognition.learning.algorithm.clustering.AffinityPropagation;
44
import gov.sandia.cognition.math.DivergenceFunction;
55

6+
import java.io.IOException;
67
import java.util.ArrayList;
78
import java.util.Date;
89
import java.util.LinkedHashMap;
@@ -15,10 +16,14 @@
1516
import org.apache.commons.math3.ml.clustering.DBSCANClusterer;
1617
import org.apache.commons.math3.ml.clustering.DoublePoint;
1718
import org.apache.commons.math3.ml.clustering.FuzzyKMeansClusterer;
18-
import org.apache.commons.math3.ml.clustering.KMeansPlusPlusClusterer;
1919
import org.apache.commons.math3.ml.clustering.MultiKMeansPlusPlusClusterer;
2020
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;
2125

26+
import br.ufu.facom.lsi.prefrec.clusterer.apache.KMeansPlusPlusClusterer;
2227
import br.ufu.facom.lsi.prefrec.clusterer.distance.CosineDistance;
2328
import br.ufu.facom.lsi.prefrec.clusterer.distance.MyEuclideanDistance;
2429
import br.ufu.facom.lsi.prefrec.clusterer.distance.MySimilarityDivergence;
@@ -58,7 +63,7 @@ public void execute(PrefMatrixScorer pms, String[] args) {
5863
dm);
5964

6065
List<Cluster<DoublePoint>> clusters = dbscan
61-
.cluster(toDoublePointList(pms));
66+
.cluster(toDoublePointList(pms, 0));
6267
System.out.println(clusters.size());
6368
this.cluster = clusterVectorToMatrix(clusters, pms);
6469
break;
@@ -68,16 +73,17 @@ public void execute(PrefMatrixScorer pms, String[] args) {
6873
Integer.parseInt(args[1]), Double.parseDouble(args[2]));
6974

7075
List<CentroidCluster<DoublePoint>> clustersFuzzy = fkmc
71-
.cluster(toDoublePointList(pms));
76+
.cluster(toDoublePointList(pms, 0));
7277

7378
this.cluster = clusterVectorToMatrix(clustersFuzzy, pms);
7479
break;
7580

7681
case "AFFINITY":
7782
AffinityPropagation<DoublePoint> ap = new AffinityPropagation<>();
83+
int indx = Integer.parseInt(args[1]);
7884
DivergenceFunction<DoublePoint, DoublePoint> df = new MySimilarityDivergence();
7985
ap.setDivergence(df);
80-
ap.learn(toDoublePointList(pms));
86+
ap.learn(toDoublePointList(pms, indx));
8187

8288
List<gov.sandia.cognition.learning.algorithm.clustering.cluster.CentroidCluster<DoublePoint>> clustersAff = ap
8389
.getResult();
@@ -89,35 +95,79 @@ public void execute(PrefMatrixScorer pms, String[] args) {
8995
case "KMEANSPLUSPLUS":
9096
MyEuclideanDistance distance = new MyEuclideanDistance();
9197
KMeansPlusPlusClusterer<DoublePoint> kMeanPlusPlus = new KMeansPlusPlusClusterer<>(
92-
12, -1, distance);
98+
5, -1, distance);
9399
List<CentroidCluster<DoublePoint>> clustersMKmeansPlusPlus = kMeanPlusPlus
94-
.cluster(toDoublePointList(pms));
100+
.cluster(toDoublePointList(pms, 0));
95101
this.cluster = clusterVectorToMatrix(clustersMKmeansPlusPlus,
96102
pms);
97103
break;
98104

99105
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<>(
101107
12);
102-
MultiKMeansPlusPlusClusterer<DoublePoint> mKMeans = new MultiKMeansPlusPlusClusterer<>(
108+
MultiKMeansPlusPlusClusterer<DoublePoint> mKMeans = new MultiKMeansPlusPlusClusterer<DoublePoint>(
103109
kMean, 200);
104110
List<CentroidCluster<DoublePoint>> clustersMKmeans = mKMeans
105-
.cluster(toDoublePointList(pms));
111+
.cluster(toDoublePointList(pms, 0));
106112
this.cluster = clusterVectorToMatrix(clustersMKmeans, pms);
107113
break;
108114

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);
110157

158+
break;
159+
default:
160+
// No cluster
111161
Map<Long, List<Map<Long, Double[][]>>> matrix = new LinkedHashMap<>();
112162

113163
List<Map<Long, Double[][]>> clusterList = new ArrayList<>();
114164
for (Long id : pms.getMatrixMap().keySet()) {
115-
165+
116166
Map<Long, Double[][]> userMatrix = new LinkedHashMap<>();
117167
Double[][] matrixDouble = pms.getMatrixMap().get(id);
118168
userMatrix.put(id, matrixDouble);
119169
clusterList.add(userMatrix);
120-
170+
121171
}
122172
matrix.put(0L, clusterList);
123173
this.cluster = matrix;
@@ -129,6 +179,18 @@ public void execute(PrefMatrixScorer pms, String[] args) {
129179
}
130180
}
131181

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+
132194
private Map<Long, List<Map<Long, Double[][]>>> clusterAffinityVectorToMatrix(
133195
List<gov.sandia.cognition.learning.algorithm.clustering.cluster.CentroidCluster<DoublePoint>> clusters,
134196
PrefMatrixScorer pms) {
@@ -145,16 +207,6 @@ private Map<Long, List<Map<Long, Double[][]>>> clusterAffinityVectorToMatrix(
145207
Long id = userScoreDoublePoint.get(dp);
146208
Double[][] matrixDouble = pms.getMatrixMap().get(id);
147209

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-
158210
userMatrix.put(id, matrixDouble);
159211
clusterList.add(userMatrix);
160212
}
@@ -204,24 +256,36 @@ private <T extends Cluster<DoublePoint>> Map<Long, List<Map<Long, Double[][]>>>
204256

205257
}
206258

207-
private List<DoublePoint> toDoublePointList(PrefMatrixScorer pms) {
259+
private List<DoublePoint> toDoublePointList(PrefMatrixScorer pms, int indx)
260+
throws IOException {
208261

209262
List<DoublePoint> result = new ArrayList<>();
263+
// BufferedWriter bw = new BufferedWriter(new
264+
// FileWriter("matrix_round"+indx));
210265

211266
for (Long key : pms.getMatrixMap().keySet()) {
212267
Double[][] d = pms.getMatrixMap().get(key);
213268
List<Double> pointList = new ArrayList<>();
269+
214270
matrixLength = d.length;
271+
// int count=0;
272+
// Double[] rawRm = new Double[d.length*d.length];
215273
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
217276
if (d[i][j] == null) {
218277
d[i][j] = 0.0;
219278
pointList.add(d[i][j]);
220279
} else {
221280
pointList.add(d[i][j]);
222281
}
282+
// rawRm[count] = d[i][j];
283+
// bw.write(String.valueOf(rawRm[count]) + ";");
284+
// count++;
285+
223286
}
224287
}
288+
// bw.write("\n");
225289
// Create double point and add to the result list
226290
Double[] ds = pointList.toArray(new Double[pointList.size()]);
227291
DoublePoint dp = new DoublePoint(ArrayUtils.toPrimitive(ds));
@@ -230,6 +294,8 @@ private List<DoublePoint> toDoublePointList(PrefMatrixScorer pms) {
230294
// the matrix score
231295
userScoreDoublePoint.put(dp, key);
232296
}
297+
// bw.flush();
298+
// bw.close();
233299
return result;
234300
}
235301

0 commit comments

Comments
 (0)