Aufgrund einer Wartung wird GitLab am 28.09. zwischen 10:00 und 11:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 28.09. between 10:00 and 11:00 am.

DBSCAN: proof of concept

parent 1bcf007d
package de.monticore.lang.monticar.generator.middleware.clustering.algorithms;
import smile.math.distance.Metric;
import java.io.Serializable;
public class DBSCANDistance implements Metric<double[]>, Serializable {
private static final long serialVersionUID = 1L;
private double[][] weightedAdjacencyMatrix = null;
public DBSCANDistance(double[][] weightedAdjacencyMatrix) {
this.weightedAdjacencyMatrix = weightedAdjacencyMatrix;
}
public String toString() {
return String.format("DBSCAN distance");
}
public double d(double[] x, double[] y) {
System.out.println((int)x[0] + ", " + (int)y[0] + ": " + weightedAdjacencyMatrix[(int)x[0]][(int)y[0]]);
if (x.length != y.length) {
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
} else {
double ret= weightedAdjacencyMatrix[(int)x[0]][(int)y[0]];
return ret > 0 ? ret : Double.MAX_VALUE; // set zeros in adj. matrix to infinity
}
}
}
...@@ -6,6 +6,7 @@ import de.monticore.lang.monticar.generator.middleware.clustering.AutomaticClust ...@@ -6,6 +6,7 @@ import de.monticore.lang.monticar.generator.middleware.clustering.AutomaticClust
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm; import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithmFactory; import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithmFactory;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringKind; import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringKind;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.DBSCANDistance;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.MarkovClusteringAlgorithm; import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.MarkovClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringAlgorithm; import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringBuilder; import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringBuilder;
...@@ -21,8 +22,10 @@ import net.sf.javaml.core.DefaultDataset; ...@@ -21,8 +22,10 @@ import net.sf.javaml.core.DefaultDataset;
import net.sf.javaml.core.DenseInstance; import net.sf.javaml.core.DenseInstance;
import net.sf.javaml.core.Instance; import net.sf.javaml.core.Instance;
import org.junit.Test; import org.junit.Test;
import smile.clustering.DBSCAN;
import smile.clustering.KMeans; import smile.clustering.KMeans;
import smile.clustering.SpectralClustering; import smile.clustering.SpectralClustering;
import smile.math.distance.MinkowskiDistance;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
...@@ -148,6 +151,82 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{ ...@@ -148,6 +151,82 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
return output; return output;
} }
@Test
public void testDBSCANClustering(){
/*
0----1----4---6
| \/ | \ /
| /\ | 5
2----3
expected: 2 clusters a, b with a={0,1,2,3} and b={4,5,6}
*/
// for DBSCAN this could be directly weighted
double[][] adjacencyMatrix =
{
{0, 1, 1, 1, 0, 0, 0},
{1, 0, 1, 1, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 0},
{1, 1, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 1, 0},
};
// |nodes| instances of data with pseudo x,y coords. set to node no.
double[][] data = new double[adjacencyMatrix.length][2];
for (int i=0; i<data.length; i++) {
data[i][0]= i;
data[i][1]= i;
}
// mission critical
int minPts= 2;
double radius= 5;
DBSCAN clustering = new DBSCAN(data, new DBSCANDistance(adjacencyMatrix), minPts, radius);
int[] labels = clustering.getClusterLabel();
for (int label : labels) {
System.out.println(label);
}
assertEquals(7, labels.length);
assertTrue(labels[0] == labels[1]);
assertTrue(labels[0] == labels[2]);
assertTrue(labels[0] == labels[3]);
assertTrue(labels[1] == labels[0]);
assertTrue(labels[1] == labels[2]);
assertTrue(labels[1] == labels[3]);
assertTrue(labels[1] != labels[4]); // expected cut
assertTrue(labels[2] == labels[0]);
assertTrue(labels[2] == labels[1]);
assertTrue(labels[2] == labels[3]);
assertTrue(labels[3] == labels[0]);
assertTrue(labels[3] == labels[1]);
assertTrue(labels[3] == labels[2]);
assertTrue(labels[4] != labels[1]); // expected cut
assertTrue(labels[4] == labels[5]);
assertTrue(labels[4] == labels[6]);
assertTrue(labels[5] == labels[4]);
assertTrue(labels[5] == labels[6]);
assertTrue(labels[6] == labels[4]);
assertTrue(labels[6] == labels[5]);
}
@Test @Test
public void testMarkovClustering(){ public void testMarkovClustering(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment